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