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

1.1       misho       1: /*
                      2:  * Support rsync daemon authentication.
                      3:  *
                      4:  * Copyright (C) 1998-2000 Andrew Tridgell
1.1.1.3 ! misho       5:  * Copyright (C) 2002-2015 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;
1.1.1.3 ! misho     105:        int 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.1.3 ! misho     109:        FILE *fh;
1.1       misho     110: 
1.1.1.3 ! misho     111:        if (!fname || !*fname || (fh = fopen(fname, "r")) == NULL)
1.1.1.2   misho     112:                return "no secrets file";
1.1       misho     113: 
1.1.1.3 ! misho     114:        if (do_fstat(fileno(fh), &st) == -1) {
1.1.1.2   misho     115:                rsyserr(FLOG, errno, "fstat(%s)", fname);
1.1       misho     116:                ok = 0;
                    117:        } else if (lp_strict_modes(module)) {
                    118:                if ((st.st_mode & 06) != 0) {
                    119:                        rprintf(FLOG, "secrets file must not be other-accessible (see strict modes option)\n");
                    120:                        ok = 0;
                    121:                } else if (MY_UID() == 0 && st.st_uid != 0) {
                    122:                        rprintf(FLOG, "secrets file must be owned by root when running as root (see strict modes)\n");
                    123:                        ok = 0;
                    124:                }
                    125:        }
                    126:        if (!ok) {
1.1.1.3 ! misho     127:                fclose(fh);
1.1.1.2   misho     128:                return "ignoring secrets file";
1.1       misho     129:        }
                    130: 
                    131:        if (*user == '#') {
                    132:                /* Reject attempt to match a comment. */
1.1.1.3 ! misho     133:                fclose(fh);
1.1.1.2   misho     134:                return "invalid username";
1.1       misho     135:        }
                    136: 
1.1.1.2   misho     137:        /* Try to find a line that starts with the user (or @group) name and a ':'. */
                    138:        err = "secret not found";
1.1.1.3 ! misho     139:        while ((user || group) && fgets(line, sizeof line, fh) != NULL) {
        !           140:                const char **ptr, *s = strtok(line, "\n\r");
1.1.1.2   misho     141:                int len;
1.1.1.3 ! misho     142:                if (!s)
        !           143:                        continue;
        !           144:                if (*s == '@') {
1.1.1.2   misho     145:                        ptr = &group;
                    146:                        len = group_len;
1.1.1.3 ! misho     147:                        s++;
1.1.1.2   misho     148:                } else {
                    149:                        ptr = &user;
                    150:                        len = user_len;
1.1       misho     151:                }
1.1.1.2   misho     152:                if (!*ptr || strncmp(s, *ptr, len) != 0 || s[len] != ':')
                    153:                        continue;
                    154:                generate_hash(s+len+1, challenge, pass2);
                    155:                if (strcmp(pass, pass2) == 0) {
                    156:                        err = NULL;
                    157:                        break;
1.1       misho     158:                }
1.1.1.2   misho     159:                err = "password mismatch";
                    160:                *ptr = NULL; /* Don't look for name again. */
1.1       misho     161:        }
                    162: 
1.1.1.3 ! misho     163:        fclose(fh);
1.1       misho     164: 
1.1.1.2   misho     165:        memset(line, 0, sizeof line);
                    166:        memset(pass2, 0, sizeof pass2);
                    167: 
                    168:        return err;
1.1       misho     169: }
                    170: 
                    171: static const char *getpassf(const char *filename)
                    172: {
                    173:        STRUCT_STAT st;
                    174:        char buffer[512], *p;
1.1.1.2   misho     175:        int n;
1.1       misho     176: 
                    177:        if (!filename)
                    178:                return NULL;
                    179: 
1.1.1.2   misho     180:        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:                }
1.1       misho     189: 
1.1.1.2   misho     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);
1.1       misho     205:        }
                    206: 
                    207:        if (n > 0) {
                    208:                buffer[n] = '\0';
                    209:                if ((p = strtok(buffer, "\n\r")) != NULL)
                    210:                        return strdup(p);
                    211:        }
                    212: 
                    213:        rprintf(FERROR, "ERROR: failed to read a password from %s\n", filename);
                    214:        exit_cleanup(RERR_SYNTAX);
                    215: }
                    216: 
                    217: /* 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:  */
                    223: char *auth_server(int f_in, int f_out, int module, const char *host,
                    224:                  const char *addr, const char *leader)
                    225: {
                    226:        char *users = lp_auth_users(module);
                    227:        char challenge[MAX_DIGEST_LEN*2];
                    228:        char line[BIGPATHBUFLEN];
1.1.1.2   misho     229:        char **auth_uid_groups = NULL;
                    230:        int auth_uid_groups_cnt = -1;
                    231:        const char *err = NULL;
                    232:        int group_match = -1;
1.1       misho     233:        char *tok, *pass;
1.1.1.2   misho     234:        char opt_ch = '\0';
1.1       misho     235: 
                    236:        /* if no auth list then allow anyone in! */
                    237:        if (!users || !*users)
                    238:                return "";
                    239: 
                    240:        gen_challenge(addr, challenge);
                    241: 
                    242:        io_printf(f_out, "%s%s\n", leader, challenge);
                    243: 
1.1.1.2   misho     244:        if (!read_line_old(f_in, line, sizeof line, 0)
1.1       misho     245:         || (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);
                    249:                return NULL;
                    250:        }
                    251:        *pass++ = '\0';
                    252: 
                    253:        if (!(users = strdup(users)))
                    254:                out_of_memory("auth_server");
                    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.2   misho     290:                                        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++)
1.1.1.3 ! misho     293:                                                auth_uid_groups[j] = gid_to_group(gid_array[j]);
1.1.1.2   misho     294:                                }
                    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:                }
1.1       misho     308:        }
1.1.1.2   misho     309: 
1.1       misho     310:        free(users);
                    311: 
1.1.1.2   misho     312:        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);
1.1       misho     331:        }
                    332: 
1.1.1.2   misho     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);
1.1       misho     336:                return NULL;
                    337:        }
                    338: 
1.1.1.2   misho     339:        if (opt_ch == 'r')
                    340:                read_only = 1;
                    341:        else if (opt_ch == 'w')
                    342:                read_only = 0;
1.1       misho     343: 
                    344:        return strdup(line);
                    345: }
                    346: 
                    347: void auth_client(int fd, const char *user, const char *challenge)
                    348: {
                    349:        const char *pass;
                    350:        char pass2[MAX_DIGEST_LEN*2];
                    351: 
                    352:        if (!user || !*user)
                    353:                user = "nobody";
                    354: 
                    355:        if (!(pass = getpassf(password_file))
                    356:         && !(pass = getenv("RSYNC_PASSWORD"))) {
                    357:                /* XXX: cyeoh says that getpass is deprecated, because
                    358:                 * 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:                  */
                    366:                pass = getpass("Password: ");
                    367:        }
                    368: 
                    369:        if (!pass)
                    370:                pass = "";
                    371: 
                    372:        generate_hash(pass, challenge, pass2);
                    373:        io_printf(fd, "%s %s\n", user, pass2);
                    374: }

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