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