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

Annotation of src/usr.bin/chpass/pw_yp.c, Revision 1.17

1.17    ! pvalchev    1: /*     $OpenBSD: pw_yp.c,v 1.16 2002/06/27 19:02:40 deraadt Exp $      */
1.1       deraadt     2: /*     $NetBSD: pw_yp.c,v 1.5 1995/03/26 04:55:33 glass Exp $  */
                      3:
                      4: /*
                      5:  * Copyright (c) 1988 The Regents of the University of California.
                      6:  * All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36: #ifndef lint
                     37: #if 0
                     38: static char sccsid[] = "@(#)pw_yp.c    1.0 2/2/93";
                     39: #else
1.17    ! pvalchev   40: static char rcsid[] = "$OpenBSD: pw_yp.c,v 1.16 2002/06/27 19:02:40 deraadt Exp $";
1.1       deraadt    41: #endif
                     42: #endif /* not lint */
                     43:
                     44: #ifdef YP
                     45:
                     46: #include <stdio.h>
                     47: #include <string.h>
                     48: #include <netdb.h>
                     49: #include <time.h>
                     50: #include <pwd.h>
1.8       deraadt    51: #include <err.h>
1.1       deraadt    52: #include <errno.h>
1.16      deraadt    53: #include <unistd.h>
1.7       niklas     54: #include <stdlib.h>
1.1       deraadt    55: #include <rpc/rpc.h>
                     56: #include <rpcsvc/yp_prot.h>
                     57: #include <rpcsvc/ypclnt.h>
                     58: #define passwd yp_passwd_rec
                     59: #include <rpcsvc/yppasswd.h>
                     60: #undef passwd
                     61:
1.9       millert    62: extern char *__progname;
1.1       deraadt    63:
                     64: static char *domain;
                     65:
1.8       deraadt    66: int
1.16      deraadt    67: pw_yp(struct passwd *pw, uid_t uid)
1.1       deraadt    68: {
1.17    ! pvalchev   69:        char buf[11], *master, *p;
1.5       deraadt    70:        int r, rpcport, status, alen;
1.1       deraadt    71:        struct yppasswd yppasswd;
                     72:        struct timeval tv;
                     73:        CLIENT *client;
1.16      deraadt    74:
1.1       deraadt    75:        /*
                     76:         * Get local domain
                     77:         */
                     78:        if (!domain && (r = yp_get_default_domain(&domain))) {
                     79:                fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
1.9       millert    80:                    __progname, yperr_string(r));
1.1       deraadt    81:                return(0);
                     82:        }
                     83:
                     84:        /*
                     85:         * Find the host for the passwd map; it should be running
                     86:         * the daemon.
                     87:         */
                     88:        if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
                     89:                fprintf(stderr,
                     90:                    "%s: can't find the master YP server. Reason: %s\n",
1.9       millert    91:                    __progname, yperr_string(r));
1.1       deraadt    92:                return(0);
                     93:        }
                     94:
                     95:        /*
                     96:         * Ask the portmapper for the port of the daemon.
                     97:         */
                     98:        if ((rpcport = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE,
                     99:            IPPROTO_UDP)) == 0) {
                    100:                fprintf(stderr,
                    101:                    "%s: master YP server not running yppasswd daemon.\n",
1.9       millert   102:                    __progname);
1.1       deraadt   103:                fprintf(stderr, "\tCan't change password.\n");
                    104:                return(0);
                    105:        }
                    106:
                    107:        /*
1.15      deraadt   108:         * Be sure the port is privileged
1.1       deraadt   109:         */
                    110:        if (rpcport >= IPPORT_RESERVED) {
                    111:                (void)fprintf(stderr,
                    112:                    "%s: yppasswd daemon running on an invalid port.\n",
1.9       millert   113:                    __progname);
1.1       deraadt   114:                return(0);
                    115:        }
                    116:
                    117:        /* prompt for old password */
                    118:        bzero(&yppasswd, sizeof yppasswd);
                    119:        yppasswd.oldpass = "none";
                    120:        yppasswd.oldpass = getpass("Old password:");
                    121:        if (!yppasswd.oldpass) {
                    122:                (void)fprintf(stderr, "Cancelled.\n");
                    123:                return(0);
                    124:        }
