Make daemon-exclude errors more error-like.
[rsync/rsync.git] / authenticate.c
CommitLineData
0f78b815
WD
1/*
2 * Support rsync daemon authentication.
3 *
4 * Copyright (C) 1998-2000 Andrew Tridgell
b3bf9b9d 5 * Copyright (C) 2002-2009 Wayne Davison
0f78b815
WD
6 *
7 * This program is free software; you can redistribute it and/or modify
8e41b68e
WD
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
0f78b815
WD
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
e7c67065 17 * You should have received a copy of the GNU General Public License along
4fd842f9 18 * with this program; if not, visit the http://fsf.org website.
0f78b815 19 */
18cc8c7e 20
31593dd6 21#include "rsync.h"
5ebe9a46 22#include "itypes.h"
31593dd6 23
5ebe9a46 24extern int read_only;
38cab94d 25extern char *password_file;
38cab94d 26
bcb7e502
AT
27/***************************************************************************
28encode a buffer using base64 - simple and slow algorithm. null terminates
29the result.
30 ***************************************************************************/
4a19c3b2 31void base64_encode(const char *buf, int len, char *out, int pad)
bcb7e502
AT
32{
33 char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
34 int bit_offset, byte_offset, idx, i;
4a19c3b2 35 const uchar *d = (const uchar *)buf;
bcb7e502
AT
36 int bytes = (len*8 + 5)/6;
37
57385128 38 for (i = 0; i < bytes; i++) {
bcb7e502
AT
39 byte_offset = (i*6)/8;
40 bit_offset = (i*6)%8;
41 if (bit_offset < 3) {
42 idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
43 } else {
44 idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
45 if (byte_offset+1 < len) {
46 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
47 }
48 }
49 out[i] = b64[idx];
50 }
293def60
WD
51
52 while (pad && (i % 4))
53 out[i++] = '=';
54
55 out[i] = '\0';
bcb7e502
AT
56}
57
bf011fed 58/* Generate a challenge buffer and return it base64-encoded. */
4a19c3b2 59static void gen_challenge(const char *addr, char *challenge)
bcb7e502
AT
60{
61 char input[32];
a0456b9c 62 char digest[MAX_DIGEST_LEN];
bcb7e502 63 struct timeval tv;
a0456b9c 64 int len;
bcb7e502 65
58c9b4b7 66 memset(input, 0, sizeof input);
bcb7e502 67
4a19c3b2 68 strlcpy(input, addr, 17);
3060d4aa 69 sys_gettimeofday(&tv);
bcb7e502
AT
70 SIVAL(input, 16, tv.tv_sec);
71 SIVAL(input, 20, tv.tv_usec);
72 SIVAL(input, 24, getpid());
73
ba582f75 74 sum_init(0);
58c9b4b7 75 sum_update(input, sizeof input);
a0456b9c 76 len = sum_end(digest);
bf011fed 77
a0456b9c 78 base64_encode(digest, len, challenge, 0);
bcb7e502
AT
79}
80
5ebe9a46
WD
81/* Generate an MD4 hash created from the combination of the password
82 * and the challenge string and return it base64-encoded. */
83static void generate_hash(const char *in, const char *challenge, char *out)
84{
85 char buf[MAX_DIGEST_LEN];
86 int len;
87
88 sum_init(0);
89 sum_update(in, strlen(in));
90 sum_update(challenge, strlen(challenge));
91 len = sum_end(buf);
92
93 base64_encode(buf, len, out, 0);
94}
bcb7e502 95
38cab94d
WD
96/* Return the secret for a user from the secret file, null terminated.
97 * Maximum length is len (not counting the null). */
5ebe9a46
WD
98static const char *check_secret(int module, const char *user, const char *group,
99 const char *challenge, const char *pass)
bcb7e502 100{
5ebe9a46
WD
101 char line[1024];
102 char pass2[MAX_DIGEST_LEN*2];
4a19c3b2 103 const char *fname = lp_secrets_file(module);
d1be2312 104 STRUCT_STAT st;
38cab94d 105 int fd, ok = 1;
5ebe9a46
WD
106 int user_len = strlen(user);
107 int group_len = group ? strlen(group) : 0;
108 char *err;
bcb7e502 109
5ebe9a46
WD
110 if (!fname || !*fname || (fd = open(fname, O_RDONLY)) < 0)
111 return "no secrets file";
bcb7e502 112
5ebe9a46
WD
113 if (do_fstat(fd, &st) == -1) {
114 rsyserr(FLOG, errno, "fstat(%s)", fname);
d1be2312 115 ok = 0;
3ca8e68f
DD
116 } else if (lp_strict_modes(module)) {
117 if ((st.st_mode & 06) != 0) {
30c041f9 118 rprintf(FLOG, "secrets file must not be other-accessible (see strict modes option)\n");
3ca8e68f 119 ok = 0;
351f5e2f 120 } else if (MY_UID() == 0 && st.st_uid != 0) {
30c041f9 121 rprintf(FLOG, "secrets file must be owned by root when running as root (see strict modes)\n");
3ca8e68f
DD
122 ok = 0;
123 }
d1be2312
DD
124 }
125 if (!ok) {
d1be2312 126 close(fd);
5ebe9a46 127 return "ignoring secrets file";
d1be2312
DD
128 }
129
38cab94d
WD
130 if (*user == '#') {
131 /* Reject attempt to match a comment. */
132 close(fd);
5ebe9a46 133 return "invalid username";
38cab94d
WD
134 }
135
5ebe9a46
WD
136 /* Try to find a line that starts with the user (or @group) name and a ':'. */
137 err = "secret not found";
138 while ((user || group) && read_line_old(fd, line, sizeof line, 1)) {
139 const char **ptr, *s;
140 int len;
141 if (*line == '@') {
142 ptr = &group;
143 len = group_len;
144 s = line+1;
145 } else {
146 ptr = &user;
147 len = user_len;
148 s = line;
38cab94d 149 }
5ebe9a46
WD
150 if (!*ptr || strncmp(s, *ptr, len) != 0 || s[len] != ':')
151 continue;
152 generate_hash(s+len+1, challenge, pass2);
153 if (strcmp(pass, pass2) == 0) {
154 err = NULL;
155 break;
bcb7e502 156 }
5ebe9a46
WD
157 err = "password mismatch";
158 *ptr = NULL; /* Don't look for name again. */
bcb7e502
AT
159 }
160
161 close(fd);
bcb7e502 162
5ebe9a46
WD
163 memset(line, 0, sizeof line);
164 memset(pass2, 0, sizeof pass2);
165
166 return err;
bcb7e502
AT
167}
168
4a19c3b2 169static const char *getpassf(const char *filename)
65575e96 170{
65575e96 171 STRUCT_STAT st;
38cab94d
WD
172 char buffer[512], *p;
173 int fd, n, ok = 1;
4a19c3b2 174 const char *envpw = getenv("RSYNC_PASSWORD");
65575e96 175
38cab94d
WD
176 if (!filename)
177 return NULL;
65575e96 178
38cab94d 179 if ((fd = open(filename,O_RDONLY)) < 0) {
3f0211b6 180 rsyserr(FWARNING, errno, "could not open password file \"%s\"",
45c49b52 181 filename);
38cab94d 182 if (envpw)
3f655ca0 183 rprintf(FINFO, "falling back to RSYNC_PASSWORD environment variable.\n");
65575e96
AT
184 return NULL;
185 }
18cc8c7e 186
65575e96 187 if (do_stat(filename, &st) == -1) {
3f0211b6 188 rsyserr(FWARNING, errno, "stat(%s)", filename);
65575e96
AT
189 ok = 0;
190 } else if ((st.st_mode & 06) != 0) {
3f0211b6 191 rprintf(FWARNING, "password file must not be other-accessible\n");
65575e96 192 ok = 0;
351f5e2f 193 } else if (MY_UID() == 0 && st.st_uid != 0) {
3f0211b6 194 rprintf(FWARNING, "password file must be owned by root when running as root\n");
65575e96
AT
195 ok = 0;
196 }
197 if (!ok) {
65575e96 198 close(fd);
3f0211b6 199 rprintf(FWARNING, "continuing without password file\n");
3f655ca0
WD
200 if (envpw)
201 rprintf(FINFO, "falling back to RSYNC_PASSWORD environment variable.\n");
65575e96
AT
202 return NULL;
203 }
204
38cab94d
WD
205 n = read(fd, buffer, sizeof buffer - 1);
206 close(fd);
207 if (n > 0) {
208 buffer[n] = '\0';
209 if ((p = strtok(buffer, "\n\r")) != NULL)
210 return strdup(p);
18cc8c7e 211 }
65575e96
AT
212
213 return NULL;
214}
215
18cc8c7e
WD
216/* Possibly negotiate authentication with the client. Use "leader" to
217 * start off the auth if necessary.
218 *
219 * Return NULL if authentication failed. Return "" if anonymous access.
220 * Otherwise return username.
221 */
4a19c3b2
WD
222char *auth_server(int f_in, int f_out, int module, const char *host,
223 const char *addr, const char *leader)
bcb7e502
AT
224{
225 char *users = lp_auth_users(module);
a0456b9c 226 char challenge[MAX_DIGEST_LEN*2];
d999d312 227 char line[BIGPATHBUFLEN];
5ebe9a46
WD
228 char **auth_uid_groups = NULL;
229 int auth_uid_groups_cnt = -1;
230 const char *err = NULL;
231 int group_match = -1;
5037cf3a 232 char *tok, *pass;
5ebe9a46 233 char opt_ch = '\0';
bcb7e502
AT
234
235 /* if no auth list then allow anyone in! */
58c9b4b7
WD
236 if (!users || !*users)
237 return "";
bcb7e502
AT
238
239 gen_challenge(addr, challenge);
18cc8c7e 240
bf011fed 241 io_printf(f_out, "%s%s\n", leader, challenge);
bcb7e502 242
5ebe9a46 243 if (!read_line_old(f_in, line, sizeof line, 0)
5037cf3a
WD
244 || (pass = strchr(line, ' ')) == NULL) {
245 rprintf(FLOG, "auth failed on module %s from %s (%s): "
246 "invalid challenge response\n",
247 lp_name(module), host, addr);
d0d56395 248 return NULL;
5037cf3a
WD
249 }
250 *pass++ = '\0';
18cc8c7e 251
5037cf3a
WD
252 if (!(users = strdup(users)))
253 out_of_memory("auth_server");
c8e78d87 254
5037cf3a 255 for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
5ebe9a46
WD
256 char *opts;
257 /* See if the user appended :deny, :ro, or :rw. */
258 if ((opts = strchr(tok, ':')) != NULL) {
259 *opts++ = '\0';
260 opt_ch = isUpper(opts) ? toLower(opts) : *opts;
261 if (opt_ch == 'r') { /* handle ro and rw */
262 opt_ch = isUpper(opts+1) ? toLower(opts+1) : opts[1];
263 if (opt_ch == 'o')
264 opt_ch = 'r';
265 else if (opt_ch != 'w')
266 opt_ch = '\0';
267 } else if (opt_ch != 'd') /* if it's not deny, ignore it */
268 opt_ch = '\0';
269 } else
270 opt_ch = '\0';
271 if (*tok != '@') {
272 /* Match the username */
273 if (wildmatch(tok, line))
274 break;
275 } else {
276#ifdef HAVE_GETGROUPLIST
277 int j;
278 /* See if authorizing user is a real user, and if so, see
279 * if it is in a group that matches tok+1 wildmat. */
280 if (auth_uid_groups_cnt < 0) {
281 gid_t gid_list[64];
282 uid_t auth_uid;
283 auth_uid_groups_cnt = sizeof gid_list / sizeof (gid_t);
284 if (!user_to_uid(line, &auth_uid, False)
285 || getallgroups(auth_uid, gid_list, &auth_uid_groups_cnt) != NULL)
286 auth_uid_groups_cnt = 0;
287 else {
288 if ((auth_uid_groups = new_array(char *, auth_uid_groups_cnt)) == NULL)
289 out_of_memory("auth_server");
290 for (j = 0; j < auth_uid_groups_cnt; j++)
291 auth_uid_groups[j] = gid_to_group(gid_list[j]);
292 }
293 }
294 for (j = 0; j < auth_uid_groups_cnt; j++) {
295 if (auth_uid_groups[j] && wildmatch(tok+1, auth_uid_groups[j])) {
296 group_match = j;
297 break;
298 }
299 }
300 if (group_match >= 0)
301 break;
302#else
303 rprintf(FLOG, "your computer doesn't support getgrouplist(), so no @group authorization is possible.\n");
304#endif
305 }
c8e78d87 306 }
5ebe9a46 307
c8e78d87
AT
308 free(users);
309
5ebe9a46
WD
310 if (!tok)
311 err = "no matching rule";
312 else if (opt_ch == 'd')
313 err = "denied by rule";
314 else {
315 char *group = group_match >= 0 ? auth_uid_groups[group_match] : NULL;
316 err = check_secret(module, line, group, challenge, pass);
5037cf3a 317 }
18cc8c7e 318
5ebe9a46
WD
319 memset(challenge, 0, sizeof challenge);
320 memset(pass, 0, strlen(pass));
bcb7e502 321
5ebe9a46
WD
322 if (auth_uid_groups) {
323 int j;
324 for (j = 0; j < auth_uid_groups_cnt; j++) {
325 if (auth_uid_groups[j])
326 free(auth_uid_groups[j]);
327 }
328 free(auth_uid_groups);
329 }
18cc8c7e 330
5ebe9a46
WD
331 if (err) {
332 rprintf(FLOG, "auth failed on module %s from %s (%s) for %s: %s\n",
333 lp_name(module), host, addr, line, err);
5037cf3a
WD
334 return NULL;
335 }
d0d56395 336
5ebe9a46
WD
337 if (opt_ch == 'r')
338 read_only = 1;
339 else if (opt_ch == 'w')
340 read_only = 0;
341
5037cf3a 342 return strdup(line);
bcb7e502
AT
343}
344
4a19c3b2 345void auth_client(int fd, const char *user, const char *challenge)
bcb7e502 346{
4a19c3b2 347 const char *pass;
a0456b9c 348 char pass2[MAX_DIGEST_LEN*2];
bcb7e502 349
ef383c0d 350 if (!user || !*user)
4b2f6a7c 351 user = "nobody";
bcb7e502 352
58c9b4b7
WD
353 if (!(pass = getpassf(password_file))
354 && !(pass = getenv("RSYNC_PASSWORD"))) {
64bd7568 355 /* XXX: cyeoh says that getpass is deprecated, because
908f5a9f
MP
356 * it may return a truncated password on some systems,
357 * and it is not in the LSB.
358 *
359 * Andrew Klein says that getpassphrase() is present
360 * on Solaris and reads up to 256 characters.
361 *
362 * OpenBSD has a readpassphrase() that might be more suitable.
363 */
bcb7e502
AT
364 pass = getpass("Password: ");
365 }
366
38cab94d 367 if (!pass)
bcb7e502 368 pass = "";
bcb7e502
AT
369
370 generate_hash(pass, challenge, pass2);
bcb7e502
AT
371 io_printf(fd, "%s %s\n", user, pass2);
372}