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