Annotation of src/usr.bin/chpass/pw_yp.c, Revision 1.4
1.4 ! deraadt 1: /* $OpenBSD: pw_yp.c,v 1.3 1996/08/30 13:09:41 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.4 ! deraadt 40: static char rcsid[] = "$OpenBSD: pw_yp.c,v 1.3 1996/08/30 13:09:41 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>
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: extern char *progname;
60:
61: static char *domain;
62:
63: pw_yp(pw, uid)
64: struct passwd *pw;
65: uid_t uid;
66: {
67: char *master;
68: char *pp;
69: int r, rpcport, status;
70: struct yppasswd yppasswd;
71: struct timeval tv;
72: CLIENT *client;
73: extern char *getpass();
74:
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",
80: progname, yperr_string(r));
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",
91: progname, yperr_string(r));
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",
102: progname);
103: fprintf(stderr, "\tCan't change password.\n");
104: return(0);
105: }
106:
107: /*
108: * Be sure the port is priviledged
109: */
110: if (rpcport >= IPPORT_RESERVED) {
111: (void)fprintf(stderr,
112: "%s: yppasswd daemon running on an invalid port.\n",
113: progname);
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: }
125:
126: /* tell rpc.yppasswdd */
127: yppasswd.newpw.pw_name = pw->pw_name;
128: yppasswd.newpw.pw_passwd= pw->pw_passwd;
129: yppasswd.newpw.pw_uid = pw->pw_uid;
130: yppasswd.newpw.pw_gid = pw->pw_gid;
131: yppasswd.newpw.pw_gecos = pw->pw_gecos;
132: yppasswd.newpw.pw_dir = pw->pw_dir;
133: yppasswd.newpw.pw_shell = pw->pw_shell;
134:
135: client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
136: if (client==NULL) {
137: fprintf(stderr, "can't contact yppasswdd on %s: Reason: %s\n",
138: master, yperr_string(YPERR_YPBIND));
139: return(0);
140: }
141: client->cl_auth = authunix_create_default();
142: tv.tv_sec = 5;
143: tv.tv_usec = 0;
144: r = clnt_call(client, YPPASSWDPROC_UPDATE,
145: xdr_yppasswd, &yppasswd, xdr_int, &status, tv);
146: if (r) {
147: fprintf(stderr, "%s: rpc to yppasswdd failed. %d\n", progname, r);
1.4 ! deraadt 148: clnt_destroy(client);
1.1 deraadt 149: return(0);
150: } else if (status) {
151: printf("Couldn't change YP password information.\n");
1.4 ! deraadt 152: clnt_destroy(client);
1.1 deraadt 153: return(0);
154: }
155: printf("The YP password information has been changed on %s, the master YP passwd server.\n", master);
156:
1.4 ! deraadt 157: clnt_destroy(client);
1.1 deraadt 158: return(1);
159: }
160:
161: static char *
162: pwskip(p)
163: register char *p;
164: {
165: while (*p && *p != ':' && *p != '\n')
166: ++p;
167: if (*p)
168: *p++ = 0;
169: return (p);
170: }
171:
172: static struct passwd *
173: interpret(pwent, line)
174: struct passwd *pwent;
175: char *line;
176: {
177: register char *p = line;
178: register int c;
179:
180: pwent->pw_passwd = "*";
181: pwent->pw_uid = 0;
182: pwent->pw_gid = 0;
183: pwent->pw_gecos = "";
184: pwent->pw_dir = "";
185: pwent->pw_shell = "";
186: pwent->pw_change = 0;
187: pwent->pw_expire = 0;
188: pwent->pw_class = "";
189:
190: /* line without colon separators is no good, so ignore it */
191: if(!strchr(p,':'))
192: return(NULL);
193:
194: pwent->pw_name = p;
195: p = pwskip(p);
196: pwent->pw_passwd = p;
197: p = pwskip(p);
198: pwent->pw_uid = (uid_t)strtoul(p, NULL, 10);
199: p = pwskip(p);
200: pwent->pw_gid = (gid_t)strtoul(p, NULL, 10);
201: p = pwskip(p);
202: pwent->pw_gecos = p;
203: p = pwskip(p);
204: pwent->pw_dir = p;
205: p = pwskip(p);
206: pwent->pw_shell = p;
207: while (*p && *p != '\n')
208: p++;
209: *p = '\0';
210: return (pwent);
211: }
212:
1.3 deraadt 213: static char *__yplin;
214:
1.1 deraadt 215: struct passwd *
216: ypgetpwnam(nam)
217: char *nam;
218: {
219: static struct passwd pwent;
220: char *val;
221: int reason, vallen;
222:
223: /*
224: * Get local domain
225: */
226: if (!domain && (reason = yp_get_default_domain(&domain))) {
227: fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
228: progname, yperr_string(reason));
229: exit(1);
230: }
231:
232: reason = yp_match(domain, "passwd.byname", nam, strlen(nam),
233: &val, &vallen);
234: switch(reason) {
235: case 0:
236: break;
237: default:
238: return (NULL);
239: break;
240: }
241: val[vallen] = '\0';
1.3 deraadt 242: if (__yplin)
243: free(__yplin);
244: __yplin = (char *)malloc(vallen + 1);
245: strcpy(__yplin, val);
1.1 deraadt 246: free(val);
247:
1.3 deraadt 248: return(interpret(&pwent, __yplin));
1.1 deraadt 249: }
250:
251: struct passwd *
252: ypgetpwuid(uid)
253: uid_t uid;
254: {
255: static struct passwd pwent;
256: char *val;
257: int reason, vallen;
258: char namebuf[16];
259:
260: if (!domain && (reason = yp_get_default_domain(&domain))) {
261: fprintf(stderr, "%s: can't get local YP domain. Reason: %s\n",
262: progname, yperr_string(reason));
263: exit(1);
264: }
265:
266: sprintf(namebuf, "%d", uid);
267: reason = yp_match(domain, "passwd.byuid", namebuf, strlen(namebuf),
268: &val, &vallen);
269: switch(reason) {
270: case 0:
271: break;
272: default:
273: return (NULL);
274: break;
275: }
276: val[vallen] = '\0';
1.3 deraadt 277: if (__yplin)
278: free(__yplin);
279: __yplin = (char *)malloc(vallen + 1);
280: strcpy(__yplin, val);
1.1 deraadt 281: free(val);
282:
1.3 deraadt 283: return(interpret(&pwent, __yplin));
1.1 deraadt 284: }
285:
286: #endif /* YP */