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 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A *
2N/A * nis/getpwnam.c -- "nis" backend for nsswitch "passwd" database
2N/A */
2N/A
2N/A#include <pwd.h>
2N/A#include "nis_common.h"
2N/A
2N/Astatic nss_status_t
2N/Agetbyname(be, a)
2N/A nis_backend_ptr_t be;
2N/A void *a;
2N/A{
2N/A nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
2N/A
2N/A return (_nss_nis_lookup(be, argp, 0,
2N/A "passwd.byname", argp->key.name, 0));
2N/A}
2N/A
2N/Astatic nss_status_t
2N/Agetbyuid(be, a)
2N/A nis_backend_ptr_t be;
2N/A void *a;
2N/A{
2N/A nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
2N/A char uidstr[12]; /* More than enough */
2N/A
2N/A if (argp->key.uid > MAXUID)
2N/A return (NSS_NOTFOUND);
2N/A (void) snprintf(uidstr, 12, "%u", argp->key.uid);
2N/A return (_nss_nis_lookup(be, argp, 0, "passwd.byuid", uidstr, 0));
2N/A}
2N/A
2N/A/*
2N/A * Validates passwd entry replacing uid/gid > MAXUID by ID_NOBODY.
2N/A */
2N/Aint
2N/Avalidate_passwd_ids(char **linepp, int *linelenp, int allocbuf)
2N/A{
2N/A char *linep, *limit, *uidp, *gidp, *newline;
2N/A uid_t uid;
2N/A gid_t gid;
2N/A ulong_t uidl, gidl;
2N/A int olduidlen, oldgidlen, idlen;
2N/A int linelen = *linelenp, newlinelen;
2N/A
2N/A linep = *linepp;
2N/A limit = linep + linelen;
2N/A
2N/A /* +/- entries valid for compat source only */
2N/A if (linelen == 0 || *linep == '+' || *linep == '-')
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A
2N/A while (linep < limit && *linep++ != ':') /* skip username */
2N/A continue;
2N/A while (linep < limit && *linep++ != ':') /* skip password */
2N/A continue;
2N/A if (linep == limit)
2N/A return (NSS_STR_PARSE_PARSE);
2N/A
2N/A uidp = linep;
2N/A uidl = strtoul(uidp, (char **)&linep, 10); /* grab uid */
2N/A olduidlen = linep - uidp;
2N/A if (++linep >= limit || olduidlen == 0)
2N/A return (NSS_STR_PARSE_PARSE);
2N/A
2N/A gidp = linep;
2N/A gidl = strtoul(gidp, (char **)&linep, 10); /* grab gid */
2N/A oldgidlen = linep - gidp;
2N/A if (linep >= limit || oldgidlen == 0)
2N/A return (NSS_STR_PARSE_PARSE);
2N/A
2N/A if (uidl <= MAXUID && gidl <= MAXUID)
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A uid = (uidl > MAXUID) ? UID_NOBODY : (uid_t)uidl;
2N/A gid = (gidl > MAXUID) ? GID_NOBODY : (gid_t)gidl;
2N/A
2N/A /* Check if we have enough space in the buffer */
2N/A idlen = snprintf(NULL, 0, "%u:%u", uid, gid);
2N/A newlinelen = linelen + idlen - olduidlen - oldgidlen - 1;
2N/A if (newlinelen > linelen) {
2N/A /* need a larger buffer */
2N/A if (!allocbuf || (newline = malloc(newlinelen + 1)) == NULL)
2N/A return (NSS_STR_PARSE_ERANGE);
2N/A /* Replace ephemeral ids by ID_NOBODY in the new buffer */
2N/A *(uidp - 1) = '\0';
2N/A (void) snprintf(newline, newlinelen + 1, "%s:%u:%u%s",
2N/A *linepp, uid, gid, linep);
2N/A free(*linepp);
2N/A *linepp = newline;
2N/A *linelenp = newlinelen;
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A }
2N/A
2N/A /* Replace ephemeral ids by ID_NOBODY in the same buffer */
2N/A (void) bcopy(linep, uidp + idlen, limit - linep + 1);
2N/A (void) snprintf(uidp, idlen + 1, "%u:%u", uid, gid);
2N/A *(uidp + idlen) = ':'; /* restore : that was overwritten by snprintf */
2N/A *linelenp = newlinelen;
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A}
2N/A
2N/Astatic nis_backend_op_t passwd_ops[] = {
2N/A _nss_nis_destr,
2N/A _nss_nis_endent,
2N/A _nss_nis_setent,
2N/A _nss_nis_getent_rigid,
2N/A getbyname,
2N/A getbyuid
2N/A};
2N/A
2N/A/*ARGSUSED*/
2N/Anss_backend_t *
2N/A_nss_nis_passwd_constr(dummy1, dummy2, dummy3)
2N/A const char *dummy1, *dummy2, dummy3;
2N/A{
2N/A return (_nss_nis_constr(passwd_ops,
2N/A sizeof (passwd_ops) / sizeof (passwd_ops[0]),
2N/A "passwd.byname"));
2N/A}