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