[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.21

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