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