Tweaked some whitespace to match the latest version from autoconf.
[rsync/rsync.git] / authenticate.c
CommitLineData
4a13b9d5 1/* -*- c-file-style: "linux"; -*-
18cc8c7e
WD
2
3 Copyright (C) 1998-2000 by Andrew Tridgell
4
31593dd6
AT
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
18cc8c7e 9
31593dd6
AT
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
18cc8c7e 14
31593dd6
AT
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20/* support rsync authentication */
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 ***************************************************************************/
293def60 29void base64_encode(char *buf, int len, char *out, int pad)
bcb7e502
AT
30{
31 char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
32 int bit_offset, byte_offset, idx, i;
33 unsigned char *d = (unsigned char *)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. */
bcb7e502
AT
57static void gen_challenge(char *addr, char *challenge)
58{
59 char input[32];
bf011fed 60 char md4_out[MD4_SUM_LENGTH];
bcb7e502
AT
61 struct timeval tv;
62
58c9b4b7 63 memset(input, 0, sizeof input);
bcb7e502 64
37f9805d 65 strlcpy((char *)input, addr, 17);
3060d4aa 66 sys_gettimeofday(&tv);
bcb7e502
AT
67 SIVAL(input, 16, tv.tv_sec);
68 SIVAL(input, 20, tv.tv_usec);
69 SIVAL(input, 24, getpid());
70
ba582f75 71 sum_init(0);
58c9b4b7 72 sum_update(input, sizeof input);
bf011fed
WD
73 sum_end(md4_out);
74
293def60 75 base64_encode(md4_out, MD4_SUM_LENGTH, challenge, 0);
bcb7e502
AT
76}
77
78
38cab94d
WD
79/* Return the secret for a user from the secret file, null terminated.
80 * Maximum length is len (not counting the null). */
bcb7e502
AT
81static int get_secret(int module, char *user, char *secret, int len)
82{
83 char *fname = lp_secrets_file(module);
d1be2312 84 STRUCT_STAT st;
38cab94d
WD
85 int fd, ok = 1;
86 char ch, *p;
bcb7e502 87
38cab94d
WD
88 if (!fname || !*fname)
89 return 0;
bcb7e502 90
38cab94d
WD
91 if ((fd = open(fname, O_RDONLY)) < 0)
92 return 0;
bcb7e502 93
d1be2312 94 if (do_stat(fname, &st) == -1) {
45c49b52 95 rsyserr(FLOG, errno, "stat(%s)", fname);
d1be2312 96 ok = 0;
3ca8e68f
DD
97 } else if (lp_strict_modes(module)) {
98 if ((st.st_mode & 06) != 0) {
30c041f9 99 rprintf(FLOG, "secrets file must not be other-accessible (see strict modes option)\n");
3ca8e68f 100 ok = 0;
351f5e2f 101 } else if (MY_UID() == 0 && st.st_uid != 0) {
30c041f9 102 rprintf(FLOG, "secrets file must be owned by root when running as root (see strict modes)\n");
3ca8e68f
DD
103 ok = 0;
104 }
d1be2312
DD
105 }
106 if (!ok) {
30c041f9 107 rprintf(FLOG, "continuing without secrets file\n");
d1be2312
DD
108 close(fd);
109 return 0;
110 }
111
38cab94d
WD
112 if (*user == '#') {
113 /* Reject attempt to match a comment. */
114 close(fd);
115 return 0;
116 }
117
118 /* Try to find a line that starts with the user name and a ':'. */
119 p = user;
120 while (1) {
121 if (read(fd, &ch, 1) != 1) {
122 close(fd);
123 return 0;
124 }
125 if (ch == '\n')
126 p = user;
127 else if (p) {
128 if (*p == ch)
129 p++;
130 else if (!*p && ch == ':')
131 break;
132 else
133 p = NULL;
bcb7e502 134 }
bcb7e502
AT
135 }
136
38cab94d
WD
137 /* Slurp the secret into the "secret" buffer. */
138 p = secret;
139 while (len > 0) {
140 if (read(fd, p, 1) != 1 || *p == '\n')
141 break;
142 if (*p == '\r')
143 continue;
144 p++;
145 len--;
146 }
147 *p = '\0';
bcb7e502 148 close(fd);
bcb7e502 149
bcb7e502
AT
150 return 1;
151}
152
65575e96
AT
153static char *getpassf(char *filename)
154{
65575e96 155 STRUCT_STAT st;
38cab94d
WD
156 char buffer[512], *p;
157 int fd, n, ok = 1;
158 char *envpw = getenv("RSYNC_PASSWORD");
65575e96 159
38cab94d
WD
160 if (!filename)
161 return NULL;
65575e96 162
38cab94d 163 if ((fd = open(filename,O_RDONLY)) < 0) {
4875d6b6 164 rsyserr(FERROR, errno, "could not open password file \"%s\"",
45c49b52 165 filename);
38cab94d 166 if (envpw)
18cc8c7e 167 rprintf(FERROR, "falling back to RSYNC_PASSWORD environment variable.\n");
65575e96
AT
168 return NULL;
169 }
18cc8c7e 170
65575e96 171 if (do_stat(filename, &st) == -1) {
45c49b52 172 rsyserr(FERROR, errno, "stat(%s)", filename);
65575e96
AT
173 ok = 0;
174 } else if ((st.st_mode & 06) != 0) {
175 rprintf(FERROR,"password file must not be other-accessible\n");
176 ok = 0;
351f5e2f 177 } else if (MY_UID() == 0 && st.st_uid != 0) {
65575e96
AT
178 rprintf(FERROR,"password file must be owned by root when running as root\n");
179 ok = 0;
180 }
181 if (!ok) {
182 rprintf(FERROR,"continuing without password file\n");
38cab94d
WD
183 if (envpw)
184 rprintf(FERROR, "using RSYNC_PASSWORD environment variable.\n");
65575e96
AT
185 close(fd);
186 return NULL;
187 }
188
38cab94d
WD
189 if (envpw)
190 rprintf(FERROR, "RSYNC_PASSWORD environment variable ignored\n");
65575e96 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. */
6e4fb64e 205static void generate_hash(char *in, char *challenge, char *out)
bcb7e502 206{
5037cf3a 207 char buf[MD4_SUM_LENGTH];
bcb7e502 208
ba582f75 209 sum_init(0);
bcb7e502
AT
210 sum_update(in, strlen(in));
211 sum_update(challenge, strlen(challenge));
212 sum_end(buf);
213
293def60 214 base64_encode(buf, MD4_SUM_LENGTH, out, 0);
bcb7e502
AT
215}
216
18cc8c7e
WD
217/* Possibly negotiate authentication with the client. Use "leader" to
218 * start off the auth if necessary.
219 *
220 * Return NULL if authentication failed. Return "" if anonymous access.
221 * Otherwise return username.
222 */
5037cf3a
WD
223char *auth_server(int f_in, int f_out, int module, char *host, char *addr,
224 char *leader)
bcb7e502
AT
225{
226 char *users = lp_auth_users(module);
bf011fed 227 char challenge[MD4_SUM_LENGTH*2];
d999d312 228 char line[BIGPATHBUFLEN];
5037cf3a
WD
229 char secret[512];
230 char pass2[MD4_SUM_LENGTH*2];
231 char *tok, *pass;
bcb7e502
AT
232
233 /* if no auth list then allow anyone in! */
58c9b4b7
WD
234 if (!users || !*users)
235 return "";
bcb7e502
AT
236
237 gen_challenge(addr, challenge);
18cc8c7e 238
bf011fed 239 io_printf(f_out, "%s%s\n", leader, challenge);
bcb7e502 240
5037cf3a
WD
241 if (!read_line(f_in, line, sizeof line - 1)
242 || (pass = strchr(line, ' ')) == NULL) {
243 rprintf(FLOG, "auth failed on module %s from %s (%s): "
244 "invalid challenge response\n",
245 lp_name(module), host, addr);
d0d56395 246 return NULL;
5037cf3a
WD
247 }
248 *pass++ = '\0';
18cc8c7e 249
5037cf3a
WD
250 if (!(users = strdup(users)))
251 out_of_memory("auth_server");
c8e78d87 252
5037cf3a
WD
253 for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
254 if (wildmatch(tok, line))
58c9b4b7 255 break;
c8e78d87
AT
256 }
257 free(users);
258
5037cf3a
WD
259 if (!tok) {
260 rprintf(FLOG, "auth failed on module %s from %s (%s): "
261 "unauthorized user\n",
262 lp_name(module), host, addr);
d0d56395 263 return NULL;
5037cf3a 264 }
18cc8c7e 265
58c9b4b7 266 memset(secret, 0, sizeof secret);
5037cf3a 267 if (!get_secret(module, line, secret, sizeof secret - 1)) {
58c9b4b7 268 memset(secret, 0, sizeof secret);
5037cf3a
WD
269 rprintf(FLOG, "auth failed on module %s from %s (%s): "
270 "missing secret for user \"%s\"\n",
271 lp_name(module), host, addr, line);
d0d56395 272 return NULL;
bcb7e502
AT
273 }
274
bf011fed 275 generate_hash(secret, challenge, pass2);
58c9b4b7 276 memset(secret, 0, sizeof secret);
18cc8c7e 277
5037cf3a
WD
278 if (strcmp(pass, pass2) != 0) {
279 rprintf(FLOG, "auth failed on module %s from %s (%s): "
280 "password mismatch\n",
281 lp_name(module), host, addr);
282 return NULL;
283 }
d0d56395 284
5037cf3a 285 return strdup(line);
bcb7e502
AT
286}
287
288
289void auth_client(int fd, char *user, char *challenge)
290{
291 char *pass;
5037cf3a 292 char pass2[MD4_SUM_LENGTH*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
38cab94d 311 if (!pass)
bcb7e502 312 pass = "";
bcb7e502
AT
313
314 generate_hash(pass, challenge, pass2);
bcb7e502
AT
315 io_printf(fd, "%s %s\n", user, pass2);
316}