preparing for release of 2.0.9
[rsync/rsync.git] / clientserver.c
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
23 extern int module_id;
24 extern int read_only;
25 extern int verbose;
26 extern int rsync_port;
27
28 int start_socket_client(char *host, char *path, int argc, char *argv[])
29 {
30         int fd, i;
31         char *sargs[MAX_ARGS];
32         int sargc=0;
33         char line[MAXPATHLEN];
34         char *p, *user=NULL;
35         extern int remote_version;
36         extern int am_client;
37         extern int am_sender;
38
39         p = strchr(host, '@');
40         if (p) {
41                 user = host;
42                 host = p+1;
43                 *p = 0;
44         }
45
46         if (!user) user = getenv("USER");
47         if (!user) user = getenv("LOGNAME");
48
49         am_client = 1;
50
51         fd = open_socket_out(host, rsync_port);
52         if (fd == -1) {
53                 exit_cleanup(1);
54         }
55         
56         server_options(sargs,&sargc);
57
58         sargs[sargc++] = ".";
59
60         if (path && *path) 
61                 sargs[sargc++] = path;
62
63         sargs[sargc] = NULL;
64
65         io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
66
67         if (!read_line(fd, line, sizeof(line)-1)) {
68                 return -1;
69         }
70
71         if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
72                 return -1;
73         }
74
75         p = strchr(path,'/');
76         if (p) *p = 0;
77         io_printf(fd,"%s\n",path);
78         if (p) *p = '/';
79
80         while (1) {
81                 if (!read_line(fd, line, sizeof(line)-1)) {
82                         return -1;
83                 }
84
85                 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
86                         auth_client(fd, user, line+18);
87                         continue;
88                 }
89
90                 if (strcmp(line,"@RSYNCD: OK") == 0) break;
91                 rprintf(FINFO,"%s\n", line);
92         }
93
94         for (i=0;i<sargc;i++) {
95                 io_printf(fd,"%s\n", sargs[i]);
96         }
97         io_printf(fd,"\n");
98
99         if (remote_version > 17 && !am_sender)
100                 io_start_multiplex_in(fd);
101
102         return client_run(fd, fd, -1, argc, argv);
103 }
104
105
106
107 static int rsync_module(int fd, int i)
108 {
109         int argc=0;
110         char *argv[MAX_ARGS];
111         char **argp;
112         char line[MAXPATHLEN];
113         uid_t uid = (uid_t)-2;
114         gid_t gid = (gid_t)-2;
115         char *p;
116         char *addr = client_addr(fd);
117         char *host = client_name(fd);
118         char *name = lp_name(i);
119         char *user;
120         int start_glob=0;
121         char *request=NULL;
122         extern int am_sender;
123         extern int remote_version;
124
125         if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
126                 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
127                         name, client_name(fd), client_addr(fd));
128                 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
129                           name, client_name(fd), client_addr(fd));
130                 return -1;
131         }
132
133         if (!claim_connection(lp_lock_file(), lp_max_connections())) {
134                 rprintf(FERROR,"max connections (%d) reached\n",
135                         lp_max_connections());
136                 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections());
137                 return -1;
138         }
139
140         
141         user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
142
143         if (!user) {
144                 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
145                         name, client_name(fd), client_addr(fd));
146                 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
147                 return -1;              
148         }
149
150         module_id = i;
151
152         if (lp_read_only(i))
153                 read_only = 1;
154
155         p = lp_uid(i);
156         if (!name_to_uid(p, &uid)) {
157                 if (!isdigit(*p)) {
158                         rprintf(FERROR,"Invalid uid %s\n", p);
159                         io_printf(fd,"@ERROR: invalid uid\n");
160                         return -1;
161                 } 
162                 uid = atoi(p);
163         }
164
165         p = lp_gid(i);
166         if (!name_to_gid(p, &gid)) {
167                 if (!isdigit(*p)) {
168                         rprintf(FERROR,"Invalid gid %s\n", p);
169                         io_printf(fd,"@ERROR: invalid gid\n");
170                         return -1;
171                 } 
172                 gid = atoi(p);
173         }
174
175         p = lp_exclude_from(i);
176         add_exclude_file(p, 1);
177
178         p = lp_exclude(i);
179         add_exclude_line(p);
180
181         log_open();
182
183         if (chroot(lp_path(i))) {
184                 rprintf(FERROR,"chroot %s failed\n", lp_path(i));
185                 io_printf(fd,"@ERROR: chroot failed\n");
186                 return -1;
187         }
188
189         if (chdir("/")) {
190                 rprintf(FERROR,"chdir %s failed\n", lp_path(i));
191                 io_printf(fd,"@ERROR: chdir failed\n");
192                 return -1;
193         }
194
195         if (setgid(gid) || getgid() != gid) {
196                 rprintf(FERROR,"setgid %d failed\n", gid);
197                 io_printf(fd,"@ERROR: setgid failed\n");
198                 return -1;
199         }
200
201         if (setuid(uid) || getuid() != uid) {
202                 rprintf(FERROR,"setuid %d failed\n", uid);
203                 io_printf(fd,"@ERROR: setuid failed\n");
204                 return -1;
205         }
206
207         io_printf(fd,"@RSYNCD: OK\n");
208
209         argv[argc++] = "rsyncd";
210
211         while (1) {
212                 if (!read_line(fd, line, sizeof(line)-1)) {
213                         return -1;
214                 }
215
216                 if (!*line) break;
217
218                 p = line;
219
220                 argv[argc] = strdup(p);
221                 if (!argv[argc]) {
222                         return -1;
223                 }
224
225                 if (start_glob) {
226                         if (start_glob == 1) {
227                                 request = strdup(p);
228                                 start_glob++;
229                         }
230                         glob_expand(name, argv, &argc, MAX_ARGS);
231                 } else {
232                         argc++;
233                 }
234
235                 if (strcmp(line,".") == 0) {
236                         start_glob = 1;
237                 }
238
239                 if (argc == MAX_ARGS) {
240                         return -1;
241                 }
242         }
243
244         parse_arguments(argc, argv);
245
246         if (request) {
247                 if (*user) {
248                         rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
249                                 am_sender?"on":"to",
250                                 request, user, host, addr);
251                 } else {
252                         rprintf(FINFO,"rsync %s %s from %s (%s)\n",
253                                 am_sender?"on":"to",
254                                 request, host, addr);
255                 }
256                 free(request);
257         }
258
259         /* don't allow the logs to be flooded too fast */
260         if (verbose > 1) verbose = 1;
261
262         argc -= optind;
263         argp = argv + optind;
264         optind = 0;
265
266         if (remote_version > 17 && am_sender)
267                 io_start_multiplex_out(fd);
268
269         start_server(fd, fd, argc, argp);
270
271         return 0;
272 }
273
274 /* send a list of available modules to the client. Don't list those
275    with "list = False". */
276 static void send_listing(int fd)
277 {
278         int n = lp_numservices();
279         int i;
280         
281         for (i=0;i<n;i++)
282                 if (lp_list(i))
283                     io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
284 }
285
286 /* this is called when a socket connection is established to a client
287    and we want to start talking. The setup of the system is done from
288    here */
289 static int start_daemon(int fd)
290 {
291         char line[200];
292         char *motd;
293         int i = -1;
294         extern char *config_file;
295         extern int remote_version;
296
297         if (!lp_load(config_file, 0)) {
298                 exit_cleanup(1);
299         }
300
301         set_socket_options(fd,"SO_KEEPALIVE");
302         set_socket_options(fd,lp_socket_options());
303         
304
305         io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
306
307         motd = lp_motd_file();
308         if (*motd) {
309                 FILE *f = fopen(motd,"r");
310                 while (f && !feof(f)) {
311                         int len = fread(line, 1, sizeof(line)-1, f);
312                         if (len > 0) {
313                                 line[len] = 0;
314                                 io_printf(fd,"%s", line);
315                         }
316                 }
317                 if (f) fclose(f);
318                 io_printf(fd,"\n");
319         }
320
321         if (!read_line(fd, line, sizeof(line)-1)) {
322                 return -1;
323         }
324
325         if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
326                 io_printf(fd,"@ERROR: protocol startup error\n");
327                 return -1;
328         }       
329
330         while (i == -1) {
331                 line[0] = 0;
332                 if (!read_line(fd, line, sizeof(line)-1)) {
333                         return -1;
334                 }
335
336                 if (!*line || strcmp(line,"#list")==0) {
337                         send_listing(fd);
338                         return -1;
339                 } 
340
341                 if (*line == '#') {
342                         /* it's some sort of command that I don't understand */
343                         io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
344                         return -1;
345                 }
346
347                 i = lp_number(line);
348                 if (i == -1) {
349                         io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
350                         return -1;
351                 }
352         }
353
354         return rsync_module(fd, i);
355 }
356
357
358 int daemon_main(void)
359 {
360         extern char *config_file;
361
362         if (is_a_socket(STDIN_FILENO)) {
363                 /* we are running via inetd */
364                 return start_daemon(STDIN_FILENO);
365         }
366
367         become_daemon();
368
369         if (!lp_load(config_file, 1)) {
370                 fprintf(stderr,"failed to load config file %s\n", config_file);
371                 exit_cleanup(1);
372         }
373
374         log_open();
375
376         rprintf(FINFO,"rsyncd version %s starting\n",VERSION);
377
378         start_accept_loop(rsync_port, start_daemon);
379         return -1;
380 }
381