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