1.16      deraadt   125:
1.5       deraadt   126:        for (alen = 0, p = pw->pw_gecos; *p; p++)
                    127:                if (*p == '&')
                    128:                        alen = alen + strlen(pw->pw_name) - 1;
                    129:        if (strlen(pw->pw_name) + 1 + strlen(pw->pw_passwd) + 1 +
1.14      deraadt   130:            strlen((snprintf(buf, sizeof buf, "%u", pw->pw_uid), buf)) + 1 +
                    131:            strlen((snprintf(buf, sizeof buf, "%u", pw->pw_gid), buf)) + 1 +
1.5       deraadt   132:            strlen(pw->pw_gecos) + alen + 1 + strlen(pw->pw_dir) + 1 +
                    133:            strlen(pw->pw_shell) >= 1023) {
                    134:                warnx("entries too long");
                    135:                return (0);
                    136:        }
                    137:
1.1       deraadt   138:        /* tell rpc.yppasswdd */
                    139:        yppasswd.newpw.pw_name  = pw->pw_name;
                    140:        yppasswd.newpw.pw_passwd= pw->pw_passwd;
1.16      deraadt   141:        yppasswd.newpw.pw_uid   = pw->pw_uid;
1.1       deraadt   142:        yppasswd.newpw.pw_gid   = pw->pw_gid;
                    143:        yppasswd.newpw.pw_gecos = pw->pw_gecos;
                    144:        yppasswd.newpw.pw_dir   = pw->pw_dir;
                    145:        yppasswd.newpw.pw_shell = pw->pw_shell;
1.5       deraadt   146:
1.1       deraadt   147:        client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
                    148:        if (client==NULL) {
                    149:                fprintf(stderr, "can't contact yppasswdd on %s: Reason: %s\n",
                    150:                    master, yperr_string(YPERR_YPBIND));
1.6       deraadt   151:                return(1);
1.1       deraadt   152:        }
                    153:        client->cl_auth = authunix_create_default();
                    154:        tv.tv_sec = 5;
                    155:        tv.tv_usec = 0;
                    156:        r = clnt_call(client, YPPASSWDPROC_UPDATE,
                    157:            xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
                    158:        if (r) {
1.9       millert   159:                fprintf(stderr, "%s: rpc to yppasswdd failed. %d\n",
                    160:                    __progname, r);
1.4       deraadt   161:                clnt_destroy(client);
1.6       deraadt   162:                return(1);
1.1       deraadt   163:        } else if (status) {
                    164:                printf("Couldn't change YP password information.\n");
1.4       deraadt   165:                clnt_destroy(client);
1.6       deraadt   166:                return(1);
1.1       deraadt   167:        }
                    168:        printf("The YP password information has been changed on %s, the master YP passwd server.\n", master);
                    169:
1.4       deraadt   170:        clnt_destroy(client);
1.6       deraadt   171:        return(0);
1.1       deraadt   172: }
                    173:
                    174: static char *
1.16      deraadt   175: pwskip(char *p)
1.1       deraadt   176: {
                    177:        while (*p && *p != ':' && *p != '\n')
                    178:                ++p;
                    179:        if (*p)
                    180:                *p++ = 0;
                    181:        return (p);
                    182: }
                    183:
                    184: static struct passwd *
