blob: 8b969e97321b8aabf9a393115e29a376025becd5 [file] [log] [blame]
Wayne Davison0f78b812006-04-25 20:23:34 +00001/*
2 * Support rsync daemon authentication.
3 *
4 * Copyright (C) 1998-2000 Andrew Tridgell
Wayne Davison445640e2009-01-03 10:57:14 -08005 * Copyright (C) 2002-2009 Wayne Davison
Wayne Davison0f78b812006-04-25 20:23:34 +00006 *
7 * This program is free software; you can redistribute it and/or modify
Wayne Davison8e41b682007-07-10 13:55:49 +00008 * 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.
Wayne Davison0f78b812006-04-25 20:23:34 +000011 *
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 *
Wayne Davisone7c67062006-04-25 23:51:12 +000017 * You should have received a copy of the GNU General Public License along
Wayne Davison4fd842f2007-07-07 05:33:14 +000018 * with this program; if not, visit the http://fsf.org website.
Wayne Davison0f78b812006-04-25 20:23:34 +000019 */
Wayne Davison18cc8c72004-05-08 19:37:28 +000020
Andrew Tridgell31593dd1998-05-13 09:38:54 +000021#include "rsync.h"
22
Wayne Davison38cab942004-05-08 18:18:42 +000023extern char *password_file;
Wayne Davison38cab942004-05-08 18:18:42 +000024
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000025/***************************************************************************
26encode a buffer using base64 - simple and slow algorithm. null terminates
27the result.
28 ***************************************************************************/
Wayne Davison4a19c3b2006-11-19 00:23:21 +000029void base64_encode(const char *buf, int len, char *out, int pad)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000030{
31 char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
32 int bit_offset, byte_offset, idx, i;
Wayne Davison4a19c3b2006-11-19 00:23:21 +000033 const uchar *d = (const uchar *)buf;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000034 int bytes = (len*8 + 5)/6;
35
Wayne Davison57385122004-01-03 08:53:36 +000036 for (i = 0; i < bytes; i++) {
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000037 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 }
Wayne Davison293def62006-03-06 18:22:20 +000049
50 while (pad && (i % 4))
51 out[i++] = '=';
52
53 out[i] = '\0';
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000054}
55
Wayne Davisonbf011fe2005-04-10 17:09:10 +000056/* Generate a challenge buffer and return it base64-encoded. */
Wayne Davison4a19c3b2006-11-19 00:23:21 +000057static void gen_challenge(const char *addr, char *challenge)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000058{
59 char input[32];
Wayne Davisona0456b92007-03-18 06:00:53 +000060 char digest[MAX_DIGEST_LEN];
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000061 struct timeval tv;
Wayne Davisona0456b92007-03-18 06:00:53 +000062 int len;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000063
Wayne Davison58c9b4b2004-05-08 19:26:53 +000064 memset(input, 0, sizeof input);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000065
Wayne Davison4a19c3b2006-11-19 00:23:21 +000066 strlcpy(input, addr, 17);
Andrew Tridgell3060d4a2000-01-23 02:16:51 +000067 sys_gettimeofday(&tv);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000068 SIVAL(input, 16, tv.tv_sec);
69 SIVAL(input, 20, tv.tv_usec);
70 SIVAL(input, 24, getpid());
71
Wayne Davisonba582f72004-05-21 08:27:04 +000072 sum_init(0);
Wayne Davison58c9b4b2004-05-08 19:26:53 +000073 sum_update(input, sizeof input);
Wayne Davisona0456b92007-03-18 06:00:53 +000074 len = sum_end(digest);
Wayne Davisonbf011fe2005-04-10 17:09:10 +000075
Wayne Davisona0456b92007-03-18 06:00:53 +000076 base64_encode(digest, len, challenge, 0);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000077}
78
79
Wayne Davison38cab942004-05-08 18:18:42 +000080/* Return the secret for a user from the secret file, null terminated.
81 * Maximum length is len (not counting the null). */
Wayne Davison4a19c3b2006-11-19 00:23:21 +000082static int get_secret(int module, const char *user, char *secret, int len)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000083{
Wayne Davison4a19c3b2006-11-19 00:23:21 +000084 const char *fname = lp_secrets_file(module);
David Dykstrad1be2311998-11-24 19:52:35 +000085 STRUCT_STAT st;
Wayne Davison38cab942004-05-08 18:18:42 +000086 int fd, ok = 1;
Wayne Davison4a19c3b2006-11-19 00:23:21 +000087 const char *p;
88 char ch, *s;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000089
Wayne Davison38cab942004-05-08 18:18:42 +000090 if (!fname || !*fname)
91 return 0;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000092
Wayne Davison38cab942004-05-08 18:18:42 +000093 if ((fd = open(fname, O_RDONLY)) < 0)
94 return 0;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000095
David Dykstrad1be2311998-11-24 19:52:35 +000096 if (do_stat(fname, &st) == -1) {
Wayne Davison45c49b52006-01-13 21:17:09 +000097 rsyserr(FLOG, errno, "stat(%s)", fname);
David Dykstrad1be2311998-11-24 19:52:35 +000098 ok = 0;
David Dykstra3ca8e681999-02-09 19:27:15 +000099 } else if (lp_strict_modes(module)) {
100 if ((st.st_mode & 06) != 0) {
Wayne Davison30c041f2004-09-24 17:04:05 +0000101 rprintf(FLOG, "secrets file must not be other-accessible (see strict modes option)\n");
David Dykstra3ca8e681999-02-09 19:27:15 +0000102 ok = 0;
Wayne Davison351f5e22006-01-26 11:01:00 +0000103 } else if (MY_UID() == 0 && st.st_uid != 0) {
Wayne Davison30c041f2004-09-24 17:04:05 +0000104 rprintf(FLOG, "secrets file must be owned by root when running as root (see strict modes)\n");
David Dykstra3ca8e681999-02-09 19:27:15 +0000105 ok = 0;
106 }
David Dykstrad1be2311998-11-24 19:52:35 +0000107 }
108 if (!ok) {
Wayne Davison30c041f2004-09-24 17:04:05 +0000109 rprintf(FLOG, "continuing without secrets file\n");
David Dykstrad1be2311998-11-24 19:52:35 +0000110 close(fd);
111 return 0;
112 }
113
Wayne Davison38cab942004-05-08 18:18:42 +0000114 if (*user == '#') {
115 /* Reject attempt to match a comment. */
116 close(fd);
117 return 0;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000118 }
119
Wayne Davison38cab942004-05-08 18:18:42 +0000120 /* 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;
136 }
137 }
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000138
Wayne Davison38cab942004-05-08 18:18:42 +0000139 /* Slurp the secret into the "secret" buffer. */
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000140 s = secret;
Wayne Davison38cab942004-05-08 18:18:42 +0000141 while (len > 0) {
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000142 if (read(fd, s, 1) != 1 || *s == '\n')
Wayne Davison38cab942004-05-08 18:18:42 +0000143 break;
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000144 if (*s == '\r')
Wayne Davison38cab942004-05-08 18:18:42 +0000145 continue;
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000146 s++;
Wayne Davison38cab942004-05-08 18:18:42 +0000147 len--;
148 }
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000149 *s = '\0';
Wayne Davison38cab942004-05-08 18:18:42 +0000150 close(fd);
151
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000152 return 1;
153}
154
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000155static const char *getpassf(const char *filename)
Andrew Tridgell65575e91999-01-08 10:32:56 +0000156{
Andrew Tridgell65575e91999-01-08 10:32:56 +0000157 STRUCT_STAT st;
Wayne Davison38cab942004-05-08 18:18:42 +0000158 char buffer[512], *p;
Wayne Davison5a9933c2011-09-06 21:18:32 -0700159 int fd, n;
Andrew Tridgell65575e91999-01-08 10:32:56 +0000160
Wayne Davison38cab942004-05-08 18:18:42 +0000161 if (!filename)
162 return NULL;
Andrew Tridgell65575e91999-01-08 10:32:56 +0000163
Wayne Davison38cab942004-05-08 18:18:42 +0000164 if ((fd = open(filename,O_RDONLY)) < 0) {
Wayne Davison5a9933c2011-09-06 21:18:32 -0700165 rsyserr(FERROR, errno, "could not open password file %s", filename);
166 exit_cleanup(RERR_SYNTAX);
Andrew Tridgell65575e91999-01-08 10:32:56 +0000167 }
Wayne Davison18cc8c72004-05-08 19:37:28 +0000168
Andrew Tridgell65575e91999-01-08 10:32:56 +0000169 if (do_stat(filename, &st) == -1) {
Wayne Davison5a9933c2011-09-06 21:18:32 -0700170 rsyserr(FERROR, errno, "stat(%s)", filename);
171 exit_cleanup(RERR_SYNTAX);
Andrew Tridgell65575e91999-01-08 10:32:56 +0000172 }
Wayne Davison5a9933c2011-09-06 21:18:32 -0700173 if ((st.st_mode & 06) != 0) {
174 rprintf(FERROR, "ERROR: password file must not be other-accessible\n");
175 exit_cleanup(RERR_SYNTAX);
176 }
177 if (MY_UID() == 0 && st.st_uid != 0) {
178 rprintf(FERROR, "ERROR: password file must be owned by root when running as root\n");
179 exit_cleanup(RERR_SYNTAX);
Andrew Tridgell65575e91999-01-08 10:32:56 +0000180 }
181
Wayne Davison38cab942004-05-08 18:18:42 +0000182 n = read(fd, buffer, sizeof buffer - 1);
183 close(fd);
184 if (n > 0) {
185 buffer[n] = '\0';
186 if ((p = strtok(buffer, "\n\r")) != NULL)
187 return strdup(p);
Wayne Davison18cc8c72004-05-08 19:37:28 +0000188 }
Andrew Tridgell65575e91999-01-08 10:32:56 +0000189
Wayne Davison5a9933c2011-09-06 21:18:32 -0700190 rprintf(FERROR, "ERROR: failed to read a password from %s\n", filename);
191 exit_cleanup(RERR_SYNTAX);
Andrew Tridgell65575e91999-01-08 10:32:56 +0000192}
193
Wayne Davisonbf011fe2005-04-10 17:09:10 +0000194/* Generate an MD4 hash created from the combination of the password
195 * and the challenge string and return it base64-encoded. */
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000196static void generate_hash(const char *in, const char *challenge, char *out)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000197{
Wayne Davisona0456b92007-03-18 06:00:53 +0000198 char buf[MAX_DIGEST_LEN];
199 int len;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000200
Wayne Davisonba582f72004-05-21 08:27:04 +0000201 sum_init(0);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000202 sum_update(in, strlen(in));
203 sum_update(challenge, strlen(challenge));
Wayne Davisona0456b92007-03-18 06:00:53 +0000204 len = sum_end(buf);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000205
Wayne Davisona0456b92007-03-18 06:00:53 +0000206 base64_encode(buf, len, out, 0);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000207}
208
Wayne Davison18cc8c72004-05-08 19:37:28 +0000209/* Possibly negotiate authentication with the client. Use "leader" to
210 * start off the auth if necessary.
211 *
212 * Return NULL if authentication failed. Return "" if anonymous access.
213 * Otherwise return username.
214 */
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000215char *auth_server(int f_in, int f_out, int module, const char *host,
216 const char *addr, const char *leader)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000217{
218 char *users = lp_auth_users(module);
Wayne Davisona0456b92007-03-18 06:00:53 +0000219 char challenge[MAX_DIGEST_LEN*2];
Wayne Davisond999d312005-07-29 18:31:05 +0000220 char line[BIGPATHBUFLEN];
Wayne Davison5037cf32005-04-09 18:11:23 +0000221 char secret[512];
Wayne Davisona0456b92007-03-18 06:00:53 +0000222 char pass2[MAX_DIGEST_LEN*2];
Wayne Davison5037cf32005-04-09 18:11:23 +0000223 char *tok, *pass;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000224
225 /* if no auth list then allow anyone in! */
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000226 if (!users || !*users)
227 return "";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000228
229 gen_challenge(addr, challenge);
Wayne Davison18cc8c72004-05-08 19:37:28 +0000230
Wayne Davisonbf011fe2005-04-10 17:09:10 +0000231 io_printf(f_out, "%s%s\n", leader, challenge);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000232
Wayne Davisonf74a3d82007-09-09 07:53:02 +0000233 if (!read_line_old(f_in, line, sizeof line)
Wayne Davison5037cf32005-04-09 18:11:23 +0000234 || (pass = strchr(line, ' ')) == NULL) {
235 rprintf(FLOG, "auth failed on module %s from %s (%s): "
236 "invalid challenge response\n",
237 lp_name(module), host, addr);
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000238 return NULL;
Wayne Davison5037cf32005-04-09 18:11:23 +0000239 }
240 *pass++ = '\0';
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000241
Wayne Davison5037cf32005-04-09 18:11:23 +0000242 if (!(users = strdup(users)))
243 out_of_memory("auth_server");
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000244
Wayne Davison5037cf32005-04-09 18:11:23 +0000245 for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
246 if (wildmatch(tok, line))
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000247 break;
Andrew Tridgellc8e78d81998-05-13 12:21:10 +0000248 }
249 free(users);
250
Wayne Davison5037cf32005-04-09 18:11:23 +0000251 if (!tok) {
252 rprintf(FLOG, "auth failed on module %s from %s (%s): "
253 "unauthorized user\n",
254 lp_name(module), host, addr);
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000255 return NULL;
Wayne Davison5037cf32005-04-09 18:11:23 +0000256 }
Wayne Davison18cc8c72004-05-08 19:37:28 +0000257
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000258 memset(secret, 0, sizeof secret);
Wayne Davison5037cf32005-04-09 18:11:23 +0000259 if (!get_secret(module, line, secret, sizeof secret - 1)) {
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000260 memset(secret, 0, sizeof secret);
Wayne Davison5037cf32005-04-09 18:11:23 +0000261 rprintf(FLOG, "auth failed on module %s from %s (%s): "
262 "missing secret for user \"%s\"\n",
263 lp_name(module), host, addr, line);
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000264 return NULL;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000265 }
266
Wayne Davisonbf011fe2005-04-10 17:09:10 +0000267 generate_hash(secret, challenge, pass2);
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000268 memset(secret, 0, sizeof secret);
Wayne Davison18cc8c72004-05-08 19:37:28 +0000269
Wayne Davison5037cf32005-04-09 18:11:23 +0000270 if (strcmp(pass, pass2) != 0) {
271 rprintf(FLOG, "auth failed on module %s from %s (%s): "
272 "password mismatch\n",
273 lp_name(module), host, addr);
274 return NULL;
275 }
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000276
Wayne Davison5037cf32005-04-09 18:11:23 +0000277 return strdup(line);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000278}
279
Dylan Simonec883bc2010-03-08 22:01:26 -0500280#ifdef __BIONIC__
281#include <termios.h>
282static const char *getpass(char *prompt)
283{
284 struct termios old, new;
285 static char res[256];
286 char *p;
287
288 /* Turn echoing off and fail if we can't. */
289 if (tcgetattr (0, &old) != 0)
290 return NULL;
291 new = old;
292 new.c_lflag &= ~ECHO;
293 if (tcsetattr (0, TCSAFLUSH, &new) != 0)
294 return NULL;
295
296 fprintf(stderr, "%s", prompt);
297 fflush(stderr);
298 fgets(res, 256, stdin);
299 if ((p = strchr(res, '\n')))
300 *p = 0;
301
302 /* Restore terminal. */
303 tcsetattr (0, TCSAFLUSH, &old);
304
305 return res;
306}
307
308#endif
309
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000310void auth_client(int fd, const char *user, const char *challenge)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000311{
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000312 const char *pass;
Wayne Davisona0456b92007-03-18 06:00:53 +0000313 char pass2[MAX_DIGEST_LEN*2];
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000314
Wayne Davisonef383c02004-03-31 18:52:38 +0000315 if (!user || !*user)
Wayne Davison4b2f6a72004-04-01 18:05:40 +0000316 user = "nobody";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000317
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000318 if (!(pass = getpassf(password_file))
319 && !(pass = getenv("RSYNC_PASSWORD"))) {
Martin Pool64bd7562001-08-29 07:23:30 +0000320 /* XXX: cyeoh says that getpass is deprecated, because
Martin Pool908f5a92003-06-17 04:46:32 +0000321 * it may return a truncated password on some systems,
322 * and it is not in the LSB.
323 *
324 * Andrew Klein says that getpassphrase() is present
325 * on Solaris and reads up to 256 characters.
326 *
327 * OpenBSD has a readpassphrase() that might be more suitable.
328 */
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000329 pass = getpass("Password: ");
330 }
331
Wayne Davison38cab942004-05-08 18:18:42 +0000332 if (!pass)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000333 pass = "";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000334
335 generate_hash(pass, challenge, pass2);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000336 io_printf(fd, "%s %s\n", user, pass2);
337}