Tweaked the mode of the files in the tar file.
[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 ***************************************************************************/
57385128 29void base64_encode(char *buf, int len, char *out)
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
36 memset(out, 0, bytes+1);
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 }
51}
52
bf011fed 53/* Generate a challenge buffer and return it base64-encoded. */
bcb7e502
AT
54static void gen_challenge(char *addr, char *challenge)
55{
56 char input[32];
bf011fed 57 char md4_out[MD4_SUM_LENGTH];
bcb7e502
AT
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);
bf011fed
WD
70 sum_end(md4_out);
71
72 base64_encode(md4_out, MD4_SUM_LENGTH, challenge);
bcb7e502
AT
73}
74
75
38cab94d
WD
76/* Return the secret for a user from the secret file, null terminated.
77 * Maximum length is len (not counting the null). */
bcb7e502
AT
78static int get_secret(int module, char *user, char *secret, int len)
79{
80 char *fname = lp_secrets_file(module);
d1be2312 81 STRUCT_STAT st;
38cab94d
WD
82 int fd, ok = 1;
83 char ch, *p;
bcb7e502 84
38cab94d
WD
85 if (!fname || !*fname)
86 return 0;
bcb7e502 87
38cab94d
WD
88 if ((fd = open(fname, O_RDONLY)) < 0)
89 return 0;
bcb7e502 90
d1be2312 91 if (do_stat(fname, &st) == -1) {
45c49b52 92 rsyserr(FLOG, errno, "stat(%s)", fname);
d1be2312 93 ok = 0;
3ca8e68f
DD
94 } else if (lp_strict_modes(module)) {
95 if ((st.st_mode & 06) != 0) {
30c041f9 96 rprintf(FLOG, "secrets file must not be other-accessible (see strict modes option)\n");
3ca8e68f 97 ok = 0;
351f5e2f 98 } else if (MY_UID() == 0 && st.st_uid != 0) {
30c041f9 99 rprintf(FLOG, "secrets file must be owned by root when running as root (see strict modes)\n");
3ca8e68f
DD
100 ok = 0;
101 }
d1be2312
DD
102 }
103 if (!ok) {
30c041f9 104 rprintf(FLOG, "continuing without secrets file\n");
d1be2312
DD
105 close(fd);
106 return 0;
107 }
108
38cab94d
WD
109 if (*user == '#') {
110 /* Reject attempt to match a comment. */
111 close(fd);
112 return 0;
113 }
114
115 /* Try to find a line that starts with the user name and a ':'. */
116 p = user;
117 while (1) {
118 if (read(fd, &ch, 1) != 1) {
119 close(fd);
120 return 0;
121 }
122 if (ch == '\n')
123 p = user;
124 else if (p) {
125 if (*p == ch)
126 p++;
127 else if (!*p && ch == ':')
128 break;
129 else
130 p = NULL;
bcb7e502 131 }
bcb7e502
AT
132 }
133
38cab94d
WD
134 /* Slurp the secret into the "secret" buffer. */
135 p = secret;
136 while (len > 0) {
137 if (read(fd, p, 1) != 1 || *p == '\n')
138 break;
139 if (*p == '\r')
140 continue;
141 p++;
142 len--;
143 }
144 *p = '\0';
bcb7e502 145 close(fd);
bcb7e502 146
bcb7e502
AT
147 return 1;
148}
149
65575e96
AT
150static char *getpassf(char *filename)
151{
65575e96 152 STRUCT_STAT st;
38cab94d
WD
153 char buffer[512], *p;
154 int fd, n, ok = 1;
155 char *envpw = getenv("RSYNC_PASSWORD");
65575e96 156
38cab94d
WD
157 if (!filename)
158 return NULL;
65575e96 159
38cab94d 160 if ((fd = open(filename,O_RDONLY)) < 0) {
4875d6b6 161 rsyserr(FERROR, errno, "could not open password file \"%s\"",
45c49b52 162 filename);
38cab94d 163 if (envpw)
18cc8c7e 164 rprintf(FERROR, "falling back to RSYNC_PASSWORD environment variable.\n");
65575e96
AT
165 return NULL;
166 }
18cc8c7e 167
65575e96 168 if (do_stat(filename, &st) == -1) {
45c49b52 169 rsyserr(FERROR, errno, "stat(%s)", filename);
65575e96
AT
170 ok = 0;
171 } else if ((st.st_mode & 06) != 0) {
172 rprintf(FERROR,"password file must not be other-accessible\n");
173 ok = 0;
351f5e2f 174 } else if (MY_UID() == 0 && st.st_uid != 0) {
65575e96
AT
175 rprintf(FERROR,"password file must be owned by root when running as root\n");
176 ok = 0;
177 }
178 if (!ok) {
179 rprintf(FERROR,"continuing without password file\n");
38cab94d
WD
180 if (envpw)
181 rprintf(FERROR, "using RSYNC_PASSWORD environment variable.\n");
65575e96
AT
182 close(fd);
183 return NULL;
184 }
185
38cab94d
WD
186 if (envpw)
187 rprintf(FERROR, "RSYNC_PASSWORD environment variable ignored\n");
65575e96 188
38cab94d
WD
189 n = read(fd, buffer, sizeof buffer - 1);
190 close(fd);
191 if (n > 0) {
192 buffer[n] = '\0';
193 if ((p = strtok(buffer, "\n\r")) != NULL)
194 return strdup(p);
18cc8c7e 195 }
65575e96
AT
196
197 return NULL;
198}
199
bf011fed
WD
200/* Generate an MD4 hash created from the combination of the password
201 * and the challenge string and return it base64-encoded. */
6e4fb64e 202static void generate_hash(char *in, char *challenge, char *out)
bcb7e502 203{
5037cf3a 204 char buf[MD4_SUM_LENGTH];
bcb7e502 205
ba582f75 206 sum_init(0);
bcb7e502
AT
207 sum_update(in, strlen(in));
208 sum_update(challenge, strlen(challenge));
209 sum_end(buf);
210
5037cf3a 211 base64_encode(buf, MD4_SUM_LENGTH, out);
bcb7e502
AT
212}
213
18cc8c7e
WD
214/* Possibly negotiate authentication with the client. Use "leader" to
215 * start off the auth if necessary.
216 *
217 * Return NULL if authentication failed. Return "" if anonymous access.
218 * Otherwise return username.
219 */
5037cf3a
WD
220char *auth_server(int f_in, int f_out, int module, char *host, char *addr,
221 char *leader)
bcb7e502
AT
222{
223 char *users = lp_auth_users(module);
bf011fed 224 char challenge[MD4_SUM_LENGTH*2];
d999d312 225 char line[BIGPATHBUFLEN];
5037cf3a
WD
226 char secret[512];
227 char pass2[MD4_SUM_LENGTH*2];
228 char *tok, *pass;
bcb7e502
AT
229
230 /* if no auth list then allow anyone in! */
58c9b4b7
WD
231 if (!users || !*users)
232 return "";
bcb7e502
AT
233
234 gen_challenge(addr, challenge);
18cc8c7e 235
bf011fed 236 io_printf(f_out, "%s%s\n", leader, 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
bf011fed 272 generate_hash(secret, 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}