2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include "lint.h"
2N/A#include <mtlib.h>
2N/A#include <sys/types.h>
2N/A#include <shadow.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <nss_dbdefs.h>
2N/A#include <stdio.h>
2N/A#include <synch.h>
2N/A
2N/Aint str2spwd(const char *, int, void *,
2N/A char *, int);
2N/A
2N/Astatic DEFINE_NSS_DB_ROOT(db_root);
2N/Astatic DEFINE_NSS_GETENT(context);
2N/A
2N/Avoid
2N/A_nss_initf_shadow(nss_db_params_t *p)
2N/A{
2N/A p->name = NSS_DBNAM_SHADOW;
2N/A p->config_name = NSS_DBNAM_PASSWD; /* Use config for "passwd" */
2N/A p->default_config = NSS_DEFCONF_PASSWD;
2N/A}
2N/A
2N/Astruct spwd *
2N/Agetspnam_r(const char *name, struct spwd *result, char *buffer, int buflen)
2N/A{
2N/A nss_XbyY_args_t arg;
2N/A
2N/A NSS_XbyY_INIT(&arg, result, buffer, buflen, str2spwd);
2N/A arg.key.name = name;
2N/A (void) nss_search(&db_root, _nss_initf_shadow,
2N/A NSS_DBOP_SHADOW_BYNAME, &arg);
2N/A return ((struct spwd *)NSS_XbyY_FINI(&arg));
2N/A}
2N/A
2N/Avoid
2N/Asetspent(void)
2N/A{
2N/A nss_setent(&db_root, _nss_initf_shadow, &context);
2N/A}
2N/A
2N/Avoid
2N/Aendspent(void)
2N/A{
2N/A nss_endent(&db_root, _nss_initf_shadow, &context);
2N/A nss_delete(&db_root);
2N/A}
2N/A
2N/Astruct spwd *
2N/Agetspent_r(struct spwd *result, char *buffer, int buflen)
2N/A{
2N/A nss_XbyY_args_t arg;
2N/A char *nam;
2N/A
2N/A /* In getXXent_r(), protect the unsuspecting caller from +/- entries */
2N/A
2N/A do {
2N/A NSS_XbyY_INIT(&arg, result, buffer, buflen, str2spwd);
2N/A /* No key to fill in */
2N/A (void) nss_getent(&db_root, _nss_initf_shadow, &context, &arg);
2N/A } while (arg.returnval != 0 &&
2N/A (nam = ((struct spwd *)arg.returnval)->sp_namp) != 0 &&
2N/A (*nam == '+' || *nam == '-'));
2N/A
2N/A return (struct spwd *)NSS_XbyY_FINI(&arg);
2N/A}
2N/A
2N/Astruct spwd *
2N/Afgetspent_r(FILE *f, struct spwd *result, char *buffer, int buflen)
2N/A{
2N/A extern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t *);
2N/A nss_XbyY_args_t arg;
2N/A
2N/A /* ... but in fgetXXent_r, the caller deserves any +/- entry he gets */
2N/A
2N/A /* No key to fill in */
2N/A NSS_XbyY_INIT(&arg, result, buffer, buflen, str2spwd);
2N/A _nss_XbyY_fgets(f, &arg);
2N/A return (struct spwd *)NSS_XbyY_FINI(&arg);
2N/A}
2N/A
2N/Atypedef const char *constp;
2N/A
2N/Astatic int /* 1 means success and more input, 0 means error or no more */
2N/Agetfield(constp *nextp, constp limit, int uns, void *valp)
2N/A{
2N/A constp p = *nextp;
2N/A char *endfield;
2N/A char numbuf[12]; /* Holds -2^31 and trailing ':' */
2N/A size_t len;
2N/A
2N/A if (p == 0 || p >= limit) {
2N/A return (0);
2N/A }
2N/A if (*p == ':') {
2N/A p++;
2N/A *nextp = p;
2N/A return (p < limit);
2N/A }
2N/A if ((len = limit - p) > sizeof (numbuf) - 1) {
2N/A len = sizeof (numbuf) - 1;
2N/A }
2N/A /*
2N/A * We want to use strtol() and we have a readonly non-zero-terminated
2N/A * string, so first we copy and terminate the interesting bit.
2N/A * Ugh. (It's convenient to terminate with a colon rather than \0).
2N/A */
2N/A if ((endfield = memccpy(numbuf, p, ':', len)) == 0) {
2N/A if (len != limit - p) {
2N/A /* Error -- field is too big to be a legit number */
2N/A return (0);
2N/A }
2N/A numbuf[len] = ':';
2N/A p = limit;
2N/A } else {
2N/A p += (endfield - numbuf);
2N/A }
2N/A if (uns) {
2N/A unsigned long ux = strtoul(numbuf, &endfield, 10);
2N/A if (*endfield != ':') {
2N/A /* Error -- expected <integer><colon> */
2N/A return (0);
2N/A }
2N/A *((unsigned int *)valp) = (unsigned int)ux;
2N/A } else {
2N/A long x = strtol(numbuf, &endfield, 10);
2N/A if (*endfield != ':') {
2N/A /* Error -- expected <integer><colon> */
2N/A return (0);
2N/A }
2N/A *((int *)valp) = (int)x;
2N/A }
2N/A *nextp = p;
2N/A return (p < limit);
2N/A}
2N/A
2N/A/*
2N/A * str2spwd() -- convert a string to a shadow passwd entry. The parser is
2N/A * more liberal than the passwd or group parsers; since it's legitimate
2N/A * for almost all the fields here to be blank, the parser lets one omit
2N/A * any number of blank fields at the end of the entry. The acceptable
2N/A * forms for '+' and '-' entries are the same as those for normal entries.
2N/A * === Is this likely to do more harm than good?
2N/A *
2N/A * Return values: 0 = success, 1 = parse error, 2 = erange ...
2N/A * The structure pointer passed in is a structure in the caller's space
2N/A * wherein the field pointers would be set to areas in the buffer if
2N/A * need be. instring and buffer should be separate areas.
2N/A */
2N/Aint
2N/Astr2spwd(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
2N/A{
2N/A struct spwd *shadow = (struct spwd *)ent;
2N/A const char *p = instr, *limit;
2N/A char *bufp;
2N/A int black_magic;
2N/A size_t lencopy;
2N/A
2N/A limit = p + lenstr;
2N/A if ((p = memchr(instr, ':', lenstr)) == 0 ||
2N/A ++p >= limit ||
2N/A (p = memchr(p, ':', limit - p)) == 0) {
2N/A lencopy = (size_t)lenstr;
2N/A p = 0;
2N/A } else {
2N/A lencopy = p - instr;
2N/A p++;
2N/A }
2N/A if (lencopy + 1 > buflen) {
2N/A return (NSS_STR_PARSE_ERANGE);
2N/A }
2N/A
2N/A if (instr != buffer) {
2N/A /* Overlapping buffer copies are OK */
2N/A (void) memmove(buffer, instr, lencopy);
2N/A buffer[lencopy] = 0;
2N/A }
2N/A
2N/A /* quick exit do not entry fill if not needed */
2N/A if (ent == (void *)NULL)
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A
2N/A black_magic = (*instr == '+' || *instr == '-');
2N/A shadow->sp_namp = bufp = buffer;
2N/A shadow->sp_pwdp = 0;
2N/A shadow->sp_lstchg = -1;
2N/A shadow->sp_min = -1;
2N/A shadow->sp_max = -1;
2N/A shadow->sp_warn = -1;
2N/A shadow->sp_inact = -1;
2N/A shadow->sp_expire = -1;
2N/A shadow->sp_flag = 0;
2N/A
2N/A if ((bufp = strchr(bufp, ':')) == 0) {
2N/A if (black_magic)
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A else
2N/A return (NSS_STR_PARSE_PARSE);
2N/A }
2N/A *bufp++ = '\0';
2N/A
2N/A shadow->sp_pwdp = bufp;
2N/A if (instr == 0) {
2N/A if ((bufp = strchr(bufp, ':')) == 0) {
2N/A if (black_magic)
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A else
2N/A return (NSS_STR_PARSE_PARSE);
2N/A }
2N/A *bufp++ = '\0';
2N/A p = bufp;
2N/A } /* else p was set when we copied name and passwd into the buffer */
2N/A
2N/A if (!getfield(&p, limit, 0, &shadow->sp_lstchg))
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A if (!getfield(&p, limit, 0, &shadow->sp_min))
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A if (!getfield(&p, limit, 0, &shadow->sp_max))
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A if (!getfield(&p, limit, 0, &shadow->sp_warn))
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A if (!getfield(&p, limit, 0, &shadow->sp_inact))
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A if (!getfield(&p, limit, 0, &shadow->sp_expire))
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A if (!getfield(&p, limit, 1, &shadow->sp_flag))
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A if (p != limit) {
2N/A /* Syntax error -- garbage at end of line */
2N/A return (NSS_STR_PARSE_PARSE);
2N/A }
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A}