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