Commit | Line | Data |
---|---|---|
3591c066 AT |
1 | /* |
2 | Copyright (C) Andrew Tridgell 1998 | |
3 | ||
4 | This program is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 2 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software | |
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
17 | */ | |
18 | ||
19 | /* the socket based protocol for setting up a connection wit rsyncd */ | |
20 | ||
21 | #include "rsync.h" | |
22 | ||
23 | extern int module_id; | |
24 | extern int read_only; | |
25 | extern int verbose; | |
26 | extern int rsync_port; | |
27 | ||
28 | int start_socket_client(char *host, char *path, int argc, char *argv[]) | |
29 | { | |
30 | int fd, i; | |
31 | char *sargs[MAX_ARGS]; | |
32 | int sargc=0; | |
33 | char line[1024]; | |
bcb7e502 | 34 | char *p, *user=NULL; |
13c5fc0e | 35 | extern int remote_version; |
3591c066 | 36 | |
bcb7e502 AT |
37 | p = strchr(host, '@'); |
38 | if (p) { | |
39 | user = host; | |
40 | host = p+1; | |
41 | *p = 0; | |
42 | } | |
43 | ||
44 | if (!user) user = getenv("USER"); | |
45 | if (!user) user = getenv("LOGNAME"); | |
46 | ||
3591c066 AT |
47 | fd = open_socket_out(host, rsync_port); |
48 | if (fd == -1) { | |
49 | exit_cleanup(1); | |
50 | } | |
51 | ||
52 | server_options(sargs,&sargc); | |
53 | ||
54 | sargs[sargc++] = "."; | |
55 | ||
56 | if (path && *path) | |
57 | sargs[sargc++] = path; | |
58 | ||
59 | sargs[sargc] = NULL; | |
60 | ||
91eee594 AT |
61 | io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION); |
62 | ||
3591c066 AT |
63 | if (!read_line(fd, line, sizeof(line)-1)) { |
64 | return -1; | |
65 | } | |
66 | ||
13c5fc0e | 67 | if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) { |
3591c066 AT |
68 | return -1; |
69 | } | |
70 | ||
2c91d3d3 AT |
71 | p = strchr(path,'/'); |
72 | if (p) *p = 0; | |
73 | io_printf(fd,"%s\n",path); | |
74 | if (p) *p = '/'; | |
75 | ||
3591c066 AT |
76 | while (1) { |
77 | if (!read_line(fd, line, sizeof(line)-1)) { | |
78 | return -1; | |
79 | } | |
31593dd6 | 80 | |
31593dd6 | 81 | if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) { |
bcb7e502 | 82 | auth_client(fd, user, line+18); |
31593dd6 AT |
83 | continue; |
84 | } | |
31593dd6 | 85 | |
3591c066 AT |
86 | if (strcmp(line,"@RSYNCD: OK") == 0) break; |
87 | rprintf(FINFO,"%s\n", line); | |
88 | } | |
89 | ||
90 | for (i=0;i<sargc;i++) { | |
91 | io_printf(fd,"%s\n", sargs[i]); | |
92 | } | |
93 | io_printf(fd,"\n"); | |
94 | ||
3591c066 AT |
95 | return client_run(fd, fd, -1, argc, argv); |
96 | } | |
97 | ||
98 | ||
99 | ||
100 | static int rsync_module(int fd, int i) | |
101 | { | |
102 | int argc=0; | |
103 | char *argv[MAX_ARGS]; | |
104 | char **argp; | |
105 | char line[1024]; | |
8ef4ffd6 AT |
106 | uid_t uid; |
107 | gid_t gid; | |
108 | char *p; | |
56c473b7 AT |
109 | char *addr = client_addr(fd); |
110 | char *host = client_name(fd); | |
31593dd6 | 111 | char *auth; |
56c473b7 AT |
112 | |
113 | if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) { | |
114 | rprintf(FERROR,"rsync denied on module %s from %s (%s)\n", | |
115 | lp_name(i), client_name(fd), client_addr(fd)); | |
116 | return -1; | |
117 | } | |
3591c066 | 118 | |
bcb7e502 | 119 | if (!auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ")) { |
31593dd6 AT |
120 | rprintf(FERROR,"auth failed on module %s from %s (%s)\n", |
121 | lp_name(i), client_name(fd), client_addr(fd)); | |
122 | return -1; | |
123 | } | |
31593dd6 | 124 | |
0c515f17 AT |
125 | if (!claim_connection(lp_lock_file(), lp_max_connections())) { |
126 | rprintf(FERROR,"ERROR: max connections reached\n"); | |
127 | return -1; | |
128 | } | |
129 | ||
ff8b29b8 | 130 | rprintf(FINFO,"rsync on module %s from %s (%s)\n", |
56c473b7 | 131 | lp_name(i), host, addr); |
31593dd6 | 132 | |
3591c066 AT |
133 | module_id = i; |
134 | ||
135 | if (lp_read_only(i)) | |
136 | read_only = 1; | |
137 | ||
8ef4ffd6 AT |
138 | p = lp_uid(i); |
139 | if (!name_to_uid(p, &uid)) { | |
140 | if (!isdigit(*p)) { | |
141 | rprintf(FERROR,"Invalid uid %s\n", p); | |
142 | return -1; | |
143 | } | |
144 | uid = atoi(p); | |
145 | } | |
146 | ||
147 | p = lp_gid(i); | |
148 | if (!name_to_gid(p, &gid)) { | |
149 | if (!isdigit(*p)) { | |
150 | rprintf(FERROR,"Invalid gid %s\n", p); | |
151 | return -1; | |
152 | } | |
153 | gid = atoi(p); | |
154 | } | |
155 | ||
3591c066 AT |
156 | if (chroot(lp_path(i))) { |
157 | io_printf(fd,"@ERROR: chroot failed\n"); | |
158 | return -1; | |
159 | } | |
160 | ||
161 | if (chdir("/")) { | |
162 | io_printf(fd,"@ERROR: chdir failed\n"); | |
163 | return -1; | |
164 | } | |
165 | ||
8ef4ffd6 | 166 | if (setgid(gid)) { |
3591c066 AT |
167 | io_printf(fd,"@ERROR: setgid failed\n"); |
168 | return -1; | |
169 | } | |
170 | ||
8ef4ffd6 | 171 | if (setuid(uid)) { |
3591c066 AT |
172 | io_printf(fd,"@ERROR: setuid failed\n"); |
173 | return -1; | |
174 | } | |
175 | ||
176 | io_printf(fd,"@RSYNCD: OK\n"); | |
177 | ||
178 | argv[argc++] = "rsyncd"; | |
179 | ||
180 | while (1) { | |
181 | if (!read_line(fd, line, sizeof(line)-1)) { | |
182 | return -1; | |
183 | } | |
184 | ||
185 | if (!*line) break; | |
186 | ||
187 | argv[argc] = strdup(line); | |
188 | if (!argv[argc]) { | |
189 | return -1; | |
190 | } | |
191 | ||
192 | argc++; | |
193 | if (argc == MAX_ARGS) { | |
194 | return -1; | |
195 | } | |
196 | } | |
197 | ||
198 | parse_arguments(argc, argv); | |
199 | ||
200 | /* don't allow the logs to be flooded too fast */ | |
201 | if (verbose > 1) verbose = 1; | |
202 | ||
203 | argc -= optind; | |
204 | argp = argv + optind; | |
205 | optind = 0; | |
206 | ||
207 | start_server(fd, fd, argc, argp); | |
208 | ||
209 | return 0; | |
210 | } | |
211 | ||
7a6421fa AT |
212 | /* send a list of available modules to the client. Don't list those |
213 | with "list = False". */ | |
3591c066 AT |
214 | static void send_listing(int fd) |
215 | { | |
216 | int n = lp_numservices(); | |
217 | int i; | |
218 | ||
219 | for (i=0;i<n;i++) | |
220 | if (lp_list(i)) | |
221 | io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i)); | |
222 | } | |
223 | ||
224 | /* this is called when a socket connection is established to a client | |
225 | and we want to start talking. The setup of the system is done from | |
226 | here */ | |
227 | static int start_daemon(int fd) | |
228 | { | |
7a6421fa | 229 | char line[200]; |
3591c066 | 230 | char *motd; |
8ef4ffd6 | 231 | int i = -1; |
4cdf25e4 | 232 | extern char *config_file; |
13c5fc0e | 233 | extern int remote_version; |
4cdf25e4 AT |
234 | |
235 | if (!lp_load(config_file)) { | |
236 | exit_cleanup(1); | |
237 | } | |
3591c066 AT |
238 | |
239 | set_socket_options(fd,"SO_KEEPALIVE"); | |
240 | ||
241 | io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION); | |
242 | ||
243 | motd = lp_motd_file(); | |
244 | if (*motd) { | |
245 | FILE *f = fopen(motd,"r"); | |
246 | while (f && !feof(f)) { | |
247 | int len = fread(line, 1, sizeof(line)-1, f); | |
248 | if (len > 0) { | |
249 | line[len] = 0; | |
250 | io_printf(fd,"%s", line); | |
251 | } | |
252 | } | |
253 | if (f) fclose(f); | |
254 | io_printf(fd,"\n"); | |
255 | } | |
256 | ||
91eee594 AT |
257 | if (!read_line(fd, line, sizeof(line)-1)) { |
258 | return -1; | |
259 | } | |
260 | ||
261 | if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) { | |
262 | return -1; | |
263 | } | |
264 | ||
8ef4ffd6 | 265 | while (i == -1) { |
3591c066 AT |
266 | line[0] = 0; |
267 | if (!read_line(fd, line, sizeof(line)-1)) { | |
268 | return -1; | |
269 | } | |
270 | ||
271 | if (!*line || strcmp(line,"#list")==0) { | |
272 | send_listing(fd); | |
273 | return -1; | |
274 | } | |
275 | ||
276 | if (*line == '#') { | |
277 | /* it's some sort of command that I don't understand */ | |
278 | io_printf(fd,"ERROR: Unknown command '%s'\n", line); | |
279 | return -1; | |
280 | } | |
281 | ||
282 | i = lp_number(line); | |
283 | if (i == -1) { | |
284 | io_printf(fd,"ERROR: Unknown module '%s'\n", line); | |
285 | return -1; | |
286 | } | |
3591c066 AT |
287 | } |
288 | ||
8ef4ffd6 | 289 | return rsync_module(fd, i); |
3591c066 AT |
290 | } |
291 | ||
292 | ||
293 | int daemon_main(void) | |
294 | { | |
3591c066 AT |
295 | if (is_a_socket(STDIN_FILENO)) { |
296 | /* we are running via inetd */ | |
297 | return start_daemon(STDIN_FILENO); | |
298 | } | |
299 | ||
300 | become_daemon(); | |
301 | ||
8ef4ffd6 AT |
302 | start_accept_loop(rsync_port, start_daemon); |
303 | return -1; | |
3591c066 AT |
304 | } |
305 |