blob: 12f142ecbff45472bf6b28cb5ae5354d8b1f1d62 [file] [log] [blame]
Martin Pool4a13b9d2000-10-26 07:31:29 +00001/* -*- c-file-style: "linux"; -*-
Wayne Davison18cc8c72004-05-08 19:37:28 +00002
3 Copyright (C) 1998-2000 by Andrew Tridgell
4
Andrew Tridgell31593dd1998-05-13 09:38:54 +00005 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.
Wayne Davison18cc8c72004-05-08 19:37:28 +00009
Andrew Tridgell31593dd1998-05-13 09:38:54 +000010 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.
Wayne Davison18cc8c72004-05-08 19:37:28 +000014
Andrew Tridgell31593dd1998-05-13 09:38:54 +000015 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
Wayne Davison38cab942004-05-08 18:18:42 +000023extern char *password_file;
24extern int am_root;
25
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000026/***************************************************************************
27encode a buffer using base64 - simple and slow algorithm. null terminates
28the result.
29 ***************************************************************************/
Wayne Davison57385122004-01-03 08:53:36 +000030void base64_encode(char *buf, int len, char *out)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000031{
32 char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
33 int bit_offset, byte_offset, idx, i;
34 unsigned char *d = (unsigned char *)buf;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000035 int bytes = (len*8 + 5)/6;
36
37 memset(out, 0, bytes+1);
38
Wayne Davison57385122004-01-03 08:53:36 +000039 for (i = 0; i < bytes; i++) {
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000040 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
Wayne Davisonbf011fe2005-04-10 17:09:10 +000054/* Generate a challenge buffer and return it base64-encoded. */
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000055static void gen_challenge(char *addr, char *challenge)
56{
57 char input[32];
Wayne Davisonbf011fe2005-04-10 17:09:10 +000058 char md4_out[MD4_SUM_LENGTH];
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000059 struct timeval tv;
60
Wayne Davison58c9b4b2004-05-08 19:26:53 +000061 memset(input, 0, sizeof input);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000062
Andrew Tridgell37f98051998-11-14 23:31:58 +000063 strlcpy((char *)input, addr, 17);
Andrew Tridgell3060d4a2000-01-23 02:16:51 +000064 sys_gettimeofday(&tv);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000065 SIVAL(input, 16, tv.tv_sec);
66 SIVAL(input, 20, tv.tv_usec);
67 SIVAL(input, 24, getpid());
68
Wayne Davisonba582f72004-05-21 08:27:04 +000069 sum_init(0);
Wayne Davison58c9b4b2004-05-08 19:26:53 +000070 sum_update(input, sizeof input);
Wayne Davisonbf011fe2005-04-10 17:09:10 +000071 sum_end(md4_out);
72
73 base64_encode(md4_out, MD4_SUM_LENGTH, challenge);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000074}
75
76
Wayne Davison38cab942004-05-08 18:18:42 +000077/* Return the secret for a user from the secret file, null terminated.
78 * Maximum length is len (not counting the null). */
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000079static int get_secret(int module, char *user, char *secret, int len)
80{
81 char *fname = lp_secrets_file(module);
David Dykstrad1be2311998-11-24 19:52:35 +000082 STRUCT_STAT st;
Wayne Davison38cab942004-05-08 18:18:42 +000083 int fd, ok = 1;
84 char ch, *p;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000085
Wayne Davison38cab942004-05-08 18:18:42 +000086 if (!fname || !*fname)
87 return 0;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000088
Wayne Davison38cab942004-05-08 18:18:42 +000089 if ((fd = open(fname, O_RDONLY)) < 0)
90 return 0;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000091
David Dykstrad1be2311998-11-24 19:52:35 +000092 if (do_stat(fname, &st) == -1) {
Wayne Davison4875d6b2005-02-07 20:36:43 +000093 rsyserr(FLOG, errno, "stat(%s)", safe_fname(fname));
David Dykstrad1be2311998-11-24 19:52:35 +000094 ok = 0;
David Dykstra3ca8e681999-02-09 19:27:15 +000095 } else if (lp_strict_modes(module)) {
96 if ((st.st_mode & 06) != 0) {
Wayne Davison30c041f2004-09-24 17:04:05 +000097 rprintf(FLOG, "secrets file must not be other-accessible (see strict modes option)\n");
David Dykstra3ca8e681999-02-09 19:27:15 +000098 ok = 0;
99 } else if (am_root && (st.st_uid != 0)) {
Wayne Davison30c041f2004-09-24 17:04:05 +0000100 rprintf(FLOG, "secrets file must be owned by root when running as root (see strict modes)\n");
David Dykstra3ca8e681999-02-09 19:27:15 +0000101 ok = 0;
102 }
David Dykstrad1be2311998-11-24 19:52:35 +0000103 }
104 if (!ok) {
Wayne Davison30c041f2004-09-24 17:04:05 +0000105 rprintf(FLOG, "continuing without secrets file\n");
David Dykstrad1be2311998-11-24 19:52:35 +0000106 close(fd);
107 return 0;
108 }
109
Wayne Davison38cab942004-05-08 18:18:42 +0000110 if (*user == '#') {
111 /* Reject attempt to match a comment. */
112 close(fd);
113 return 0;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000114 }
115
Wayne Davison38cab942004-05-08 18:18:42 +0000116 /* Try to find a line that starts with the user name and a ':'. */
117 p = user;
118 while (1) {
119 if (read(fd, &ch, 1) != 1) {
120 close(fd);
121 return 0;
122 }
123 if (ch == '\n')
124 p = user;
125 else if (p) {
126 if (*p == ch)
127 p++;
128 else if (!*p && ch == ':')
129 break;
130 else
131 p = NULL;
132 }
133 }
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000134
Wayne Davison38cab942004-05-08 18:18:42 +0000135 /* Slurp the secret into the "secret" buffer. */
136 p = secret;
137 while (len > 0) {
138 if (read(fd, p, 1) != 1 || *p == '\n')
139 break;
140 if (*p == '\r')
141 continue;
142 p++;
143 len--;
144 }
145 *p = '\0';
146 close(fd);
147
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000148 return 1;
149}
150
Andrew Tridgell65575e91999-01-08 10:32:56 +0000151static char *getpassf(char *filename)
152{
Andrew Tridgell65575e91999-01-08 10:32:56 +0000153 STRUCT_STAT st;
Wayne Davison38cab942004-05-08 18:18:42 +0000154 char buffer[512], *p;
155 int fd, n, ok = 1;
156 char *envpw = getenv("RSYNC_PASSWORD");
Andrew Tridgell65575e91999-01-08 10:32:56 +0000157
Wayne Davison38cab942004-05-08 18:18:42 +0000158 if (!filename)
159 return NULL;
Andrew Tridgell65575e91999-01-08 10:32:56 +0000160
Wayne Davison38cab942004-05-08 18:18:42 +0000161 if ((fd = open(filename,O_RDONLY)) < 0) {
Wayne Davison4875d6b2005-02-07 20:36:43 +0000162 rsyserr(FERROR, errno, "could not open password file \"%s\"",
163 safe_fname(filename));
Wayne Davison38cab942004-05-08 18:18:42 +0000164 if (envpw)
Wayne Davison18cc8c72004-05-08 19:37:28 +0000165 rprintf(FERROR, "falling back to RSYNC_PASSWORD environment variable.\n");
Andrew Tridgell65575e91999-01-08 10:32:56 +0000166 return NULL;
167 }
Wayne Davison18cc8c72004-05-08 19:37:28 +0000168
Andrew Tridgell65575e91999-01-08 10:32:56 +0000169 if (do_stat(filename, &st) == -1) {
Wayne Davison4875d6b2005-02-07 20:36:43 +0000170 rsyserr(FERROR, errno, "stat(%s)", safe_fname(filename));
Andrew Tridgell65575e91999-01-08 10:32:56 +0000171 ok = 0;
172 } else if ((st.st_mode & 06) != 0) {
173 rprintf(FERROR,"password file must not be other-accessible\n");
174 ok = 0;
Wayne Davison38cab942004-05-08 18:18:42 +0000175 } else if (am_root && st.st_uid != 0) {
Andrew Tridgell65575e91999-01-08 10:32:56 +0000176 rprintf(FERROR,"password file must be owned by root when running as root\n");
177 ok = 0;
178 }
179 if (!ok) {
180 rprintf(FERROR,"continuing without password file\n");
Wayne Davison38cab942004-05-08 18:18:42 +0000181 if (envpw)
182 rprintf(FERROR, "using RSYNC_PASSWORD environment variable.\n");
Andrew Tridgell65575e91999-01-08 10:32:56 +0000183 close(fd);
184 return NULL;
185 }
186
Wayne Davison38cab942004-05-08 18:18:42 +0000187 if (envpw)
188 rprintf(FERROR, "RSYNC_PASSWORD environment variable ignored\n");
Andrew Tridgell65575e91999-01-08 10:32:56 +0000189
Wayne Davison38cab942004-05-08 18:18:42 +0000190 n = read(fd, buffer, sizeof buffer - 1);
191 close(fd);
192 if (n > 0) {
193 buffer[n] = '\0';
194 if ((p = strtok(buffer, "\n\r")) != NULL)
195 return strdup(p);
Wayne Davison18cc8c72004-05-08 19:37:28 +0000196 }
Andrew Tridgell65575e91999-01-08 10:32:56 +0000197
198 return NULL;
199}
200
Wayne Davisonbf011fe2005-04-10 17:09:10 +0000201/* Generate an MD4 hash created from the combination of the password
202 * and the challenge string and return it base64-encoded. */
Andrew Tridgell6e4fb641998-09-09 05:57:34 +0000203static void generate_hash(char *in, char *challenge, char *out)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000204{
Wayne Davison5037cf32005-04-09 18:11:23 +0000205 char buf[MD4_SUM_LENGTH];
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000206
Wayne Davisonba582f72004-05-21 08:27:04 +0000207 sum_init(0);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000208 sum_update(in, strlen(in));
209 sum_update(challenge, strlen(challenge));
210 sum_end(buf);
211
Wayne Davison5037cf32005-04-09 18:11:23 +0000212 base64_encode(buf, MD4_SUM_LENGTH, out);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000213}
214
Wayne Davison18cc8c72004-05-08 19:37:28 +0000215/* Possibly negotiate authentication with the client. Use "leader" to
216 * start off the auth if necessary.
217 *
218 * Return NULL if authentication failed. Return "" if anonymous access.
219 * Otherwise return username.
220 */
Wayne Davison5037cf32005-04-09 18:11:23 +0000221char *auth_server(int f_in, int f_out, int module, char *host, char *addr,
222 char *leader)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000223{
224 char *users = lp_auth_users(module);
Wayne Davisonbf011fe2005-04-10 17:09:10 +0000225 char challenge[MD4_SUM_LENGTH*2];
Wayne Davisond999d312005-07-29 18:31:05 +0000226 char line[BIGPATHBUFLEN];
Wayne Davison5037cf32005-04-09 18:11:23 +0000227 char secret[512];
228 char pass2[MD4_SUM_LENGTH*2];
229 char *tok, *pass;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000230
231 /* if no auth list then allow anyone in! */
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000232 if (!users || !*users)
233 return "";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000234
235 gen_challenge(addr, challenge);
Wayne Davison18cc8c72004-05-08 19:37:28 +0000236
Wayne Davisonbf011fe2005-04-10 17:09:10 +0000237 io_printf(f_out, "%s%s\n", leader, challenge);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000238
Wayne Davison5037cf32005-04-09 18:11:23 +0000239 if (!read_line(f_in, line, sizeof line - 1)
240 || (pass = strchr(line, ' ')) == NULL) {
241 rprintf(FLOG, "auth failed on module %s from %s (%s): "
242 "invalid challenge response\n",
243 lp_name(module), host, addr);
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000244 return NULL;
Wayne Davison5037cf32005-04-09 18:11:23 +0000245 }
246 *pass++ = '\0';
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000247
Wayne Davison5037cf32005-04-09 18:11:23 +0000248 if (!(users = strdup(users)))
249 out_of_memory("auth_server");
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000250
Wayne Davison5037cf32005-04-09 18:11:23 +0000251 for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
252 if (wildmatch(tok, line))
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000253 break;
Andrew Tridgellc8e78d81998-05-13 12:21:10 +0000254 }
255 free(users);
256
Wayne Davison5037cf32005-04-09 18:11:23 +0000257 if (!tok) {
258 rprintf(FLOG, "auth failed on module %s from %s (%s): "
259 "unauthorized user\n",
260 lp_name(module), host, addr);
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000261 return NULL;
Wayne Davison5037cf32005-04-09 18:11:23 +0000262 }
Wayne Davison18cc8c72004-05-08 19:37:28 +0000263
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000264 memset(secret, 0, sizeof secret);
Wayne Davison5037cf32005-04-09 18:11:23 +0000265 if (!get_secret(module, line, secret, sizeof secret - 1)) {
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000266 memset(secret, 0, sizeof secret);
Wayne Davison5037cf32005-04-09 18:11:23 +0000267 rprintf(FLOG, "auth failed on module %s from %s (%s): "
268 "missing secret for user \"%s\"\n",
269 lp_name(module), host, addr, line);
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000270 return NULL;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000271 }
272
Wayne Davisonbf011fe2005-04-10 17:09:10 +0000273 generate_hash(secret, challenge, pass2);
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000274 memset(secret, 0, sizeof secret);
Wayne Davison18cc8c72004-05-08 19:37:28 +0000275
Wayne Davison5037cf32005-04-09 18:11:23 +0000276 if (strcmp(pass, pass2) != 0) {
277 rprintf(FLOG, "auth failed on module %s from %s (%s): "
278 "password mismatch\n",
279 lp_name(module), host, addr);
280 return NULL;
281 }
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000282
Wayne Davison5037cf32005-04-09 18:11:23 +0000283 return strdup(line);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000284}
285
286
287void auth_client(int fd, char *user, char *challenge)
288{
289 char *pass;
Wayne Davison5037cf32005-04-09 18:11:23 +0000290 char pass2[MD4_SUM_LENGTH*2];
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000291
Wayne Davisonef383c02004-03-31 18:52:38 +0000292 if (!user || !*user)
Wayne Davison4b2f6a72004-04-01 18:05:40 +0000293 user = "nobody";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000294
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000295 if (!(pass = getpassf(password_file))
296 && !(pass = getenv("RSYNC_PASSWORD"))) {
Martin Pool64bd7562001-08-29 07:23:30 +0000297 /* XXX: cyeoh says that getpass is deprecated, because
Martin Pool908f5a92003-06-17 04:46:32 +0000298 * it may return a truncated password on some systems,
299 * and it is not in the LSB.
300 *
301 * Andrew Klein says that getpassphrase() is present
302 * on Solaris and reads up to 256 characters.
303 *
304 * OpenBSD has a readpassphrase() that might be more suitable.
305 */
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000306 pass = getpass("Password: ");
307 }
308
Wayne Davison38cab942004-05-08 18:18:42 +0000309 if (!pass)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000310 pass = "";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000311
312 generate_hash(pass, challenge, pass2);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000313 io_printf(fd, "%s %s\n", user, pass2);
314}