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