10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*2829Ssdussud * Common Development and Distribution License (the "License").
6*2829Ssdussud * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*2829Ssdussud * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * DESCRIPTION: This is the N2L equivalent of changepasswd.c. The traditional
300Sstevel@tonic-gate * version modifies the NIS source files and then initiates a
310Sstevel@tonic-gate * ypmake to make the maps and push them.
320Sstevel@tonic-gate *
330Sstevel@tonic-gate * For N2L there are no source files and the policy is that the
340Sstevel@tonic-gate * definitive information is that contained in the DIT. Old
350Sstevel@tonic-gate * information is read from LDAP. Assuming this authenticates, and
360Sstevel@tonic-gate * the change is acceptable, this information is modified and
370Sstevel@tonic-gate * written back to LDAP.
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * Related map entries are then found and updated finally
400Sstevel@tonic-gate * yppushes of the changed maps are initiated. Since the
410Sstevel@tonic-gate * definitive information has already correctly been updated the
420Sstevel@tonic-gate * code is tolerant of some errors during this operation.
430Sstevel@tonic-gate *
440Sstevel@tonic-gate * What was previously in the maps is irrelevant.
450Sstevel@tonic-gate *
460Sstevel@tonic-gate * Some less than perfect code (like inline constants for
470Sstevel@tonic-gate * return values and a few globals) is retained from the original.
480Sstevel@tonic-gate */
490Sstevel@tonic-gate
500Sstevel@tonic-gate #include <sys/types.h>
510Sstevel@tonic-gate #include <sys/stat.h>
520Sstevel@tonic-gate #include <ctype.h>
530Sstevel@tonic-gate #include <unistd.h>
540Sstevel@tonic-gate #include <stdlib.h>
550Sstevel@tonic-gate #include <string.h>
560Sstevel@tonic-gate #include <stdio.h>
570Sstevel@tonic-gate #include <errno.h>
580Sstevel@tonic-gate #include <syslog.h>
590Sstevel@tonic-gate #include <pwd.h>
600Sstevel@tonic-gate #include <signal.h>
610Sstevel@tonic-gate #include <crypt.h>
620Sstevel@tonic-gate #include <rpc/rpc.h>
630Sstevel@tonic-gate #include <rpcsvc/yppasswd.h>
640Sstevel@tonic-gate #include <utmpx.h>
650Sstevel@tonic-gate #include <shadow.h>
660Sstevel@tonic-gate
670Sstevel@tonic-gate #include <ndbm.h>
680Sstevel@tonic-gate /* DO NOT INCLUDE SHIM_HOOKS.H */
690Sstevel@tonic-gate #include "shim.h"
700Sstevel@tonic-gate #include "yptol.h"
710Sstevel@tonic-gate #include "../ldap_util.h"
720Sstevel@tonic-gate
730Sstevel@tonic-gate /* Constants */
740Sstevel@tonic-gate #define CRYPTPWSIZE CRYPT_MAXCIPHERTEXTLEN
750Sstevel@tonic-gate #define STRSIZE 100
760Sstevel@tonic-gate #define FINGERSIZE (4 * STRSIZE - 4)
770Sstevel@tonic-gate #define SHELLSIZE (STRSIZE - 2)
780Sstevel@tonic-gate
790Sstevel@tonic-gate #define UTUSERLEN (sizeof (((struct utmpx *)0)->ut_user))
800Sstevel@tonic-gate #define COLON_CHAR ':'
810Sstevel@tonic-gate
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate * Path to DBM files. This is only required for N2L mode. Traditional mode
840Sstevel@tonic-gate * works with the source files and uses the NIS Makefile to generate the maps.
850Sstevel@tonic-gate * Seems to be hard coded in the rest of NIS so same is done here.
860Sstevel@tonic-gate */
870Sstevel@tonic-gate #define YPDBPATH "/var/yp"
880Sstevel@tonic-gate
890Sstevel@tonic-gate /* Names of password and adjunct mappings. Used to access DIT */
900Sstevel@tonic-gate #define BYNAME ".byname"
910Sstevel@tonic-gate #define BYUID ".byuid"
920Sstevel@tonic-gate #define BYGID ".bygid"
930Sstevel@tonic-gate #define PASSWD_MAPPING "passwd" BYNAME
940Sstevel@tonic-gate #define PASSWD_ADJUNCT_MAPPING "passwd.adjunct" BYNAME
950Sstevel@tonic-gate #define AGEING_MAPPING "ageing" BYNAME
960Sstevel@tonic-gate
970Sstevel@tonic-gate /* Bitmasks used in list of fields to change */
980Sstevel@tonic-gate #define CNG_PASSWD 0x0001
990Sstevel@tonic-gate #define CNG_SH 0x0002
1000Sstevel@tonic-gate #define CNG_GECOS 0x0004
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate /* Globals :-( */
1030Sstevel@tonic-gate extern int single, nogecos, noshell, nopw, mflag;
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate * Structure for containing the information is currently in the DIT. This is
1070Sstevel@tonic-gate * similar to the passwd structure defined in getpwent(3C) apart from.
1080Sstevel@tonic-gate *
1090Sstevel@tonic-gate * 1. Since GID and UID are never changed they are not converted to integers.
1100Sstevel@tonic-gate * 2. There are extra fields to hold adjunct information.
1110Sstevel@tonic-gate * 3. There are extra fields to hold widely used information.
1120Sstevel@tonic-gate */
1130Sstevel@tonic-gate struct passwd_entry {
1140Sstevel@tonic-gate char *pw_name;
1150Sstevel@tonic-gate char *pw_passwd;
1160Sstevel@tonic-gate char *pw_uid;
1170Sstevel@tonic-gate char *pw_gid;
1180Sstevel@tonic-gate char *pw_gecos;
1190Sstevel@tonic-gate char *pw_dir;
1200Sstevel@tonic-gate char *pw_shell;
1210Sstevel@tonic-gate char *adjunct_tail; /* Tail of adjunct entry (opaque) */
1220Sstevel@tonic-gate bool_t adjunct; /* Flag indicating if DIT has adjunct info */
1230Sstevel@tonic-gate char *pwd_str; /* New password string */
1240Sstevel@tonic-gate char *adjunct_str; /* New adjunct string */
1250Sstevel@tonic-gate };
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate /* Prototypes */
1280Sstevel@tonic-gate extern bool_t validloginshell(char *sh, char *arg, int);
1290Sstevel@tonic-gate extern int validstr(char *str, size_t size);
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate suc_code write_shadow_info(char *, struct spwd *);
1320Sstevel@tonic-gate int put_new_info(struct passwd_entry *, char *);
1330Sstevel@tonic-gate char *create_pwd_str(struct passwd_entry *, bool_t);
1340Sstevel@tonic-gate int proc_domain(struct yppasswd *, bool_t, char *);
1350Sstevel@tonic-gate int proc_request(struct yppasswd *, struct passwd_entry *, bool_t, char *);
1360Sstevel@tonic-gate int modify_ent(struct yppasswd *, struct passwd_entry *t, bool_t, char *);
1370Sstevel@tonic-gate int get_change_list(struct yppasswd *, struct passwd_entry *);
1380Sstevel@tonic-gate struct passwd_entry *get_old_info(char *, char *);
1390Sstevel@tonic-gate static char *get_next_token(char *, char **, char *);
1400Sstevel@tonic-gate void free_pwd_entry(struct passwd_entry *);
1410Sstevel@tonic-gate struct spwd *get_old_shadow(char *, char *);
1420Sstevel@tonic-gate suc_code decode_shadow_entry(datum *, struct spwd *);
1430Sstevel@tonic-gate void free_shadow_entry(struct spwd *);
1440Sstevel@tonic-gate int proc_maps(char *, struct passwd_entry *);
1450Sstevel@tonic-gate int proc_map_list(char **, char *, struct passwd_entry *, bool_t);
1460Sstevel@tonic-gate int update_single_map(char *, struct passwd_entry *, bool_t);
1470Sstevel@tonic-gate bool_t strend(char *s1, char *s2);
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate * FUNCTION: shim_changepasswd()
1510Sstevel@tonic-gate *
1520Sstevel@tonic-gate * DESCRIPTION: N2L version of changepasswd(). When this is called 'useshadow'
1530Sstevel@tonic-gate * etc. will have been set up but are meaningless. We work out
1540Sstevel@tonic-gate * what to change based on information from the DIT.
1550Sstevel@tonic-gate *
1560Sstevel@tonic-gate * INPUTS: Identical to changepasswd()
1570Sstevel@tonic-gate *
1580Sstevel@tonic-gate * OUTPUTS: Identical to changepasswd()
1590Sstevel@tonic-gate */
1600Sstevel@tonic-gate void
shim_changepasswd(SVCXPRT * transp)1610Sstevel@tonic-gate shim_changepasswd(SVCXPRT *transp)
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate struct yppasswd yppwd;
1640Sstevel@tonic-gate bool_t root_on_master = FALSE;
1650Sstevel@tonic-gate char domain[MAXNETNAMELEN+1];
1660Sstevel@tonic-gate char **domain_list;
1670Sstevel@tonic-gate int dom_count, i;
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate int ret, ans = 2; /* Answer codes */
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate /* Clean out yppwd ... maybe we don't trust RPC */
1720Sstevel@tonic-gate memset(&yppwd, 0, sizeof (struct yppasswd));
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate /* Get the RPC args */
1750Sstevel@tonic-gate if (!svc_getargs(transp, xdr_yppasswd, (caddr_t)&yppwd)) {
1760Sstevel@tonic-gate svcerr_decode(transp);
1770Sstevel@tonic-gate return;
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate /* Perform basic validation */
1810Sstevel@tonic-gate if ((!validstr(yppwd.newpw.pw_passwd, CRYPTPWSIZE)) ||
1820Sstevel@tonic-gate (!validstr(yppwd.newpw.pw_name, UTUSERLEN)) ||
1830Sstevel@tonic-gate (!validstr(yppwd.newpw.pw_gecos, FINGERSIZE)) ||
1840Sstevel@tonic-gate (!validstr(yppwd.newpw.pw_shell, SHELLSIZE))) {
1850Sstevel@tonic-gate svcerr_decode(transp);
1860Sstevel@tonic-gate return;
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate * Special case: root on the master server can change other
1910Sstevel@tonic-gate * users' passwords without first entering the old password.
1920Sstevel@tonic-gate * We need to ensure that this is indeed root on the master
1930Sstevel@tonic-gate * server. (bug 1253949)
1940Sstevel@tonic-gate */
1950Sstevel@tonic-gate if (strcmp(transp->xp_netid, "ticlts") == 0) {
1960Sstevel@tonic-gate svc_local_cred_t cred;
1970Sstevel@tonic-gate if (!svc_get_local_cred(transp, &cred)) {
1980Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
1990Sstevel@tonic-gate "Couldn't get local user credentials");
2000Sstevel@tonic-gate } else if (cred.ruid == 0)
2010Sstevel@tonic-gate root_on_master = TRUE;
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate /*
2050Sstevel@tonic-gate * Get the domain name. This is tricky because a N2L server may be
2060Sstevel@tonic-gate * handling multiple domains. There is nothing in the request to
2070Sstevel@tonic-gate * indicate which one we are trying to change a passwd for. First
2080Sstevel@tonic-gate * we try to get a list of password related domains from the mapping
2090Sstevel@tonic-gate * file.
2100Sstevel@tonic-gate */
211*2829Ssdussud if (0 !=
212*2829Ssdussud (dom_count = get_mapping_yppasswdd_domain_list(&domain_list))) {
2130Sstevel@tonic-gate /* Got a domain list ... process all the domains */
2140Sstevel@tonic-gate for (i = 0; i < dom_count; i ++) {
2150Sstevel@tonic-gate ret = proc_domain(&yppwd, root_on_master,
2160Sstevel@tonic-gate domain_list[i]);
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate /* If one has worked don't care if others fail */
2190Sstevel@tonic-gate if (0 != ans)
2200Sstevel@tonic-gate ans = ret;
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate else
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate * There was no domain list in the mapping file. The
2270Sstevel@tonic-gate * traditional version of this code calls ypmake which picks
2280Sstevel@tonic-gate * up the domain returned by getdomainname(). Fall back to the
2290Sstevel@tonic-gate * same mechanism.
2300Sstevel@tonic-gate */
2310Sstevel@tonic-gate if (0 > getdomainname(domain, MAXNETNAMELEN+1)) {
2320Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
2330Sstevel@tonic-gate "Could not get any domain info");
2340Sstevel@tonic-gate } else {
2350Sstevel@tonic-gate /* Got one domain ... process it. */
2360Sstevel@tonic-gate ans = proc_domain(&yppwd, root_on_master, domain);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate /* Send reply packet */
2410Sstevel@tonic-gate if (!svc_sendreply(transp, xdr_int, (char *)&ans))
2420Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_WARNING,
2430Sstevel@tonic-gate "could not reply to RPC call");
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate * FUNCTION : proc_domain()
2480Sstevel@tonic-gate *
2490Sstevel@tonic-gate * DESCRIPTION: Process a request for one domain
2500Sstevel@tonic-gate *
2510Sstevel@tonic-gate * GIVEN : Pointer to the request.
2520Sstevel@tonic-gate * Root on master flag
2530Sstevel@tonic-gate * Domain
2540Sstevel@tonic-gate *
2550Sstevel@tonic-gate * OUTPUTS : Answer code for reply
2560Sstevel@tonic-gate */
2570Sstevel@tonic-gate int
proc_domain(struct yppasswd * yppwd,bool_t root_on_master,char * domain)2580Sstevel@tonic-gate proc_domain(struct yppasswd *yppwd, bool_t root_on_master, char *domain)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate struct passwd_entry *old_pwd;
2610Sstevel@tonic-gate char *p;
2620Sstevel@tonic-gate int ans = 2;
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate /* security hole fix from original source */
2650Sstevel@tonic-gate for (p = yppwd->newpw.pw_name; (*p != '\0'); p++)
2660Sstevel@tonic-gate if ((*p == ':') || !(isprint(*p)))
2670Sstevel@tonic-gate *p = '$'; /* you lose buckwheat */
2680Sstevel@tonic-gate for (p = yppwd->newpw.pw_passwd; (*p != '\0'); p++)
2690Sstevel@tonic-gate if ((*p == ':') || !(isprint(*p)))
2700Sstevel@tonic-gate *p = '$'; /* you lose buckwheat */
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate /* Get old info from DIT for this domain */
2730Sstevel@tonic-gate old_pwd = get_old_info(yppwd->newpw.pw_name, domain);
2740Sstevel@tonic-gate if (NULL == old_pwd) {
2750Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
2760Sstevel@tonic-gate "Could not get old information for %s in "
2770Sstevel@tonic-gate "domain %s", yppwd->newpw.pw_name, domain);
2780Sstevel@tonic-gate return (ans);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate /* Have a request that can be replied to */
2820Sstevel@tonic-gate ans = proc_request(yppwd, old_pwd, root_on_master, domain);
2830Sstevel@tonic-gate free_pwd_entry(old_pwd);
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate return (ans);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate * FUNCTION : proc_request()
2900Sstevel@tonic-gate *
2910Sstevel@tonic-gate * DESCRIPTION: Process a request
2920Sstevel@tonic-gate *
2930Sstevel@tonic-gate * GIVEN : Pointer to the request.
2940Sstevel@tonic-gate * Pointer to old information from LDAP
2950Sstevel@tonic-gate * Root on master flag
2960Sstevel@tonic-gate * Domain
2970Sstevel@tonic-gate *
2980Sstevel@tonic-gate * OUTPUTS : Answer code for reply
2990Sstevel@tonic-gate */
3000Sstevel@tonic-gate int
proc_request(struct yppasswd * yppwd,struct passwd_entry * old_pwd,bool_t root_on_master,char * domain)3010Sstevel@tonic-gate proc_request(struct yppasswd *yppwd, struct passwd_entry *old_pwd,
3020Sstevel@tonic-gate bool_t root_on_master, char *domain)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate struct sigaction sa, osa1, osa2, osa3;
3050Sstevel@tonic-gate int ans;
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate /* Authenticate */
3080Sstevel@tonic-gate if ((0 != strcmp(crypt(yppwd->oldpass, old_pwd->pw_passwd),
3090Sstevel@tonic-gate old_pwd->pw_passwd)) && !root_on_master) {
3100Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_NOTICE, "Passwd incorrect %s",
3110Sstevel@tonic-gate yppwd->newpw.pw_name);
3120Sstevel@tonic-gate return (7);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate
3150Sstevel@tonic-gate /* Work out what we have to change and change it */
3160Sstevel@tonic-gate ans = modify_ent(yppwd, old_pwd, root_on_master, domain);
3170Sstevel@tonic-gate if (0 != ans)
3180Sstevel@tonic-gate return (ans);
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate * Generate passwd and adjunct map entries. This creates extra
3220Sstevel@tonic-gate * malloced strings in old_pwd. These will be freed when
3230Sstevel@tonic-gate * free_pwd_entry() is called to free up the rest of the structure.
3240Sstevel@tonic-gate */
3250Sstevel@tonic-gate old_pwd->pwd_str = create_pwd_str(old_pwd, FALSE);
3260Sstevel@tonic-gate if (NULL == old_pwd->pwd_str) {
3270Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
3280Sstevel@tonic-gate "Could not create passwd entry");
3290Sstevel@tonic-gate return (2);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate if (old_pwd->adjunct) {
3320Sstevel@tonic-gate old_pwd->adjunct_str = create_pwd_str(old_pwd, TRUE);
3330Sstevel@tonic-gate if (NULL == old_pwd->adjunct_str) {
3340Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
3350Sstevel@tonic-gate "Could not create adjunct entry");
3360Sstevel@tonic-gate return (2);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate } else {
3390Sstevel@tonic-gate old_pwd->adjunct_str = NULL;
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate /* Put the information back to DIT */
3430Sstevel@tonic-gate ans = put_new_info(old_pwd, domain);
3440Sstevel@tonic-gate if (0 != ans) {
3450Sstevel@tonic-gate return (ans);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate /* Are going to be forking pushes, set up signals */
3490Sstevel@tonic-gate memset(&sa, 0, sizeof (struct sigaction));
3500Sstevel@tonic-gate sa.sa_handler = SIG_IGN;
3510Sstevel@tonic-gate sigaction(SIGTSTP, &sa, (struct sigaction *)0);
3520Sstevel@tonic-gate sigaction(SIGHUP, &sa, &osa1);
3530Sstevel@tonic-gate sigaction(SIGINT, &sa, &osa2);
3540Sstevel@tonic-gate sigaction(SIGQUIT, &sa, &osa3);
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate /* Update and push all the maps */
3570Sstevel@tonic-gate ans = proc_maps(domain, old_pwd);
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate /* Tidy up signals */
3600Sstevel@tonic-gate sigaction(SIGHUP, &osa1, (struct sigaction *)0);
3610Sstevel@tonic-gate sigaction(SIGINT, &osa2, (struct sigaction *)0);
3620Sstevel@tonic-gate sigaction(SIGQUIT, &osa3, (struct sigaction *)0);
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate return (ans);
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate /*
3680Sstevel@tonic-gate * FUNCTION: proc_maps()
3690Sstevel@tonic-gate *
3700Sstevel@tonic-gate * DESCRIPTION: Gets all the map lists and processes them.
3710Sstevel@tonic-gate *
3720Sstevel@tonic-gate * INPUTS: Domain name
3730Sstevel@tonic-gate * New info to write into maps
3740Sstevel@tonic-gate *
3750Sstevel@tonic-gate * OUTPUT : Answer code
3760Sstevel@tonic-gate */
3770Sstevel@tonic-gate int
proc_maps(char * domain,struct passwd_entry * pwd)3780Sstevel@tonic-gate proc_maps(char *domain, struct passwd_entry *pwd)
3790Sstevel@tonic-gate {
3800Sstevel@tonic-gate char **map_list; /* Array of passwd or adjunct maps */
3810Sstevel@tonic-gate int ans = 0;
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate /* Get list of passwd maps from mapping file */
3840Sstevel@tonic-gate map_list = get_passwd_list(FALSE, domain);
3850Sstevel@tonic-gate if (map_list != NULL) {
3860Sstevel@tonic-gate /* Process list of passwd maps */
3870Sstevel@tonic-gate ans = proc_map_list(map_list, domain, pwd, FALSE);
3880Sstevel@tonic-gate free_passwd_list(map_list);
3890Sstevel@tonic-gate if (0 != ans)
3900Sstevel@tonic-gate return (ans);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate
3930Sstevel@tonic-gate /*
3940Sstevel@tonic-gate * If we get here either there were no passwd maps or there were
3950Sstevel@tonic-gate * some and they were processed successfully. Either case is good
3960Sstevel@tonic-gate * continue and process passwd.adjunct maps.
3970Sstevel@tonic-gate */
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate /* Get list of adjunct maps from mapping file */
4000Sstevel@tonic-gate map_list = get_passwd_list(TRUE, domain);
4010Sstevel@tonic-gate if (map_list != NULL) {
4020Sstevel@tonic-gate /*
4030Sstevel@tonic-gate * Process list of adjunct maps. If the required information
4040Sstevel@tonic-gate * is not present in LDAP then the updates attempts will log
4050Sstevel@tonic-gate * an error. No need to make the check here
4060Sstevel@tonic-gate */
4070Sstevel@tonic-gate ans = proc_map_list(map_list, domain, pwd, TRUE);
4080Sstevel@tonic-gate free_passwd_list(map_list);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate return (ans);
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate
4140Sstevel@tonic-gate /*
4150Sstevel@tonic-gate * FUNCTION: proc_map_list()
4160Sstevel@tonic-gate *
4170Sstevel@tonic-gate * DESCRIPTION: Finds entries in one list of map that need to be updated.
4180Sstevel@tonic-gate * updates them and writes them back.
4190Sstevel@tonic-gate *
4200Sstevel@tonic-gate * INPUTS: Null terminated list of maps to process.
4210Sstevel@tonic-gate * Domain name
4220Sstevel@tonic-gate * Information to write (including user name)
4230Sstevel@tonic-gate * Flag indicating if this is the adjunct list
4240Sstevel@tonic-gate *
4250Sstevel@tonic-gate * OUTPUTS: An error code
4260Sstevel@tonic-gate */
4270Sstevel@tonic-gate int
proc_map_list(char ** map_list,char * domain,struct passwd_entry * pwd,bool_t adjunct_flag)4280Sstevel@tonic-gate proc_map_list(char **map_list, char *domain,
4290Sstevel@tonic-gate struct passwd_entry *pwd, bool_t adjunct_flag)
4300Sstevel@tonic-gate {
4310Sstevel@tonic-gate char *myself = "proc_map_list";
4320Sstevel@tonic-gate char *map_name;
4330Sstevel@tonic-gate char cmdbuf[BUFSIZ];
4340Sstevel@tonic-gate int map_name_len = 0;
4350Sstevel@tonic-gate int index, ans = 0;
4360Sstevel@tonic-gate int res;
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate /* If this is a adjunct list check LDAP had some adjunct info */
4390Sstevel@tonic-gate if ((adjunct_flag) && (!pwd->adjunct)) {
4400Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_INFO,
4410Sstevel@tonic-gate "Have adjunct map list but no adjunct data in DIT");
4420Sstevel@tonic-gate /* Not a disaster */
4430Sstevel@tonic-gate return (0);
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate /* Allocate enough buffer to take longest map name */
4470Sstevel@tonic-gate for (index = 0; map_list[index] != NULL; index ++)
4480Sstevel@tonic-gate if (map_name_len < strlen(map_list[index]))
4490Sstevel@tonic-gate map_name_len = strlen(map_list[index]);
4500Sstevel@tonic-gate map_name_len += strlen(YPDBPATH);
4510Sstevel@tonic-gate map_name_len += strlen(NTOL_PREFIX);
4520Sstevel@tonic-gate map_name_len += strlen(domain);
4530Sstevel@tonic-gate map_name_len += 3;
4540Sstevel@tonic-gate if (NULL == (map_name = am(myself, map_name_len))) {
4550Sstevel@tonic-gate logmsg(MSG_NOMEM, LOG_ERR, "Could not alloc map name");
4560Sstevel@tonic-gate return (2);
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate /* For all maps in list */
4600Sstevel@tonic-gate for (index = 0; map_list[index] != NULL; index ++) {
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate /* Generate full map name */
4630Sstevel@tonic-gate strcpy(map_name, YPDBPATH);
4640Sstevel@tonic-gate add_separator(map_name);
4650Sstevel@tonic-gate strcat(map_name, domain);
4660Sstevel@tonic-gate add_separator(map_name);
4670Sstevel@tonic-gate strcat(map_name, NTOL_PREFIX);
4680Sstevel@tonic-gate strcat(map_name, map_list[index]);
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate if (0 != (ans = update_single_map(map_name, pwd, adjunct_flag)))
4710Sstevel@tonic-gate break;
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate
4740Sstevel@tonic-gate /* Done with full map path */
4750Sstevel@tonic-gate sfree(map_name);
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate /*
4780Sstevel@tonic-gate * If (ans != 0) then one more maps have failed. LDAP has however been
4790Sstevel@tonic-gate * updates. This is the definitive source for information there is no
4800Sstevel@tonic-gate * need to unwind. (This was probably due to maps that were already
4810Sstevel@tonic-gate * corrupt).
4820Sstevel@tonic-gate */
4830Sstevel@tonic-gate
4840Sstevel@tonic-gate /*
4850Sstevel@tonic-gate * If it all worked fork off push operations for the maps. Since we
4860Sstevel@tonic-gate * want the map to end up with it's traditional name on the slave send
4870Sstevel@tonic-gate * the name without its LDAP_ prefix. The slave will call ypxfrd
4880Sstevel@tonic-gate * which, since it is running in N2L mode, will put the prefix back on
4890Sstevel@tonic-gate * before reading the file.
4900Sstevel@tonic-gate */
4910Sstevel@tonic-gate if (mflag && (0 == ans)) {
4920Sstevel@tonic-gate for (index = 0; (map_name = map_list[index]) != NULL;
4930Sstevel@tonic-gate index ++) {
4940Sstevel@tonic-gate if (fork() == 0) {
4950Sstevel@tonic-gate /*
4960Sstevel@tonic-gate * Define full path to yppush. Probably also
4970Sstevel@tonic-gate * best for security.
4980Sstevel@tonic-gate */
4990Sstevel@tonic-gate strcpy(cmdbuf, "/usr/lib/netsvc/yp/yppush ");
5000Sstevel@tonic-gate strcat(cmdbuf, map_name);
5010Sstevel@tonic-gate if (0 > system(cmdbuf))
5020Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
5030Sstevel@tonic-gate "Could not initiate yppush");
5040Sstevel@tonic-gate exit(0);
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate }
5080Sstevel@tonic-gate return (ans);
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate
5110Sstevel@tonic-gate /*
5120Sstevel@tonic-gate * FUNCTION : update_single_map()
5130Sstevel@tonic-gate *
5140Sstevel@tonic-gate * DESCRIPTION: Updates one map. This is messy because we want to lock the map
5150Sstevel@tonic-gate * to prevent other processes from updating it at the same time.
5160Sstevel@tonic-gate * This mandates that we open it using the shim. When we
5170Sstevel@tonic-gate * write to it however we DO NOT want to write through to LDAP
5180Sstevel@tonic-gate * i.e. do not want to use the shim.
5190Sstevel@tonic-gate *
5200Sstevel@tonic-gate * Solution : Do not include shim_hooks.h but call the shim
5210Sstevel@tonic-gate * versions of dbm_functions explicitly where needed.
5220Sstevel@tonic-gate *
5230Sstevel@tonic-gate * INPUT : Full name of map
5240Sstevel@tonic-gate * Information to write (including user name)
5250Sstevel@tonic-gate * Flag indicating if this is an adjunct map.
5260Sstevel@tonic-gate *
5270Sstevel@tonic-gate * OUTPUT : Answer code
5280Sstevel@tonic-gate *
5290Sstevel@tonic-gate */
5300Sstevel@tonic-gate int
update_single_map(char * map_name,struct passwd_entry * pwd,bool_t adjunct_flag)5310Sstevel@tonic-gate update_single_map(char *map_name, struct passwd_entry *pwd, bool_t adjunct_flag)
5320Sstevel@tonic-gate {
5330Sstevel@tonic-gate DBM *map;
5340Sstevel@tonic-gate int res;
5350Sstevel@tonic-gate datum data, key;
5360Sstevel@tonic-gate
5370Sstevel@tonic-gate /* Set up data */
5380Sstevel@tonic-gate if (adjunct_flag)
5390Sstevel@tonic-gate data.dptr = pwd->adjunct_str;
5400Sstevel@tonic-gate else
5410Sstevel@tonic-gate data.dptr = pwd->pwd_str;
5420Sstevel@tonic-gate data.dsize = strlen(data.dptr);
5430Sstevel@tonic-gate
5440Sstevel@tonic-gate /* Set up key dependent on which type of map this is */
5450Sstevel@tonic-gate key.dptr = NULL;
5460Sstevel@tonic-gate if (strend(map_name, BYNAME))
5470Sstevel@tonic-gate key.dptr = pwd->pw_name;
5480Sstevel@tonic-gate if (strend(map_name, BYUID))
5490Sstevel@tonic-gate key.dptr = pwd->pw_uid;
5500Sstevel@tonic-gate if (strend(map_name, BYGID))
5510Sstevel@tonic-gate key.dptr = pwd->pw_gid;
5520Sstevel@tonic-gate
5530Sstevel@tonic-gate if (NULL == key.dptr) {
5540Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
5550Sstevel@tonic-gate "Unrecognized map type %s", map_name);
5560Sstevel@tonic-gate return (0); /* Next map */
5570Sstevel@tonic-gate }
5580Sstevel@tonic-gate key.dsize = strlen(key.dptr);
5590Sstevel@tonic-gate
5600Sstevel@tonic-gate /* Open the map */
5610Sstevel@tonic-gate map = shim_dbm_open(map_name, O_RDWR, 0600);
5620Sstevel@tonic-gate if (NULL == map) {
5630Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR, "Could not open %s", map_name);
5640Sstevel@tonic-gate return (0); /* Next map */
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate /* Lock map for update. Painful and may block but have to do it */
5680Sstevel@tonic-gate if (SUCCESS != lock_map_update((map_ctrl *)map)) {
5690Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
5700Sstevel@tonic-gate "Could not lock map %s for update", map_name);
5710Sstevel@tonic-gate shim_dbm_close(map);
5720Sstevel@tonic-gate return (2);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate
5750Sstevel@tonic-gate /* Do the update use simple DBM operation */
5760Sstevel@tonic-gate res = dbm_store(((map_ctrl *)map)->entries, key, data, DBM_REPLACE);
5770Sstevel@tonic-gate
5780Sstevel@tonic-gate /* update entry TTL. If we fail not a problem will just timeout early */
5790Sstevel@tonic-gate update_entry_ttl((map_ctrl *)map, &key, TTL_RAND);
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate /*
5820Sstevel@tonic-gate * Map has been modified so update YP_LAST_MODIFIED. In the vanilla
5830Sstevel@tonic-gate * NIS case this would have been done by the ypmake done after updating
5840Sstevel@tonic-gate * the passwd source file. If this fails not a great problem the map
5850Sstevel@tonic-gate */
5860Sstevel@tonic-gate if (FAILURE == update_timestamp(((map_ctrl *)map)->entries)) {
5870Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR, "Could not update "
5880Sstevel@tonic-gate "YP_LAST_MODIFIED %s will not be pushed this time",
5890Sstevel@tonic-gate map_name);
5900Sstevel@tonic-gate }
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate /*
5930Sstevel@tonic-gate * Possibly should hold the lock until after push is complete
5940Sstevel@tonic-gate * but this could deadlock if client is slow and ypxfrd also
5950Sstevel@tonic-gate * decides to do an update.
5960Sstevel@tonic-gate */
5970Sstevel@tonic-gate unlock_map_update((map_ctrl *)map);
5980Sstevel@tonic-gate
5990Sstevel@tonic-gate /* Close the map */
6000Sstevel@tonic-gate shim_dbm_close(map);
6010Sstevel@tonic-gate
6020Sstevel@tonic-gate if (0 != res) {
6030Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
6040Sstevel@tonic-gate "Could not update map %s", map_name);
6050Sstevel@tonic-gate return (2);
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate
6080Sstevel@tonic-gate return (0);
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate
6110Sstevel@tonic-gate /*
6120Sstevel@tonic-gate * FUNCTION : strend()
6130Sstevel@tonic-gate *
6140Sstevel@tonic-gate * DESCRIPTION: Determines if one string ends with another.
6150Sstevel@tonic-gate */
6160Sstevel@tonic-gate bool_t
strend(char * s1,char * s2)6170Sstevel@tonic-gate strend(char *s1, char *s2)
6180Sstevel@tonic-gate {
6190Sstevel@tonic-gate int len_dif;
6200Sstevel@tonic-gate
6210Sstevel@tonic-gate len_dif = strlen(s1) - strlen(s2);
6220Sstevel@tonic-gate if (0 > len_dif)
6230Sstevel@tonic-gate return (FALSE);
6240Sstevel@tonic-gate if (0 == strcmp(s1 + len_dif, s2))
6250Sstevel@tonic-gate return (TRUE);
6260Sstevel@tonic-gate return (FALSE);
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate
6290Sstevel@tonic-gate /*
6300Sstevel@tonic-gate * FUNCTION: modify_ent()
6310Sstevel@tonic-gate *
6320Sstevel@tonic-gate * DESCRIPTION: Modify an entry to reflect a request.
6330Sstevel@tonic-gate *
6340Sstevel@tonic-gate * INPUT: Pointer to the request.
6350Sstevel@tonic-gate * Pointer to the entry to modify.
6360Sstevel@tonic-gate * Flag indication if we are root on master
6370Sstevel@tonic-gate * Domain
6380Sstevel@tonic-gate *
6390Sstevel@tonic-gate * OUTPUT: Error code
6400Sstevel@tonic-gate */
6410Sstevel@tonic-gate int
modify_ent(struct yppasswd * yppwd,struct passwd_entry * old_ent,bool_t root_on_master,char * domain)6420Sstevel@tonic-gate modify_ent(struct yppasswd *yppwd, struct passwd_entry *old_ent,
6430Sstevel@tonic-gate bool_t root_on_master, char *domain)
6440Sstevel@tonic-gate {
6450Sstevel@tonic-gate int change_list;
6460Sstevel@tonic-gate struct spwd *shadow;
6470Sstevel@tonic-gate time_t now;
6480Sstevel@tonic-gate
6490Sstevel@tonic-gate /* Get list of changes */
6500Sstevel@tonic-gate change_list = get_change_list(yppwd, old_ent);
6510Sstevel@tonic-gate
6520Sstevel@tonic-gate if (!change_list) {
6530Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_NOTICE,
6540Sstevel@tonic-gate "No change for %s", yppwd->newpw.pw_name);
6550Sstevel@tonic-gate return (3);
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate
6580Sstevel@tonic-gate /* Check that the shell we have been given is acceptable. */
6590Sstevel@tonic-gate if ((change_list & CNG_SH) && (!validloginshell(old_ent->pw_shell,
6600Sstevel@tonic-gate yppwd->newpw.pw_shell, root_on_master)))
6610Sstevel@tonic-gate return (2);
6620Sstevel@tonic-gate
6630Sstevel@tonic-gate /*
6640Sstevel@tonic-gate * If changing the password do any aging checks.
6650Sstevel@tonic-gate * Since there are no shadow maps this is done by accessing
6660Sstevel@tonic-gate * attributes in the DIT via the mapping system.
6670Sstevel@tonic-gate */
6680Sstevel@tonic-gate if (change_list & CNG_PASSWD) {
6690Sstevel@tonic-gate
6700Sstevel@tonic-gate /* Try to get shadow information */
6710Sstevel@tonic-gate shadow = get_old_shadow(yppwd->newpw.pw_name, domain);
6720Sstevel@tonic-gate
6730Sstevel@tonic-gate /* If there is shadow information make password aging checks */
6740Sstevel@tonic-gate if (NULL != shadow) {
6750Sstevel@tonic-gate now = DAY_NOW;
6760Sstevel@tonic-gate /* password aging - bug for bug compatibility */
6770Sstevel@tonic-gate if (shadow->sp_max != -1) {
6780Sstevel@tonic-gate if (now < shadow->sp_lstchg + shadow->sp_min) {
6790Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
6800Sstevel@tonic-gate "Sorry: < %ld days since "
6810Sstevel@tonic-gate "the last change", shadow->sp_min);
6820Sstevel@tonic-gate free_shadow_entry(shadow);
6830Sstevel@tonic-gate return (2);
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate
6870Sstevel@tonic-gate /* Update time of change */
6880Sstevel@tonic-gate shadow->sp_lstchg = now;
6890Sstevel@tonic-gate
6900Sstevel@tonic-gate /* Write it back */
6910Sstevel@tonic-gate write_shadow_info(domain, shadow);
6920Sstevel@tonic-gate
6930Sstevel@tonic-gate free_shadow_entry(shadow);
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate
6970Sstevel@tonic-gate /* Make changes to old entity */
6980Sstevel@tonic-gate if (change_list & CNG_GECOS) {
6990Sstevel@tonic-gate if (NULL != old_ent->pw_gecos)
7000Sstevel@tonic-gate sfree(old_ent->pw_gecos);
7010Sstevel@tonic-gate old_ent->pw_gecos = strdup(yppwd->newpw.pw_gecos);
7020Sstevel@tonic-gate if (NULL == old_ent->pw_gecos) {
7030Sstevel@tonic-gate logmsg(MSG_NOMEM, LOG_ERR, "Could not allocate gecos");
7040Sstevel@tonic-gate return (2);
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate
7080Sstevel@tonic-gate if (change_list & CNG_SH) {
7090Sstevel@tonic-gate if (NULL != old_ent->pw_shell)
7100Sstevel@tonic-gate sfree(old_ent->pw_shell);
7110Sstevel@tonic-gate old_ent->pw_shell = strdup(yppwd->newpw.pw_shell);
7120Sstevel@tonic-gate if (NULL == old_ent->pw_shell) {
7130Sstevel@tonic-gate logmsg(MSG_NOMEM, LOG_ERR, "Could not allocate shell");
7140Sstevel@tonic-gate return (2);
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate }
7170Sstevel@tonic-gate
7180Sstevel@tonic-gate if (change_list & CNG_PASSWD) {
7190Sstevel@tonic-gate if (NULL != old_ent->pw_passwd)
7200Sstevel@tonic-gate sfree(old_ent->pw_passwd);
7210Sstevel@tonic-gate old_ent->pw_passwd = strdup(yppwd->newpw.pw_passwd);
7220Sstevel@tonic-gate if (NULL == old_ent->pw_passwd) {
7230Sstevel@tonic-gate logmsg(MSG_NOMEM, LOG_ERR, "Could not allocate passwd");
7240Sstevel@tonic-gate return (2);
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate
7280Sstevel@tonic-gate return (0);
7290Sstevel@tonic-gate }
7300Sstevel@tonic-gate
7310Sstevel@tonic-gate /*
7320Sstevel@tonic-gate * FUNCTION : get_change_list()
7330Sstevel@tonic-gate *
7340Sstevel@tonic-gate * DESCRIPTION: Works out what we have to change.
7350Sstevel@tonic-gate *
7360Sstevel@tonic-gate * INPUTS : Request.
7370Sstevel@tonic-gate * Structure containing current state of entry
7380Sstevel@tonic-gate *
7390Sstevel@tonic-gate * OUTPUTS : A bitmask signaling what to change. (Implemented in this
7400Sstevel@tonic-gate * way to make it easy to pass between functions).
7410Sstevel@tonic-gate */
7420Sstevel@tonic-gate int
get_change_list(struct yppasswd * yppwd,struct passwd_entry * old_ent)7430Sstevel@tonic-gate get_change_list(struct yppasswd *yppwd, struct passwd_entry *old_ent)
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate int list = 0;
7460Sstevel@tonic-gate char *p;
7470Sstevel@tonic-gate
7480Sstevel@tonic-gate p = yppwd->newpw.pw_passwd;
7490Sstevel@tonic-gate if ((!nopw) &&
7500Sstevel@tonic-gate p && *p &&
7510Sstevel@tonic-gate !(*p++ == '#' && *p++ == '#' &&
7520Sstevel@tonic-gate (strcmp(p, old_ent->pw_name) == 0)) &&
7530Sstevel@tonic-gate (strcmp(crypt(old_ent->pw_passwd,
7540Sstevel@tonic-gate yppwd->newpw.pw_passwd), yppwd->newpw.pw_passwd) != 0))
7550Sstevel@tonic-gate list |= CNG_PASSWD;
7560Sstevel@tonic-gate
7570Sstevel@tonic-gate if ((NULL != old_ent->pw_shell) &&
7580Sstevel@tonic-gate (!noshell) &&
7590Sstevel@tonic-gate (strcmp(old_ent->pw_shell, yppwd->newpw.pw_shell) != 0)) {
7600Sstevel@tonic-gate if (single)
7610Sstevel@tonic-gate list = 0;
7620Sstevel@tonic-gate list |= CNG_SH;
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate
7650Sstevel@tonic-gate if ((NULL != old_ent->pw_gecos) &&
7660Sstevel@tonic-gate (!nogecos) &&
7670Sstevel@tonic-gate (strcmp(old_ent->pw_gecos, yppwd->newpw.pw_gecos) != 0)) {
7680Sstevel@tonic-gate if (single)
7690Sstevel@tonic-gate list = 0;
7700Sstevel@tonic-gate list |= CNG_GECOS;
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate
7730Sstevel@tonic-gate return (list);
7740Sstevel@tonic-gate }
7750Sstevel@tonic-gate
7760Sstevel@tonic-gate /*
7770Sstevel@tonic-gate * FUNCTION : decode_pwd_entry()
7780Sstevel@tonic-gate *
7790Sstevel@tonic-gate * DESCRIPTION: Pulls apart a password entry. Because the password entry has
7800Sstevel@tonic-gate * come from the mapping system it can be assumed to be correctly
7810Sstevel@tonic-gate * formatted and relatively simple parsing can be done.
7820Sstevel@tonic-gate *
7830Sstevel@tonic-gate * Substrings are put into malloced memory. Caller to free.
7840Sstevel@tonic-gate *
7850Sstevel@tonic-gate * For adjunct files most of it is left empty.
7860Sstevel@tonic-gate *
7870Sstevel@tonic-gate * It would be nice to use getpwent and friends for this work but
7880Sstevel@tonic-gate * these only seem to exist for files and it seems excessive to
7890Sstevel@tonic-gate * create a temporary file for this operation.
7900Sstevel@tonic-gate *
7910Sstevel@tonic-gate * INPUTS: Pointer to datum containing password string.
7920Sstevel@tonic-gate * Pointer to structure in which to return results
7930Sstevel@tonic-gate * Flag indicating if we are decoding passwd or passwd.adjunct
7940Sstevel@tonic-gate *
7950Sstevel@tonic-gate * OUTPUTS: SUCCESS = Decoded successfully
7960Sstevel@tonic-gate * FAILURE = Not decoded successfully. Caller to tidy up.
7970Sstevel@tonic-gate */
7980Sstevel@tonic-gate suc_code
decode_pwd_entry(datum * data,struct passwd_entry * pwd,bool_t adjunct)7990Sstevel@tonic-gate decode_pwd_entry(datum *data, struct passwd_entry *pwd, bool_t adjunct)
8000Sstevel@tonic-gate {
8010Sstevel@tonic-gate char *myself = "decode_pwd_entry";
8020Sstevel@tonic-gate char *p, *str_end, *temp;
8030Sstevel@tonic-gate
8040Sstevel@tonic-gate /* Work out last location in string */
8050Sstevel@tonic-gate str_end = data->dptr + data->dsize;
8060Sstevel@tonic-gate
8070Sstevel@tonic-gate /* Name */
8080Sstevel@tonic-gate if (NULL == (p = get_next_token(data->dptr, &temp, str_end)))
8090Sstevel@tonic-gate return (FAILURE);
8100Sstevel@tonic-gate if (adjunct) {
8110Sstevel@tonic-gate /* If we found an adjunct version this is the one to use */
8120Sstevel@tonic-gate if (NULL != pwd->pw_name)
8130Sstevel@tonic-gate sfree(pwd->pw_name);
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate pwd->pw_name = temp;
8160Sstevel@tonic-gate
8170Sstevel@tonic-gate /* Password */
8180Sstevel@tonic-gate if (NULL == (p = get_next_token(p, &temp, str_end)))
8190Sstevel@tonic-gate return (FAILURE);
8200Sstevel@tonic-gate if (adjunct) {
8210Sstevel@tonic-gate /* If we found an adjunct version this is the one to use */
8220Sstevel@tonic-gate if (NULL != pwd->pw_passwd)
8230Sstevel@tonic-gate sfree(pwd->pw_passwd);
8240Sstevel@tonic-gate }
8250Sstevel@tonic-gate pwd->pw_passwd = temp;
8260Sstevel@tonic-gate
8270Sstevel@tonic-gate if (adjunct) {
8280Sstevel@tonic-gate /* Store adjunct information in opaque string */
8290Sstevel@tonic-gate pwd->adjunct_tail = am(myself, str_end - p + 1);
8300Sstevel@tonic-gate if (NULL == pwd->adjunct_tail)
8310Sstevel@tonic-gate return (FAILURE);
8320Sstevel@tonic-gate strncpy(pwd->adjunct_tail, p, str_end - p);
8330Sstevel@tonic-gate pwd->adjunct_tail[str_end - p] = '\0';
8340Sstevel@tonic-gate
8350Sstevel@tonic-gate /* Remember that LDAP contained adjunct data */
8360Sstevel@tonic-gate pwd->adjunct = TRUE;
8370Sstevel@tonic-gate return (SUCCESS);
8380Sstevel@tonic-gate }
8390Sstevel@tonic-gate
8400Sstevel@tonic-gate /* If we get here not adjunct. Decode rest of passwd */
8410Sstevel@tonic-gate
8420Sstevel@tonic-gate /* UID */
8430Sstevel@tonic-gate if (NULL == (p = get_next_token(p, &(pwd->pw_uid), str_end)))
8440Sstevel@tonic-gate return (FAILURE);
8450Sstevel@tonic-gate
8460Sstevel@tonic-gate /* GID */
8470Sstevel@tonic-gate if (NULL == (p = get_next_token(p, &(pwd->pw_gid), str_end)))
8480Sstevel@tonic-gate return (FAILURE);
8490Sstevel@tonic-gate
8500Sstevel@tonic-gate /* Gecos */
8510Sstevel@tonic-gate if (NULL == (p = get_next_token(p, &(pwd->pw_gecos), str_end)))
8520Sstevel@tonic-gate return (FAILURE);
8530Sstevel@tonic-gate
8540Sstevel@tonic-gate /* Home dir */
8550Sstevel@tonic-gate if (NULL == (p = get_next_token(p, &(pwd->pw_dir), str_end)))
8560Sstevel@tonic-gate return (FAILURE);
8570Sstevel@tonic-gate
8580Sstevel@tonic-gate /* Shell may not be present so don't check return */
8590Sstevel@tonic-gate get_next_token(p, &(pwd->pw_shell), str_end);
8600Sstevel@tonic-gate
8610Sstevel@tonic-gate if (NULL == pwd->pw_shell)
8620Sstevel@tonic-gate return (FAILURE);
8630Sstevel@tonic-gate
8640Sstevel@tonic-gate return (SUCCESS);
8650Sstevel@tonic-gate }
8660Sstevel@tonic-gate
8670Sstevel@tonic-gate /*
8680Sstevel@tonic-gate * FUNCTION : get_next_token()
8690Sstevel@tonic-gate *
8700Sstevel@tonic-gate * DESCRIPTION: Gets the next token from a string upto the next colon or the
8710Sstevel@tonic-gate * end of the string. The duplicates this token into malloced
8720Sstevel@tonic-gate * memory removing any spaces.
8730Sstevel@tonic-gate *
8740Sstevel@tonic-gate * INPUTS : String to search for token. NOT NULL TERMINATED
8750Sstevel@tonic-gate * Location to return result (NULL if result not required)
8760Sstevel@tonic-gate * Last location in string
8770Sstevel@tonic-gate *
8780Sstevel@tonic-gate * OUTPUT : Pointer into the string immediately after the token.
8790Sstevel@tonic-gate * NULL if end of string reached or error.
8800Sstevel@tonic-gate */
8810Sstevel@tonic-gate static char *
get_next_token(char * str,char ** op,char * str_end)8820Sstevel@tonic-gate get_next_token(char *str, char **op, char *str_end)
8830Sstevel@tonic-gate {
8840Sstevel@tonic-gate char *myself = "get_next_token";
8850Sstevel@tonic-gate char *p, *tok_start, *tok_end;
8860Sstevel@tonic-gate
8870Sstevel@tonic-gate p = str;
8880Sstevel@tonic-gate /* Skip leading whitespace */
8890Sstevel@tonic-gate while (' ' == *p)
8900Sstevel@tonic-gate p++;
8910Sstevel@tonic-gate tok_start = p;
8920Sstevel@tonic-gate tok_end = p;
8930Sstevel@tonic-gate
8940Sstevel@tonic-gate while ((str_end + 1 != p) && (COLON_CHAR != *p)) {
8950Sstevel@tonic-gate if (' ' != *p)
8960Sstevel@tonic-gate tok_end = p;
8970Sstevel@tonic-gate p++;
8980Sstevel@tonic-gate }
8990Sstevel@tonic-gate
9000Sstevel@tonic-gate /* Required string is now between start and end */
9010Sstevel@tonic-gate if (NULL != op) {
9020Sstevel@tonic-gate *op = am(myself, tok_end - tok_start + 2);
9030Sstevel@tonic-gate if (NULL == *op) {
9040Sstevel@tonic-gate logmsg(MSG_NOMEM, LOG_ERR,
9050Sstevel@tonic-gate "Could not alloc memory for token");
9060Sstevel@tonic-gate return (NULL);
9070Sstevel@tonic-gate }
9080Sstevel@tonic-gate strncpy(*op, tok_start, tok_end - tok_start + 1);
9090Sstevel@tonic-gate
9100Sstevel@tonic-gate /* Terminate token */
9110Sstevel@tonic-gate (*op)[tok_end - tok_start + 1] = '\0';
9120Sstevel@tonic-gate
9130Sstevel@tonic-gate }
9140Sstevel@tonic-gate
9150Sstevel@tonic-gate /* Check if we reached the end of the input string */
9160Sstevel@tonic-gate if ('\0' == *p)
9170Sstevel@tonic-gate return (NULL);
9180Sstevel@tonic-gate
9190Sstevel@tonic-gate /* There is some more */
9200Sstevel@tonic-gate p++;
9210Sstevel@tonic-gate return (p);
9220Sstevel@tonic-gate }
9230Sstevel@tonic-gate
9240Sstevel@tonic-gate /*
9250Sstevel@tonic-gate * FUNCTION : free_pwd_entry()
9260Sstevel@tonic-gate *
9270Sstevel@tonic-gate * DESCRIPTION: Frees up a pwd_entry structure and its contents.
9280Sstevel@tonic-gate *
9290Sstevel@tonic-gate * INPUTS: Pointer to the structure to free.
9300Sstevel@tonic-gate *
9310Sstevel@tonic-gate * OUTPUT: Nothing
9320Sstevel@tonic-gate */
9330Sstevel@tonic-gate void
free_pwd_entry(struct passwd_entry * pwd)9340Sstevel@tonic-gate free_pwd_entry(struct passwd_entry *pwd)
9350Sstevel@tonic-gate {
9360Sstevel@tonic-gate /* Free up strings */
9370Sstevel@tonic-gate if (NULL != pwd->pw_name)
9380Sstevel@tonic-gate sfree(pwd->pw_name);
9390Sstevel@tonic-gate
9400Sstevel@tonic-gate if (NULL != pwd->pw_passwd)
9410Sstevel@tonic-gate sfree(pwd->pw_passwd);
9420Sstevel@tonic-gate
9430Sstevel@tonic-gate if (NULL != pwd->pw_gecos)
9440Sstevel@tonic-gate sfree(pwd->pw_gecos);
9450Sstevel@tonic-gate
9460Sstevel@tonic-gate if (NULL != pwd->pw_shell)
9470Sstevel@tonic-gate sfree(pwd->pw_shell);
9480Sstevel@tonic-gate
9490Sstevel@tonic-gate if (NULL != pwd->pw_dir)
9500Sstevel@tonic-gate sfree(pwd->pw_dir);
9510Sstevel@tonic-gate
9520Sstevel@tonic-gate if (NULL != pwd->adjunct_tail)
9530Sstevel@tonic-gate sfree(pwd->adjunct_tail);
9540Sstevel@tonic-gate
9550Sstevel@tonic-gate if (NULL != pwd->pwd_str)
9560Sstevel@tonic-gate sfree(pwd->pwd_str);
9570Sstevel@tonic-gate
9580Sstevel@tonic-gate if (NULL != pwd->adjunct_str)
9590Sstevel@tonic-gate sfree(pwd->adjunct_str);
9600Sstevel@tonic-gate
9610Sstevel@tonic-gate /* Free up structure */
9620Sstevel@tonic-gate sfree(pwd);
9630Sstevel@tonic-gate }
9640Sstevel@tonic-gate
9650Sstevel@tonic-gate /*
9660Sstevel@tonic-gate * FUNCTION : create_pwd_str()
9670Sstevel@tonic-gate *
9680Sstevel@tonic-gate * DESCRIPTION: Builds up a new password entity string from a passwd structure.
9690Sstevel@tonic-gate *
9700Sstevel@tonic-gate * INPUTS : Structure containing password details
9710Sstevel@tonic-gate * Flag indicating if we should create an adjunct or passwd string.
9720Sstevel@tonic-gate *
9730Sstevel@tonic-gate * OUTPUTS : String in malloced memory (to be freed by caller).
9740Sstevel@tonic-gate * NULL on failure.
9750Sstevel@tonic-gate */
9760Sstevel@tonic-gate char *
create_pwd_str(struct passwd_entry * pwd,bool_t adjunct)9770Sstevel@tonic-gate create_pwd_str(struct passwd_entry *pwd, bool_t adjunct)
9780Sstevel@tonic-gate {
9790Sstevel@tonic-gate char *myself = "create_pwd_str";
9800Sstevel@tonic-gate char *s;
9810Sstevel@tonic-gate int len;
9820Sstevel@tonic-gate
9830Sstevel@tonic-gate /* Separator string so we can strcat separator onto things */
9840Sstevel@tonic-gate char sep_str[2] = {COLON_CHAR, '\0'};
9850Sstevel@tonic-gate
9860Sstevel@tonic-gate /* Work out the size */
9870Sstevel@tonic-gate len = strlen(pwd->pw_name) + 1;
9880Sstevel@tonic-gate len += strlen(pwd->pw_passwd) + 1;
9890Sstevel@tonic-gate if (adjunct) {
9900Sstevel@tonic-gate len += strlen(pwd->adjunct_tail) + 1;
9910Sstevel@tonic-gate } else {
9920Sstevel@tonic-gate len += strlen(pwd->pw_uid) + 1;
9930Sstevel@tonic-gate len += strlen(pwd->pw_gid) + 1;
9940Sstevel@tonic-gate len += strlen(pwd->pw_gecos) + 1;
9950Sstevel@tonic-gate len += strlen(pwd->pw_dir) + 1;
9960Sstevel@tonic-gate len += strlen(pwd->pw_shell) + 1;
9970Sstevel@tonic-gate }
9980Sstevel@tonic-gate
9990Sstevel@tonic-gate /* Allocate some memory for it */
10000Sstevel@tonic-gate s = am(myself, len);
10010Sstevel@tonic-gate if (NULL == s)
10020Sstevel@tonic-gate return (NULL);
10030Sstevel@tonic-gate
10040Sstevel@tonic-gate strcpy(s, pwd->pw_name);
10050Sstevel@tonic-gate strcat(s, sep_str);
10060Sstevel@tonic-gate if (!adjunct) {
10070Sstevel@tonic-gate /* Build up a passwd string */
10080Sstevel@tonic-gate
10090Sstevel@tonic-gate /* If LDAP contains adjunct info then passwd is 'x' */
10100Sstevel@tonic-gate if (pwd->adjunct) {
10110Sstevel@tonic-gate strcat(s, "##");
10120Sstevel@tonic-gate strcat(s, pwd->pw_name);
10130Sstevel@tonic-gate } else {
10140Sstevel@tonic-gate strcat(s, pwd->pw_passwd);
10150Sstevel@tonic-gate }
10160Sstevel@tonic-gate strcat(s, sep_str);
10170Sstevel@tonic-gate strcat(s, pwd->pw_uid);
10180Sstevel@tonic-gate strcat(s, sep_str);
10190Sstevel@tonic-gate strcat(s, pwd->pw_gid);
10200Sstevel@tonic-gate strcat(s, sep_str);
10210Sstevel@tonic-gate strcat(s, pwd->pw_gecos);
10220Sstevel@tonic-gate strcat(s, sep_str);
10230Sstevel@tonic-gate strcat(s, pwd->pw_dir);
10240Sstevel@tonic-gate strcat(s, sep_str);
10250Sstevel@tonic-gate strcat(s, pwd->pw_shell);
10260Sstevel@tonic-gate } else {
10270Sstevel@tonic-gate /* Build up a passwd_adjunct string */
10280Sstevel@tonic-gate strcat(s, pwd->pw_passwd);
10290Sstevel@tonic-gate strcat(s, sep_str);
10300Sstevel@tonic-gate strcat(s, pwd->adjunct_tail);
10310Sstevel@tonic-gate }
10320Sstevel@tonic-gate
10330Sstevel@tonic-gate return (s);
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate
10360Sstevel@tonic-gate /*
10370Sstevel@tonic-gate * FUNCTION: get_old_info()
10380Sstevel@tonic-gate *
10390Sstevel@tonic-gate * DESCRIPTION: Gets as much information as possible from LDAP about one user.
10400Sstevel@tonic-gate *
10410Sstevel@tonic-gate * This goes through the mapping system. This is messy because
10420Sstevel@tonic-gate * them mapping system will build up a password entry from the
10430Sstevel@tonic-gate * contents of the DIT. We then have to parse this to recover
10440Sstevel@tonic-gate * it's individual fields.
10450Sstevel@tonic-gate *
10460Sstevel@tonic-gate * INPUT: Pointer to user name
10470Sstevel@tonic-gate * Domain
10480Sstevel@tonic-gate *
10490Sstevel@tonic-gate * OUTPUT: The info in malloced space. To be freed by caller.
10500Sstevel@tonic-gate * NULL on failure.
10510Sstevel@tonic-gate */
10520Sstevel@tonic-gate struct passwd_entry *
get_old_info(char * name,char * domain)10530Sstevel@tonic-gate get_old_info(char *name, char *domain)
10540Sstevel@tonic-gate {
10550Sstevel@tonic-gate char *myself = "get_old_info";
10560Sstevel@tonic-gate struct passwd_entry *old_passwd;
10570Sstevel@tonic-gate char *p;
10580Sstevel@tonic-gate datum key, data;
10590Sstevel@tonic-gate suc_code res;
10600Sstevel@tonic-gate
10610Sstevel@tonic-gate /* Get the password entry */
10620Sstevel@tonic-gate key.dptr = name;
10630Sstevel@tonic-gate key.dsize = strlen(key.dptr);
10640Sstevel@tonic-gate read_from_dit(PASSWD_MAPPING, domain, &key, &data);
10650Sstevel@tonic-gate if (NULL == data.dptr) {
10660Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
10670Sstevel@tonic-gate "Could not read old pwd for %s", name);
10680Sstevel@tonic-gate return (NULL);
10690Sstevel@tonic-gate }
10700Sstevel@tonic-gate
10710Sstevel@tonic-gate /* Pull password apart */
10720Sstevel@tonic-gate old_passwd = am(myself, sizeof (struct passwd_entry));
10730Sstevel@tonic-gate if (NULL == old_passwd) {
10740Sstevel@tonic-gate logmsg(MSG_NOMEM, LOG_ERR, "Could not alloc for pwd decode");
10750Sstevel@tonic-gate sfree(data.dptr);
10760Sstevel@tonic-gate return (NULL);
10770Sstevel@tonic-gate }
10780Sstevel@tonic-gate
10790Sstevel@tonic-gate /* No data yet */
10800Sstevel@tonic-gate old_passwd->pw_name = NULL;
10810Sstevel@tonic-gate old_passwd->pw_passwd = NULL;
10820Sstevel@tonic-gate old_passwd->pw_uid = NULL;
10830Sstevel@tonic-gate old_passwd->pw_gid = NULL;
10840Sstevel@tonic-gate old_passwd->pw_gecos = NULL;
10850Sstevel@tonic-gate old_passwd->pw_dir = NULL;
10860Sstevel@tonic-gate old_passwd->pw_shell = NULL;
10870Sstevel@tonic-gate old_passwd->adjunct_tail = NULL;
10880Sstevel@tonic-gate old_passwd->pwd_str = NULL;
10890Sstevel@tonic-gate old_passwd->adjunct_str = NULL;
10900Sstevel@tonic-gate old_passwd->adjunct = FALSE;
10910Sstevel@tonic-gate
10920Sstevel@tonic-gate res = decode_pwd_entry(&data, old_passwd, FALSE);
10930Sstevel@tonic-gate sfree(data.dptr);
10940Sstevel@tonic-gate if (SUCCESS != res) {
10950Sstevel@tonic-gate free_pwd_entry(old_passwd);
10960Sstevel@tonic-gate return (NULL);
10970Sstevel@tonic-gate }
10980Sstevel@tonic-gate
10990Sstevel@tonic-gate /* Try to get the adjunct entry */
11000Sstevel@tonic-gate read_from_dit(PASSWD_ADJUNCT_MAPPING, domain, &key, &data);
11010Sstevel@tonic-gate if (NULL == data.dptr) {
11020Sstevel@tonic-gate /* Fine just no adjunct data */
11030Sstevel@tonic-gate old_passwd->adjunct = FALSE;
11040Sstevel@tonic-gate } else {
11050Sstevel@tonic-gate res = decode_pwd_entry(&data, old_passwd, TRUE);
11060Sstevel@tonic-gate sfree(data.dptr);
11070Sstevel@tonic-gate if (SUCCESS != res) {
11080Sstevel@tonic-gate free_pwd_entry(old_passwd);
11090Sstevel@tonic-gate return (NULL);
11100Sstevel@tonic-gate }
11110Sstevel@tonic-gate }
11120Sstevel@tonic-gate
11130Sstevel@tonic-gate return (old_passwd);
11140Sstevel@tonic-gate }
11150Sstevel@tonic-gate
11160Sstevel@tonic-gate /*
11170Sstevel@tonic-gate * FUNCTION : put_new_info()
11180Sstevel@tonic-gate *
11190Sstevel@tonic-gate * DESCRIPTION: Generates new map strings and puts them back to LDAP
11200Sstevel@tonic-gate *
11210Sstevel@tonic-gate * INPUTS: Info to put back
11220Sstevel@tonic-gate * Domain
11230Sstevel@tonic-gate *
11240Sstevel@tonic-gate * OUTPUT: Answer code.
11250Sstevel@tonic-gate */
11260Sstevel@tonic-gate int
put_new_info(struct passwd_entry * pwd,char * domain)11270Sstevel@tonic-gate put_new_info(struct passwd_entry *pwd, char *domain)
11280Sstevel@tonic-gate {
11290Sstevel@tonic-gate datum key, data;
11300Sstevel@tonic-gate
11310Sstevel@tonic-gate /* Write it back to LDAP */
11320Sstevel@tonic-gate data.dptr = pwd->pwd_str;
11330Sstevel@tonic-gate data.dsize = strlen(data.dptr);
11340Sstevel@tonic-gate key.dptr = pwd->pw_name;
11350Sstevel@tonic-gate key.dsize = strlen(key.dptr);
11360Sstevel@tonic-gate if (SUCCESS != write_to_dit(PASSWD_MAPPING, domain, key, data,
11370Sstevel@tonic-gate TRUE, FALSE))
11380Sstevel@tonic-gate return (2);
11390Sstevel@tonic-gate
11400Sstevel@tonic-gate
11410Sstevel@tonic-gate /* If DIT contains adjunct information do the same for adjunct */
11420Sstevel@tonic-gate if (pwd->adjunct) {
11430Sstevel@tonic-gate data.dptr = pwd->adjunct_str;
11440Sstevel@tonic-gate data.dsize = strlen(data.dptr);
11450Sstevel@tonic-gate key.dptr = pwd->pw_name;
11460Sstevel@tonic-gate key.dsize = strlen(key.dptr);
11470Sstevel@tonic-gate if (SUCCESS != write_to_dit(PASSWD_ADJUNCT_MAPPING, domain,
11480Sstevel@tonic-gate key, data, TRUE, FALSE))
11490Sstevel@tonic-gate return (2);
11500Sstevel@tonic-gate }
11510Sstevel@tonic-gate
11520Sstevel@tonic-gate return (0);
11530Sstevel@tonic-gate
11540Sstevel@tonic-gate }
11550Sstevel@tonic-gate
11560Sstevel@tonic-gate /*
11570Sstevel@tonic-gate * FUNCTION : get_old_shadow()
11580Sstevel@tonic-gate *
11590Sstevel@tonic-gate * DESCRIPTION :Extracts and decodes shadow information from the DIT
11600Sstevel@tonic-gate * See also comments under decode_pwd_entry().
11610Sstevel@tonic-gate *
11620Sstevel@tonic-gate * INPUTS : User name
11630Sstevel@tonic-gate * Domain name
11640Sstevel@tonic-gate *
11650Sstevel@tonic-gate * OUTPUT : Shadow information in malloced memory. To be freed by caller.
11660Sstevel@tonic-gate */
11670Sstevel@tonic-gate struct spwd *
get_old_shadow(char * name,char * domain)11680Sstevel@tonic-gate get_old_shadow(char *name, char *domain)
11690Sstevel@tonic-gate {
11700Sstevel@tonic-gate char *myself = "get_old_shadow";
11710Sstevel@tonic-gate struct spwd *sp;
11720Sstevel@tonic-gate datum key, data;
11730Sstevel@tonic-gate suc_code res;
11740Sstevel@tonic-gate
11750Sstevel@tonic-gate /* Get the info */
11760Sstevel@tonic-gate key.dptr = name;
11770Sstevel@tonic-gate key.dsize = strlen(key.dptr); /* Len excluding terminator */
11780Sstevel@tonic-gate read_from_dit(AGEING_MAPPING, domain, &key, &data);
11790Sstevel@tonic-gate
11800Sstevel@tonic-gate if (NULL == data.dptr) {
11810Sstevel@tonic-gate /* OK just have no shadow info in DIT */
11820Sstevel@tonic-gate return (NULL);
11830Sstevel@tonic-gate }
11840Sstevel@tonic-gate
11850Sstevel@tonic-gate /* Pull shadow apart */
11860Sstevel@tonic-gate if (NULL == (sp = am(myself, sizeof (struct spwd)))) {
11870Sstevel@tonic-gate logmsg(MSG_NOMEM, LOG_ERR,
11880Sstevel@tonic-gate "Could not alloc for shadow decode");
11890Sstevel@tonic-gate sfree(data.dptr);
11900Sstevel@tonic-gate return (NULL);
11910Sstevel@tonic-gate }
11920Sstevel@tonic-gate sp->sp_namp = NULL;
11930Sstevel@tonic-gate sp->sp_pwdp = NULL;
11940Sstevel@tonic-gate
11950Sstevel@tonic-gate res = decode_shadow_entry(&data, sp);
11960Sstevel@tonic-gate sfree(data.dptr);
11970Sstevel@tonic-gate if (SUCCESS != res) {
11980Sstevel@tonic-gate free_shadow_entry(sp);
11990Sstevel@tonic-gate return (NULL);
12000Sstevel@tonic-gate }
12010Sstevel@tonic-gate
12020Sstevel@tonic-gate return (sp);
12030Sstevel@tonic-gate }
12040Sstevel@tonic-gate
12050Sstevel@tonic-gate /*
12060Sstevel@tonic-gate * FUNCTION : decode_shadow_entry()
12070Sstevel@tonic-gate *
12080Sstevel@tonic-gate * DESCRIPTION: Pulls apart ageing information. For convenience this is stored
12090Sstevel@tonic-gate * in a partially filled spwd structure.
12100Sstevel@tonic-gate *
12110Sstevel@tonic-gate * SEE COMMENTS FOR decode_pwd_entry()
12120Sstevel@tonic-gate */
12130Sstevel@tonic-gate suc_code
decode_shadow_entry(datum * data,struct spwd * sp)12140Sstevel@tonic-gate decode_shadow_entry(datum *data, struct spwd *sp)
12150Sstevel@tonic-gate {
12160Sstevel@tonic-gate char *p, *str_end, *temp;
12170Sstevel@tonic-gate
12180Sstevel@tonic-gate /* Work out last location in string */
12190Sstevel@tonic-gate str_end = data->dptr + data->dsize;
12200Sstevel@tonic-gate
12210Sstevel@tonic-gate /* Name */
12220Sstevel@tonic-gate if (NULL == (p = get_next_token(data->dptr, &(sp->sp_namp), str_end)))
12230Sstevel@tonic-gate return (FAILURE);
12240Sstevel@tonic-gate
12250Sstevel@tonic-gate /* date of last change */
12260Sstevel@tonic-gate if (NULL == (p = get_next_token(p, &temp, str_end)))
12270Sstevel@tonic-gate return (FAILURE);
12280Sstevel@tonic-gate sp->sp_lstchg = atoi(temp);
12290Sstevel@tonic-gate
12300Sstevel@tonic-gate /* min days to passwd change */
12310Sstevel@tonic-gate if (NULL == (p = get_next_token(p, &temp, str_end)))
12320Sstevel@tonic-gate return (FAILURE);
12330Sstevel@tonic-gate sp->sp_min = atoi(temp);
12340Sstevel@tonic-gate
12350Sstevel@tonic-gate /* max days to passwd change */
12360Sstevel@tonic-gate if (NULL == (p = get_next_token(p, &temp, str_end)))
12370Sstevel@tonic-gate return (FAILURE);
12380Sstevel@tonic-gate sp->sp_max = atoi(temp);
12390Sstevel@tonic-gate
12400Sstevel@tonic-gate /* warning period */
12410Sstevel@tonic-gate if (NULL == (p = get_next_token(p, &temp, str_end)))
12420Sstevel@tonic-gate return (FAILURE);
12430Sstevel@tonic-gate sp->sp_warn = atoi(temp);
12440Sstevel@tonic-gate
12450Sstevel@tonic-gate /* max days inactive */
12460Sstevel@tonic-gate if (NULL == (p = get_next_token(p, &temp, str_end)))
12470Sstevel@tonic-gate return (FAILURE);
12480Sstevel@tonic-gate sp->sp_inact = atoi(temp);
12490Sstevel@tonic-gate
12500Sstevel@tonic-gate /* account expiry date */
12510Sstevel@tonic-gate if (NULL == (p = get_next_token(p, &temp, str_end)))
12520Sstevel@tonic-gate return (FAILURE);
12530Sstevel@tonic-gate sp->sp_expire = atoi(temp);
12540Sstevel@tonic-gate
12550Sstevel@tonic-gate /* flag */
12560Sstevel@tonic-gate if (NULL != (p = get_next_token(p, &temp, str_end)))
12570Sstevel@tonic-gate return (FAILURE);
12580Sstevel@tonic-gate sp->sp_flag = atoi(temp);
12590Sstevel@tonic-gate
12600Sstevel@tonic-gate return (SUCCESS);
12610Sstevel@tonic-gate }
12620Sstevel@tonic-gate
12630Sstevel@tonic-gate /*
12640Sstevel@tonic-gate * FUNCTION : write_shadow_info()
12650Sstevel@tonic-gate *
12660Sstevel@tonic-gate * DESCRIPTION: Writes shadow information back to the DIT.
12670Sstevel@tonic-gate *
12680Sstevel@tonic-gate * INPUTS : Domain
12690Sstevel@tonic-gate * Information to write
12700Sstevel@tonic-gate *
12710Sstevel@tonic-gate * OUTPUT : Success code
12720Sstevel@tonic-gate *
12730Sstevel@tonic-gate */
12740Sstevel@tonic-gate suc_code
write_shadow_info(char * domain,struct spwd * sp)12750Sstevel@tonic-gate write_shadow_info(char *domain, struct spwd *sp)
12760Sstevel@tonic-gate {
12770Sstevel@tonic-gate char *myself = "write_shadow_info";
12780Sstevel@tonic-gate datum key, data;
12790Sstevel@tonic-gate char *str;
12800Sstevel@tonic-gate suc_code res;
12810Sstevel@tonic-gate int len;
12820Sstevel@tonic-gate
12830Sstevel@tonic-gate /* Work out how long string will be */
12840Sstevel@tonic-gate len = strlen(sp->sp_namp) + 1;
12850Sstevel@tonic-gate
12860Sstevel@tonic-gate /*
12870Sstevel@tonic-gate * Bit crude but if we assume 1 byte is 3 decimal characters
12880Sstevel@tonic-gate * will get enough buffer for the longs and some spare.
12890Sstevel@tonic-gate */
12900Sstevel@tonic-gate len += 7 * (3 * sizeof (long) + 1);
12910Sstevel@tonic-gate
12920Sstevel@tonic-gate /* Allocate some memory */
12930Sstevel@tonic-gate str = am(myself, len);
12940Sstevel@tonic-gate if (NULL == str) {
12950Sstevel@tonic-gate logmsg(MSG_NOMEM, LOG_ERR, "Could not aloc for shadow write");
12960Sstevel@tonic-gate return (FAILURE);
12970Sstevel@tonic-gate }
12980Sstevel@tonic-gate
12990Sstevel@tonic-gate /* Build up shadow string */
13000Sstevel@tonic-gate sprintf(str, "%s%c%d%c%d%c%d%c%d%c%d%c%d%c%d",
13010Sstevel@tonic-gate sp->sp_namp, COLON_CHAR,
13020Sstevel@tonic-gate sp->sp_lstchg, COLON_CHAR,
13030Sstevel@tonic-gate sp->sp_min, COLON_CHAR,
13040Sstevel@tonic-gate sp->sp_max, COLON_CHAR,
13050Sstevel@tonic-gate sp->sp_warn, COLON_CHAR,
13060Sstevel@tonic-gate sp->sp_inact, COLON_CHAR,
13070Sstevel@tonic-gate sp->sp_expire, COLON_CHAR,
13080Sstevel@tonic-gate sp->sp_flag);
13090Sstevel@tonic-gate
13100Sstevel@tonic-gate /* Write it */
13110Sstevel@tonic-gate data.dptr = str;
13120Sstevel@tonic-gate data.dsize = strlen(data.dptr);
13130Sstevel@tonic-gate key.dptr = sp->sp_namp;
13140Sstevel@tonic-gate key.dsize = strlen(key.dptr);
13150Sstevel@tonic-gate res = write_to_dit(AGEING_MAPPING, domain, key, data, TRUE, FALSE);
13160Sstevel@tonic-gate
13170Sstevel@tonic-gate sfree(str);
13180Sstevel@tonic-gate return (res);
13190Sstevel@tonic-gate }
13200Sstevel@tonic-gate
13210Sstevel@tonic-gate /*
13220Sstevel@tonic-gate * FUNCTION : free_shadow_entry()
13230Sstevel@tonic-gate *
13240Sstevel@tonic-gate * DESCRIPTION: Frees up a shadow information structure
13250Sstevel@tonic-gate *
13260Sstevel@tonic-gate * INPUTS : Structure to free
13270Sstevel@tonic-gate *
13280Sstevel@tonic-gate * OUTPUTS : Nothing
13290Sstevel@tonic-gate */
13300Sstevel@tonic-gate void
free_shadow_entry(struct spwd * spwd)13310Sstevel@tonic-gate free_shadow_entry(struct spwd *spwd)
13320Sstevel@tonic-gate {
13330Sstevel@tonic-gate if (NULL != spwd->sp_namp)
13340Sstevel@tonic-gate sfree(spwd->sp_namp);
13350Sstevel@tonic-gate
13360Sstevel@tonic-gate if (NULL != spwd->sp_pwdp)
13370Sstevel@tonic-gate sfree(spwd->sp_pwdp);
13380Sstevel@tonic-gate
13390Sstevel@tonic-gate /* No need to free numerics */
13400Sstevel@tonic-gate
13410Sstevel@tonic-gate /* Free up structure */
13420Sstevel@tonic-gate sfree(spwd);
13430Sstevel@tonic-gate }
1344