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