Updated to work with git instead of cvs.
[rsync/rsync.git] / authenticate.c
CommitLineData
0f78b815
WD
1/*
2 * Support rsync daemon authentication.
3 *
4 * Copyright (C) 1998-2000 Andrew Tridgell
ba2133d6 5 * Copyright (C) 2002-2007 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
AT
21#include "rsync.h"
22
38cab94d 23extern char *password_file;
c9b16cda 24extern int log_got_error;
38cab94d 25
bcb7e502
AT
26/***************************************************************************
27encode a buffer using base64 - simple and slow algorithm. null terminates
28the result.
29 ***************************************************************************/
4a19c3b2 30void base64_encode(const char *buf, int len, char *out, int pad)
bcb7e502
AT
31{
32 char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
33 int bit_offset, byte_offset, idx, i;
4a19c3b2 34 const uchar *d = (const uchar *)buf;
bcb7e502
AT
35 int bytes = (len*8 + 5)/6;
36
57385128 37 for (i = 0; i < bytes; i++) {
bcb7e502
AT
38 byte_offset = (i*6)/8;
39 bit_offset = (i*6)%8;
40 if (bit_offset < 3) {
41 idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
42 } else {
43 idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
44 if (byte_offset+1 < len) {
45 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
46 }
47 }
48 out[i] = b64[idx];
49 }
293def60
WD
50
51 while (pad && (i % 4))
52 out[i++] = '=';
53
54 out[i] = '\0';
bcb7e502
AT
55}
56
bf011fed 57/* Generate a challenge buffer and return it base64-encoded. */
4a19c3b2 58static void gen_challenge(const char *addr, char *challenge)
bcb7e502
AT
59{
60 char input[32];
a0456b9c 61 char digest[MAX_DIGEST_LEN];
bcb7e502 62 struct timeval tv;
a0456b9c 63 int len;
bcb7e502 64
58c9b4b7 65 memset(input, 0, sizeof input);
bcb7e502 66
4a19c3b2 67 strlcpy(input, addr, 17);
3060d4aa 68 sys_gettimeofday(&tv);
bcb7e502
AT
69 SIVAL(input, 16, tv.tv_sec);
70 SIVAL(input, 20, tv.tv_usec);
71 SIVAL(input, 24, getpid());
72
ba582f75 73 sum_init(0);
58c9b4b7 74 sum_update(input, sizeof input);
a0456b9c 75 len = sum_end(digest);
bf011fed 76
a0456b9c 77 base64_encode(digest, len, challenge, 0);
bcb7e502
AT
78}
79
80
38cab94d
WD
81/* Return the secret for a user from the secret file, null terminated.
82 * Maximum length is len (not counting the null). */
4a19c3b2 83static int get_secret(int module, const char *user, char *secret, int len)
bcb7e502 84{
4a19c3b2 85 const char *fname = lp_secrets_file(module);
d1be2312 86 STRUCT_STAT st;
38cab94d 87 int fd, ok = 1;
4a19c3b2
WD
88 const char *p;
89 char ch, *s;
bcb7e502 90
38cab94d
WD
91 if (!fname || !*fname)
92 return 0;
bcb7e502 93
38cab94d
WD
94 if ((fd = open(fname, O_RDONLY)) < 0)
95 return 0;
bcb7e502 96
d1be2312 97 if (do_stat(fname, &st) == -1) {
45c49b52 98 rsyserr(FLOG, errno, "stat(%s)", fname);
d1be2312 99 ok = 0;
3ca8e68f
DD
100 } else if (lp_strict_modes(module)) {
101 if ((st.st_mode & 06) != 0) {
30c041f9 102 rprintf(FLOG, "secrets file must not be other-accessible (see strict modes option)\n");
3ca8e68f 103 ok = 0;
351f5e2f 104 } else if (MY_UID() == 0 && st.st_uid != 0) {
30c041f9 105 rprintf(FLOG, "secrets file must be owned by root when running as root (see strict modes)\n");
3ca8e68f
DD
106 ok = 0;
107 }
d1be2312
DD
108 }
109 if (!ok) {
30c041f9 110 rprintf(FLOG, "continuing without secrets file\n");
d1be2312
DD
111 close(fd);
112 return 0;
113 }
114
38cab94d
WD
115 if (*user == '#') {
116 /* Reject attempt to match a comment. */
117 close(fd);
118 return 0;
119 }
120
121 /* Try to find a line that starts with the user name and a ':'. */
122 p = user;
123 while (1) {
124 if (read(fd, &ch, 1) != 1) {
125 close(fd);
126 return 0;
127 }
128 if (ch == '\n')
129 p = user;
130 else if (p) {
131 if (*p == ch)
132 p++;
133 else if (!*p && ch == ':')
134 break;
135 else
136 p = NULL;
bcb7e502 137 }
bcb7e502
AT
138 }
139
38cab94d 140 /* Slurp the secret into the "secret" buffer. */
4a19c3b2 141 s = secret;
38cab94d 142 while (len > 0) {
4a19c3b2 143 if (read(fd, s, 1) != 1 || *s == '\n')
38cab94d 144 break;
4a19c3b2 145 if (*s == '\r')
38cab94d 146 continue;
4a19c3b2 147 s++;
38cab94d
WD
148 len--;
149 }
4a19c3b2 150 *s = '\0';
bcb7e502 151 close(fd);
bcb7e502 152
bcb7e502
AT
153 return 1;
154}
155
4a19c3b2 156static const char *getpassf(const char *filename)
65575e96 157{
65575e96 158 STRUCT_STAT st;
38cab94d
WD
159 char buffer[512], *p;
160 int fd, n, ok = 1;
4a19c3b2 161 const char *envpw = getenv("RSYNC_PASSWORD");
65575e96 162
38cab94d
WD
163 if (!filename)
164 return NULL;
65575e96 165
38cab94d 166 if ((fd = open(filename,O_RDONLY)) < 0) {
4875d6b6 167 rsyserr(FERROR, errno, "could not open password file \"%s\"",
45c49b52 168 filename);
38cab94d 169 if (envpw)
3f655ca0 170 rprintf(FINFO, "falling back to RSYNC_PASSWORD environment variable.\n");
65575e96
AT
171 return NULL;
172 }
18cc8c7e 173
65575e96 174 if (do_stat(filename, &st) == -1) {
45c49b52 175 rsyserr(FERROR, errno, "stat(%s)", filename);
65575e96
AT
176 ok = 0;
177 } else if ((st.st_mode & 06) != 0) {
3f655ca0 178 rprintf(FERROR, "password file must not be other-accessible\n");
65575e96 179 ok = 0;
351f5e2f 180 } else if (MY_UID() == 0 && st.st_uid != 0) {
3f655ca0 181 rprintf(FERROR, "password file must be owned by root when running as root\n");
65575e96
AT
182 ok = 0;
183 }
184 if (!ok) {
65575e96 185 close(fd);
3f655ca0
WD
186 rprintf(FERROR, "continuing without password file\n");
187 if (envpw)
188 rprintf(FINFO, "falling back to RSYNC_PASSWORD environment variable.\n");
65575e96
AT
189 return NULL;
190 }
191
38cab94d
WD
192 n = read(fd, buffer, sizeof buffer - 1);
193 close(fd);
194 if (n > 0) {
195 buffer[n] = '\0';
196 if ((p = strtok(buffer, "\n\r")) != NULL)
197 return strdup(p);
18cc8c7e 198 }
65575e96
AT
199
200 return NULL;
201}
202
bf011fed
WD
203/* Generate an MD4 hash created from the combination of the password
204 * and the challenge string and return it base64-encoded. */
4a19c3b2 205static void generate_hash(const char *in, const char *challenge, char *out)
bcb7e502 206{
a0456b9c
WD
207 char buf[MAX_DIGEST_LEN];
208 int len;
bcb7e502 209
ba582f75 210 sum_init(0);
bcb7e502
AT
211 sum_update(in, strlen(in));
212 sum_update(challenge, strlen(challenge));
a0456b9c 213 len = sum_end(buf);
bcb7e502 214
a0456b9c 215 base64_encode(buf, len, out, 0);
bcb7e502
AT
216}
217
18cc8c7e
WD
218/* Possibly negotiate authentication with the client. Use "leader" to
219 * start off the auth if necessary.
220 *
221 * Return NULL if authentication failed. Return "" if anonymous access.
222 * Otherwise return username.
223 */
4a19c3b2
WD
224char *auth_server(int f_in, int f_out, int module, const char *host,
225 const char *addr, const char *leader)
bcb7e502
AT
226{
227 char *users = lp_auth_users(module);
a0456b9c 228 char challenge[MAX_DIGEST_LEN*2];
d999d312 229 char line[BIGPATHBUFLEN];
5037cf3a 230 char secret[512];
a0456b9c 231 char pass2[MAX_DIGEST_LEN*2];
5037cf3a 232 char *tok, *pass;
bcb7e502
AT
233
234 /* if no auth list then allow anyone in! */
58c9b4b7
WD
235 if (!users || !*users)
236 return "";
bcb7e502
AT
237
238 gen_challenge(addr, challenge);
18cc8c7e 239
bf011fed 240 io_printf(f_out, "%s%s\n", leader, challenge);
bcb7e502 241
f74a3d81 242 if (!read_line_old(f_in, line, sizeof line)
5037cf3a
WD
243 || (pass = strchr(line, ' ')) == NULL) {
244 rprintf(FLOG, "auth failed on module %s from %s (%s): "
245 "invalid challenge response\n",
246 lp_name(module), host, addr);
d0d56395 247 return NULL;
5037cf3a
WD
248 }
249 *pass++ = '\0';
18cc8c7e 250
5037cf3a
WD
251 if (!(users = strdup(users)))
252 out_of_memory("auth_server");
c8e78d87 253
5037cf3a
WD
254 for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
255 if (wildmatch(tok, line))
58c9b4b7 256 break;
c8e78d87
AT
257 }
258 free(users);
259
5037cf3a
WD
260 if (!tok) {
261 rprintf(FLOG, "auth failed on module %s from %s (%s): "
262 "unauthorized user\n",
263 lp_name(module), host, addr);
d0d56395 264 return NULL;
5037cf3a 265 }
18cc8c7e 266
58c9b4b7 267 memset(secret, 0, sizeof secret);
5037cf3a 268 if (!get_secret(module, line, secret, sizeof secret - 1)) {
58c9b4b7 269 memset(secret, 0, sizeof secret);
5037cf3a
WD
270 rprintf(FLOG, "auth failed on module %s from %s (%s): "
271 "missing secret for user \"%s\"\n",
272 lp_name(module), host, addr, line);
d0d56395 273 return NULL;
bcb7e502
AT
274 }
275
bf011fed 276 generate_hash(secret, challenge, pass2);
58c9b4b7 277 memset(secret, 0, sizeof secret);
18cc8c7e 278
5037cf3a
WD
279 if (strcmp(pass, pass2) != 0) {
280 rprintf(FLOG, "auth failed on module %s from %s (%s): "
281 "password mismatch\n",
282 lp_name(module), host, addr);
283 return NULL;
284 }
d0d56395 285
5037cf3a 286 return strdup(line);
bcb7e502
AT
287}
288
4a19c3b2 289void auth_client(int fd, const char *user, const char *challenge)
bcb7e502 290{
4a19c3b2 291 const char *pass;
a0456b9c 292 char pass2[MAX_DIGEST_LEN*2];
bcb7e502 293
ef383c0d 294 if (!user || !*user)
4b2f6a7c 295 user = "nobody";
bcb7e502 296
58c9b4b7
WD
297 if (!(pass = getpassf(password_file))
298 && !(pass = getenv("RSYNC_PASSWORD"))) {
64bd7568 299 /* XXX: cyeoh says that getpass is deprecated, because
908f5a9f
MP
300 * it may return a truncated password on some systems,
301 * and it is not in the LSB.
302 *
303 * Andrew Klein says that getpassphrase() is present
304 * on Solaris and reads up to 256 characters.
305 *
306 * OpenBSD has a readpassphrase() that might be more suitable.
307 */
bcb7e502
AT
308 pass = getpass("Password: ");
309 }
310
c9b16cda
WD
311 /* Any errors output during password handling aren't transfer errors. */
312 log_got_error = 0;
313
38cab94d 314 if (!pass)
bcb7e502 315 pass = "";
bcb7e502
AT
316
317 generate_hash(pass, challenge, pass2);
bcb7e502
AT
318 io_printf(fd, "%s %s\n", user, pass2);
319}