Annotation of embedaddon/rsync/authenticate.c, revision 1.1.1.2
1.1 misho 1: /*
2: * Support rsync daemon authentication.
3: *
4: * Copyright (C) 1998-2000 Andrew Tridgell
1.1.1.2 ! misho 5: * Copyright (C) 2002-2013 Wayne Davison
1.1 misho 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 3 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: *
17: * You should have received a copy of the GNU General Public License along
18: * with this program; if not, visit the http://fsf.org website.
19: */
20:
21: #include "rsync.h"
1.1.1.2 ! misho 22: #include "itypes.h"
1.1 misho 23:
1.1.1.2 ! misho 24: extern int read_only;
1.1 misho 25: extern char *password_file;
26:
27: /***************************************************************************
28: encode a buffer using base64 - simple and slow algorithm. null terminates
29: the result.
30: ***************************************************************************/
31: void base64_encode(const char *buf, int len, char *out, int pad)
32: {
33: char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
34: int bit_offset, byte_offset, idx, i;
35: const uchar *d = (const uchar *)buf;
36: int bytes = (len*8 + 5)/6;
37:
38: for (i = 0; i < bytes; i++) {
39: 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: }
51:
52: while (pad && (i % 4))
53: out[i++] = '=';
54:
55: out[i] = '\0';
56: }
57:
58: /* Generate a challenge buffer and return it base64-encoded. */
59: static void gen_challenge(const char *addr, char *challenge)
60: {
61: char input[32];
62: char digest[MAX_DIGEST_LEN];
63: struct timeval tv;
64: int len;
65:
66: memset(input, 0, sizeof input);
67:
68: strlcpy(input, addr, 17);
69: sys_gettimeofday(&tv);
70: SIVAL(input, 16, tv.tv_sec);
71: SIVAL(input, 20, tv.tv_usec);
72: SIVAL(input, 24, getpid());
73:
74: sum_init(0);
75: sum_update(input, sizeof input);
76: len = sum_end(digest);
77:
78: base64_encode(digest, len, challenge, 0);
79: }
80:
1.1.1.2 ! misho 81: /* Generate an MD4 hash created from the combination of the password
! 82: * and the challenge string and return it base64-encoded. */
! 83: static 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: }
1.1 misho 95:
96: /* Return the secret for a user from the secret file, null terminated.
97: * Maximum length is len (not counting the null). */
1.1.1.2 ! misho 98: static const char *check_secret(int module, const char *user, const char *group,
! 99: const char *challenge, const char *pass)
1.1 misho 100: {
1.1.1.2 ! misho 101: char line[1024];
! 102: char pass2[MAX_DIGEST_LEN*2];
1.1 misho 103: const char *fname = lp_secrets_file(module);
104: STRUCT_STAT st;
105: int fd, ok = 1;
1.1.1.2 ! misho 106: int user_len = strlen(user);
! 107: int group_len = group ? strlen(group) : 0;
! 108: char *err;
1.1 misho 109:
1.1.1.2 ! misho 110: if (!fname || !*fname || (fd = open(fname, O_RDONLY)) < 0)
! 111: return "no secrets file";
1.1 misho 112:
1.1.1.2 ! misho 113: if (do_fstat(fd, &st) == -1) {
! 114: rsyserr(FLOG, errno, "fstat(%s)", fname);
1.1 misho 115: ok = 0;
116: } else if (lp_strict_modes(module)) {
117: if ((st.st_mode & 06) != 0) {
118: rprintf(FLOG, "secrets file must not be other-accessible (see strict modes option)\n");
119: ok = 0;
120: } else if (MY_UID() == 0 && st.st_uid != 0) {
121: rprintf(FLOG, "secrets file must be owned by root when running as root (see strict modes)\n");
122: ok = 0;
123: }
124: }
125: if (!ok) {
126: close(fd);
1.1.1.2 ! misho 127: return "ignoring secrets file";
1.1 misho 128: }
129:
130: if (*user == '#') {
131: /* Reject attempt to match a comment. */
132: close(fd);
1.1.1.2 ! misho 133: return "invalid username";
1.1 misho 134: }
135:
1.1.1.2 ! misho 136: /* Try to find a line that starts with the user (or @group) name and a ':'. */
! 137: err = "secret not found";
! 138: while ((user || group) && read_line_old(fd, line, sizeof line, 1)) {
! 139: const char **ptr, *s;
! 140: int len;
! 141: if (*line == '@') {
! 142: ptr = &group;
! 143: len = group_len;
! 144: s = line+1;
! 145: } else {
! 146: ptr = &user;
! 147: len = user_len;
! 148: s = line;
1.1 misho 149: }
1.1.1.2 ! misho 150: if (!*ptr || strncmp(s, *ptr, len) != 0 || s[len] != ':')
! 151: continue;
! 152: generate_hash(s+len+1, challenge, pass2);
! 153: if (strcmp(pass, pass2) == 0) {
! 154: err = NULL;
! 155: break;
1.1 misho 156: }
1.1.1.2 ! misho 157: err = "password mismatch";
! 158: *ptr = NULL; /* Don't look for name again. */
1.1 misho 159: }
160:
161: close(fd);
162:
1.1.1.2 ! misho 163: memset(line, 0, sizeof line);
! 164: memset(pass2, 0, sizeof pass2);
! 165:
! 166: return err;
1.1 misho 167: }
168:
169: static const char *getpassf(const char *filename)
170: {
171: STRUCT_STAT st;
172: char buffer[512], *p;
1.1.1.2 ! misho 173: int n;
1.1 misho 174:
175: if (!filename)
176: return NULL;
177:
1.1.1.2 ! misho 178: if (strcmp(filename, "-") == 0) {
! 179: n = fgets(buffer, sizeof buffer, stdin) == NULL ? -1 : (int)strlen(buffer);
! 180: } else {
! 181: int fd;
! 182:
! 183: if ((fd = open(filename,O_RDONLY)) < 0) {
! 184: rsyserr(FERROR, errno, "could not open password file %s", filename);
! 185: exit_cleanup(RERR_SYNTAX);
! 186: }
1.1 misho 187:
1.1.1.2 ! misho 188: if (do_stat(filename, &st) == -1) {
! 189: rsyserr(FERROR, errno, "stat(%s)", filename);
! 190: exit_cleanup(RERR_SYNTAX);
! 191: }
! 192: if ((st.st_mode & 06) != 0) {
! 193: rprintf(FERROR, "ERROR: password file must not be other-accessible\n");
! 194: exit_cleanup(RERR_SYNTAX);
! 195: }
! 196: if (MY_UID() == 0 && st.st_uid != 0) {
! 197: rprintf(FERROR, "ERROR: password file must be owned by root when running as root\n");
! 198: exit_cleanup(RERR_SYNTAX);
! 199: }
! 200:
! 201: n = read(fd, buffer, sizeof buffer - 1);
! 202: close(fd);
1.1 misho 203: }
204:
205: if (n > 0) {
206: buffer[n] = '\0';
207: if ((p = strtok(buffer, "\n\r")) != NULL)
208: return strdup(p);
209: }
210:
211: rprintf(FERROR, "ERROR: failed to read a password from %s\n", filename);
212: exit_cleanup(RERR_SYNTAX);
213: }
214:
215: /* 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: */
221: char *auth_server(int f_in, int f_out, int module, const char *host,
222: const char *addr, const char *leader)
223: {
224: char *users = lp_auth_users(module);
225: char challenge[MAX_DIGEST_LEN*2];
226: char line[BIGPATHBUFLEN];
1.1.1.2 ! misho 227: char **auth_uid_groups = NULL;
! 228: int auth_uid_groups_cnt = -1;
! 229: const char *err = NULL;
! 230: int group_match = -1;
1.1 misho 231: char *tok, *pass;
1.1.1.2 ! misho 232: char opt_ch = '\0';
1.1 misho 233:
234: /* if no auth list then allow anyone in! */
235: if (!users || !*users)
236: return "";
237:
238: gen_challenge(addr, challenge);
239:
240: io_printf(f_out, "%s%s\n", leader, challenge);
241:
1.1.1.2 ! misho 242: if (!read_line_old(f_in, line, sizeof line, 0)
1.1 misho 243: || (pass = strchr(line, ' ')) == NULL) {
244: rprintf(FLOG, "auth failed on module %s from %s (%s): "
245: "invalid challenge response\n",
246: lp_name(module), host, addr);
247: return NULL;
248: }
249: *pass++ = '\0';
250:
251: if (!(users = strdup(users)))
252: out_of_memory("auth_server");
253:
254: for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
1.1.1.2 ! misho 255: char *opts;
! 256: /* See if the user appended :deny, :ro, or :rw. */
! 257: if ((opts = strchr(tok, ':')) != NULL) {
! 258: *opts++ = '\0';
! 259: opt_ch = isUpper(opts) ? toLower(opts) : *opts;
! 260: if (opt_ch == 'r') { /* handle ro and rw */
! 261: opt_ch = isUpper(opts+1) ? toLower(opts+1) : opts[1];
! 262: if (opt_ch == 'o')
! 263: opt_ch = 'r';
! 264: else if (opt_ch != 'w')
! 265: opt_ch = '\0';
! 266: } else if (opt_ch != 'd') /* if it's not deny, ignore it */
! 267: opt_ch = '\0';
! 268: } else
! 269: opt_ch = '\0';
! 270: if (*tok != '@') {
! 271: /* Match the username */
! 272: if (wildmatch(tok, line))
! 273: break;
! 274: } else {
! 275: #ifdef HAVE_GETGROUPLIST
! 276: int j;
! 277: /* See if authorizing user is a real user, and if so, see
! 278: * if it is in a group that matches tok+1 wildmat. */
! 279: if (auth_uid_groups_cnt < 0) {
! 280: gid_t gid_list[64];
! 281: uid_t auth_uid;
! 282: auth_uid_groups_cnt = sizeof gid_list / sizeof (gid_t);
! 283: if (!user_to_uid(line, &auth_uid, False)
! 284: || getallgroups(auth_uid, gid_list, &auth_uid_groups_cnt) != NULL)
! 285: auth_uid_groups_cnt = 0;
! 286: else {
! 287: if ((auth_uid_groups = new_array(char *, auth_uid_groups_cnt)) == NULL)
! 288: out_of_memory("auth_server");
! 289: for (j = 0; j < auth_uid_groups_cnt; j++)
! 290: auth_uid_groups[j] = gid_to_group(gid_list[j]);
! 291: }
! 292: }
! 293: for (j = 0; j < auth_uid_groups_cnt; j++) {
! 294: if (auth_uid_groups[j] && wildmatch(tok+1, auth_uid_groups[j])) {
! 295: group_match = j;
! 296: break;
! 297: }
! 298: }
! 299: if (group_match >= 0)
! 300: break;
! 301: #else
! 302: rprintf(FLOG, "your computer doesn't support getgrouplist(), so no @group authorization is possible.\n");
! 303: #endif
! 304: }
1.1 misho 305: }
1.1.1.2 ! misho 306:
1.1 misho 307: free(users);
308:
1.1.1.2 ! misho 309: if (!tok)
! 310: err = "no matching rule";
! 311: else if (opt_ch == 'd')
! 312: err = "denied by rule";
! 313: else {
! 314: char *group = group_match >= 0 ? auth_uid_groups[group_match] : NULL;
! 315: err = check_secret(module, line, group, challenge, pass);
! 316: }
! 317:
! 318: memset(challenge, 0, sizeof challenge);
! 319: memset(pass, 0, strlen(pass));
! 320:
! 321: if (auth_uid_groups) {
! 322: int j;
! 323: for (j = 0; j < auth_uid_groups_cnt; j++) {
! 324: if (auth_uid_groups[j])
! 325: free(auth_uid_groups[j]);
! 326: }
! 327: free(auth_uid_groups);
1.1 misho 328: }
329:
1.1.1.2 ! misho 330: if (err) {
! 331: rprintf(FLOG, "auth failed on module %s from %s (%s) for %s: %s\n",
! 332: lp_name(module), host, addr, line, err);
1.1 misho 333: return NULL;
334: }
335:
1.1.1.2 ! misho 336: if (opt_ch == 'r')
! 337: read_only = 1;
! 338: else if (opt_ch == 'w')
! 339: read_only = 0;
1.1 misho 340:
341: return strdup(line);
342: }
343:
344: void auth_client(int fd, const char *user, const char *challenge)
345: {
346: const char *pass;
347: char pass2[MAX_DIGEST_LEN*2];
348:
349: if (!user || !*user)
350: user = "nobody";
351:
352: if (!(pass = getpassf(password_file))
353: && !(pass = getenv("RSYNC_PASSWORD"))) {
354: /* XXX: cyeoh says that getpass is deprecated, because
355: * it may return a truncated password on some systems,
356: * and it is not in the LSB.
357: *
358: * Andrew Klein says that getpassphrase() is present
359: * on Solaris and reads up to 256 characters.
360: *
361: * OpenBSD has a readpassphrase() that might be more suitable.
362: */
363: pass = getpass("Password: ");
364: }
365:
366: if (!pass)
367: pass = "";
368:
369: generate_hash(pass, challenge, pass2);
370: io_printf(fd, "%s %s\n", user, pass2);
371: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>