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

1.16    ! deraadt     1: /*     $OpenBSD: yp_passwd.c,v 1.15 2000/12/12 02:19:59 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
                     36: /*static char sccsid[] = "from: @(#)yp_passwd.c        1.0 2/2/93";*/
1.16    ! deraadt    37: static char rcsid[] = "$OpenBSD: yp_passwd.c,v 1.15 2000/12/12 02:19:59 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.1       deraadt    52: #include <rpc/rpc.h>
                     53: #include <rpcsvc/yp_prot.h>
                     54: #include <rpcsvc/ypclnt.h>
                     55: #define passwd yp_passwd_rec
                     56: #include <rpcsvc/yppasswd.h>
                     57: #undef passwd
                     58:
                     59: #ifndef _PASSWORD_LEN
                     60: #define _PASSWORD_LEN PASS_MAX
                     61: #endif
                     62:
1.14      provos     63: extern int pwd_gensalt __P(( char *, int, struct passwd *, char));
                     64: extern int pwd_check __P((struct passwd *, char *));
                     65: extern int pwd_gettries __P((struct passwd *));
                     66:
1.15      millert    67: char *ypgetnewpasswd __P((struct passwd *, char **));
                     68: struct passwd *ypgetpwnam __P((char *));
1.1       deraadt    69:
                     70: char *domain;
                     71:
1.8       weingart   72: static int
1.1       deraadt    73: pw_error(name, err, eval)
                     74:        char *name;
                     75:        int err, eval;
                     76: {
1.15      millert    77:        if (err)
1.13      millert    78:                warn("%s", name);
1.1       deraadt    79:
1.8       weingart   80:        warnx("YP passwd database unchanged.");
1.1       deraadt    81:        exit(eval);
                     82: }
                     83:
