| 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, *user=NULL; |
| 35 | extern int remote_version; |
| 36 | |
| 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 | |
| 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 | |
| 61 | io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION); |
| 62 | |
| 63 | if (!read_line(fd, line, sizeof(line)-1)) { |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) { |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | p = strchr(path,'/'); |
| 72 | if (p) *p = 0; |
| 73 | io_printf(fd,"%s\n",path); |
| 74 | if (p) *p = '/'; |
| 75 | |
| 76 | while (1) { |
| 77 | if (!read_line(fd, line, sizeof(line)-1)) { |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) { |
| 82 | auth_client(fd, user, line+18); |
| 83 | continue; |
| 84 | } |
| 85 | |
| 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 | |
| 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]; |
| 106 | uid_t uid; |
| 107 | gid_t gid; |
| 108 | char *p; |
| 109 | char *addr = client_addr(fd); |
| 110 | char *host = client_name(fd); |
| 111 | char *auth; |
| 112 | char *name = lp_name(i); |
| 113 | int start_glob=0; |
| 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", |
| 117 | name, client_name(fd), client_addr(fd)); |
| 118 | io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n", |
| 119 | name, client_name(fd), client_addr(fd)); |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | if (!auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ")) { |
| 124 | rprintf(FERROR,"auth failed on module %s from %s (%s)\n", |
| 125 | name, client_name(fd), client_addr(fd)); |
| 126 | io_printf(fd,"@ERROR: auth failed on module %s\n",name); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | if (!claim_connection(lp_lock_file(), lp_max_connections())) { |
| 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()); |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | rprintf(FINFO,"rsync on module %s from %s (%s)\n", |
| 138 | name, host, addr); |
| 139 | |
| 140 | module_id = i; |
| 141 | |
| 142 | if (lp_read_only(i)) |
| 143 | read_only = 1; |
| 144 | |
| 145 | p = lp_uid(i); |
| 146 | if (!name_to_uid(p, &uid)) { |
| 147 | if (!isdigit(*p)) { |
| 148 | rprintf(FERROR,"Invalid uid %s\n", p); |
| 149 | io_printf(fd,"@ERROR: invalid uid\n"); |
| 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); |
| 159 | io_printf(fd,"@ERROR: invalid gid\n"); |
| 160 | return -1; |
| 161 | } |
| 162 | gid = atoi(p); |
| 163 | } |
| 164 | |
| 165 | p = lp_exclude_from(i); |
| 166 | add_exclude_file(p, 1); |
| 167 | |
| 168 | p = lp_exclude(i); |
| 169 | add_exclude_line(p); |
| 170 | |
| 171 | if (chroot(lp_path(i))) { |
| 172 | io_printf(fd,"@ERROR: chroot failed\n"); |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | if (chdir("/")) { |
| 177 | io_printf(fd,"@ERROR: chdir failed\n"); |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | if (setgid(gid)) { |
| 182 | io_printf(fd,"@ERROR: setgid failed\n"); |
| 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | if (setuid(uid)) { |
| 187 | io_printf(fd,"@ERROR: setuid failed\n"); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | io_printf(fd,"@RSYNCD: OK\n"); |
| 192 | |
| 193 | argv[argc++] = "rsyncd"; |
| 194 | |
| 195 | while (1) { |
| 196 | if (!read_line(fd, line, sizeof(line)-1)) { |
| 197 | return -1; |
| 198 | } |
| 199 | |
| 200 | if (!*line) break; |
| 201 | |
| 202 | p = line; |
| 203 | |
| 204 | if (start_glob && strncmp(p, name, strlen(name)) == 0) { |
| 205 | p += strlen(name); |
| 206 | if (!*p) p = "."; |
| 207 | } |
| 208 | |
| 209 | argv[argc] = strdup(p); |
| 210 | if (!argv[argc]) { |
| 211 | return -1; |
| 212 | } |
| 213 | |
| 214 | if (start_glob) { |
| 215 | glob_expand(argv, &argc, MAX_ARGS); |
| 216 | } else { |
| 217 | argc++; |
| 218 | } |
| 219 | |
| 220 | if (strcmp(line,".") == 0) { |
| 221 | start_glob = 1; |
| 222 | } |
| 223 | |
| 224 | if (argc == MAX_ARGS) { |
| 225 | return -1; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | parse_arguments(argc, argv); |
| 230 | |
| 231 | /* don't allow the logs to be flooded too fast */ |
| 232 | if (verbose > 1) verbose = 1; |
| 233 | |
| 234 | argc -= optind; |
| 235 | argp = argv + optind; |
| 236 | optind = 0; |
| 237 | |
| 238 | start_server(fd, fd, argc, argp); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | /* send a list of available modules to the client. Don't list those |
| 244 | with "list = False". */ |
| 245 | static void send_listing(int fd) |
| 246 | { |
| 247 | int n = lp_numservices(); |
| 248 | int i; |
| 249 | |
| 250 | for (i=0;i<n;i++) |
| 251 | if (lp_list(i)) |
| 252 | io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i)); |
| 253 | } |
| 254 | |
| 255 | /* this is called when a socket connection is established to a client |
| 256 | and we want to start talking. The setup of the system is done from |
| 257 | here */ |
| 258 | static int start_daemon(int fd) |
| 259 | { |
| 260 | char line[200]; |
| 261 | char *motd; |
| 262 | int i = -1; |
| 263 | extern char *config_file; |
| 264 | extern int remote_version; |
| 265 | |
| 266 | if (!lp_load(config_file)) { |
| 267 | exit_cleanup(1); |
| 268 | } |
| 269 | |
| 270 | set_socket_options(fd,"SO_KEEPALIVE"); |
| 271 | |
| 272 | io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION); |
| 273 | |
| 274 | motd = lp_motd_file(); |
| 275 | if (*motd) { |
| 276 | FILE *f = fopen(motd,"r"); |
| 277 | while (f && !feof(f)) { |
| 278 | int len = fread(line, 1, sizeof(line)-1, f); |
| 279 | if (len > 0) { |
| 280 | line[len] = 0; |
| 281 | io_printf(fd,"%s", line); |
| 282 | } |
| 283 | } |
| 284 | if (f) fclose(f); |
| 285 | io_printf(fd,"\n"); |
| 286 | } |
| 287 | |
| 288 | if (!read_line(fd, line, sizeof(line)-1)) { |
| 289 | return -1; |
| 290 | } |
| 291 | |
| 292 | if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) { |
| 293 | io_printf(fd,"@ERROR: protocol startup error\n"); |
| 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | while (i == -1) { |
| 298 | line[0] = 0; |
| 299 | if (!read_line(fd, line, sizeof(line)-1)) { |
| 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | if (!*line || strcmp(line,"#list")==0) { |
| 304 | send_listing(fd); |
| 305 | return -1; |
| 306 | } |
| 307 | |
| 308 | if (*line == '#') { |
| 309 | /* it's some sort of command that I don't understand */ |
| 310 | io_printf(fd,"@ERROR: Unknown command '%s'\n", line); |
| 311 | return -1; |
| 312 | } |
| 313 | |
| 314 | i = lp_number(line); |
| 315 | if (i == -1) { |
| 316 | io_printf(fd,"@ERROR: Unknown module '%s'\n", line); |
| 317 | return -1; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return rsync_module(fd, i); |
| 322 | } |
| 323 | |
| 324 | |
| 325 | int daemon_main(void) |
| 326 | { |
| 327 | if (is_a_socket(STDIN_FILENO)) { |
| 328 | /* we are running via inetd */ |
| 329 | return start_daemon(STDIN_FILENO); |
| 330 | } |
| 331 | |
| 332 | become_daemon(); |
| 333 | |
| 334 | start_accept_loop(rsync_port, start_daemon); |
| 335 | return -1; |
| 336 | } |
| 337 | |