Annotation of src/usr.bin/chpass/pw_yp.c, Revision 1.7
1.7 ! niklas 1: /* $OpenBSD: pw_yp.c,v 1.6 1997/02/13 17:28: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.7 ! niklas 40: static char rcsid[] = "$OpenBSD: pw_yp.c,v 1.6 1997/02/13 17:28: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>
51: #include <errno.h>
1.7 ! niklas 52: #include <stdlib.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: extern char *progname;
61:
62: static char *domain;
63:
64: pw_yp(pw, uid)
65: struct passwd *pw;
66: uid_t uid;
67: {
68: char *master;
1.5 deraadt 69: char *pp, *p;
70: char buf[10];
71: int r, rpcport, status, alen;
1.1 deraadt 72: struct yppasswd yppasswd;
73: struct timeval tv;
74: CLIENT *client;
75: extern char *getpass();
76:
77: /*
78: * Get local domain
79: */
80: if (!domain && (r = yp_get_default_domain(&domain))) {
81: fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
82: progname, yperr_string(r));
83: return(0);
84: }
85:
86: /*
87: * Find the host for the passwd map; it should be running
88: * the daemon.
89: */
90: if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
91: fprintf(stderr,
92: "%s: can't find the master YP server. Reason: %s\n",
93: progname, yperr_string(r));
94: return(0);
95: }
96:
97: /*
98: * Ask the portmapper for the port of the daemon.
99: */
100: if ((rpcport = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE,
101: IPPROTO_UDP)) == 0) {
102: fprintf(stderr,
103: "%s: master YP server not running yppasswd daemon.\n",
104: progname);
105: fprintf(stderr, "\tCan't change password.\n");
106: return(0);
107: }
108:
109: /*
110: * Be sure the port is priviledged
111: */
112: if (rpcport >= IPPORT_RESERVED) {
113: (void)fprintf(stderr,
114: "%s: yppasswd daemon running on an invalid port.\n",
115: progname);
116: return(0);
117: }
118:
119: /* prompt for old password */
120: bzero(&yppasswd, sizeof yppasswd);
121: yppasswd.oldpass = "none";
122: yppasswd.oldpass = getpass("Old password:");
123: if (!yppasswd.oldpass) {
124: (void)fprintf(stderr, "Cancelled.\n");
125: return(0);
126: }
127:
1.5 deraadt 128: for (alen = 0, p = pw->pw_gecos; *p; p++)
129: if (*p == '&')
130: alen = alen + strlen(pw->pw_name) - 1;
131: if (strlen(pw->pw_name) + 1 + strlen(pw->pw_passwd) + 1 +
132: strlen((sprintf(buf, "%d", pw->pw_uid), buf)) + 1 +
133: strlen((sprintf(buf, "%d", pw->pw_gid), buf)) + 1 +
134: strlen(pw->pw_gecos) + alen + 1 + strlen(pw->pw_dir) + 1 +
135: strlen(pw->pw_shell) >= 1023) {
136: warnx("entries too long");
137: return (0);
138: }
139:
1.1 deraadt 140: /* tell rpc.yppasswdd */
141: yppasswd.newpw.pw_name = pw->pw_name;
142: yppasswd.newpw.pw_passwd= pw->pw_passwd;
143: yppasswd.newpw.pw_uid = pw->pw_uid;
144: yppasswd.newpw.pw_gid = pw->pw_gid;
145: yppasswd.newpw.pw_gecos = pw->pw_gecos;
146: yppasswd.newpw.pw_dir = pw->pw_dir;
147: yppasswd.newpw.pw_shell = pw->pw_shell;
1.5 deraadt 148:
1.1 deraadt 149: client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
150: if (client==NULL) {
151: fprintf(stderr, "can't contact yppasswdd on %s: Reason: %s\n",
152: master, yperr_string(YPERR_YPBIND));
1.6 deraadt 153: return(1);
1.1 deraadt 154: }
155: client->cl_auth = authunix_create_default();
156: tv.tv_sec = 5;
157: tv.tv_usec = 0;
158: r = clnt_call(client, YPPASSWDPROC_UPDATE,
159: xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
160: if (r) {
161: fprintf(stderr, "%s: rpc to yppasswdd failed. %d\n", progname, r);
1.4 deraadt 162: clnt_destroy(client);
1.6 deraadt 163: return(1);
1.1 deraadt 164: } else if (status) {
165: printf("Couldn't change YP password information.\n");
1.4 deraadt 166: clnt_destroy(client);
1.6 deraadt 167: return(1);
1.1 deraadt 168: }
169: printf("The YP password information has been changed on %s, the master YP passwd server.\n", master);
170:
1.4 deraadt 171: clnt_destroy(client);
1.6 deraadt 172: return(0);
1.1 deraadt 173: }
174:
175: static char *
176: pwskip(p)
177: register char *p;
178: {
179: while (*p && *p != ':' && *p != '\n')
180: ++p;
181: if (*p)
182: *p++ = 0;
183: return (p);
184: }
185:
186: static struct passwd *
187: interpret(pwent, line)
188: struct passwd *pwent;
189: char *line;
190: {
191: register char *p = line;
192: register int c;
193:
194: pwent->pw_passwd = "*";
195: pwent->pw_uid = 0;
196: pwent->pw_gid = 0;
197: pwent->pw_gecos = "";
198: pwent->pw_dir = "";
199: pwent->pw_shell = "";
200: pwent->pw_change = 0;
201: pwent->pw_expire = 0;
202: pwent->pw_class = "";
203:
204: /* line without colon separators is no good, so ignore it */
205: if(!strchr(p,':'))
206: return(NULL);
207:
208: pwent->pw_name = p;
209: p = pwskip(p);
210: pwent->pw_passwd = p;
211: p = pwskip(p);
212: pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
213: p = pwskip(p);
214: pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
215: p = pwskip(p);
216: pwent->pw_gecos = p;
217: p = pwskip(p);
218: pwent->pw_dir = p;
219: p = pwskip(p);
220: pwent->pw_shell = p;
221: while (*p && *p != '\n')
222: p++;
223: *p = '\0';
224: return (pwent);
225: }
226:
1.3 deraadt 227: static char *__yplin;
228:
1.1 deraadt 229: struct passwd *
230: ypgetpwnam(nam)
231: char *nam;
232: {
233: static struct passwd pwent;
234: char *val;
235: int reason, vallen;
236:
237: /*
238: * Get local domain
239: */
240: if (!domain && (reason = yp_get_default_domain(&domain))) {
241: fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
242: progname, yperr_string(reason));
243: exit(1);
244: }
245:
246: reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
247: &val, &vallen);
248: switch(reason) {
249: case 0:
250: break;
251: default:
252: return (NULL);
253: break;
254: }
255: val[vallen] = '\0';
1.3 deraadt 256: if (__yplin)
257: free(__yplin);
258: __yplin = (char *)malloc(vallen + 1);
259: strcpy(__yplin, val);
1.1 deraadt 260: free(val);
261:
1.3 deraadt 262: return(interpret(&pwent, __yplin));
1.1 deraadt 263: }
264:
265: struct passwd *
266: ypgetpwuid(uid)
267: uid_t uid;
268: {
269: static struct passwd pwent;
270: char *val;
271: int reason, vallen;
272: char namebuf[16];
273:
274: if (!domain && (reason = yp_get_default_domain(&domain))) {
275: fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
276: progname, yperr_string(reason));
277: exit(1);
278: }
279:
280: sprintf(namebuf, "%d", uid);
281: reason = yp_match(domain, "passwd.byuid", namebuf, strlen(namebuf),
282: &val, &vallen);
283: switch(reason) {
284: case 0:
285: break;
286: default:
287: return (NULL);
288: break;
289: }
290: val[vallen] = '\0';
1.3 deraadt 291: if (__yplin)
292: free(__yplin);
293: __yplin = (char *)malloc(vallen + 1);
294: strcpy(__yplin, val);
1.1 deraadt 295: free(val);
296:
1.3 deraadt 297: return(interpret(&pwent, __yplin));
1.1 deraadt 298: }
299:
300: #endif /* YP */