1.8       weingart   84: int
1.1       deraadt    85: yp_passwd(username)
                     86:        char *username;
                     87: {
                     88:        char *master;
                     89:        int r, rpcport, status;
1.15      millert    90:        uid_t uid;
1.1       deraadt    91:        struct yppasswd yppasswd;
                     92:        struct passwd *pw;
                     93:        struct timeval tv;
                     94:        CLIENT *client;
                     95:
                     96:        /*
                     97:         * Get local domain
                     98:         */
1.8       weingart   99:        if ((r = yp_get_default_domain(&domain)) != 0) {
                    100:                warnx("can't get local YP domain. Reason: %s", yperr_string(r));
1.15      millert   101:                return(1);
1.1       deraadt   102:        }
                    103:
                    104:        /*
                    105:         * Find the host for the passwd map; it should be running
                    106:         * the daemon.
                    107:         */
                    108:        if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
1.9       millert   109:                warnx("can't find the master YP server. Reason: %s",
                    110:                    yperr_string(r));
1.15      millert   111:                return(1);
1.1       deraadt   112:        }
                    113:
                    114:        /*
                    115:         * Ask the portmapper for the port of the daemon.
                    116:         */
                    117:        if ((rpcport = getrpcport(master, YPPASSWDPROG,
                    118:            YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
1.8       weingart  119:                warnx("master YP server not running yppasswd daemon.");
                    120:                warnx("Can't change password.");
1.15      millert   121:                return(1);
1.1       deraadt   122:        }
                    123:
                    124:        /*
                    125:         * Be sure the port is priviledged
                    126:         */
                    127:        if (rpcport >= IPPORT_RESERVED) {
1.8       weingart  128:                warnx("yppasswd daemon is on an invalid port.");
1.15      millert   129:                return(1);
1.1       deraadt   130:        }
                    131:
                    132:        /* Get user's login identity */
                    133:        if (!(pw = ypgetpwnam(username))) {
1.8       weingart  134:                warnx("unknown user %s.", username);
1.15      millert   135:                return(1);
1.1       deraadt   136:        }
                    137:
1.15      millert   138:        uid = getuid();
1.1       deraadt   139:        if (uid && uid != pw->pw_uid) {
1.8       weingart  140:                warnx("you may only change your own password: %s", strerror(EACCES));
1.15      millert   141:                return(1);
1.1       deraadt   142:        }
                    143:
                    144:        /* prompt for new password */
1.15      millert   145:        yppasswd.newpw.pw_passwd = ypgetnewpasswd(pw, &yppasswd.oldpass);
1.1       deraadt   146:
                    147:        /* tell rpc.yppasswdd */
                    148:        yppasswd.newpw.pw_name  = pw->pw_name;
                    149:        yppasswd.newpw.pw_uid   = pw->pw_uid;
                    150:        yppasswd.newpw.pw_gid   = pw->pw_gid;
                    151:        yppasswd.newpw.pw_gecos = pw->pw_gecos;
                    152:        yppasswd.newpw.pw_dir   = pw->pw_dir;
                    153:        yppasswd.newpw.pw_shell = pw->pw_shell;
                    154:
                    155:        client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
                    156:        if (client==NULL) {
1.9       millert   157:                warnx("cannot contact yppasswdd on %s: Reason: %s",
1.1       deraadt   158:                    master, yperr_string(YPERR_YPBIND));
1.7       provos    159:                free(yppasswd.newpw.pw_passwd);
1.1       deraadt   160:                return(YPERR_YPBIND);
                    161:        }
                    162:        client->cl_auth = authunix_create_default();
                    163:        tv.tv_sec = 2;
                    164:        tv.tv_usec = 0;
                    165:        r = clnt_call(client, YPPASSWDPROC_UPDATE,
1.3       deraadt   166:            xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
1.1       deraadt   167:        if (r)
1.8       weingart  168:                warnx("rpc to yppasswdd failed.");
1.3       deraadt   169:        else if (status) {
1.1       deraadt   170:                printf("Couldn't change YP password.\n");
1.7       provos    171:                free(yppasswd.newpw.pw_passwd);
1.15      millert   172:                return(1);
1.3       deraadt   173:        }
                    174:        printf("The YP password has been changed on %s, the master YP passwd server.\n",
                    175:            master);
1.7       provos    176:        free(yppasswd.newpw.pw_passwd);
1.15      millert   177:        return(0);
1.1       deraadt   178: }
                    179:
1.15      millert   180: char *
                    181: ypgetnewpasswd(pw, old_pass)
                    182:        struct passwd *pw;
1.1       deraadt   183:        char **old_pass;
                    184: {
                    185:        static char buf[_PASSWORD_LEN+1];
1.15      millert   186:        char *p;
1.14      provos    187:        int tries, pwd_tries;
1.15      millert   188:        char salt[_PASSWORD_LEN];
1.1       deraadt   189:
1.3       deraadt   190:        printf("Changing YP password for %s.\n", pw->pw_name);
1.1       deraadt   191:        if (old_pass) {
                    192:                *old_pass = NULL;
                    193:
                    194:                if (pw->pw_passwd[0]) {
1.3       deraadt   195:                        p = getpass("Old password:");
                    196:                        if (strcmp(crypt(p, pw->pw_passwd), pw->pw_passwd)) {
                    197:                                errno = EACCES;
                    198:                                pw_error(NULL, 1, 1);
1.1       deraadt   199:                        }
1.3       deraadt   200:                } else
1.1       deraadt   201:                        p = "";
                    202:                *old_pass = strdup(p);
1.16    ! deraadt   203:                if (*old_pass == NULL)
        !           204:                        pw_error(NULL, 1, 1);
1.1       deraadt   205:        }
1.3       deraadt   206:
1.14      provos    207:        pwd_tries = pwd_gettries(pw);
                    208:
1.1       deraadt   209:        for (buf[0] = '\0', tries = 0;;) {
                    210:                p = getpass("New password:");
                    211:                if (!*p) {
1.3       deraadt   212:                        printf("Password unchanged.\n");
1.1       deraadt   213:                        pw_error(NULL, 0, 0);
1.10      deraadt   214:                }
                    215:                if (strcmp(p, "s/key") == 0) {
                    216:                        printf("That password collides with a system feature. Choose another.\n");
                    217:                        continue;
1.1       deraadt   218:                }
1.14      provos    219:                if ((tries++ < pwd_tries || pwd_tries == 0)
                    220:                    && pwd_check(pw, p) == 0)
1.1       deraadt   221:                        continue;
1.12      deraadt   222:                strncpy(buf, p, sizeof buf-1);
                    223:                buf[sizeof buf-1] = '\0';
1.1       deraadt   224:                if (!strcmp(buf, getpass("Retype new password:")))
                    225:                        break;
                    226:                (void)printf("Mismatch; try again, EOF to quit.\n");
                    227:        }
1.6       provos    228:         if( !pwd_gensalt( salt, _PASSWORD_LEN, pw, 'y' )) {
                    229:                 (void)printf("Couldn't generate salt.\n");
                    230:                 pw_error(NULL, 0, 0);
                    231:         }
1.16    ! deraadt   232:        p = strdup(crypt(buf, salt));
        !           233:        if (p == NULL)
        !           234:                pw_error(NULL, 1, 1);
        !           235:        return (p);
1.1       deraadt   236: }
                    237:
                    238: static char *
1.15      millert   239: pwskip(char *p)
1.1       deraadt   240: {
                    241:        while (*p && *p != ':' && *p != '\n')
                    242:                ++p;
                    243:        if (*p)
                    244:                *p++ = 0;
                    245:        return (p);
                    246: }
                    247:
                    248: struct passwd *
                    249: interpret(struct passwd *pwent, char *line)
                    250: {
1.15      millert   251:        char    *p = line;
1.1       deraadt   252:
                    253:        pwent->pw_passwd = "*";
                    254:        pwent->pw_uid = 0;
                    255:        pwent->pw_gid = 0;
                    256:        pwent->pw_gecos = "";
                    257:        pwent->pw_dir = "";
                    258:        pwent->pw_shell = "";
                    259:        pwent->pw_change = 0;
                    260:        pwent->pw_expire = 0;
                    261:        pwent->pw_class = "";
                    262:
                    263:        /* line without colon separators is no good, so ignore it */
1.3       deraadt   264:        if(!strchr(p, ':'))
1.1       deraadt   265:                return(NULL);
                    266:
                    267:        pwent->pw_name = p;
                    268:        p = pwskip(p);
                    269:        pwent->pw_passwd = p;
                    270:        p = pwskip(p);
                    271:        pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
                    272:        p = pwskip(p);
                    273:        pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
                    274:        p = pwskip(p);
                    275:        pwent->pw_gecos = p;
                    276:        p = pwskip(p);
                    277:        pwent->pw_dir = p;
                    278:        p = pwskip(p);
                    279:        pwent->pw_shell = p;
                    280:        while (*p && *p != '\n')
                    281:                p++;
                    282:        *p = '\0';
                    283:        return (pwent);
                    284: }
                    285:
1.4       deraadt   286: static char *__yplin;
                    287:
1.15      millert   288: struct passwd *
1.1       deraadt   289: ypgetpwnam(nam)
                    290:        char *nam;
                    291: {
                    292:        static struct passwd pwent;
                    293:        char *val;
                    294:        int reason, vallen;
                    295:
                    296:        reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
1.3       deraadt   297:            &val, &vallen);
1.1       deraadt   298:        switch(reason) {
                    299:        case 0:
                    300:                break;
                    301:        default:
                    302:                return (NULL);
                    303:                break;
                    304:        }
                    305:        val[vallen] = '\0';
1.4       deraadt   306:        if (__yplin)
                    307:                free(__yplin);
                    308:        __yplin = (char *)malloc(vallen + 1);
1.16    ! deraadt   309:        if (__yplin == NULL)
        !           310:                pw_error(NULL, 1, 1);
1.12      deraadt   311:        strncpy(__yplin, val, vallen);
                    312:        __yplin[vallen] = '\0';
1.1       deraadt   313:        free(val);
                    314:
1.4       deraadt   315:        return(interpret(&pwent, __yplin));
1.1       deraadt   316: }
                    317:
                    318: #endif /* YP */