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