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