Draft documentation of the client-server protocol.
[rsync/rsync.git] / clientserver.c
... / ...
CommitLineData
1/* -*- c-file-style: "linux"; -*-
2
3 Copyright (C) 1998-2000 by Andrew Tridgell
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20/* the socket based protocol for setting up a connection wit rsyncd */
21
22#include "rsync.h"
23
24extern int module_id;
25extern int read_only;
26extern int verbose;
27extern int rsync_port;
28char *auth_user;
29int sanitize_paths = 0;
30
31int start_socket_client(char *host, char *path, int argc, char *argv[])
32{
33 int fd, i;
34 char *sargs[MAX_ARGS];
35 int sargc=0;
36 char line[MAXPATHLEN];
37 char *p, *user=NULL;
38 extern int remote_version;
39 extern int am_sender;
40 extern struct in_addr socket_address;
41
42 if (argc == 0 && !am_sender) {
43 extern int list_only;
44 list_only = 1;
45 }
46
47 if (*path == '/') {
48 rprintf(FERROR,"ERROR: The remote path must start with a module name\n");
49 return -1;
50 }
51
52 p = strchr(host, '@');
53 if (p) {
54 user = host;
55 host = p+1;
56 *p = 0;
57 }
58
59 if (!user) user = getenv("USER");
60 if (!user) user = getenv("LOGNAME");
61
62 fd = open_socket_out(host, rsync_port, &socket_address);
63 if (fd == -1) {
64 exit_cleanup(RERR_SOCKETIO);
65 }
66
67 server_options(sargs,&sargc);
68
69 sargs[sargc++] = ".";
70
71 if (path && *path)
72 sargs[sargc++] = path;
73
74 sargs[sargc] = NULL;
75
76 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
77
78 if (!read_line(fd, line, sizeof(line)-1)) {
79 return -1;
80 }
81
82 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
83 return -1;
84 }
85
86 p = strchr(path,'/');
87 if (p) *p = 0;
88 io_printf(fd,"%s\n",path);
89 if (p) *p = '/';
90
91 while (1) {
92 if (!read_line(fd, line, sizeof(line)-1)) {
93 return -1;
94 }
95
96 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
97 auth_client(fd, user, line+18);
98 continue;
99 }
100
101 if (strcmp(line,"@RSYNCD: OK") == 0) break;
102 rprintf(FINFO,"%s\n", line);
103 }
104
105 for (i=0;i<sargc;i++) {
106 io_printf(fd,"%s\n", sargs[i]);
107 }
108 io_printf(fd,"\n");
109
110 if (remote_version < 23) {
111 if (remote_version == 22 || (remote_version > 17 && !am_sender))
112 io_start_multiplex_in(fd);
113 }
114
115 return client_run(fd, fd, -1, argc, argv);
116}
117
118
119
120static int rsync_module(int fd, int i)
121{
122 int argc=0;
123 char *argv[MAX_ARGS];
124 char **argp;
125 char line[MAXPATHLEN];
126 uid_t uid = (uid_t)-2;
127 gid_t gid = (gid_t)-2;
128 char *p;
129 char *addr = client_addr(fd);
130 char *host = client_name(fd);
131 char *name = lp_name(i);
132 int use_chroot = lp_use_chroot(i);
133 int start_glob=0;
134 int ret;
135 char *request=NULL;
136 extern int am_sender;
137 extern int remote_version;
138 extern int am_root;
139
140 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
141 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
142 name, client_name(fd), client_addr(fd));
143 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
144 name, client_name(fd), client_addr(fd));
145 return -1;
146 }
147
148 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
149 if (errno) {
150 rprintf(FERROR,"failed to open lock file %s : %s\n",
151 lp_lock_file(i), strerror(errno));
152 io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
153 lp_lock_file(i), strerror(errno));
154 } else {
155 rprintf(FERROR,"max connections (%d) reached\n",
156 lp_max_connections(i));
157 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
158 }
159 return -1;
160 }
161
162
163 auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
164
165 if (!auth_user) {
166 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
167 name, client_name(fd), client_addr(fd));
168 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
169 return -1;
170 }
171
172 module_id = i;
173
174 am_root = (getuid() == 0);
175
176 if (am_root) {
177 p = lp_uid(i);
178 if (!name_to_uid(p, &uid)) {
179 if (!isdigit(*p)) {
180 rprintf(FERROR,"Invalid uid %s\n", p);
181 io_printf(fd,"@ERROR: invalid uid\n");
182 return -1;
183 }
184 uid = atoi(p);
185 }
186
187 p = lp_gid(i);
188 if (!name_to_gid(p, &gid)) {
189 if (!isdigit(*p)) {
190 rprintf(FERROR,"Invalid gid %s\n", p);
191 io_printf(fd,"@ERROR: invalid gid\n");
192 return -1;
193 }
194 gid = atoi(p);
195 }
196 }
197
198 p = lp_include_from(i);
199 add_exclude_file(p, 1, 1);
200
201 p = lp_include(i);
202 add_include_line(p);
203
204 p = lp_exclude_from(i);
205 add_exclude_file(p, 1, 0);
206
207 p = lp_exclude(i);
208 add_exclude_line(p);
209
210 log_init();
211
212 if (use_chroot) {
213 if (chroot(lp_path(i))) {
214 rsyserr(FERROR, errno, "chroot %s failed", lp_path(i));
215 io_printf(fd,"@ERROR: chroot failed\n");
216 return -1;
217 }
218
219 if (!push_dir("/", 0)) {
220 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
221 io_printf(fd,"@ERROR: chdir failed\n");
222 return -1;
223 }
224
225 } else {
226 if (!push_dir(lp_path(i), 0)) {
227 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
228 io_printf(fd,"@ERROR: chdir failed\n");
229 return -1;
230 }
231 sanitize_paths = 1;
232 }
233
234 if (am_root) {
235 if (setgid(gid)) {
236 rsyserr(FERROR, errno, "setgid %d failed", gid);
237 io_printf(fd,"@ERROR: setgid failed\n");
238 return -1;
239 }
240
241 if (setuid(uid)) {
242 rsyserr(FERROR, errno, "setuid %d failed", uid);
243 io_printf(fd,"@ERROR: setuid failed\n");
244 return -1;
245 }
246
247 am_root = (getuid() == 0);
248 }
249
250 io_printf(fd,"@RSYNCD: OK\n");
251
252 argv[argc++] = "rsyncd";
253
254 while (1) {
255 if (!read_line(fd, line, sizeof(line)-1)) {
256 return -1;
257 }
258
259 if (!*line) break;
260
261 p = line;
262
263 argv[argc] = strdup(p);
264 if (!argv[argc]) {
265 return -1;
266 }
267
268 if (start_glob) {
269 if (start_glob == 1) {
270 request = strdup(p);
271 start_glob++;
272 }
273 glob_expand(name, argv, &argc, MAX_ARGS);
274 } else {
275 argc++;
276 }
277
278 if (strcmp(line,".") == 0) {
279 start_glob = 1;
280 }
281
282 if (argc == MAX_ARGS) {
283 return -1;
284 }
285 }
286
287 if (sanitize_paths) {
288 /*
289 * Note that this is applied to all parameters, whether or not
290 * they are filenames, but no other legal parameters contain
291 * the forms that need to be sanitized so it doesn't hurt;
292 * it is not known at this point which parameters are files
293 * and which aren't.
294 */
295 for (i = 1; i < argc; i++) {
296 sanitize_path(argv[i], NULL);
297 }
298 }
299
300 ret = parse_arguments(argc, argv, 0);
301
302 if (request) {
303 if (*auth_user) {
304 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
305 am_sender?"on":"to",
306 request, auth_user, host, addr);
307 } else {
308 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
309 am_sender?"on":"to",
310 request, host, addr);
311 }
312 free(request);
313 }
314
315#if !TRIDGE
316 /* don't allow the logs to be flooded too fast */
317 if (verbose > 1) verbose = 1;
318#endif
319
320 argc -= optind;
321 argp = argv + optind;
322 optind = 0;
323
324 if (remote_version < 23) {
325 if (remote_version == 22 || (remote_version > 17 && am_sender))
326 io_start_multiplex_out(fd);
327 }
328
329 if (!ret) {
330 option_error();
331 }
332
333 if (lp_timeout(i)) {
334 extern int io_timeout;
335 io_timeout = lp_timeout(i);
336 }
337
338 start_server(fd, fd, argc, argp);
339
340 return 0;
341}
342
343/* send a list of available modules to the client. Don't list those
344 with "list = False". */
345static void send_listing(int fd)
346{
347 int n = lp_numservices();
348 int i;
349
350 for (i=0;i<n;i++)
351 if (lp_list(i))
352 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
353}
354
355/* this is called when a socket connection is established to a client
356 and we want to start talking. The setup of the system is done from
357 here */
358static int start_daemon(int fd)
359{
360 char line[200];
361 char *motd;
362 int i = -1;
363 extern char *config_file;
364 extern int remote_version;
365
366 if (!lp_load(config_file, 0)) {
367 exit_cleanup(RERR_SYNTAX);
368 }
369
370 set_socket_options(fd,"SO_KEEPALIVE");
371 set_socket_options(fd,lp_socket_options());
372 set_nonblocking(fd);
373
374 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
375
376 motd = lp_motd_file();
377 if (motd && *motd) {
378 FILE *f = fopen(motd,"r");
379 while (f && !feof(f)) {
380 int len = fread(line, 1, sizeof(line)-1, f);
381 if (len > 0) {
382 line[len] = 0;
383 io_printf(fd,"%s", line);
384 }
385 }
386 if (f) fclose(f);
387 io_printf(fd,"\n");
388 }
389
390 if (!read_line(fd, line, sizeof(line)-1)) {
391 return -1;
392 }
393
394 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
395 io_printf(fd,"@ERROR: protocol startup error\n");
396 return -1;
397 }
398
399 while (i == -1) {
400 line[0] = 0;
401 if (!read_line(fd, line, sizeof(line)-1)) {
402 return -1;
403 }
404
405 if (!*line || strcmp(line,"#list")==0) {
406 send_listing(fd);
407 return -1;
408 }
409
410 if (*line == '#') {
411 /* it's some sort of command that I don't understand */
412 io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
413 return -1;
414 }
415
416 i = lp_number(line);
417 if (i == -1) {
418 io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
419 return -1;
420 }
421 }
422
423 return rsync_module(fd, i);
424}
425
426
427int daemon_main(void)
428{
429 extern char *config_file;
430 extern int orig_umask;
431 char *pid_file;
432
433 if (is_a_socket(STDIN_FILENO)) {
434 int i;
435
436 /* we are running via inetd - close off stdout and
437 stderr so that library functions (and getopt) don't
438 try to use them. Redirect them to /dev/null */
439 for (i=1;i<3;i++) {
440 close(i);
441 open("/dev/null", O_RDWR);
442 }
443
444 return start_daemon(STDIN_FILENO);
445 }
446
447 become_daemon();
448
449 if (!lp_load(config_file, 1)) {
450 exit_cleanup(RERR_SYNTAX);
451 }
452
453 log_init();
454
455 rprintf(FINFO,"rsyncd version %s starting\n",VERSION);
456
457 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
458 char pidbuf[16];
459 int fd;
460 int pid = (int) getpid();
461 cleanup_set_pid(pid);
462 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
463 0666 & ~orig_umask)) == -1) {
464 cleanup_set_pid(0);
465 rsyserr(FLOG, errno, "failed to create pid file %s", pid_file);
466 exit_cleanup(RERR_FILEIO);
467 }
468 slprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
469 write(fd, pidbuf, strlen(pidbuf));
470 close(fd);
471 }
472
473 start_accept_loop(rsync_port, start_daemon);
474 return -1;
475}
476