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