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