don't clobber argv[0], so ps shows the right thing
[rsync/rsync.git] / clientserver.c
CommitLineData
3591c066
AT
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
23extern int module_id;
24extern int read_only;
25extern int verbose;
26extern int rsync_port;
97cb8dc2 27char *auth_user;
cb13abfe 28int sanitize_paths = 0;
3591c066
AT
29
30int 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;
e42c9458 35 char line[MAXPATHLEN];
bcb7e502 36 char *p, *user=NULL;
13c5fc0e 37 extern int remote_version;
8d9dc9f9 38 extern int am_sender;
e30f0657 39 extern struct in_addr socket_address;
3591c066 40
27e3e9c9
AT
41 if (argc == 0 && !am_sender) {
42 extern int list_only;
43 list_only = 1;
44 }
45
f98df1d9
AT
46 if (*path == '/') {
47 rprintf(FERROR,"ERROR: The remote path must start with a module name\n");
48 return -1;
49 }
50
bcb7e502
AT
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
e30f0657 61 fd = open_socket_out(host, rsync_port, &socket_address);
3591c066 62 if (fd == -1) {
65417579 63 exit_cleanup(RERR_SOCKETIO);
3591c066
AT
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
91eee594
AT
75 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
76
3591c066
AT
77 if (!read_line(fd, line, sizeof(line)-1)) {
78 return -1;
79 }
80
13c5fc0e 81 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
3591c066
AT
82 return -1;
83 }
84
2c91d3d3
AT
85 p = strchr(path,'/');
86 if (p) *p = 0;
87 io_printf(fd,"%s\n",path);
88 if (p) *p = '/';
89
3591c066
AT
90 while (1) {
91 if (!read_line(fd, line, sizeof(line)-1)) {
92 return -1;
93 }
31593dd6 94
31593dd6 95 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
bcb7e502 96 auth_client(fd, user, line+18);
31593dd6
AT
97 continue;
98 }
31593dd6 99
3591c066
AT
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
09b7f5db
AT
109 if (remote_version < 23) {
110 if (remote_version == 22 || (remote_version > 17 && !am_sender))
111 io_start_multiplex_in(fd);
112 }
8d9dc9f9 113
3591c066
AT
114 return client_run(fd, fd, -1, argc, argv);
115}
116
117
118
119static int rsync_module(int fd, int i)
120{
121 int argc=0;
122 char *argv[MAX_ARGS];
123 char **argp;
e42c9458 124 char line[MAXPATHLEN];
1a016bfd
AT
125 uid_t uid = (uid_t)-2;
126 gid_t gid = (gid_t)-2;
8ef4ffd6 127 char *p;
56c473b7
AT
128 char *addr = client_addr(fd);
129 char *host = client_name(fd);
874895d5 130 char *name = lp_name(i);
8638dd48 131 int use_chroot = lp_use_chroot(i);
874895d5 132 int start_glob=0;
41979ff8 133 int ret;
7b372642
AT
134 char *request=NULL;
135 extern int am_sender;
8d9dc9f9 136 extern int remote_version;
943882a2 137 extern int am_root;
56c473b7
AT
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",
874895d5 141 name, client_name(fd), client_addr(fd));
c8e78d87 142 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
874895d5 143 name, client_name(fd), client_addr(fd));
56c473b7
AT
144 return -1;
145 }
3591c066 146
5e71c444 147 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
8d72ef6e
AT
148 if (errno) {
149 rprintf(FERROR,"failed to open lock file %s : %s\n",
5e71c444 150 lp_lock_file(i), strerror(errno));
8d72ef6e 151 io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
5e71c444 152 lp_lock_file(i), strerror(errno));
8d72ef6e
AT
153 } else {
154 rprintf(FERROR,"max connections (%d) reached\n",
5e71c444
AT
155 lp_max_connections(i));
156 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
8d72ef6e 157 }
0c515f17
AT
158 return -1;
159 }
160
31593dd6 161
97cb8dc2 162 auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
d0d56395 163
97cb8dc2 164 if (!auth_user) {
d0d56395
AT
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
3591c066
AT
171 module_id = i;
172
716baed7 173 am_root = (getuid() == 0);
8ef4ffd6 174
716baed7
DD
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 }
8ef4ffd6
AT
195 }
196
cd64343a
DD
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
8f3a2d54 203 p = lp_exclude_from(i);
2b6b4d53 204 add_exclude_file(p, 1, 0);
8f3a2d54 205
5805327b 206 p = lp_exclude(i);
8f3a2d54
AT
207 add_exclude_line(p);
208
1a016bfd
AT
209 log_open();
210
8638dd48
DD
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 }
3591c066 217
c226b7c2 218 if (!push_dir("/", 0)) {
8638dd48
DD
219 rprintf(FERROR,"chdir %s failed\n", lp_path(i));
220 io_printf(fd,"@ERROR: chdir failed\n");
221 return -1;
222 }
3591c066 223
716baed7
DD
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 }
cb13abfe 230 sanitize_paths = 1;
716baed7
DD
231 }
232
233 if (am_root) {
234 if (setgid(gid)) {
8638dd48
DD
235 rprintf(FERROR,"setgid %d failed\n", gid);
236 io_printf(fd,"@ERROR: setgid failed\n");
237 return -1;
238 }
3591c066 239
716baed7 240 if (setuid(uid)) {
8638dd48
DD
241 rprintf(FERROR,"setuid %d failed\n", uid);
242 io_printf(fd,"@ERROR: setuid failed\n");
243 return -1;
244 }
245
716baed7 246 am_root = (getuid() == 0);
3591c066
AT
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
874895d5
AT
260 p = line;
261
874895d5 262 argv[argc] = strdup(p);
3591c066
AT
263 if (!argv[argc]) {
264 return -1;
265 }
266
874895d5 267 if (start_glob) {
1a016bfd 268 if (start_glob == 1) {
7b372642 269 request = strdup(p);
1a016bfd
AT
270 start_glob++;
271 }
cb13abfe 272 glob_expand(name, argv, &argc, MAX_ARGS);
874895d5
AT
273 } else {
274 argc++;
275 }
276
277 if (strcmp(line,".") == 0) {
278 start_glob = 1;
279 }
280
3591c066
AT
281 if (argc == MAX_ARGS) {
282 return -1;
283 }
284 }
285
cb13abfe 286 if (sanitize_paths) {
8638dd48
DD
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++) {
cb13abfe 295 sanitize_path(argv[i], NULL);
8638dd48
DD
296 }
297 }
298
b86f0cef 299 ret = parse_arguments(argc, argv, 0);
3591c066 300
7b372642 301 if (request) {
97cb8dc2 302 if (*auth_user) {
d0d56395
AT
303 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
304 am_sender?"on":"to",
97cb8dc2 305 request, auth_user, host, addr);
d0d56395
AT
306 } else {
307 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
308 am_sender?"on":"to",
309 request, host, addr);
310 }
7b372642
AT
311 free(request);
312 }
313
0199b05f 314#if !TRIDGE
3591c066
AT
315 /* don't allow the logs to be flooded too fast */
316 if (verbose > 1) verbose = 1;
0199b05f 317#endif
3591c066
AT
318
319 argc -= optind;
320 argp = argv + optind;
321 optind = 0;
322
09b7f5db
AT
323 if (remote_version < 23) {
324 if (remote_version == 22 || (remote_version > 17 && am_sender))
325 io_start_multiplex_out(fd);
554e0a8d
AT
326 }
327
41979ff8 328 if (!ret) {
cd8185f2 329 option_error();
41979ff8
AT
330 }
331
81791cfc
AT
332 if (lp_timeout(i)) {
333 extern int io_timeout;
334 io_timeout = lp_timeout(i);
335 }
336
3591c066
AT
337 start_server(fd, fd, argc, argp);
338
339 return 0;
340}
341
7a6421fa
AT
342/* send a list of available modules to the client. Don't list those
343 with "list = False". */
3591c066
AT
344static 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 */
357static int start_daemon(int fd)
358{
7a6421fa 359 char line[200];
3591c066 360 char *motd;
8ef4ffd6 361 int i = -1;
4cdf25e4 362 extern char *config_file;
13c5fc0e 363 extern int remote_version;
4cdf25e4 364
f9e940ef 365 if (!lp_load(config_file, 0)) {
65417579 366 exit_cleanup(RERR_SYNTAX);
4cdf25e4 367 }
3591c066
AT
368
369 set_socket_options(fd,"SO_KEEPALIVE");
a6801c39 370 set_socket_options(fd,lp_socket_options());
f0359dd0 371 set_nonblocking(fd);
3591c066
AT
372
373 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
374
375 motd = lp_motd_file();
27d3cdbc 376 if (motd && *motd) {
3591c066
AT
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
91eee594
AT
389 if (!read_line(fd, line, sizeof(line)-1)) {
390 return -1;
391 }
392
393 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
c8e78d87 394 io_printf(fd,"@ERROR: protocol startup error\n");
91eee594
AT
395 return -1;
396 }
397
8ef4ffd6 398 while (i == -1) {
3591c066
AT
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 */
c8e78d87 411 io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
3591c066
AT
412 return -1;
413 }
414
415 i = lp_number(line);
416 if (i == -1) {
c8e78d87 417 io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
3591c066
AT
418 return -1;
419 }
3591c066
AT
420 }
421
8ef4ffd6 422 return rsync_module(fd, i);
3591c066
AT
423}
424
425
426int daemon_main(void)
427{
f9e940ef 428 extern char *config_file;
ad517ce5 429 extern int orig_umask;
8638dd48 430 char *pid_file;
1a016bfd 431
3591c066 432 if (is_a_socket(STDIN_FILENO)) {
41979ff8
AT
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 }
3eb38818 442
3591c066
AT
443 return start_daemon(STDIN_FILENO);
444 }
445
446 become_daemon();
447
f9e940ef 448 if (!lp_load(config_file, 1)) {
65417579 449 exit_cleanup(RERR_SYNTAX);
f9e940ef
AT
450 }
451
452 log_open();
453
e42c9458
AT
454 rprintf(FINFO,"rsyncd version %s starting\n",VERSION);
455
8638dd48 456 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
ad517ce5
DD
457 char pidbuf[16];
458 int fd;
8638dd48
DD
459 int pid = (int) getpid();
460 cleanup_set_pid(pid);
ad517ce5
DD
461 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
462 0666 & ~orig_umask)) == -1) {
8638dd48 463 cleanup_set_pid(0);
8a5d6bba 464 rprintf(FLOG,"failed to create pid file %s\n", pid_file);
65417579 465 exit_cleanup(RERR_FILEIO);
8638dd48 466 }
ad517ce5
DD
467 slprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
468 write(fd, pidbuf, strlen(pidbuf));
469 close(fd);
8638dd48
DD
470 }
471
8ef4ffd6
AT
472 start_accept_loop(rsync_port, start_daemon);
473 return -1;
3591c066
AT
474}
475