1c0bd7ecd6b11555ff8cd39057921fd4569078f1
[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;
107         gid_t gid;
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
114         if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
115                 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
116                         name, client_name(fd), client_addr(fd));
117                 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
118                           name, client_name(fd), client_addr(fd));
119                 return -1;
120         }
121
122         if (!auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ")) {
123                 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
124                         name, client_name(fd), client_addr(fd));
125                 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
126                 return -1;              
127         }
128
129         if (!claim_connection(lp_lock_file(), lp_max_connections())) {
130                 rprintf(FERROR,"max connections (%d) reached\n",
131                         lp_max_connections());
132                 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections());
133                 return -1;
134         }
135
136         rprintf(FINFO,"rsync on module %s from %s (%s)\n",
137                 name, host, addr);
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         if (chroot(lp_path(i))) {
171                 io_printf(fd,"@ERROR: chroot failed\n");
172                 return -1;
173         }
174
175         if (chdir("/")) {
176                 io_printf(fd,"@ERROR: chdir failed\n");
177                 return -1;
178         }
179
180         if (setgid(gid)) {
181                 io_printf(fd,"@ERROR: setgid failed\n");
182                 return -1;
183         }
184
185         if (setuid(uid)) {
186                 io_printf(fd,"@ERROR: setuid failed\n");
187                 return -1;
188         }
189
190         io_printf(fd,"@RSYNCD: OK\n");
191
192         argv[argc++] = "rsyncd";
193
194         while (1) {
195                 if (!read_line(fd, line, sizeof(line)-1)) {
196                         return -1;
197                 }
198
199                 if (!*line) break;
200
201                 p = line;
202
203                 argv[argc] = strdup(p);
204                 if (!argv[argc]) {
205                         return -1;
206                 }
207
208                 if (start_glob) {
209                         rprintf(FINFO,"transferring %s\n",p);
210                         glob_expand(name, argv, &argc, MAX_ARGS);
211                 } else {
212                         argc++;
213                 }
214
215                 if (strcmp(line,".") == 0) {
216                         start_glob = 1;
217                 }
218
219                 if (argc == MAX_ARGS) {
220                         return -1;
221                 }
222         }
223
224         parse_arguments(argc, argv);
225
226         /* don't allow the logs to be flooded too fast */
227         if (verbose > 1) verbose = 1;
228
229         argc -= optind;
230         argp = argv + optind;
231         optind = 0;
232
233         start_server(fd, fd, argc, argp);
234
235         return 0;
236 }
237
238 /* send a list of available modules to the client. Don't list those
239    with "list = False". */
240 static void send_listing(int fd)
241 {
242         int n = lp_numservices();
243         int i;
244         
245         for (i=0;i<n;i++)
246                 if (lp_list(i))
247                     io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
248 }
249
250 /* this is called when a socket connection is established to a client
251    and we want to start talking. The setup of the system is done from
252    here */
253 static int start_daemon(int fd)
254 {
255         char line[200];
256         char *motd;
257         int i = -1;
258         extern char *config_file;
259         extern int remote_version;
260
261         if (!lp_load(config_file)) {
262                 exit_cleanup(1);
263         }
264
265         set_socket_options(fd,"SO_KEEPALIVE");
266
267         io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
268
269         motd = lp_motd_file();
270         if (*motd) {
271                 FILE *f = fopen(motd,"r");
272                 while (f && !feof(f)) {
273                         int len = fread(line, 1, sizeof(line)-1, f);
274                         if (len > 0) {
275                                 line[len] = 0;
276                                 io_printf(fd,"%s", line);
277                         }
278                 }
279                 if (f) fclose(f);
280                 io_printf(fd,"\n");
281         }
282
283         if (!read_line(fd, line, sizeof(line)-1)) {
284                 return -1;
285         }
286
287         if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
288                 io_printf(fd,"@ERROR: protocol startup error\n");
289                 return -1;
290         }       
291
292         while (i == -1) {
293                 line[0] = 0;
294                 if (!read_line(fd, line, sizeof(line)-1)) {
295                         return -1;
296                 }
297
298                 if (!*line || strcmp(line,"#list")==0) {
299                         send_listing(fd);
300                         return -1;
301                 } 
302
303                 if (*line == '#') {
304                         /* it's some sort of command that I don't understand */
305                         io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
306                         return -1;
307                 }
308
309                 i = lp_number(line);
310                 if (i == -1) {
311                         io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
312                         return -1;
313                 }
314         }
315
316         return rsync_module(fd, i);
317 }
318
319
320 int daemon_main(void)
321 {
322         if (is_a_socket(STDIN_FILENO)) {
323                 /* we are running via inetd */
324                 return start_daemon(STDIN_FILENO);
325         }
326
327         become_daemon();
328
329         rprintf(FINFO,"rsyncd version %s starting\n",VERSION);
330
331         start_accept_loop(rsync_port, start_daemon);
332         return -1;
333 }
334