[BACK]Return to yp_passwd.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / passwd

Annotation of src/usr.bin/passwd/yp_passwd.c, Revision 1.37

1.37    ! ajacouto    1: /*     $OpenBSD: yp_passwd.c,v 1.36 2015/08/20 22:32:41 deraadt Exp $  */
1.2       deraadt     2:
1.1       deraadt     3: /*
                      4:  * Copyright (c) 1988 The Regents of the University of California.
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.24      millert    15:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
                     31:
                     32: #ifdef YP
                     33:
                     34: #include <stdio.h>
1.8       weingart   35: #include <stdlib.h>
1.1       deraadt    36: #include <string.h>
1.8       weingart   37: #include <unistd.h>
1.1       deraadt    38: #include <netdb.h>
                     39: #include <time.h>
                     40: #include <pwd.h>
1.8       weingart   41: #include <err.h>
1.35      deraadt    42: #include <signal.h>
1.1       deraadt    43: #include <errno.h>
1.8       weingart   44: #include <ctype.h>
1.17      millert    45: #include <login_cap.h>
1.1       deraadt    46: #include <rpc/rpc.h>
                     47: #include <rpcsvc/yp_prot.h>
                     48: #include <rpcsvc/ypclnt.h>
                     49: #define passwd yp_passwd_rec
                     50: #include <rpcsvc/yppasswd.h>
                     51: #undef passwd
                     52:
                     53: #ifndef _PASSWORD_LEN
                     54: #define _PASSWORD_LEN PASS_MAX
                     55: #endif
                     56:
1.28      millert    57: extern int     pwd_check(login_cap_t *, char *);
                     58: extern int     pwd_gettries(login_cap_t *);
1.21      fgsch      59: extern void    kbintr(int);
1.31      djm        60: int            yp_passwd(char *);
1.14      provos     61:
1.23      deraadt    62: char           *ypgetnewpasswd(struct passwd *, login_cap_t *, char **);
1.25      maja       63: struct passwd  *ypgetpwnam(char *, int);
1.31      djm        64: struct passwd  *interpret(struct passwd *, char *, int);
1.1       deraadt    65:
                     66: char *domain;
                     67:
1.8       weingart   68: static int
1.31      djm        69: pw_error(char *name, int error, int eval)
1.1       deraadt    70: {
1.31      djm        71:        if (error) {
1.21      fgsch      72:                if (name)
                     73:                        warn("%s", name);
                     74:                else
                     75:                        warn(NULL);
                     76:        }
1.1       deraadt    77:
1.21      fgsch      78:        warnx("YP passwd database: unchanged.");
1.1       deraadt    79:        exit(eval);
                     80: }
                     81:
1.8       weingart   82: int
1.23      deraadt    83: yp_passwd(char *username)
1.1       deraadt    84: {
1.31      djm        85:        struct yppasswd yppwd;
1.25      maja       86:        int r, rpcport, status, secure=0;
1.1       deraadt    87:        struct passwd *pw;
                     88:        struct timeval tv;
1.23      deraadt    89:        login_cap_t *lc;
1.1       deraadt    90:        CLIENT *client;
1.23      deraadt    91:        char *master;
                     92:        uid_t uid;
1.1       deraadt    93:
                     94:        /*
                     95:         * Get local domain
                     96:         */
1.8       weingart   97:        if ((r = yp_get_default_domain(&domain)) != 0) {
1.21      fgsch      98:                warnx("can't get local YP domain. Reason: %s",
                     99:                    yperr_string(r));
                    100:                return (1);
1.1       deraadt   101:        }
                    102:
                    103:        /*
                    104:         * Find the host for the passwd map; it should be running
                    105:         * the daemon.
                    106:         */
1.25      maja      107:        if ((r = yp_master(domain, "master.passwd.byname", &master)) == 0) {
                    108:                secure=1;
                    109:        } else if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
1.9       millert   110:                warnx("can't find the master YP server. Reason: %s",
                    111:                    yperr_string(r));
