Go to the source code of this file.
Functions | |
int | start_socket_client (char *host, char *path, int argc, char *argv[]) |
Run a client connected to an rsyncd. More... | |
int | rsync_module (int fd, int i) |
void | send_listing (int fd) |
int | start_daemon (int fd) |
int | daemon_main (void) |
Variables | |
int | module_id |
int | read_only |
int | verbose |
int | rsync_port |
char * | auth_user |
int | sanitize_paths |
Definition in file clientserver.c.
|
Run a client connected to an rsyncd. The alternative to this function for remote-shell connections is do_cmd(). After negotiating which module to use and reading the server's motd, this hands over to client_run(). Telling the server the module will cause it to chroot/setuid/etc. Instead of doing a transfer, the client may at this stage instead get a listing of remote modules and exit.
Definition at line 51 of file clientserver.c. References am_sender, auth_client(), bind_address, client_run(), default_af_hint, FERROR, FINFO, getenv(), host, io_printf(), io_start_multiplex_in(), kludge_around_eof, open_socket_out_wrapped(), read_line(), rprintf(), rsync_port, server_options(), shell_cmd, and verbose. Referenced by start_client().
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 /* This is just a friendliness enhancement: if the connection 00072 * is to an rsyncd then there is no point specifying the -e option. 00073 * Note that this is only set if the -e was explicitly specified, 00074 * not if the environment variable just happens to be set. 00075 * See http://lists.samba.org/pipermail/rsync/2000-September/002744.html 00076 */ 00077 if (shell_cmd) { 00078 rprintf(FINFO, "WARNING: --rsh or -e option ignored when " 00079 "connecting to rsync daemon\n"); 00080 /* continue */ 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 /* FIXME: If we're going to use a socket program for 00100 * testing, then this message is wrong. We need to 00101 * say something like "(except really using %s)" */ 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 /* note that read_line strips of \n or \r */ 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 /* Old servers may just drop the connection here, 00140 rather than sending a proper EXIT command. Yuck. */ 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 /* This is sent by recent versions of the 00158 * server to terminate the listing of modules. 00159 * We don't want to go on and transfer 00160 * anything; just exit. */ 00161 exit(0); 00162 } 00163 00164 if (strncmp(line, "@ERROR", 6) == 0) { 00165 rprintf(FERROR,"%s\n", line); 00166 /* This is always fatal; the server will now 00167 * close the socket. */ 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 } |
|
Definition at line 190 of file clientserver.c. References add_exclude_file(), add_exclude_line(), add_include_line(), allow_access(), am_sender, auth_server(), auth_user, claim_connection(), client_addr(), client_name(), FERROR, FINFO, glob_expand(), host, io_printf(), io_start_multiplex_out(), log_init(), module_id, name_to_gid(), name_to_uid(), option_error(), parse_arguments(), push_dir(), read_line(), rprintf(), rsyserr(), sanitize_path(), sanitize_paths, start_server(), strdup(), and verbose. Referenced by start_daemon().
00191 { 00192 int argc=0; 00193 char *argv[MAX_ARGS]; 00194 char **argp; 00195 char line[MAXPATHLEN]; 00196 uid_t uid = (uid_t)-2; /* canonically "nobody" */ 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 /* TODO: If we're not root, but the configuration requests 00269 * that we change to some uid other than the current one, then 00270 * log a warning. */ 00271 00272 /* TODO: Perhaps take a list of gids, and make them into the 00273 * supplementary groups. */ 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 * XXX: The 'use chroot' flag is a fairly reliable 00292 * source of confusion, because it fails under two 00293 * important circumstances: running as non-root, 00294 * running on Win32 (or possibly others). On the 00295 * other hand, if you are running as root, then it 00296 * might be better to always use chroot. 00297 * 00298 * So, perhaps if we can't chroot we should just issue 00299 * a warning, unless a "require chroot" flag is set, 00300 * in which case we fail. 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 /* Get rid of any supplementary groups this process 00326 * might have inheristed. */ 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 /* XXXX: You could argue that if the daemon is started 00335 * by a non-root user and they explicitly specify a 00336 * gid, then we should try to change to that gid -- 00337 * this could be possible if it's already in their 00338 * supplementary groups. */ 00339 00340 /* TODO: Perhaps we need to document that if rsyncd is 00341 * started by somebody other than root it will inherit 00342 * all their supplementary groups. */ 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 * Note that this is applied to all parameters, whether or not 00399 * they are filenames, but no other legal parameters contain 00400 * the forms that need to be sanitized so it doesn't hurt; 00401 * it is not known at this point which parameters are files 00402 * and which aren't. 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 /* don't allow the logs to be flooded too fast */ 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 /* For later protocol versions, we don't start multiplexing 00436 * until we've configured nonblocking in start_server. That 00437 * means we're in a sticky situation now: there's no way to 00438 * convey errors to the client. */ 00439 00440 /* FIXME: Hold off on reporting option processing errors until 00441 * we've set up nonblocking and multiplexed IO and can get the 00442 * message back to them. */ 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 } |
|
Definition at line 460 of file clientserver.c. References io_printf(), and lp_numservices(). Referenced by start_daemon().
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 } |
|
Definition at line 477 of file clientserver.c. References config_file, io_printf(), lp_load(), lp_number(), read_line(), rsync_module(), send_listing(), set_nonblocking(), and set_socket_options(). Referenced by daemon_main().
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 /* it's some sort of command that I don't understand */ 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 } |
|
Definition at line 546 of file clientserver.c. References become_daemon(), cleanup_set_pid(), config_file, do_open(), FINFO, FLOG, is_a_socket(), log_init(), lp_load(), no_detach, orig_umask, rprintf(), rsync_port, rsyserr(), snprintf(), start_accept_loop(), and start_daemon(). Referenced by main().
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 /* we are running via inetd - close off stdout and 00557 stderr so that library functions (and getopt) don't 00558 try to use them. Redirect them to /dev/null */ 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 /* TODO: If listening on a particular address, then show that 00580 * address too. In fact, why not just do inet_ntop on the 00581 * local address??? */ 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 } |
|
Definition at line 30 of file clientserver.c. Referenced by parse_arguments(), and rsync_module(). |
|
Definition at line 31 of file clientserver.c. |
|
Definition at line 32 of file clientserver.c. Referenced by rsync_module(), and start_socket_client(). |
|
Definition at line 33 of file clientserver.c. Referenced by daemon_main(), and start_socket_client(). |
|
Definition at line 34 of file clientserver.c. Referenced by log_formatted(), and rsync_module(). |
|
Definition at line 35 of file clientserver.c. Referenced by rsync_module(). |