cleanup code a bit
[rsync/rsync.git] / clientserver.c
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
51         p = strchr(path,'/');
52         if (p) *p = 0;
53         io_printf(fd,"%s\n",path);
54         if (p) *p = '/';
55
56         if (!read_line(fd, line, sizeof(line)-1)) {
57                 return -1;
58         }
59
60         if (sscanf(line,"@RSYNCD: %d", &version) != 1) {
61                 return -1;
62         }
63
64         while (1) {
65                 if (!read_line(fd, line, sizeof(line)-1)) {
66                         return -1;
67                 }
68                 if (strcmp(line,"@RSYNCD: OK") == 0) break;
69                 rprintf(FINFO,"%s\n", line);
70         }
71
72         for (i=0;i<sargc;i++) {
73                 io_printf(fd,"%s\n", sargs[i]);
74         }
75         io_printf(fd,"\n");
76
77 #if 0
78         while (1) {
79                 if (!read_line(fd, line, sizeof(line)-1)) {
80                         return -1;
81                 }
82                 rprintf(FINFO,"%s\n", line);
83         }
84 #endif
85
86         return client_run(fd, fd, -1, argc, argv);
87 }
88
89
90
91 static int rsync_module(int fd, int i)
92 {
93         int argc=0;
94         char *argv[MAX_ARGS];
95         char **argp;
96         char line[1024];
97
98         module_id = i;
99
100         if (lp_read_only(i))
101                 read_only = 1;
102
103         rprintf(FERROR,"rsyncd starting\n");
104
105         if (chroot(lp_path(i))) {
106                 io_printf(fd,"@ERROR: chroot failed\n");
107                 return -1;
108         }
109
110         if (chdir("/")) {
111                 io_printf(fd,"@ERROR: chdir failed\n");
112                 return -1;
113         }
114
115         if (setgid(lp_gid(i))) {
116                 io_printf(fd,"@ERROR: setgid failed\n");
117                 return -1;
118         }
119
120         if (setuid(lp_uid(i))) {
121                 io_printf(fd,"@ERROR: setuid failed\n");
122                 return -1;
123         }
124
125         io_printf(fd,"@RSYNCD: OK\n");
126
127         argv[argc++] = "rsyncd";
128
129         while (1) {
130                 if (!read_line(fd, line, sizeof(line)-1)) {
131                         return -1;
132                 }
133
134                 if (!*line) break;
135
136                 argv[argc] = strdup(line);
137                 if (!argv[argc]) {
138                         return -1;
139                 }
140
141                 argc++;
142                 if (argc == MAX_ARGS) {
143                         return -1;
144                 }
145         }
146
147         parse_arguments(argc, argv);
148
149         /* don't allow the logs to be flooded too fast */
150         if (verbose > 1) verbose = 1;
151
152         argc -= optind;
153         argp = argv + optind;
154         optind = 0;
155
156         start_server(fd, fd, argc, argp);
157
158         return 0;
159 }
160
161 static void send_listing(int fd)
162 {
163         int n = lp_numservices();
164         int i;
165         
166         for (i=0;i<n;i++)
167                 if (lp_list(i))
168                     io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
169 }
170
171 /* this is called when a socket connection is established to a client
172    and we want to start talking. The setup of the system is done from
173    here */
174 static int start_daemon(int fd)
175 {
176         char line[1024];
177         char *motd;
178
179         set_socket_options(fd,"SO_KEEPALIVE");
180
181         io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
182
183         motd = lp_motd_file();
184         if (*motd) {
185                 FILE *f = fopen(motd,"r");
186                 while (f && !feof(f)) {
187                         int len = fread(line, 1, sizeof(line)-1, f);
188                         if (len > 0) {
189                                 line[len] = 0;
190                                 io_printf(fd,"%s", line);
191                         }
192                 }
193                 if (f) fclose(f);
194                 io_printf(fd,"\n");
195         }
196
197         /* read a single line indicating the resource that is wanted */
198         while (1) {
199                 int i;
200
201                 line[0] = 0;
202                 if (!read_line(fd, line, sizeof(line)-1)) {
203                         return -1;
204                 }
205
206                 if (!*line || strcmp(line,"#list")==0) {
207                         send_listing(fd);
208                         return -1;
209                 } 
210
211                 if (*line == '#') {
212                         /* it's some sort of command that I don't understand */
213                         io_printf(fd,"ERROR: Unknown command '%s'\n", line);
214                         return -1;
215                 }
216
217                 i = lp_number(line);
218                 if (i == -1) {
219                         io_printf(fd,"ERROR: Unknown module '%s'\n", line);
220                         return -1;
221                 }
222
223                 return rsync_module(fd, i);
224         }
225
226         return 0;
227 }
228
229
230 int daemon_main(void)
231 {
232         if (!lp_load(RSYNCD_CONF)) {
233                 exit_cleanup(1);
234         }
235
236         if (is_a_socket(STDIN_FILENO)) {
237                 /* we are running via inetd */
238                 return start_daemon(STDIN_FILENO);
239         }
240
241         become_daemon();
242
243         return start_accept_loop(rsync_port, start_daemon);
244 }
245