xref: /titanic_44/usr/src/lib/nsswitch/compat/common/getspent.c (revision 45916cd2fec6e79bca5dee0421bd39e3c2910d1e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *	getspent.c
24  *
25  *	Copyright (c) 1988-1992 Sun Microsystems Inc
26  *	All Rights Reserved.
27  *
28  * lib/nsswitch/compat/getspent.c -- name-service-switch backend for getspnam()
29  *   It looks in /etc/shadow; if it finds shadow entries there that begin
30  *   with "+" or "-", it consults other services.  By default it uses NIS (YP),
31  *   but the user can override this with a "passwd_compat" entry in
32  *   /etc/nsswitch.conf, e.g.
33  *			passwd_compat: nisplus
34  * The main criterion for this code is that it behave in the same way as
35  * the code for getpwnam() and friends (in getpwent.c).  Note that it uses
36  * the same nsswitch.conf entry, not a separate entry for "shadow_compat".
37  *
38  * Caveats:
39  *    -	More than one source may be specified, with the usual switch semantics,
40  *	but having multiple sources here is definitely odd.
41  *    -	People who recursively specify "compat" deserve what they get.
42  *    -	Entries that begin with "+@" or "-@" are interpreted using
43  *	getnetgrent() and innetgr(), which use the "netgroup" entry in
44  *	/etc/nsswitch.conf.  If the sources for "passwd_compat" and "netgroup"
45  *	differ, everything should work fine, but the semantics will be pretty
46  *	confusing.
47  */
48 
49 #pragma ident	"%Z%%M%	%I%	%E% SMI"
50 
51 #include <shadow.h>
52 #include <string.h>
53 #include <stdlib.h>
54 #include "compat_common.h"
55 
56 static DEFINE_NSS_DB_ROOT(db_root);
57 
58 void
59 _nss_initf_shadow_compat(p)
60 	nss_db_params_t	*p;
61 {
62 	p->name		  = NSS_DBNAM_SHADOW;
63 	p->config_name	  = NSS_DBNAM_PASSWD_COMPAT;
64 	p->default_config = NSS_DEFCONF_PASSWD_COMPAT;
65 }
66 
67 static const char *
68 get_spnamp(argp)
69 	nss_XbyY_args_t		*argp;
70 {
71 	struct spwd		*s = (struct spwd *)argp->returnval;
72 
73 	return (s->sp_namp);
74 }
75 
76 static int
77 check_spnamp(argp)
78 	nss_XbyY_args_t		*argp;
79 {
80 	struct spwd		*s = (struct spwd *)argp->returnval;
81 
82 	return (strcmp(s->sp_namp, argp->key.name) == 0);
83 }
84 
85 static nss_status_t
86 getbyname(be, a)
87 	compat_backend_ptr_t	be;
88 	void			*a;
89 {
90 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *) a;
91 
92 	return (_nss_compat_XY_all(be, argp, check_spnamp,
93 				NSS_DBOP_SHADOW_BYNAME));
94 }
95 
96 /*ARGSUSED*/
97 static int
98 merge_spents(be, argp, fields)
99 	compat_backend_ptr_t	be;
100 	nss_XbyY_args_t		*argp;
101 	const char		**fields;
102 {
103 	struct spwd		*sp	= (struct spwd *)argp->buf.result;
104 
105 	/*
106 	 * Don't allow overriding of the username;  apart from that,
107 	 *   anything is fair game.
108 	 */
109 
110 	if (fields[1] != 0) {
111 		size_t	namelen = strlen(sp->sp_namp) + 1;
112 		size_t	passlen = strlen(fields[1])   + 1;
113 
114 		/* ===> Probably merits an explanation... */
115 		if (namelen + passlen > argp->buf.buflen) {
116 			return (NSS_STR_PARSE_ERANGE);
117 		}
118 		if (sp->sp_namp != argp->buf.buffer) {
119 			memmove(argp->buf.buffer, sp->sp_namp, namelen);
120 			sp->sp_namp = argp->buf.buffer;
121 		}
122 		memcpy(argp->buf.buffer + namelen, fields[1], passlen);
123 	}
124 
125 #define	override(field, longp)				\
126 	if ((field) != 0) {				\
127 		char	*end;				\
128 		long	val = strtol(field, &end, 10);	\
129 							\
130 		if (*end == '\0') {			\
131 			*(longp) = val;			\
132 		} else {				\
133 			return (NSS_STR_PARSE_PARSE);	\
134 		}					\
135 	}
136 
137 	/* do not override last changed date, it never gets reset. */
138 	/* override(fields[2], &sp->sp_lstchg); */
139 	override(fields[3], &sp->sp_min);
140 	override(fields[4], &sp->sp_max);
141 	override(fields[5], &sp->sp_warn);
142 	override(fields[6], &sp->sp_inact);
143 	override(fields[7], &sp->sp_expire);
144 	override(fields[8], &sp->sp_flag);
145 	return (NSS_STR_PARSE_SUCCESS);
146 }
147 
148 static compat_backend_op_t shadow_ops[] = {
149 	_nss_compat_destr,
150 	_nss_compat_endent,
151 	_nss_compat_setent,
152 	_nss_compat_getent,
153 	getbyname
154 };
155 
156 /*ARGSUSED*/
157 nss_backend_t *
158 _nss_compat_shadow_constr(dummy1, dummy2, dummy3)
159 	const char	*dummy1, *dummy2, *dummy3;
160 {
161 	return (_nss_compat_constr(shadow_ops,
162 				sizeof (shadow_ops) / sizeof (shadow_ops[0]),
163 				SHADOW,
164 				NSS_LINELEN_SHADOW,
165 				&db_root,
166 				_nss_initf_shadow_compat,
167 				1,
168 				get_spnamp,
169 				merge_spents));
170 }
171