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