00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "rsync.h"
00029
00030 extern int module_id;
00031 extern int read_only;
00032 extern int verbose;
00033 extern int rsync_port;
00034 char *auth_user;
00035 extern int sanitize_paths;
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 int start_socket_client(char *host, char *path, int argc, char *argv[])
00052 {
00053 int fd, i;
00054 char *sargs[MAX_ARGS];
00055 int sargc=0;
00056 char line[MAXPATHLEN];
00057 char *p, *user=NULL;
00058 extern int remote_version;
00059 extern int am_sender;
00060 extern char *shell_cmd;
00061 extern int list_only;
00062 extern int kludge_around_eof;
00063 extern char *bind_address;
00064 extern int default_af_hint;
00065
00066 if (argc == 0 && !am_sender) {
00067 extern int list_only;
00068 list_only = 1;
00069 }
00070
00071
00072
00073
00074
00075
00076
00077 if (shell_cmd) {
00078 rprintf(FINFO, "WARNING: --rsh or -e option ignored when "
00079 "connecting to rsync daemon\n");
00080
00081 }
00082
00083 if (*path == '/') {
00084 rprintf(FERROR,"ERROR: The remote path must start with a module name not a /\n");
00085 return -1;
00086 }
00087
00088 p = strchr(host, '@');
00089 if (p) {
00090 user = host;
00091 host = p+1;
00092 *p = 0;
00093 }
00094
00095 if (!user) user = getenv("USER");
00096 if (!user) user = getenv("LOGNAME");
00097
00098 if (verbose >= 2) {
00099
00100
00101
00102 rprintf(FINFO, "opening tcp connection to %s port %d\n",
00103 host, rsync_port);
00104 }
00105 fd = open_socket_out_wrapped (host, rsync_port, bind_address,
00106 default_af_hint);
00107 if (fd == -1) {
00108 exit_cleanup(RERR_SOCKETIO);
00109 }
00110
00111 server_options(sargs,&sargc);
00112
00113 sargs[sargc++] = ".";
00114
00115 if (path && *path)
00116 sargs[sargc++] = path;
00117
00118 sargs[sargc] = NULL;
00119
00120 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
00121
00122 if (!read_line(fd, line, sizeof(line)-1)) {
00123 rprintf(FERROR, "rsync: did not see server greeting\n");
00124 return -1;
00125 }
00126
00127 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
00128
00129 rprintf(FERROR, "rsync: server sent \"%s\" rather than greeting\n",
00130 line);
00131 return -1;
00132 }
00133
00134 p = strchr(path,'/');
00135 if (p) *p = 0;
00136 io_printf(fd,"%s\n",path);
00137 if (p) *p = '/';
00138
00139
00140
00141 kludge_around_eof = list_only && (remote_version < 25);
00142
00143 while (1) {
00144 if (!read_line(fd, line, sizeof(line)-1)) {
00145 rprintf(FERROR, "rsync: didn't get server startup line\n");
00146 return -1;
00147 }
00148
00149 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
00150 auth_client(fd, user, line+18);
00151 continue;
00152 }
00153
00154 if (strcmp(line,"@RSYNCD: OK") == 0) break;
00155
00156 if (strcmp(line,"@RSYNCD: EXIT") == 0) {
00157
00158
00159
00160
00161 exit(0);
00162 }
00163
00164 if (strncmp(line, "@ERROR", 6) == 0) {
00165 rprintf(FERROR,"%s\n", line);
00166
00167
00168 return RERR_STARTCLIENT;
00169 } else {
00170 rprintf(FINFO,"%s\n", line);
00171 }
00172 }
00173 kludge_around_eof = False;
00174
00175 for (i=0;i<sargc;i++) {
00176 io_printf(fd,"%s\n", sargs[i]);
00177 }
00178 io_printf(fd,"\n");
00179
00180 if (remote_version < 23) {
00181 if (remote_version == 22 || (remote_version > 17 && !am_sender))
00182 io_start_multiplex_in(fd);
00183 }
00184
00185 return client_run(fd, fd, -1, argc, argv);
00186 }
00187
00188
00189
00190 static int rsync_module(int fd, int i)
00191 {
00192 int argc=0;
00193 char *argv[MAX_ARGS];
00194 char **argp;
00195 char line[MAXPATHLEN];
00196 uid_t uid = (uid_t)-2;
00197 gid_t gid = (gid_t)-2;
00198 char *p;
00199 char *addr = client_addr(fd);
00200 char *host = client_name(fd);
00201 char *name = lp_name(i);
00202 int use_chroot = lp_use_chroot(i);
00203 int start_glob=0;
00204 int ret;
00205 char *request=NULL;
00206 extern int am_sender;
00207 extern int remote_version;
00208 extern int am_root;
00209
00210 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
00211 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
00212 name, host, addr);
00213 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
00214 name, host, addr);
00215 return -1;
00216 }
00217
00218 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
00219 if (errno) {
00220 rprintf(FERROR,"failed to open lock file %s : %s\n",
00221 lp_lock_file(i), strerror(errno));
00222 io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
00223 lp_lock_file(i), strerror(errno));
00224 } else {
00225 rprintf(FERROR,"max connections (%d) reached\n",
00226 lp_max_connections(i));
00227 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
00228 }
00229 return -1;
00230 }
00231
00232
00233 auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
00234
00235 if (!auth_user) {
00236 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
00237 name, client_name(fd), client_addr(fd));
00238 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
00239 return -1;
00240 }
00241
00242 module_id = i;
00243
00244 am_root = (getuid() == 0);
00245
00246 if (am_root) {
00247 p = lp_uid(i);
00248 if (!name_to_uid(p, &uid)) {
00249 if (!isdigit(* (unsigned char *) p)) {
00250 rprintf(FERROR,"Invalid uid %s\n", p);
00251 io_printf(fd,"@ERROR: invalid uid %s\n", p);
00252 return -1;
00253 }
00254 uid = atoi(p);
00255 }
00256
00257 p = lp_gid(i);
00258 if (!name_to_gid(p, &gid)) {
00259 if (!isdigit(* (unsigned char *) p)) {
00260 rprintf(FERROR,"Invalid gid %s\n", p);
00261 io_printf(fd,"@ERROR: invalid gid %s\n", p);
00262 return -1;
00263 }
00264 gid = atoi(p);
00265 }
00266 }
00267
00268
00269
00270
00271
00272
00273
00274
00275 p = lp_include_from(i);
00276 add_exclude_file(p, 1, 1);
00277
00278 p = lp_include(i);
00279 add_include_line(p);
00280
00281 p = lp_exclude_from(i);
00282 add_exclude_file(p, 1, 0);
00283
00284 p = lp_exclude(i);
00285 add_exclude_line(p);
00286
00287 log_init();
00288
00289 if (use_chroot) {
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302 if (chroot(lp_path(i))) {
00303 rsyserr(FERROR, errno, "chroot %s failed", lp_path(i));
00304 io_printf(fd,"@ERROR: chroot failed\n");
00305 return -1;
00306 }
00307
00308 if (!push_dir("/", 0)) {
00309 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
00310 io_printf(fd,"@ERROR: chdir failed\n");
00311 return -1;
00312 }
00313
00314 } else {
00315 if (!push_dir(lp_path(i), 0)) {
00316 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
00317 io_printf(fd,"@ERROR: chdir failed\n");
00318 return -1;
00319 }
00320 sanitize_paths = 1;
00321 }
00322
00323 if (am_root) {
00324 #ifdef HAVE_SETGROUPS
00325
00326
00327 if (setgroups(0, NULL)) {
00328 rsyserr(FERROR, errno, "setgroups failed");
00329 io_printf(fd, "@ERROR: setgroups failed\n");
00330 return -1;
00331 }
00332 #endif
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344 if (setgid(gid)) {
00345 rsyserr(FERROR, errno, "setgid %d failed", (int) gid);
00346 io_printf(fd,"@ERROR: setgid failed\n");
00347 return -1;
00348 }
00349
00350 if (setuid(uid)) {
00351 rsyserr(FERROR, errno, "setuid %d failed", (int) uid);
00352 io_printf(fd,"@ERROR: setuid failed\n");
00353 return -1;
00354 }
00355
00356 am_root = (getuid() == 0);
00357 }
00358
00359 io_printf(fd,"@RSYNCD: OK\n");
00360
00361 argv[argc++] = "rsyncd";
00362
00363 while (1) {
00364 if (!read_line(fd, line, sizeof(line)-1)) {
00365 return -1;
00366 }
00367
00368 if (!*line) break;
00369
00370 p = line;
00371
00372 argv[argc] = strdup(p);
00373 if (!argv[argc]) {
00374 return -1;
00375 }
00376
00377 if (start_glob) {
00378 if (start_glob == 1) {
00379 request = strdup(p);
00380 start_glob++;
00381 }
00382 glob_expand(name, argv, &argc, MAX_ARGS);
00383 } else {
00384 argc++;
00385 }
00386
00387 if (strcmp(line,".") == 0) {
00388 start_glob = 1;
00389 }
00390
00391 if (argc == MAX_ARGS) {
00392 return -1;
00393 }
00394 }
00395
00396 if (sanitize_paths) {
00397
00398
00399
00400
00401
00402
00403
00404 for (i = 1; i < argc; i++) {
00405 sanitize_path(argv[i], NULL);
00406 }
00407 }
00408
00409 argp = argv;
00410 ret = parse_arguments(&argc, (const char ***) &argp, 0);
00411
00412 if (request) {
00413 if (*auth_user) {
00414 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
00415 am_sender?"on":"to",
00416 request, auth_user, host, addr);
00417 } else {
00418 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
00419 am_sender?"on":"to",
00420 request, host, addr);
00421 }
00422 free(request);
00423 }
00424
00425 #ifndef DEBUG
00426
00427 if (verbose > 1) verbose = 1;
00428 #endif
00429
00430 if (remote_version < 23) {
00431 if (remote_version == 22 || (remote_version > 17 && am_sender))
00432 io_start_multiplex_out(fd);
00433 }
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443 if (!ret) {
00444 option_error();
00445 exit_cleanup(RERR_UNSUPPORTED);
00446 }
00447
00448 if (lp_timeout(i)) {
00449 extern int io_timeout;
00450 io_timeout = lp_timeout(i);
00451 }
00452
00453 start_server(fd, fd, argc, argp);
00454
00455 return 0;
00456 }
00457
00458
00459
00460 static void send_listing(int fd)
00461 {
00462 int n = lp_numservices();
00463 int i;
00464 extern int remote_version;
00465
00466 for (i=0;i<n;i++)
00467 if (lp_list(i))
00468 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
00469
00470 if (remote_version >= 25)
00471 io_printf(fd,"@RSYNCD: EXIT\n");
00472 }
00473
00474
00475
00476
00477 static int start_daemon(int fd)
00478 {
00479 char line[200];
00480 char *motd;
00481 int i = -1;
00482 extern char *config_file;
00483 extern int remote_version;
00484
00485 if (!lp_load(config_file, 0)) {
00486 exit_cleanup(RERR_SYNTAX);
00487 }
00488
00489 set_socket_options(fd,"SO_KEEPALIVE");
00490 set_socket_options(fd,lp_socket_options());
00491 set_nonblocking(fd);
00492
00493 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
00494
00495 motd = lp_motd_file();
00496 if (motd && *motd) {
00497 FILE *f = fopen(motd,"r");
00498 while (f && !feof(f)) {
00499 int len = fread(line, 1, sizeof(line)-1, f);
00500 if (len > 0) {
00501 line[len] = 0;
00502 io_printf(fd,"%s", line);
00503 }
00504 }
00505 if (f) fclose(f);
00506 io_printf(fd,"\n");
00507 }
00508
00509 if (!read_line(fd, line, sizeof(line)-1)) {
00510 return -1;
00511 }
00512
00513 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
00514 io_printf(fd,"@ERROR: protocol startup error\n");
00515 return -1;
00516 }
00517
00518 while (i == -1) {
00519 line[0] = 0;
00520 if (!read_line(fd, line, sizeof(line)-1)) {
00521 return -1;
00522 }
00523
00524 if (!*line || strcmp(line,"#list")==0) {
00525 send_listing(fd);
00526 return -1;
00527 }
00528
00529 if (*line == '#') {
00530
00531 io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
00532 return -1;
00533 }
00534
00535 i = lp_number(line);
00536 if (i == -1) {
00537 io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
00538 return -1;
00539 }
00540 }
00541
00542 return rsync_module(fd, i);
00543 }
00544
00545
00546 int daemon_main(void)
00547 {
00548 extern char *config_file;
00549 extern int orig_umask;
00550 char *pid_file;
00551 extern int no_detach;
00552
00553 if (is_a_socket(STDIN_FILENO)) {
00554 int i;
00555
00556
00557
00558
00559 for (i=1;i<3;i++) {
00560 close(i);
00561 open("/dev/null", O_RDWR);
00562 }
00563
00564 return start_daemon(STDIN_FILENO);
00565 }
00566
00567 if (!no_detach)
00568 become_daemon();
00569
00570 if (!lp_load(config_file, 1)) {
00571 exit_cleanup(RERR_SYNTAX);
00572 }
00573
00574 log_init();
00575
00576 rprintf(FINFO, "rsyncd version %s starting, listening on port %d\n",
00577 RSYNC_VERSION,
00578 rsync_port);
00579
00580
00581
00582
00583 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
00584 char pidbuf[16];
00585 int fd;
00586 int pid = (int) getpid();
00587 cleanup_set_pid(pid);
00588 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
00589 0666 & ~orig_umask)) == -1) {
00590 cleanup_set_pid(0);
00591 rsyserr(FLOG, errno, "failed to create pid file %s", pid_file);
00592 exit_cleanup(RERR_FILEIO);
00593 }
00594 snprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
00595 write(fd, pidbuf, strlen(pidbuf));
00596 close(fd);
00597 }
00598
00599 start_accept_loop(rsync_port, start_daemon);
00600 return -1;
00601 }
00602