1.16      deraadt   185: interpret(struct passwd *pwent, char *line)
1.1       deraadt   186: {
1.13      mpech     187:        char    *p = line;
1.1       deraadt   188:
                    189:        pwent->pw_passwd = "*";
                    190:        pwent->pw_uid = 0;
                    191:        pwent->pw_gid = 0;
                    192:        pwent->pw_gecos = "";
                    193:        pwent->pw_dir = "";
                    194:        pwent->pw_shell = "";
                    195:        pwent->pw_change = 0;
                    196:        pwent->pw_expire = 0;
                    197:        pwent->pw_class = "";
1.16      deraadt   198:
1.1       deraadt   199:        /* line without colon separators is no good, so ignore it */
                    200:        if(!strchr(p,':'))
                    201:                return(NULL);
                    202:
                    203:        pwent->pw_name = p;
                    204:        p = pwskip(p);
                    205:        pwent->pw_passwd = p;
                    206:        p = pwskip(p);
                    207:        pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
                    208:        p = pwskip(p);
                    209:        pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
                    210:        p = pwskip(p);
                    211:        pwent->pw_gecos = p;
                    212:        p = pwskip(p);
                    213:        pwent->pw_dir = p;
                    214:        p = pwskip(p);
                    215:        pwent->pw_shell = p;
                    216:        while (*p && *p != '\n')
                    217:                p++;
                    218:        *p = '\0';
                    219:        return (pwent);
                    220: }
                    221:
1.3       deraadt   222: static char *__yplin;
                    223:
1.1       deraadt   224: struct passwd *
1.16      deraadt   225: ypgetpwnam(char *nam)
1.1       deraadt   226: {
                    227:        static struct passwd pwent;
1.16      deraadt   228:        int reason, vallen;
1.1       deraadt   229:        char *val;
1.16      deraadt   230:
1.1       deraadt   231:        /*
                    232:         * Get local domain
                    233:         */
                    234:        if (!domain && (reason = yp_get_default_domain(&domain))) {
                    235:                fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
1.9       millert   236:                    __progname, yperr_string(reason));
1.1       deraadt   237:                exit(1);
                    238:        }
                    239:
                    240:        reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
                    241:            &val, &vallen);
                    242:        switch(reason) {
                    243:        case 0:
                    244:                break;
                    245:        default:
                    246:                return (NULL);
                    247:                break;
                    248:        }
                    249:        val[vallen] = '\0';
1.3       deraadt   250:        if (__yplin)
                    251:                free(__yplin);
1.11      alex      252:        if (!(__yplin = (char *)malloc(vallen + 1)))
                    253:                err(1, NULL);
1.16      deraadt   254:        strlcpy(__yplin, val, vallen + 1);
1.1       deraadt   255:        free(val);
                    256:
1.3       deraadt   257:        return(interpret(&pwent, __yplin));
1.1       deraadt   258: }
                    259:
                    260: struct passwd *
1.16      deraadt   261: ypgetpwuid(uid_t uid)
1.1       deraadt   262: {
                    263:        static struct passwd pwent;
                    264:        int reason, vallen;
1.16      deraadt   265:        char namebuf[16], *val;
                    266:
1.1       deraadt   267:        if (!domain && (reason = yp_get_default_domain(&domain))) {
                    268:                fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
1.9       millert   269:                    __progname, yperr_string(reason));
1.1       deraadt   270:                exit(1);
                    271:        }
                    272:
1.16      deraadt   273:        snprintf(namebuf, sizeof namebuf, "%u", (u_int)uid);
1.1       deraadt   274:        reason = yp_match(domain, "passwd.byuid", namebuf, strlen(namebuf),
                    275:            &val, &vallen);
                    276:        switch(reason) {
                    277:        case 0:
                    278:                break;
                    279:        default:
                    280:                return (NULL);
                    281:                break;
                    282:        }
                    283:        val[vallen] = '\0';
1.3       deraadt   284:        if (__yplin)
                    285:                free(__yplin);
1.11      alex      286:        if (!(__yplin = (char *)malloc(vallen + 1)))
                    287:                err(1, NULL);
1.16      deraadt   288:        strlcpy(__yplin, val, vallen + 1);
1.1       deraadt   289:        free(val);
                    290:
1.3       deraadt   291:        return(interpret(&pwent, __yplin));
1.1       deraadt   292: }
                    293:
                    294: #endif /* YP */