blob: 5f125dea2aac029718fb424502f4a2fbb15b8006 [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 Davison453914e2015-08-08 12:47:03 -07005 * Copyright (C) 2002-2015 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"
Wayne Davison5ebe9a42010-10-12 08:05:43 -070022#include "itypes.h"
Andrew Tridgell31593dd1998-05-13 09:38:54 +000023
Wayne Davison5ebe9a42010-10-12 08:05:43 -070024extern int read_only;
Wayne Davison38cab942004-05-08 18:18:42 +000025extern char *password_file;
Wayne Davison38cab942004-05-08 18:18:42 +000026
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000027/***************************************************************************
28encode a buffer using base64 - simple and slow algorithm. null terminates
29the result.
30 ***************************************************************************/
Wayne Davison4a19c3b2006-11-19 00:23:21 +000031void base64_encode(const char *buf, int len, char *out, int pad)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000032{
33 char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
34 int bit_offset, byte_offset, idx, i;
Wayne Davison4a19c3b2006-11-19 00:23:21 +000035 const uchar *d = (const uchar *)buf;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000036 int bytes = (len*8 + 5)/6;
37
Wayne Davison57385122004-01-03 08:53:36 +000038 for (i = 0; i < bytes; i++) {
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000039 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 }
Wayne Davison293def62006-03-06 18:22:20 +000051
52 while (pad && (i % 4))
53 out[i++] = '=';
54
55 out[i] = '\0';
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000056}
57
Wayne Davisonbf011fe2005-04-10 17:09:10 +000058/* Generate a challenge buffer and return it base64-encoded. */
Wayne Davison4a19c3b2006-11-19 00:23:21 +000059static void gen_challenge(const char *addr, char *challenge)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000060{
61 char input[32];
Wayne Davisona0456b92007-03-18 06:00:53 +000062 char digest[MAX_DIGEST_LEN];
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000063 struct timeval tv;
Wayne Davisona0456b92007-03-18 06:00:53 +000064 int len;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000065
Wayne Davison58c9b4b2004-05-08 19:26:53 +000066 memset(input, 0, sizeof input);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000067
Wayne Davison4a19c3b2006-11-19 00:23:21 +000068 strlcpy(input, addr, 17);
Andrew Tridgell3060d4a2000-01-23 02:16:51 +000069 sys_gettimeofday(&tv);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000070 SIVAL(input, 16, tv.tv_sec);
71 SIVAL(input, 20, tv.tv_usec);
72 SIVAL(input, 24, getpid());
73
Wayne Davisonba582f72004-05-21 08:27:04 +000074 sum_init(0);
Wayne Davison58c9b4b2004-05-08 19:26:53 +000075 sum_update(input, sizeof input);
Wayne Davisona0456b92007-03-18 06:00:53 +000076 len = sum_end(digest);
Wayne Davisonbf011fe2005-04-10 17:09:10 +000077
Wayne Davisona0456b92007-03-18 06:00:53 +000078 base64_encode(digest, len, challenge, 0);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000079}
80
Wayne Davison5ebe9a42010-10-12 08:05:43 -070081/* Generate an MD4 hash created from the combination of the password
82 * and the challenge string and return it base64-encoded. */
83static void generate_hash(const char *in, const char *challenge, char *out)
84{
85 char buf[MAX_DIGEST_LEN];
86 int len;
87
88 sum_init(0);
89 sum_update(in, strlen(in));
90 sum_update(challenge, strlen(challenge));
91 len = sum_end(buf);
92
93 base64_encode(buf, len, out, 0);
94}
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000095
Wayne Davison38cab942004-05-08 18:18:42 +000096/* Return the secret for a user from the secret file, null terminated.
97 * Maximum length is len (not counting the null). */
Wayne Davison5ebe9a42010-10-12 08:05:43 -070098static const char *check_secret(int module, const char *user, const char *group,
99 const char *challenge, const char *pass)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000100{
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700101 char line[1024];
102 char pass2[MAX_DIGEST_LEN*2];
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000103 const char *fname = lp_secrets_file(module);
David Dykstrad1be2311998-11-24 19:52:35 +0000104 STRUCT_STAT st;
Wayne Davison0dedfbc2014-04-13 13:44:58 -0700105 int ok = 1;
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700106 int user_len = strlen(user);
107 int group_len = group ? strlen(group) : 0;
108 char *err;
Wayne Davison0dedfbc2014-04-13 13:44:58 -0700109 FILE *fh;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000110
Wayne Davison0dedfbc2014-04-13 13:44:58 -0700111 if (!fname || !*fname || (fh = fopen(fname, "r")) == NULL)
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700112 return "no secrets file";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000113
Wayne Davison0dedfbc2014-04-13 13:44:58 -0700114 if (do_fstat(fileno(fh), &st) == -1) {
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700115 rsyserr(FLOG, errno, "fstat(%s)", fname);
David Dykstrad1be2311998-11-24 19:52:35 +0000116 ok = 0;
David Dykstra3ca8e681999-02-09 19:27:15 +0000117 } else if (lp_strict_modes(module)) {
118 if ((st.st_mode & 06) != 0) {
Wayne Davison30c041f2004-09-24 17:04:05 +0000119 rprintf(FLOG, "secrets file must not be other-accessible (see strict modes option)\n");
David Dykstra3ca8e681999-02-09 19:27:15 +0000120 ok = 0;
Wayne Davison351f5e22006-01-26 11:01:00 +0000121 } else if (MY_UID() == 0 && st.st_uid != 0) {
Wayne Davison30c041f2004-09-24 17:04:05 +0000122 rprintf(FLOG, "secrets file must be owned by root when running as root (see strict modes)\n");
David Dykstra3ca8e681999-02-09 19:27:15 +0000123 ok = 0;
124 }
David Dykstrad1be2311998-11-24 19:52:35 +0000125 }
126 if (!ok) {
Wayne Davison0dedfbc2014-04-13 13:44:58 -0700127 fclose(fh);
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700128 return "ignoring secrets file";
David Dykstrad1be2311998-11-24 19:52:35 +0000129 }
130
Wayne Davison38cab942004-05-08 18:18:42 +0000131 if (*user == '#') {
132 /* Reject attempt to match a comment. */
Wayne Davison0dedfbc2014-04-13 13:44:58 -0700133 fclose(fh);
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700134 return "invalid username";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000135 }
136
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700137 /* Try to find a line that starts with the user (or @group) name and a ':'. */
138 err = "secret not found";
Wayne Davison0dedfbc2014-04-13 13:44:58 -0700139 while ((user || group) && fgets(line, sizeof line, fh) != NULL) {
140 const char **ptr, *s = strtok(line, "\n\r");
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700141 int len;
Wayne Davison0dedfbc2014-04-13 13:44:58 -0700142 if (!s)
143 continue;
144 if (*s == '@') {
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700145 ptr = &group;
146 len = group_len;
Wayne Davison0dedfbc2014-04-13 13:44:58 -0700147 s++;
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700148 } else {
149 ptr = &user;
150 len = user_len;
Wayne Davison38cab942004-05-08 18:18:42 +0000151 }
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700152 if (!*ptr || strncmp(s, *ptr, len) != 0 || s[len] != ':')
Wayne Davison38cab942004-05-08 18:18:42 +0000153 continue;
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700154 generate_hash(s+len+1, challenge, pass2);
155 if (strcmp(pass, pass2) == 0) {
156 err = NULL;
157 break;
158 }
159 err = "password mismatch";
160 *ptr = NULL; /* Don't look for name again. */
Wayne Davison38cab942004-05-08 18:18:42 +0000161 }
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700162
Wayne Davison0dedfbc2014-04-13 13:44:58 -0700163 fclose(fh);
Wayne Davison38cab942004-05-08 18:18:42 +0000164
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700165 memset(line, 0, sizeof line);
166 memset(pass2, 0, sizeof pass2);
167
168 return err;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000169}
170
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000171static const char *getpassf(const char *filename)
Andrew Tridgell65575e91999-01-08 10:32:56 +0000172{
Andrew Tridgell65575e91999-01-08 10:32:56 +0000173 STRUCT_STAT st;
Wayne Davison38cab942004-05-08 18:18:42 +0000174 char buffer[512], *p;
Wayne Davison12505e02013-06-09 12:04:25 -0700175 int n;
Andrew Tridgell65575e91999-01-08 10:32:56 +0000176
Wayne Davison38cab942004-05-08 18:18:42 +0000177 if (!filename)
178 return NULL;
Andrew Tridgell65575e91999-01-08 10:32:56 +0000179
Wayne Davison12505e02013-06-09 12:04:25 -0700180 if (strcmp(filename, "-") == 0) {
181 n = fgets(buffer, sizeof buffer, stdin) == NULL ? -1 : (int)strlen(buffer);
182 } else {
183 int fd;
184
185 if ((fd = open(filename,O_RDONLY)) < 0) {
186 rsyserr(FERROR, errno, "could not open password file %s", filename);
187 exit_cleanup(RERR_SYNTAX);
188 }
189
190 if (do_stat(filename, &st) == -1) {
191 rsyserr(FERROR, errno, "stat(%s)", filename);
192 exit_cleanup(RERR_SYNTAX);
193 }
194 if ((st.st_mode & 06) != 0) {
195 rprintf(FERROR, "ERROR: password file must not be other-accessible\n");
196 exit_cleanup(RERR_SYNTAX);
197 }
198 if (MY_UID() == 0 && st.st_uid != 0) {
199 rprintf(FERROR, "ERROR: password file must be owned by root when running as root\n");
200 exit_cleanup(RERR_SYNTAX);
201 }
202
203 n = read(fd, buffer, sizeof buffer - 1);
204 close(fd);
Andrew Tridgell65575e91999-01-08 10:32:56 +0000205 }
Wayne Davison18cc8c72004-05-08 19:37:28 +0000206
Wayne Davison38cab942004-05-08 18:18:42 +0000207 if (n > 0) {
208 buffer[n] = '\0';
209 if ((p = strtok(buffer, "\n\r")) != NULL)
210 return strdup(p);
Wayne Davison18cc8c72004-05-08 19:37:28 +0000211 }
Andrew Tridgell65575e91999-01-08 10:32:56 +0000212
Wayne Davison70c4bfb2011-09-06 21:18:32 -0700213 rprintf(FERROR, "ERROR: failed to read a password from %s\n", filename);
214 exit_cleanup(RERR_SYNTAX);
Andrew Tridgell65575e91999-01-08 10:32:56 +0000215}
216
Wayne Davison18cc8c72004-05-08 19:37:28 +0000217/* Possibly negotiate authentication with the client. Use "leader" to
218 * start off the auth if necessary.
219 *
220 * Return NULL if authentication failed. Return "" if anonymous access.
221 * Otherwise return username.
222 */
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000223char *auth_server(int f_in, int f_out, int module, const char *host,
224 const char *addr, const char *leader)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000225{
226 char *users = lp_auth_users(module);
Wayne Davisona0456b92007-03-18 06:00:53 +0000227 char challenge[MAX_DIGEST_LEN*2];
Wayne Davisond999d312005-07-29 18:31:05 +0000228 char line[BIGPATHBUFLEN];
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700229 char **auth_uid_groups = NULL;
230 int auth_uid_groups_cnt = -1;
231 const char *err = NULL;
232 int group_match = -1;
Wayne Davison5037cf32005-04-09 18:11:23 +0000233 char *tok, *pass;
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700234 char opt_ch = '\0';
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000235
236 /* if no auth list then allow anyone in! */
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000237 if (!users || !*users)
238 return "";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000239
240 gen_challenge(addr, challenge);
Wayne Davison18cc8c72004-05-08 19:37:28 +0000241
Wayne Davisonbf011fe2005-04-10 17:09:10 +0000242 io_printf(f_out, "%s%s\n", leader, challenge);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000243
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700244 if (!read_line_old(f_in, line, sizeof line, 0)
Wayne Davison5037cf32005-04-09 18:11:23 +0000245 || (pass = strchr(line, ' ')) == NULL) {
246 rprintf(FLOG, "auth failed on module %s from %s (%s): "
247 "invalid challenge response\n",
248 lp_name(module), host, addr);
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000249 return NULL;
Wayne Davison5037cf32005-04-09 18:11:23 +0000250 }
251 *pass++ = '\0';
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000252
Wayne Davison5037cf32005-04-09 18:11:23 +0000253 if (!(users = strdup(users)))
254 out_of_memory("auth_server");
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000255
Wayne Davison5037cf32005-04-09 18:11:23 +0000256 for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700257 char *opts;
258 /* See if the user appended :deny, :ro, or :rw. */
259 if ((opts = strchr(tok, ':')) != NULL) {
260 *opts++ = '\0';
261 opt_ch = isUpper(opts) ? toLower(opts) : *opts;
262 if (opt_ch == 'r') { /* handle ro and rw */
263 opt_ch = isUpper(opts+1) ? toLower(opts+1) : opts[1];
264 if (opt_ch == 'o')
265 opt_ch = 'r';
266 else if (opt_ch != 'w')
267 opt_ch = '\0';
268 } else if (opt_ch != 'd') /* if it's not deny, ignore it */
269 opt_ch = '\0';
270 } else
271 opt_ch = '\0';
272 if (*tok != '@') {
273 /* Match the username */
274 if (wildmatch(tok, line))
275 break;
276 } else {
277#ifdef HAVE_GETGROUPLIST
278 int j;
279 /* See if authorizing user is a real user, and if so, see
280 * if it is in a group that matches tok+1 wildmat. */
281 if (auth_uid_groups_cnt < 0) {
Wayne Davison2a7355f2015-08-24 11:23:31 -0700282 item_list gid_list = EMPTY_ITEM_LIST;
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700283 uid_t auth_uid;
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700284 if (!user_to_uid(line, &auth_uid, False)
Wayne Davison2a7355f2015-08-24 11:23:31 -0700285 || getallgroups(auth_uid, &gid_list) != NULL)
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700286 auth_uid_groups_cnt = 0;
287 else {
Wayne Davison2a7355f2015-08-24 11:23:31 -0700288 gid_t *gid_array = gid_list.items;
289 auth_uid_groups_cnt = gid_list.count;
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700290 if ((auth_uid_groups = new_array(char *, auth_uid_groups_cnt)) == NULL)
291 out_of_memory("auth_server");
292 for (j = 0; j < auth_uid_groups_cnt; j++)
Wayne Davison2a7355f2015-08-24 11:23:31 -0700293 auth_uid_groups[j] = gid_to_group(gid_array[j]);
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700294 }
295 }
296 for (j = 0; j < auth_uid_groups_cnt; j++) {
297 if (auth_uid_groups[j] && wildmatch(tok+1, auth_uid_groups[j])) {
298 group_match = j;
299 break;
300 }
301 }
302 if (group_match >= 0)
303 break;
304#else
305 rprintf(FLOG, "your computer doesn't support getgrouplist(), so no @group authorization is possible.\n");
306#endif
307 }
Andrew Tridgellc8e78d81998-05-13 12:21:10 +0000308 }
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700309
Andrew Tridgellc8e78d81998-05-13 12:21:10 +0000310 free(users);
311
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700312 if (!tok)
313 err = "no matching rule";
314 else if (opt_ch == 'd')
315 err = "denied by rule";
316 else {
317 char *group = group_match >= 0 ? auth_uid_groups[group_match] : NULL;
318 err = check_secret(module, line, group, challenge, pass);
319 }
320
321 memset(challenge, 0, sizeof challenge);
322 memset(pass, 0, strlen(pass));
323
324 if (auth_uid_groups) {
325 int j;
326 for (j = 0; j < auth_uid_groups_cnt; j++) {
327 if (auth_uid_groups[j])
328 free(auth_uid_groups[j]);
329 }
330 free(auth_uid_groups);
331 }
332
333 if (err) {
334 rprintf(FLOG, "auth failed on module %s from %s (%s) for %s: %s\n",
335 lp_name(module), host, addr, line, err);
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000336 return NULL;
Wayne Davison5037cf32005-04-09 18:11:23 +0000337 }
Wayne Davison18cc8c72004-05-08 19:37:28 +0000338
Wayne Davison5ebe9a42010-10-12 08:05:43 -0700339 if (opt_ch == 'r')
340 read_only = 1;
341 else if (opt_ch == 'w')
342 read_only = 0;
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000343
Wayne Davison5037cf32005-04-09 18:11:23 +0000344 return strdup(line);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000345}
346
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000347void auth_client(int fd, const char *user, const char *challenge)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000348{
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000349 const char *pass;
Wayne Davisona0456b92007-03-18 06:00:53 +0000350 char pass2[MAX_DIGEST_LEN*2];
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000351
Wayne Davisonef383c02004-03-31 18:52:38 +0000352 if (!user || !*user)
Wayne Davison4b2f6a72004-04-01 18:05:40 +0000353 user = "nobody";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000354
Wayne Davison58c9b4b2004-05-08 19:26:53 +0000355 if (!(pass = getpassf(password_file))
356 && !(pass = getenv("RSYNC_PASSWORD"))) {
Martin Pool64bd7562001-08-29 07:23:30 +0000357 /* XXX: cyeoh says that getpass is deprecated, because
Martin Pool908f5a92003-06-17 04:46:32 +0000358 * it may return a truncated password on some systems,
359 * and it is not in the LSB.
360 *
361 * Andrew Klein says that getpassphrase() is present
362 * on Solaris and reads up to 256 characters.
363 *
364 * OpenBSD has a readpassphrase() that might be more suitable.
365 */
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000366 pass = getpass("Password: ");
367 }
368
Wayne Davison38cab942004-05-08 18:18:42 +0000369 if (!pass)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000370 pass = "";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000371
372 generate_hash(pass, challenge, pass2);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000373 io_printf(fd, "%s %s\n", user, pass2);
374}