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