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