3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * NIS interface routines for chpass
34 * Written by Bill Paul <wpaul@ctr.columbia.edu>
35 * Center for Telecommunications Research
36 * Columbia University, New York City
38 * $FreeBSD: src/usr.bin/chpass/pw_yp.c,v 1.16.2.1 2002/02/15 00:46:56 des Exp $
47 #include <sys/types.h>
56 #include <sys/types.h>
58 #include <sys/param.h>
61 #include <rpcsvc/yp.h>
62 struct dom_binding {};
63 #include <rpcsvc/ypclnt.h>
64 #include <rpcsvc/yppasswd.h>
67 #include "ypxfr_extern.h"
68 #include "yppasswd_private.h"
70 #define PERM_SECURE (S_IRUSR|S_IWUSR)
71 static HASHINFO openinfo = {
75 2048 * 1024, /* cachesize */
82 int suser_override = 0;
83 int yp_in_pw_file = 0;
84 char *yp_domain = NULL;
85 char *yp_server = NULL;
87 extern char *tempname;
89 /* Save the local and NIS password information */
90 struct passwd local_password;
91 struct passwd yp_password;
93 void copy_yp_pass(p, x, m)
97 register char *t, *s = p;
100 yp_password.pw_fields = 0;
102 buf = (char *)realloc(buf, m + 10);
105 /* Turn all colons into NULLs */
106 while (strchr(s, ':')) {
107 s = (strchr(s, ':') + 1);
112 #define EXPAND(e) e = t; while ((*t++ = *p++));
113 EXPAND(yp_password.pw_name);
114 yp_password.pw_fields |= _PWF_NAME;
115 EXPAND(yp_password.pw_passwd);
116 yp_password.pw_fields |= _PWF_PASSWD;
117 yp_password.pw_uid = atoi(p);
118 p += (strlen(p) + 1);
119 yp_password.pw_fields |= _PWF_UID;
120 yp_password.pw_gid = atoi(p);
121 p += (strlen(p) + 1);
122 yp_password.pw_fields |= _PWF_GID;
124 EXPAND(yp_password.pw_class);
125 yp_password.pw_fields |= _PWF_CLASS;
126 yp_password.pw_change = atol(p);
127 p += (strlen(p) + 1);
128 yp_password.pw_fields |= _PWF_CHANGE;
129 yp_password.pw_expire = atol(p);
130 p += (strlen(p) + 1);
131 yp_password.pw_fields |= _PWF_EXPIRE;
133 EXPAND(yp_password.pw_gecos);
134 yp_password.pw_fields |= _PWF_GECOS;
135 EXPAND(yp_password.pw_dir);
136 yp_password.pw_fields |= _PWF_DIR;
137 EXPAND(yp_password.pw_shell);
138 yp_password.pw_fields |= _PWF_SHELL;
143 void copy_local_pass(p,m)
150 buf = (char *)realloc(buf, m + 10);
154 EXPAND(local_password.pw_name);
155 EXPAND(local_password.pw_passwd);
156 bcopy(p, (char *)&local_password.pw_uid, sizeof(int));
158 bcopy(p, (char *)&local_password.pw_gid, sizeof(int));
160 bcopy(p, (char *)&local_password.pw_change, sizeof(time_t));
162 EXPAND(local_password.pw_class);
163 EXPAND(local_password.pw_gecos);
164 EXPAND(local_password.pw_dir);
165 EXPAND(local_password.pw_shell);
166 bcopy(p, (char *)&local_password.pw_expire, sizeof(time_t));
168 bcopy(p, (char *)&local_password.pw_fields, sizeof local_password.pw_fields);
169 p += sizeof local_password.pw_fields;
175 * It is not mandatory that an NIS master server also be a client.
176 * However, if the NIS master is not configured as a client, then the
177 * domain name will not be set and ypbind will not be running, so we
178 * will be unable to use the ypclnt routines inside libc. We therefore
179 * need our own magic version of yp_match() which we can use in any
182 static int my_yp_match(server, domain, map, key, keylen, result, resultlen)
187 unsigned long keylen;
189 unsigned long *resultlen;
194 static char buf[YPMAXRECORD + 2];
196 bzero((char *)buf, sizeof(buf));
199 * Don't make this a fatal error. The inability to contact
200 * a server is, for our purposes, equivalent to not finding
201 * the record we were looking for. Letting use_yp() know
202 * that the lookup failed is sufficient.
204 if ((clnt = clnt_create(server, YPPROG,YPVERS,"udp")) == NULL) {
207 warnx("failed to create UDP handle: %s",
208 clnt_spcreateerror(server));
209 pw_error(tempname, 0, 1);
213 ypkey.domain = domain;
215 ypkey.key.keydat_len = keylen;
216 ypkey.key.keydat_val = key;
218 if ((ypval = ypproc_match_2(&ypkey, clnt)) == NULL) {
222 warnx("%s",clnt_sperror(clnt,"YPPROC_MATCH failed"));
223 pw_error(tempname, 0, 1);
229 if (ypval->stat != YP_TRUE) {
230 xdr_free(xdr_ypresp_val, (char *)ypval);
233 int stat = ypval->stat;
234 xdr_free(xdr_ypresp_val, (char *)ypval);
235 if (stat == YP_NOMAP && strstr(map, "master.passwd"))
237 if (stat == YP_NOKEY)
239 warnx("ypmatch failed: %s", yperr_string(ypprot_err(stat)));
240 pw_error(tempname, 0, 1);
245 strncpy((char *)&buf, ypval->val.valdat_val, ypval->val.valdat_len);
247 *result = (char *)&buf;
248 *resultlen = ypval->val.valdat_len;
250 xdr_free(xdr_ypresp_val, (char *)ypval);
256 * Check if the user we're working with is local or in NIS.
258 int use_yp (user, uid, which)
261 int which; /* 0 = use username, 1 = use uid */
263 int user_local = 0, user_yp = 0, user_exists = 0;
266 char bf[UT_NAMESIZE + 2];
270 char ubuf[UT_NAMESIZE + 2];
273 snprintf(ubuf, sizeof(ubuf), "%lu", (unsigned long)uid);
274 user = (char *)&ubuf;
277 /* Grope around for the user in the usual way */
279 if (getpwuid(uid) != NULL)
282 if (getpwnam(user) != NULL)
286 /* Now grope directly through the user database */
287 if ((dbp = dbopen(_PATH_SMP_DB, O_RDONLY, PERM_SECURE,
288 DB_HASH, &openinfo)) == NULL) {
289 warn("error opening database: %s.", _PATH_MP_DB);
290 pw_error(tempname, 0, 1);
293 /* Is NIS turned on */
294 bf[0] = _PW_KEYYPENABLED;
295 key.data = (u_char *)bf;
297 yp_in_pw_file = !(dbp->get)(dbp,&key,&data,0);
298 if (_yp_check(NULL) || (yp_domain && yp_server)) {
299 server = get_yp_master(0);
301 /* Is the user in the NIS passwd map */
302 if (!my_yp_match(server, yp_domain, which ? "passwd.byuid" :
303 "passwd.byname", user, strlen(user),
304 &result, &resultlen)) {
305 user_yp = user_exists = 1;
306 *(char *)(result + resultlen) = '\0';
307 copy_yp_pass(result, 0, resultlen);
309 /* Is the user in the NIS master.passwd map */
310 if (user_yp && !my_yp_match(server, yp_domain, which ?
311 "master.passwd.byuid" : "master.passwd.byname",
313 &result, &resultlen)) {
314 *(char *)(result + resultlen) = '\0';
315 copy_yp_pass(result, 1, resultlen);
319 /* Is the user in the local password database */
321 bf[0] = which ? _PW_KEYBYUID : _PW_KEYBYNAME;
323 bcopy((char *)&uid, bf + 1, sizeof(uid));
325 bcopy((char *)user, bf + 1, MIN(strlen(user), UT_NAMESIZE));
326 key.data = (u_char *)bf;
327 key.size = which ? sizeof(uid) + 1 : strlen(user) + 1;
328 if (!(dbp->get)(dbp,&key,&data,0)) {
330 copy_local_pass(data.data, data.size);
335 if (user_local && user_yp && user_exists)
336 return(USER_YP_AND_LOCAL);
337 else if (!user_local && user_yp && user_exists)
338 return(USER_YP_ONLY);
339 else if (user_local && !user_yp && user_exists)
340 return(USER_LOCAL_ONLY);
341 else if (!user_exists)
342 return(USER_UNKNOWN);
348 * Find the name of the NIS master server for this domain
349 * and make sure it's running yppasswdd.
351 char *get_yp_master(getserver)
357 char *sockname = YP_SOCKNAME;
360 * Sometimes we are called just to probe for rpc.yppasswdd and
361 * set the suser_override flag. Just return NULL and leave
362 * suser_override at 0 if _use_yp doesn't indicate that NIS is
363 * in use and we weren't called from use_yp() itself.
364 * Without this check, we might try probing and fail with an NIS
365 * error in non-NIS environments.
367 if ((_use_yp == USER_UNKNOWN || _use_yp == USER_LOCAL_ONLY) &&
371 /* Get default NIS domain. */
373 if (yp_domain == NULL && (rval = yp_get_default_domain(&yp_domain))) {
374 warnx("can't get local NIS domain name: %s",yperr_string(rval));
375 pw_error(tempname, 0, 1);
378 /* Get master server of passwd map. */
380 if ((mastername = ypxfr_get_master(yp_domain, "passwd.byname",
381 yp_server, yp_server ? 0 : 1)) == NULL) {
382 warnx("can't get name of master NIS server");
383 pw_error(tempname, 0, 1);
389 /* Check if yppasswdd is out there. */
391 if ((rval = getrpcport(mastername, YPPASSWDPROG, YPPASSWDPROC_UPDATE,
392 IPPROTO_UDP)) == 0) {
393 warnx("rpc.yppasswdd is not running on the NIS master server");
394 pw_error(tempname, 0, 1);
398 * Make sure it's on a reserved port.
399 * XXX Might break with yppasswdd servers running on Solaris 2.x.
402 if (rval >= IPPORT_RESERVED) {
403 warnx("rpc.yppasswdd server not running on reserved port");
404 pw_error(tempname, 0, 1);
407 /* See if _we_ are the master server. */
408 if (!force_old && !getuid() && (localport = getrpcport("localhost",
409 YPPASSWDPROG, YPPASSWDPROC_UPDATE, IPPROTO_UDP)) != 0) {
410 if (localport == rval && stat(sockname, &st) != -1) {
412 mastername = "localhost";
416 /* Everything checks out: return the name of the server. */
422 * Ask the user for his NIS password and submit the new information
423 * to yppasswdd. Note that rpc.yppasswdd requires password authentication
424 * and only allows changes to existing records rather than the addition
425 * of new records. (To do actual updates we would need something like
426 * secure RPC and ypupdated, which FreeBSD doesn't have yet.) The FreeBSD
427 * rpc.yppasswdd has some special hooks to allow the superuser update
428 * information without specifying a password, however this only works
429 * for the superuser on the NIS master server.
434 struct yppasswd yppasswd;
435 struct master_yppasswd master_yppasswd;
437 char *master, *password;
440 char *sockname = YP_SOCKNAME;
444 /* Get NIS master server name */
446 master = get_yp_master(1);
448 /* Populate the yppasswd structure that gets handed to yppasswdd. */
450 if (suser_override) {
451 master_yppasswd.newpw.pw_passwd = strdup(pw->pw_passwd);
452 master_yppasswd.newpw.pw_name = strdup(pw->pw_name);
453 master_yppasswd.newpw.pw_uid = pw->pw_uid;
454 master_yppasswd.newpw.pw_gid = pw->pw_gid;
455 master_yppasswd.newpw.pw_expire = pw->pw_expire;
456 master_yppasswd.newpw.pw_change = pw->pw_change;
457 master_yppasswd.newpw.pw_fields = pw->pw_fields;
458 master_yppasswd.newpw.pw_gecos = strdup(pw->pw_gecos);
459 master_yppasswd.newpw.pw_dir = strdup(pw->pw_dir);
460 master_yppasswd.newpw.pw_shell = strdup(pw->pw_shell);
461 master_yppasswd.newpw.pw_class = pw->pw_class != NULL ?
462 strdup(pw->pw_class) : "";
463 master_yppasswd.oldpass = ""; /* not really needed */
464 master_yppasswd.domain = yp_domain;
466 yppasswd.newpw.pw_passwd = strdup(pw->pw_passwd);
467 yppasswd.newpw.pw_name = strdup(pw->pw_name);
468 yppasswd.newpw.pw_uid = pw->pw_uid;
469 yppasswd.newpw.pw_gid = pw->pw_gid;
470 yppasswd.newpw.pw_gecos = strdup(pw->pw_gecos);
471 yppasswd.newpw.pw_dir = strdup(pw->pw_dir);
472 yppasswd.newpw.pw_shell = strdup(pw->pw_shell);
473 yppasswd.oldpass = "";
476 /* Get the user's password for authentication purposes. */
478 printf ("Changing NIS information for %s on %s\n",
479 pw->pw_name, master);
481 if (pw->pw_passwd[0] && !suser_override) {
482 password = getpass("Please enter password: ");
483 if (strncmp(crypt(password,pw->pw_passwd),
484 pw->pw_passwd,strlen(pw->pw_passwd))) {
485 warnx("Password incorrect.");
486 pw_error(tempname, 0, 1);
488 yppasswd.oldpass = password; /* XXX */
491 if (suser_override) {
492 /* Talk to server via AF_UNIX socket. */
493 clnt = clnt_create(sockname, MASTER_YPPASSWDPROG,
494 MASTER_YPPASSWDVERS, "unix");
496 warnx("failed to contact rpc.yppasswdd: %s",
497 clnt_spcreateerror(master));
498 pw_error(tempname, 0, 1);
501 /* Create a handle to yppasswdd. */
503 if ((clnt = clnt_create(master, YPPASSWDPROG,
504 YPPASSWDVERS, "udp")) == NULL) {
505 warnx("failed to contact rpc.yppasswdd: %s",
506 clnt_spcreateerror(master));
507 pw_error(tempname, 0, 1);
511 clnt->cl_auth = authunix_create_default();
514 status = yppasswdproc_update_master_1(&master_yppasswd, clnt);
516 status = yppasswdproc_update_1(&yppasswd, clnt);
518 clnt_geterr(clnt, &err);
520 auth_destroy(clnt->cl_auth);
523 /* Call failed: signal the error. */
525 if (err.re_status != RPC_SUCCESS || status == NULL || *status) {
526 warnx("NIS update failed: %s", clnt_sperrno(err.re_status));
527 pw_error(NULL, 0, 1);
533 warnx("NIS information changed on host %s, domain %s",
536 warnx("NIS information changed on host %s", master);