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]; |
8ef4ffd6 AT |
106 | uid_t uid; |
107 | gid_t gid; | |
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 | ||
ff8b29b8 | 136 | rprintf(FINFO,"rsync on module %s from %s (%s)\n", |
874895d5 | 137 | name, host, addr); |
31593dd6 | 138 | |
3591c066 AT |
139 | module_id = i; |
140 | ||
141 | if (lp_read_only(i)) | |
142 | read_only = 1; | |
143 | ||
8ef4ffd6 AT |
144 | p = lp_uid(i); |
145 | if (!name_to_uid(p, &uid)) { | |
146 | if (!isdigit(*p)) { | |
147 | rprintf(FERROR,"Invalid uid %s\n", p); | |
c8e78d87 | 148 | io_printf(fd,"@ERROR: invalid uid\n"); |
8ef4ffd6 AT |
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); | |
c8e78d87 | 158 | io_printf(fd,"@ERROR: invalid gid\n"); |
8ef4ffd6 AT |
159 | return -1; |
160 | } | |
161 | gid = atoi(p); | |
162 | } | |
163 | ||
8f3a2d54 AT |
164 | p = lp_exclude_from(i); |
165 | add_exclude_file(p, 1); | |
166 | ||
5805327b | 167 | p = lp_exclude(i); |
8f3a2d54 AT |
168 | add_exclude_line(p); |
169 | ||
3591c066 AT |
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 | ||
8ef4ffd6 | 180 | if (setgid(gid)) { |
3591c066 AT |
181 | io_printf(fd,"@ERROR: setgid failed\n"); |
182 | return -1; | |
183 | } | |
184 | ||
8ef4ffd6 | 185 | if (setuid(uid)) { |
3591c066 AT |
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 | ||
874895d5 AT |
201 | p = line; |
202 | ||
874895d5 | 203 | argv[argc] = strdup(p); |
3591c066 AT |
204 | if (!argv[argc]) { |
205 | return -1; | |
206 | } | |
207 | ||
874895d5 | 208 | if (start_glob) { |
e42c9458 | 209 | rprintf(FINFO,"transferring %s\n",p); |
087bf010 | 210 | glob_expand(name, argv, &argc, MAX_ARGS); |
874895d5 AT |
211 | } else { |
212 | argc++; | |
213 | } | |
214 | ||
215 | if (strcmp(line,".") == 0) { | |
216 | start_glob = 1; | |
217 | } | |
218 | ||
3591c066 AT |
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 | ||
7a6421fa AT |
238 | /* send a list of available modules to the client. Don't list those |
239 | with "list = False". */ | |
3591c066 AT |
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 | { | |
7a6421fa | 255 | char line[200]; |
3591c066 | 256 | char *motd; |
8ef4ffd6 | 257 | int i = -1; |
4cdf25e4 | 258 | extern char *config_file; |
13c5fc0e | 259 | extern int remote_version; |
4cdf25e4 AT |
260 | |
261 | if (!lp_load(config_file)) { | |
262 | exit_cleanup(1); | |
263 | } | |
3591c066 AT |
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 | ||
91eee594 AT |
283 | if (!read_line(fd, line, sizeof(line)-1)) { |
284 | return -1; | |
285 | } | |
286 | ||
287 | if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) { | |
c8e78d87 | 288 | io_printf(fd,"@ERROR: protocol startup error\n"); |
91eee594 AT |
289 | return -1; |
290 | } | |
291 | ||
8ef4ffd6 | 292 | while (i == -1) { |
3591c066 AT |
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 */ | |
c8e78d87 | 305 | io_printf(fd,"@ERROR: Unknown command '%s'\n", line); |
3591c066 AT |
306 | return -1; |
307 | } | |
308 | ||
309 | i = lp_number(line); | |
310 | if (i == -1) { | |
c8e78d87 | 311 | io_printf(fd,"@ERROR: Unknown module '%s'\n", line); |
3591c066 AT |
312 | return -1; |
313 | } | |
3591c066 AT |
314 | } |
315 | ||
8ef4ffd6 | 316 | return rsync_module(fd, i); |
3591c066 AT |
317 | } |
318 | ||
319 | ||
320 | int daemon_main(void) | |
321 | { | |
3591c066 AT |
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 | ||
e42c9458 AT |
329 | rprintf(FINFO,"rsyncd version %s starting\n",VERSION); |
330 | ||
8ef4ffd6 AT |
331 | start_accept_loop(rsync_port, start_daemon); |
332 | return -1; | |
3591c066 AT |
333 | } |
334 |