1.21      fgsch     112:                return (1);
1.1       deraadt   113:        }
                    114:
                    115:        /*
                    116:         * Ask the portmapper for the port of the daemon.
                    117:         */
                    118:        if ((rpcport = getrpcport(master, YPPASSWDPROG,
                    119:            YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
1.8       weingart  120:                warnx("master YP server not running yppasswd daemon.");
                    121:                warnx("Can't change password.");
1.21      fgsch     122:                return (1);
1.1       deraadt   123:        }
                    124:
                    125:        /*
1.22      deraadt   126:         * Be sure the port is privileged
1.1       deraadt   127:         */
                    128:        if (rpcport >= IPPORT_RESERVED) {
1.8       weingart  129:                warnx("yppasswd daemon is on an invalid port.");
1.21      fgsch     130:                return (1);
1.1       deraadt   131:        }
                    132:
                    133:        /* Get user's login identity */
1.25      maja      134:        if (!(pw = ypgetpwnam(username, secure))) {
1.8       weingart  135:                warnx("unknown user %s.", username);
1.21      fgsch     136:                return (1);
1.1       deraadt   137:        }
1.17      millert   138:        if ((lc = login_getclass(pw->pw_class)) == NULL) {
                    139:                warnx("unable to get login class for user %s.", username);
1.21      fgsch     140:                return (1);
1.17      millert   141:        }
1.21      fgsch     142:
1.15      millert   143:        uid = getuid();
1.1       deraadt   144:        if (uid && uid != pw->pw_uid) {
1.21      fgsch     145:                warnx("you may only change your own password: %s",
                    146:                    strerror(EACCES));
                    147:                return (1);
1.1       deraadt   148:        }
                    149:
                    150:        /* prompt for new password */
1.31      djm       151:        yppwd.newpw.pw_passwd = ypgetnewpasswd(pw, lc, &yppwd.oldpass);
1.1       deraadt   152:
                    153:        /* tell rpc.yppasswdd */
1.31      djm       154:        yppwd.newpw.pw_name     = pw->pw_name;
                    155:        yppwd.newpw.pw_uid      = pw->pw_uid;
                    156:        yppwd.newpw.pw_gid      = pw->pw_gid;
                    157:        yppwd.newpw.pw_gecos = pw->pw_gecos;
                    158:        yppwd.newpw.pw_dir      = pw->pw_dir;
                    159:        yppwd.newpw.pw_shell    = pw->pw_shell;
1.21      fgsch     160:
1.1       deraadt   161:        client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
                    162:        if (client==NULL) {
1.9       millert   163:                warnx("cannot contact yppasswdd on %s: Reason: %s",
1.1       deraadt   164:                    master, yperr_string(YPERR_YPBIND));
1.31      djm       165:                free(yppwd.newpw.pw_passwd);
1.21      fgsch     166:                return (YPERR_YPBIND);
1.1       deraadt   167:        }
                    168:        client->cl_auth = authunix_create_default();
                    169:        tv.tv_sec = 2;
                    170:        tv.tv_usec = 0;
                    171:        r = clnt_call(client, YPPASSWDPROC_UPDATE,
1.31      djm       172:            xdr_yppasswd, &yppwd, xdr_int, &status, tv);
1.30      aanriot   173:        if (r) {
                    174:                printf("rpc to yppasswdd failed.\n");
1.31      djm       175:                free(yppwd.newpw.pw_passwd);
1.30      aanriot   176:                return (1);
                    177:        } else if (status) {
1.1       deraadt   178:                printf("Couldn't change YP password.\n");
1.31      djm       179:                free(yppwd.newpw.pw_passwd);
1.21      fgsch     180:                return (1);
1.30      aanriot   181:        } else {
                    182:                printf("The YP password has been changed on %s, "
                    183:                    "the master YP passwd server.\n", master);
1.31      djm       184:                free(yppwd.newpw.pw_passwd);
1.30      aanriot   185:                return (0);
1.3       deraadt   186:        }
1.1       deraadt   187: }
                    188:
1.15      millert   189: char *
1.23      deraadt   190: ypgetnewpasswd(struct passwd *pw, login_cap_t *lc, char **old_pass)
1.1       deraadt   191: {
1.33      tedu      192:        char buf[1024], hash[_PASSWORD_LEN];
1.19      millert   193:        sig_t saveint, savequit;
1.23      deraadt   194:        int tries, pwd_tries;
1.34      tedu      195:        char *p, *pref;
1.19      millert   196:
                    197:        saveint = signal(SIGINT, kbintr);
                    198:        savequit = signal(SIGQUIT, kbintr);
1.21      fgsch     199:
1.3       deraadt   200:        printf("Changing YP password for %s.\n", pw->pw_name);
1.1       deraadt   201:        if (old_pass) {
                    202:                *old_pass = NULL;
1.21      fgsch     203:
1.1       deraadt   204:                if (pw->pw_passwd[0]) {
1.3       deraadt   205:                        p = getpass("Old password:");
1.26      millert   206:                        if (p == NULL ||
                    207:                            strcmp(crypt(p, pw->pw_passwd), pw->pw_passwd)) {
1.3       deraadt   208:                                errno = EACCES;
                    209:                                pw_error(NULL, 1, 1);
1.1       deraadt   210:                        }
1.3       deraadt   211:                } else
1.1       deraadt   212:                        p = "";
                    213:                *old_pass = strdup(p);
1.16      deraadt   214:                if (*old_pass == NULL)
                    215:                        pw_error(NULL, 1, 1);
1.1       deraadt   216:        }
1.3       deraadt   217:
1.28      millert   218:        pwd_tries = pwd_gettries(lc);
1.14      provos    219:
1.1       deraadt   220:        for (buf[0] = '\0', tries = 0;;) {
                    221:                p = getpass("New password:");
1.27      millert   222:                if (p == NULL || *p == '\0')
                    223:                        pw_error(NULL, 0, p == NULL ? 1 : 0);
1.10      deraadt   224:                if (strcmp(p, "s/key") == 0) {
1.21      fgsch     225:                        printf("That password collides with a system feature. "
                    226:                            "Choose another.\n");
1.10      deraadt   227:                        continue;
1.1       deraadt   228:                }
1.21      fgsch     229:                if ((tries++ < pwd_tries || pwd_tries == 0)
1.28      millert   230:                    && pwd_check(lc, p) == 0)
1.1       deraadt   231:                        continue;
1.23      deraadt   232:                strlcpy(buf, p, sizeof buf);
1.26      millert   233:                p = getpass("Retype new password:");
                    234:                if (p != NULL && strcmp(buf, p) == 0)
1.1       deraadt   235:                        break;
                    236:                (void)printf("Mismatch; try again, EOF to quit.\n");
                    237:        }
1.33      tedu      238:
                    239:        (void)signal(SIGINT, saveint);
                    240:        (void)signal(SIGQUIT, savequit);
                    241:
1.34      tedu      242:        pref = login_getcapstr(lc, "localcipher", NULL, NULL);
                    243:        if (crypt_newhash(buf, pref, hash, sizeof(hash)) == -1) {
1.33      tedu      244:                (void)printf("Couldn't generate hash.\n");
1.21      fgsch     245:                pw_error(NULL, 0, 0);
                    246:        }
1.34      tedu      247:        free(pref);
1.33      tedu      248:        p = strdup(hash);
1.16      deraadt   249:        if (p == NULL)
                    250:                pw_error(NULL, 1, 1);
1.19      millert   251:
1.16      deraadt   252:        return (p);
1.1       deraadt   253: }
                    254:
                    255: static char *
1.15      millert   256: pwskip(char *p)
1.1       deraadt   257: {
                    258:        while (*p && *p != ':' && *p != '\n')
                    259:                ++p;
                    260:        if (*p)
                    261:                *p++ = 0;
                    262:        return (p);
                    263: }
                    264:
                    265: struct passwd *
1.25      maja      266: interpret(struct passwd *pwent, char *line, int secure)
1.1       deraadt   267: {
1.15      millert   268:        char    *p = line;
1.1       deraadt   269:
                    270:        pwent->pw_passwd = "*";
                    271:        pwent->pw_uid = 0;
                    272:        pwent->pw_gid = 0;
                    273:        pwent->pw_gecos = "";
                    274:        pwent->pw_dir = "";
                    275:        pwent->pw_shell = "";
                    276:        pwent->pw_change = 0;
                    277:        pwent->pw_expire = 0;
                    278:        pwent->pw_class = "";
1.21      fgsch     279:
1.1       deraadt   280:        /* line without colon separators is no good, so ignore it */
1.23      deraadt   281:        if (!strchr(p, ':'))
1.21      fgsch     282:                return (NULL);
1.1       deraadt   283:
                    284:        pwent->pw_name = p;
                    285:        p = pwskip(p);
                    286:        pwent->pw_passwd = p;
                    287:        p = pwskip(p);
                    288:        pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
                    289:        p = pwskip(p);
                    290:        pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
                    291:        p = pwskip(p);
1.25      maja      292:        if ( secure == 1 ) {
                    293:                pwent->pw_class = p;
                    294:                p = pwskip(p);
                    295:                pwent->pw_change = (time_t)strtoul(p, NULL, 10);
                    296:                p = pwskip(p);
                    297:                pwent->pw_expire = (time_t)strtoul(p, NULL, 10);
                    298:                p = pwskip(p);
                    299:        }
1.1       deraadt   300:        pwent->pw_gecos = p;
                    301:        p = pwskip(p);
                    302:        pwent->pw_dir = p;
                    303:        p = pwskip(p);
                    304:        pwent->pw_shell = p;
                    305:        while (*p && *p != '\n')
                    306:                p++;
                    307:        *p = '\0';
                    308:        return (pwent);
                    309: }
                    310:
1.4       deraadt   311: static char *__yplin;
                    312:
1.15      millert   313: struct passwd *
1.25      maja      314: ypgetpwnam(char *nam, int secure)
1.1       deraadt   315: {
                    316:        static struct passwd pwent;
1.23      deraadt   317:        int reason, vallen;
1.1       deraadt   318:        char *val;
1.21      fgsch     319:
1.25      maja      320:        reason = yp_match(domain,
1.29      deraadt   321:            secure ? "master.passwd.byname" : "passwd.byname",
                    322:            nam, strlen(nam), &val, &vallen);
1.23      deraadt   323:        switch (reason) {
1.1       deraadt   324:        case 0:
                    325:                break;
                    326:        default:
                    327:                return (NULL);
                    328:        }
                    329:        val[vallen] = '\0';
1.4       deraadt   330:        if (__yplin)
                    331:                free(__yplin);
1.36      deraadt   332:        __yplin = malloc(vallen + 1);
1.16      deraadt   333:        if (__yplin == NULL)
                    334:                pw_error(NULL, 1, 1);
1.23      deraadt   335:        strlcpy(__yplin, val, vallen + 1);
1.1       deraadt   336:        free(val);
                    337:
1.25      maja      338:        return (interpret(&pwent, __yplin, secure));
1.1       deraadt   339: }
                    340:
                    341: #endif /* YP */