blob: c3dabd060d0b65909e0d4094efd85d3d1a554948 [file] [log] [blame]
Wayne Davison0f78b812006-04-25 20:23:34 +00001/*
2 * Support rsync daemon authentication.
3 *
4 * Copyright (C) 1998-2000 Andrew Tridgell
5 * Copyright (C) 2002, 2004, 2005, 2006 Wayne Davison
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
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 *
Wayne Davisone7c67062006-04-25 23:51:12 +000017 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
Wayne Davison0f78b812006-04-25 20:23:34 +000020 */
Wayne Davison18cc8c72004-05-08 19:37:28 +000021
Andrew Tridgell31593dd1998-05-13 09:38:54 +000022#include "rsync.h"
23
Wayne Davison38cab942004-05-08 18:18:42 +000024extern char *password_file;
Wayne Davison38cab942004-05-08 18:18:42 +000025
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000026/***************************************************************************
27encode a buffer using base64 - simple and slow algorithm. null terminates
28the result.
29 ***************************************************************************/
Wayne Davison4a19c3b2006-11-19 00:23:21 +000030void base64_encode(const char *buf, int len, char *out, int pad)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000031{
32 char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
33 int bit_offset, byte_offset, idx, i;
Wayne Davison4a19c3b2006-11-19 00:23:21 +000034 const uchar *d = (const uchar *)buf;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000035 int bytes = (len*8 + 5)/6;
36
Wayne Davison57385122004-01-03 08:53:36 +000037 for (i = 0; i < bytes; i++) {
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000038 byte_offset = (i*6)/8;
39 bit_offset = (i*6)%8;
40 if (bit_offset < 3) {
41 idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
42 } else {
43 idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
44 if (byte_offset+1 < len) {
45 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
46 }
47 }
48 out[i] = b64[idx];
49 }
Wayne Davison293def62006-03-06 18:22:20 +000050
51 while (pad && (i % 4))
52 out[i++] = '=';
53
54 out[i] = '\0';
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000055}
56
Wayne Davisonbf011fe2005-04-10 17:09:10 +000057/* Generate a challenge buffer and return it base64-encoded. */
Wayne Davison4a19c3b2006-11-19 00:23:21 +000058static void gen_challenge(const char *addr, char *challenge)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000059{
60 char input[32];
Wayne Davisonbf011fe2005-04-10 17:09:10 +000061 char md4_out[MD4_SUM_LENGTH];
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000062 struct timeval tv;
63
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 Davisonbf011fe2005-04-10 17:09:10 +000074 sum_end(md4_out);
75
Wayne Davison293def62006-03-06 18:22:20 +000076 base64_encode(md4_out, MD4_SUM_LENGTH, 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;
159 int fd, n, ok = 1;
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000160 const char *envpw = getenv("RSYNC_PASSWORD");
Andrew Tridgell65575e91999-01-08 10:32:56 +0000161
Wayne Davison38cab942004-05-08 18:18:42 +0000162 if (!filename)
163 return NULL;
Andrew Tridgell65575e91999-01-08 10:32:56 +0000164
Wayne Davison38cab942004-05-08 18:18:42 +0000165 if ((fd = open(filename,O_RDONLY)) < 0) {
Wayne Davison4875d6b2005-02-07 20:36:43 +0000166 rsyserr(FERROR, errno, "could not open password file \"%s\"",
Wayne Davison45c49b52006-01-13 21:17:09 +0000167 filename);
Wayne Davison38cab942004-05-08 18:18:42 +0000168 if (envpw)
Wayne Davison18cc8c72004-05-08 19:37:28 +0000169 rprintf(FERROR, "falling back to RSYNC_PASSWORD environment variable.\n");
Andrew Tridgell65575e91999-01-08 10:32:56 +0000170 return NULL;
171 }
Wayne Davison18cc8c72004-05-08 19:37:28 +0000172
Andrew Tridgell65575e91999-01-08 10:32:56 +0000173 if (do_stat(filename, &st) == -1) {
Wayne Davison45c49b52006-01-13 21:17:09 +0000174 rsyserr(FERROR, errno, "stat(%s)", filename);
Andrew Tridgell65575e91999-01-08 10:32:56 +0000175 ok = 0;
176 } else if ((st.st_mode & 06) != 0) {
177 rprintf(FERROR,"password file must not be other-accessible\n");
178 ok = 0;
Wayne Davison351f5e22006-01-26 11:01:00 +0000179 } else if (MY_UID() == 0 && st.st_uid != 0) {
Andrew Tridgell65575e91999-01-08 10:32:56 +0000180 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");
Wayne Davison38cab942004-05-08 18:18:42 +0000185 if (envpw)
186 rprintf(FERROR, "using RSYNC_PASSWORD environment variable.\n");
Andrew Tridgell65575e91999-01-08 10:32:56 +0000187 close(fd);
188 return NULL;
189 }
190
Wayne Davison38cab942004-05-08 18:18:42 +0000191 if (envpw)
192 rprintf(FERROR, "RSYNC_PASSWORD environment variable ignored\n");
Andrew Tridgell65575e91999-01-08 10:32:56 +0000193
Wayne Davison38cab942004-05-08 18:18:42 +0000194 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);
Wayne Davison18cc8c72004-05-08 19:37:28 +0000200 }
Andrew Tridgell65575e91999-01-08 10:32:56 +0000201
202 return NULL;
203}
204
Wayne Davisonbf011fe2005-04-10 17:09:10 +0000205/* Generate an MD4 hash created from the combination of the password
206 * and the challenge string and return it base64-encoded. */
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000207static void generate_hash(const char *in, const char *challenge, char *out)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000208{
Wayne Davison5037cf32005-04-09 18:11:23 +0000209 char buf[MD4_SUM_LENGTH];
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000210
Wayne Davisonba582f72004-05-21 08:27:04 +0000211 sum_init(0);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000212 sum_update(in, strlen(in));
213 sum_update(challenge, strlen(challenge));
214 sum_end(buf);
215
Wayne Davison293def62006-03-06 18:22:20 +0000216 base64_encode(buf, MD4_SUM_LENGTH, out, 0);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000217}
218
Wayne Davison18cc8c72004-05-08 19:37:28 +0000219/* Possibly negotiate authentication with the client. Use "leader" to
220 * start off the auth if necessary.
221 *
222 * Return NULL if authentication failed. Return "" if anonymous access.
223 * Otherwise return username.
224 */
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000225char *auth_server(int f_in, int f_out, int module, const char *host,
226 const char *addr, const char *leader)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000227{
228 char *users = lp_auth_users(module);
Wayne Davisonbf011fe2005-04-10 17:09:10 +0000229 char challenge[MD4_SUM_LENGTH*2];
Wayne Davisond999d312005-07-29 18:31:05 +0000230 char line[BIGPATHBUFLEN];
Wayne Davison5037cf32005-04-09 18:11:23 +0000231 char secret[512];
232 char pass2[MD4_SUM_LENGTH*2];
233 char *tok, *pass;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000234
235 /* if no auth list then allow anyone in! */
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000236 if (!users || !*users)
237 return "";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000238
239 gen_challenge(addr, challenge);
Wayne Davison18cc8c72004-05-08 19:37:28 +0000240
Wayne Davisonbf011fe2005-04-10 17:09:10 +0000241 io_printf(f_out, "%s%s\n", leader, challenge);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000242
Wayne Davison5037cf32005-04-09 18:11:23 +0000243 if (!read_line(f_in, line, sizeof line - 1)
244 || (pass = strchr(line, ' ')) == NULL) {
245 rprintf(FLOG, "auth failed on module %s from %s (%s): "
246 "invalid challenge response\n",
247 lp_name(module), host, addr);
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000248 return NULL;
Wayne Davison5037cf32005-04-09 18:11:23 +0000249 }
250 *pass++ = '\0';
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000251
Wayne Davison5037cf32005-04-09 18:11:23 +0000252 if (!(users = strdup(users)))
253 out_of_memory("auth_server");
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000254
Wayne Davison5037cf32005-04-09 18:11:23 +0000255 for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
256 if (wildmatch(tok, line))
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000257 break;
Andrew Tridgellc8e78d81998-05-13 12:21:10 +0000258 }
259 free(users);
260
Wayne Davison5037cf32005-04-09 18:11:23 +0000261 if (!tok) {
262 rprintf(FLOG, "auth failed on module %s from %s (%s): "
263 "unauthorized user\n",
264 lp_name(module), host, addr);
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000265 return NULL;
Wayne Davison5037cf32005-04-09 18:11:23 +0000266 }
Wayne Davison18cc8c72004-05-08 19:37:28 +0000267
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000268 memset(secret, 0, sizeof secret);
Wayne Davison5037cf32005-04-09 18:11:23 +0000269 if (!get_secret(module, line, secret, sizeof secret - 1)) {
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000270 memset(secret, 0, sizeof secret);
Wayne Davison5037cf32005-04-09 18:11:23 +0000271 rprintf(FLOG, "auth failed on module %s from %s (%s): "
272 "missing secret for user \"%s\"\n",
273 lp_name(module), host, addr, line);
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000274 return NULL;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000275 }
276
Wayne Davisonbf011fe2005-04-10 17:09:10 +0000277 generate_hash(secret, challenge, pass2);
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000278 memset(secret, 0, sizeof secret);
Wayne Davison18cc8c72004-05-08 19:37:28 +0000279
Wayne Davison5037cf32005-04-09 18:11:23 +0000280 if (strcmp(pass, pass2) != 0) {
281 rprintf(FLOG, "auth failed on module %s from %s (%s): "
282 "password mismatch\n",
283 lp_name(module), host, addr);
284 return NULL;
285 }
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000286
Wayne Davison5037cf32005-04-09 18:11:23 +0000287 return strdup(line);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000288}
289
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000290void auth_client(int fd, const char *user, const char *challenge)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000291{
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000292 const char *pass;
Wayne Davison5037cf32005-04-09 18:11:23 +0000293 char pass2[MD4_SUM_LENGTH*2];
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000294
Wayne Davisonef383c02004-03-31 18:52:38 +0000295 if (!user || !*user)
Wayne Davison4b2f6a72004-04-01 18:05:40 +0000296 user = "nobody";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000297
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000298 if (!(pass = getpassf(password_file))
299 && !(pass = getenv("RSYNC_PASSWORD"))) {
Martin Pool64bd7562001-08-29 07:23:30 +0000300 /* XXX: cyeoh says that getpass is deprecated, because
Martin Pool908f5a92003-06-17 04:46:32 +0000301 * it may return a truncated password on some systems,
302 * and it is not in the LSB.
303 *
304 * Andrew Klein says that getpassphrase() is present
305 * on Solaris and reads up to 256 characters.
306 *
307 * OpenBSD has a readpassphrase() that might be more suitable.
308 */
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000309 pass = getpass("Password: ");
310 }
311
Wayne Davison38cab942004-05-08 18:18:42 +0000312 if (!pass)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000313 pass = "";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000314
315 generate_hash(pass, challenge, pass2);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000316 io_printf(fd, "%s %s\n", user, pass2);
317}