Annotation of embedaddon/rsync/authenticate.c, revision 1.1.1.4

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>