17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5cb5caa98Sdjl * Common Development and Distribution License (the "License").
6cb5caa98Sdjl * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217257d1b4Sraf
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*48bbca81SDaniel Hoffman * Copyright (c) 2016 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287257d1b4Sraf #include "lint.h"
297c478bd9Sstevel@tonic-gate #include <mtlib.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <shadow.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <synch.h>
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate int str2spwd(const char *, int, void *,
397c478bd9Sstevel@tonic-gate char *, int);
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
427c478bd9Sstevel@tonic-gate static DEFINE_NSS_GETENT(context);
437c478bd9Sstevel@tonic-gate
44cb5caa98Sdjl void
_nss_initf_shadow(nss_db_params_t * p)457c478bd9Sstevel@tonic-gate _nss_initf_shadow(nss_db_params_t *p)
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate p->name = NSS_DBNAM_SHADOW;
487c478bd9Sstevel@tonic-gate p->config_name = NSS_DBNAM_PASSWD; /* Use config for "passwd" */
497c478bd9Sstevel@tonic-gate p->default_config = NSS_DEFCONF_PASSWD;
507c478bd9Sstevel@tonic-gate }
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate struct spwd *
getspnam_r(const char * name,struct spwd * result,char * buffer,int buflen)537c478bd9Sstevel@tonic-gate getspnam_r(const char *name, struct spwd *result, char *buffer, int buflen)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate nss_XbyY_args_t arg;
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate NSS_XbyY_INIT(&arg, result, buffer, buflen, str2spwd);
587c478bd9Sstevel@tonic-gate arg.key.name = name;
597257d1b4Sraf (void) nss_search(&db_root, _nss_initf_shadow,
607257d1b4Sraf NSS_DBOP_SHADOW_BYNAME, &arg);
617c478bd9Sstevel@tonic-gate return ((struct spwd *)NSS_XbyY_FINI(&arg));
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate void
setspent(void)657c478bd9Sstevel@tonic-gate setspent(void)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate nss_setent(&db_root, _nss_initf_shadow, &context);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate void
endspent(void)717c478bd9Sstevel@tonic-gate endspent(void)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate nss_endent(&db_root, _nss_initf_shadow, &context);
747c478bd9Sstevel@tonic-gate nss_delete(&db_root);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate struct spwd *
getspent_r(struct spwd * result,char * buffer,int buflen)787c478bd9Sstevel@tonic-gate getspent_r(struct spwd *result, char *buffer, int buflen)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate nss_XbyY_args_t arg;
817c478bd9Sstevel@tonic-gate char *nam;
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate /* In getXXent_r(), protect the unsuspecting caller from +/- entries */
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate do {
867c478bd9Sstevel@tonic-gate NSS_XbyY_INIT(&arg, result, buffer, buflen, str2spwd);
877c478bd9Sstevel@tonic-gate /* No key to fill in */
887c478bd9Sstevel@tonic-gate (void) nss_getent(&db_root, _nss_initf_shadow, &context, &arg);
897c478bd9Sstevel@tonic-gate } while (arg.returnval != 0 &&
907257d1b4Sraf (nam = ((struct spwd *)arg.returnval)->sp_namp) != 0 &&
917257d1b4Sraf (*nam == '+' || *nam == '-'));
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate return (struct spwd *)NSS_XbyY_FINI(&arg);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate struct spwd *
fgetspent_r(FILE * f,struct spwd * result,char * buffer,int buflen)977c478bd9Sstevel@tonic-gate fgetspent_r(FILE *f, struct spwd *result, char *buffer, int buflen)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate extern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t *);
1007c478bd9Sstevel@tonic-gate nss_XbyY_args_t arg;
1017c478bd9Sstevel@tonic-gate
102*48bbca81SDaniel Hoffman /* ... but in fgetXXent_r, the caller deserves any +/- entry it gets */
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate /* No key to fill in */
1057c478bd9Sstevel@tonic-gate NSS_XbyY_INIT(&arg, result, buffer, buflen, str2spwd);
1067c478bd9Sstevel@tonic-gate _nss_XbyY_fgets(f, &arg);
1077c478bd9Sstevel@tonic-gate return (struct spwd *)NSS_XbyY_FINI(&arg);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate typedef const char *constp;
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate static int /* 1 means success and more input, 0 means error or no more */
getfield(constp * nextp,constp limit,int uns,void * valp)1137c478bd9Sstevel@tonic-gate getfield(constp *nextp, constp limit, int uns, void *valp)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate constp p = *nextp;
1167c478bd9Sstevel@tonic-gate char *endfield;
1177c478bd9Sstevel@tonic-gate char numbuf[12]; /* Holds -2^31 and trailing ':' */
1187c478bd9Sstevel@tonic-gate size_t len;
1197c478bd9Sstevel@tonic-gate
120c8bbac97Sbasabi if (p == 0 || p >= limit) {
1217c478bd9Sstevel@tonic-gate return (0);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate if (*p == ':') {
1247c478bd9Sstevel@tonic-gate p++;
1257c478bd9Sstevel@tonic-gate *nextp = p;
1267c478bd9Sstevel@tonic-gate return (p < limit);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate if ((len = limit - p) > sizeof (numbuf) - 1) {
1297c478bd9Sstevel@tonic-gate len = sizeof (numbuf) - 1;
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate * We want to use strtol() and we have a readonly non-zero-terminated
1337c478bd9Sstevel@tonic-gate * string, so first we copy and terminate the interesting bit.
1347c478bd9Sstevel@tonic-gate * Ugh. (It's convenient to terminate with a colon rather than \0).
1357c478bd9Sstevel@tonic-gate */
1367c478bd9Sstevel@tonic-gate if ((endfield = memccpy(numbuf, p, ':', len)) == 0) {
1377c478bd9Sstevel@tonic-gate if (len != limit - p) {
1387c478bd9Sstevel@tonic-gate /* Error -- field is too big to be a legit number */
1397c478bd9Sstevel@tonic-gate return (0);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate numbuf[len] = ':';
1427c478bd9Sstevel@tonic-gate p = limit;
1437c478bd9Sstevel@tonic-gate } else {
1447c478bd9Sstevel@tonic-gate p += (endfield - numbuf);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate if (uns) {
1477c478bd9Sstevel@tonic-gate unsigned long ux = strtoul(numbuf, &endfield, 10);
1487c478bd9Sstevel@tonic-gate if (*endfield != ':') {
1497c478bd9Sstevel@tonic-gate /* Error -- expected <integer><colon> */
1507c478bd9Sstevel@tonic-gate return (0);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate *((unsigned int *)valp) = (unsigned int)ux;
1537c478bd9Sstevel@tonic-gate } else {
1547c478bd9Sstevel@tonic-gate long x = strtol(numbuf, &endfield, 10);
1557c478bd9Sstevel@tonic-gate if (*endfield != ':') {
1567c478bd9Sstevel@tonic-gate /* Error -- expected <integer><colon> */
1577c478bd9Sstevel@tonic-gate return (0);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate *((int *)valp) = (int)x;
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate *nextp = p;
1627c478bd9Sstevel@tonic-gate return (p < limit);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate * str2spwd() -- convert a string to a shadow passwd entry. The parser is
1677c478bd9Sstevel@tonic-gate * more liberal than the passwd or group parsers; since it's legitimate
1687c478bd9Sstevel@tonic-gate * for almost all the fields here to be blank, the parser lets one omit
1697c478bd9Sstevel@tonic-gate * any number of blank fields at the end of the entry. The acceptable
1707c478bd9Sstevel@tonic-gate * forms for '+' and '-' entries are the same as those for normal entries.
1717c478bd9Sstevel@tonic-gate * === Is this likely to do more harm than good?
1727c478bd9Sstevel@tonic-gate *
1737c478bd9Sstevel@tonic-gate * Return values: 0 = success, 1 = parse error, 2 = erange ...
1747c478bd9Sstevel@tonic-gate * The structure pointer passed in is a structure in the caller's space
1757c478bd9Sstevel@tonic-gate * wherein the field pointers would be set to areas in the buffer if
1767c478bd9Sstevel@tonic-gate * need be. instring and buffer should be separate areas.
1777c478bd9Sstevel@tonic-gate */
1787c478bd9Sstevel@tonic-gate int
str2spwd(const char * instr,int lenstr,void * ent,char * buffer,int buflen)1797c478bd9Sstevel@tonic-gate str2spwd(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate struct spwd *shadow = (struct spwd *)ent;
1827c478bd9Sstevel@tonic-gate const char *p = instr, *limit;
1837c478bd9Sstevel@tonic-gate char *bufp;
1847c478bd9Sstevel@tonic-gate int black_magic;
1857c478bd9Sstevel@tonic-gate size_t lencopy;
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate limit = p + lenstr;
1887c478bd9Sstevel@tonic-gate if ((p = memchr(instr, ':', lenstr)) == 0 ||
1897257d1b4Sraf ++p >= limit ||
1907257d1b4Sraf (p = memchr(p, ':', limit - p)) == 0) {
1917c478bd9Sstevel@tonic-gate lencopy = (size_t)lenstr;
1927c478bd9Sstevel@tonic-gate p = 0;
1937c478bd9Sstevel@tonic-gate } else {
1947c478bd9Sstevel@tonic-gate lencopy = p - instr;
1957c478bd9Sstevel@tonic-gate p++;
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate if (lencopy + 1 > buflen) {
1987c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE);
1997c478bd9Sstevel@tonic-gate }
200cb5caa98Sdjl
201cb5caa98Sdjl if (instr != buffer) {
202cb5caa98Sdjl /* Overlapping buffer copies are OK */
203cb5caa98Sdjl (void) memmove(buffer, instr, lencopy);
204cb5caa98Sdjl buffer[lencopy] = 0;
205cb5caa98Sdjl }
206cb5caa98Sdjl
207cb5caa98Sdjl /* quick exit do not entry fill if not needed */
208cb5caa98Sdjl if (ent == (void *)NULL)
209cb5caa98Sdjl return (NSS_STR_PARSE_SUCCESS);
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate black_magic = (*instr == '+' || *instr == '-');
2127c478bd9Sstevel@tonic-gate shadow->sp_namp = bufp = buffer;
2137c478bd9Sstevel@tonic-gate shadow->sp_pwdp = 0;
2147c478bd9Sstevel@tonic-gate shadow->sp_lstchg = -1;
2157c478bd9Sstevel@tonic-gate shadow->sp_min = -1;
2167c478bd9Sstevel@tonic-gate shadow->sp_max = -1;
2177c478bd9Sstevel@tonic-gate shadow->sp_warn = -1;
2187c478bd9Sstevel@tonic-gate shadow->sp_inact = -1;
2197c478bd9Sstevel@tonic-gate shadow->sp_expire = -1;
2207c478bd9Sstevel@tonic-gate shadow->sp_flag = 0;
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate if ((bufp = strchr(bufp, ':')) == 0) {
2237c478bd9Sstevel@tonic-gate if (black_magic)
2247c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS);
2257c478bd9Sstevel@tonic-gate else
2267c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate *bufp++ = '\0';
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate shadow->sp_pwdp = bufp;
2317c478bd9Sstevel@tonic-gate if (instr == 0) {
2327c478bd9Sstevel@tonic-gate if ((bufp = strchr(bufp, ':')) == 0) {
2337c478bd9Sstevel@tonic-gate if (black_magic)
2347c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS);
2357c478bd9Sstevel@tonic-gate else
2367c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate *bufp++ = '\0';
2397c478bd9Sstevel@tonic-gate p = bufp;
2407c478bd9Sstevel@tonic-gate } /* else p was set when we copied name and passwd into the buffer */
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_lstchg))
2437c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS);
2447c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_min))
2457c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS);
2467c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_max))
2477c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS);
2487c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_warn))
2497c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS);
2507c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_inact))
2517c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS);
2527c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_expire))
2537c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS);
2547c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 1, &shadow->sp_flag))
2557c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS);
2567c478bd9Sstevel@tonic-gate if (p != limit) {
2577c478bd9Sstevel@tonic-gate /* Syntax error -- garbage at end of line */
2587c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS);
2617c478bd9Sstevel@tonic-gate }
262