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