blob: 25d2ded56d4172a4cabe1d91e30f6112a7433d01 [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 Davison5037cf32005-04-09 18:11:23 +000054/* Create a 16-byte challenge buffer. */
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000055static void gen_challenge(char *addr, char *challenge)
56{
57 char input[32];
58 struct timeval tv;
59
Wayne Davison58c9b4b2004-05-08 19:26:53 +000060 memset(input, 0, sizeof input);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000061
Andrew Tridgell37f98051998-11-14 23:31:58 +000062 strlcpy((char *)input, addr, 17);
Andrew Tridgell3060d4a2000-01-23 02:16:51 +000063 sys_gettimeofday(&tv);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000064 SIVAL(input, 16, tv.tv_sec);
65 SIVAL(input, 20, tv.tv_usec);
66 SIVAL(input, 24, getpid());
67
Wayne Davisonba582f72004-05-21 08:27:04 +000068 sum_init(0);
Wayne Davison58c9b4b2004-05-08 19:26:53 +000069 sum_update(input, sizeof input);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000070 sum_end(challenge);
71}
72
73
Wayne Davison38cab942004-05-08 18:18:42 +000074/* Return the secret for a user from the secret file, null terminated.
75 * Maximum length is len (not counting the null). */
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000076static int get_secret(int module, char *user, char *secret, int len)
77{
78 char *fname = lp_secrets_file(module);
David Dykstrad1be2311998-11-24 19:52:35 +000079 STRUCT_STAT st;
Wayne Davison38cab942004-05-08 18:18:42 +000080 int fd, ok = 1;
81 char ch, *p;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000082
Wayne Davison38cab942004-05-08 18:18:42 +000083 if (!fname || !*fname)
84 return 0;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000085
Wayne Davison38cab942004-05-08 18:18:42 +000086 if ((fd = open(fname, O_RDONLY)) < 0)
87 return 0;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000088
David Dykstrad1be2311998-11-24 19:52:35 +000089 if (do_stat(fname, &st) == -1) {
Wayne Davison4875d6b2005-02-07 20:36:43 +000090 rsyserr(FLOG, errno, "stat(%s)", safe_fname(fname));
David Dykstrad1be2311998-11-24 19:52:35 +000091 ok = 0;
David Dykstra3ca8e681999-02-09 19:27:15 +000092 } else if (lp_strict_modes(module)) {
93 if ((st.st_mode & 06) != 0) {
Wayne Davison30c041f2004-09-24 17:04:05 +000094 rprintf(FLOG, "secrets file must not be other-accessible (see strict modes option)\n");
David Dykstra3ca8e681999-02-09 19:27:15 +000095 ok = 0;
96 } else if (am_root && (st.st_uid != 0)) {
Wayne Davison30c041f2004-09-24 17:04:05 +000097 rprintf(FLOG, "secrets file must be owned by root when running as root (see strict modes)\n");
David Dykstra3ca8e681999-02-09 19:27:15 +000098 ok = 0;
99 }
David Dykstrad1be2311998-11-24 19:52:35 +0000100 }
101 if (!ok) {
Wayne Davison30c041f2004-09-24 17:04:05 +0000102 rprintf(FLOG, "continuing without secrets file\n");
David Dykstrad1be2311998-11-24 19:52:35 +0000103 close(fd);
104 return 0;
105 }
106
Wayne Davison38cab942004-05-08 18:18:42 +0000107 if (*user == '#') {
108 /* Reject attempt to match a comment. */
109 close(fd);
110 return 0;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000111 }
112
Wayne Davison38cab942004-05-08 18:18:42 +0000113 /* 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;
129 }
130 }
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000131
Wayne Davison38cab942004-05-08 18:18:42 +0000132 /* 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';
143 close(fd);
144
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000145 return 1;
146}
147
Andrew Tridgell65575e91999-01-08 10:32:56 +0000148static char *getpassf(char *filename)
149{
Andrew Tridgell65575e91999-01-08 10:32:56 +0000150 STRUCT_STAT st;
Wayne Davison38cab942004-05-08 18:18:42 +0000151 char buffer[512], *p;
152 int fd, n, ok = 1;
153 char *envpw = getenv("RSYNC_PASSWORD");
Andrew Tridgell65575e91999-01-08 10:32:56 +0000154
Wayne Davison38cab942004-05-08 18:18:42 +0000155 if (!filename)
156 return NULL;
Andrew Tridgell65575e91999-01-08 10:32:56 +0000157
Wayne Davison38cab942004-05-08 18:18:42 +0000158 if ((fd = open(filename,O_RDONLY)) < 0) {
Wayne Davison4875d6b2005-02-07 20:36:43 +0000159 rsyserr(FERROR, errno, "could not open password file \"%s\"",
160 safe_fname(filename));
Wayne Davison38cab942004-05-08 18:18:42 +0000161 if (envpw)
Wayne Davison18cc8c72004-05-08 19:37:28 +0000162 rprintf(FERROR, "falling back to RSYNC_PASSWORD environment variable.\n");
Andrew Tridgell65575e91999-01-08 10:32:56 +0000163 return NULL;
164 }
Wayne Davison18cc8c72004-05-08 19:37:28 +0000165
Andrew Tridgell65575e91999-01-08 10:32:56 +0000166 if (do_stat(filename, &st) == -1) {
Wayne Davison4875d6b2005-02-07 20:36:43 +0000167 rsyserr(FERROR, errno, "stat(%s)", safe_fname(filename));
Andrew Tridgell65575e91999-01-08 10:32:56 +0000168 ok = 0;
169 } else if ((st.st_mode & 06) != 0) {
170 rprintf(FERROR,"password file must not be other-accessible\n");
171 ok = 0;
Wayne Davison38cab942004-05-08 18:18:42 +0000172 } else if (am_root && st.st_uid != 0) {
Andrew Tridgell65575e91999-01-08 10:32:56 +0000173 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");
Wayne Davison38cab942004-05-08 18:18:42 +0000178 if (envpw)
179 rprintf(FERROR, "using RSYNC_PASSWORD environment variable.\n");
Andrew Tridgell65575e91999-01-08 10:32:56 +0000180 close(fd);
181 return NULL;
182 }
183
Wayne Davison38cab942004-05-08 18:18:42 +0000184 if (envpw)
185 rprintf(FERROR, "RSYNC_PASSWORD environment variable ignored\n");
Andrew Tridgell65575e91999-01-08 10:32:56 +0000186
Wayne Davison38cab942004-05-08 18:18:42 +0000187 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);
Wayne Davison18cc8c72004-05-08 19:37:28 +0000193 }
Andrew Tridgell65575e91999-01-08 10:32:56 +0000194
195 return NULL;
196}
197
Wayne Davison5037cf32005-04-09 18:11:23 +0000198/* Generate a 16-byte hash from a password and challenge. */
Andrew Tridgell6e4fb641998-09-09 05:57:34 +0000199static void generate_hash(char *in, char *challenge, char *out)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000200{
Wayne Davison5037cf32005-04-09 18:11:23 +0000201 char buf[MD4_SUM_LENGTH];
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000202
Wayne Davisonba582f72004-05-21 08:27:04 +0000203 sum_init(0);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000204 sum_update(in, strlen(in));
205 sum_update(challenge, strlen(challenge));
206 sum_end(buf);
207
Wayne Davison5037cf32005-04-09 18:11:23 +0000208 base64_encode(buf, MD4_SUM_LENGTH, out);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000209}
210
Wayne Davison18cc8c72004-05-08 19:37:28 +0000211/* 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 */
Wayne Davison5037cf32005-04-09 18:11:23 +0000217char *auth_server(int f_in, int f_out, int module, char *host, char *addr,
218 char *leader)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000219{
220 char *users = lp_auth_users(module);
Wayne Davison5037cf32005-04-09 18:11:23 +0000221 char challenge[MD4_SUM_LENGTH];
222 char b64_challenge[MD4_SUM_LENGTH*2];
Andrew Tridgelle42c9451998-05-15 09:26:01 +0000223 char line[MAXPATHLEN];
Wayne Davison5037cf32005-04-09 18:11:23 +0000224 char secret[512];
225 char pass2[MD4_SUM_LENGTH*2];
226 char *tok, *pass;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000227
228 /* if no auth list then allow anyone in! */
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000229 if (!users || !*users)
230 return "";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000231
232 gen_challenge(addr, challenge);
Wayne Davison18cc8c72004-05-08 19:37:28 +0000233
Wayne Davison5037cf32005-04-09 18:11:23 +0000234 base64_encode(challenge, MD4_SUM_LENGTH, b64_challenge);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000235
Wayne Davison973007d2002-08-01 00:36:54 +0000236 io_printf(f_out, "%s%s\n", leader, b64_challenge);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000237
Wayne Davison5037cf32005-04-09 18:11:23 +0000238 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);
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000243 return NULL;
Wayne Davison5037cf32005-04-09 18:11:23 +0000244 }
245 *pass++ = '\0';
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000246
Wayne Davison5037cf32005-04-09 18:11:23 +0000247 if (!(users = strdup(users)))
248 out_of_memory("auth_server");
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000249
Wayne Davison5037cf32005-04-09 18:11:23 +0000250 for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
251 if (wildmatch(tok, line))
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000252 break;
Andrew Tridgellc8e78d81998-05-13 12:21:10 +0000253 }
254 free(users);
255
Wayne Davison5037cf32005-04-09 18:11:23 +0000256 if (!tok) {
257 rprintf(FLOG, "auth failed on module %s from %s (%s): "
258 "unauthorized user\n",
259 lp_name(module), host, addr);
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000260 return NULL;
Wayne Davison5037cf32005-04-09 18:11:23 +0000261 }
Wayne Davison18cc8c72004-05-08 19:37:28 +0000262
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000263 memset(secret, 0, sizeof secret);
Wayne Davison5037cf32005-04-09 18:11:23 +0000264 if (!get_secret(module, line, secret, sizeof secret - 1)) {
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000265 memset(secret, 0, sizeof secret);
Wayne Davison5037cf32005-04-09 18:11:23 +0000266 rprintf(FLOG, "auth failed on module %s from %s (%s): "
267 "missing secret for user \"%s\"\n",
268 lp_name(module), host, addr, line);
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000269 return NULL;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000270 }
271
272 generate_hash(secret, b64_challenge, pass2);
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000273 memset(secret, 0, sizeof secret);
Wayne Davison18cc8c72004-05-08 19:37:28 +0000274
Wayne Davison5037cf32005-04-09 18:11:23 +0000275 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 }
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000281
Wayne Davison5037cf32005-04-09 18:11:23 +0000282 return strdup(line);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000283}
284
285
286void auth_client(int fd, char *user, char *challenge)
287{
288 char *pass;
Wayne Davison5037cf32005-04-09 18:11:23 +0000289 char pass2[MD4_SUM_LENGTH*2];
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000290
Wayne Davisonef383c02004-03-31 18:52:38 +0000291 if (!user || !*user)
Wayne Davison4b2f6a72004-04-01 18:05:40 +0000292 user = "nobody";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000293
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000294 if (!(pass = getpassf(password_file))
295 && !(pass = getenv("RSYNC_PASSWORD"))) {
Martin Pool64bd7562001-08-29 07:23:30 +0000296 /* XXX: cyeoh says that getpass is deprecated, because
Martin Pool908f5a92003-06-17 04:46:32 +0000297 * 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 */
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000305 pass = getpass("Password: ");
306 }
307
Wayne Davison38cab942004-05-08 18:18:42 +0000308 if (!pass)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000309 pass = "";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000310
311 generate_hash(pass, challenge, pass2);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000312 io_printf(fd, "%s %s\n", user, pass2);
313}
314
Andrew Tridgell65575e91999-01-08 10:32:56 +0000315