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 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A
2N/A/*
2N/A * This module fetches group and passwd structs for the caller. It
2N/A * uses a hash table to speed up retrieval of repeated entries. If
2N/A * the attempts to initialize the hash tables fail, this just
2N/A * continues the slow way.
2N/A */
2N/A
2N/A#include <pwd.h>
2N/A#include <grp.h>
2N/A#include <string.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <unistd.h>
2N/A#include "pkglib.h"
2N/A#include "pkglocale.h"
2N/A#include "nhash.h"
2N/A
2N/A#define HASHSIZE 151
2N/A#define BSZ 4
2N/A
2N/A#define ERR_DUPFAIL "%s: strdup(%s) failed.\n"
2N/A#define ERR_ADDFAIL "%s: add_cache() failed.\n"
2N/A#define ERR_BADMEMB "%s: %s in \"%s\" %s structure is invalid.\n"
2N/A#define ERR_NOGRP "dup_gr_ent(): no group entry provided.\n"
2N/A#define ERR_NOPWD "dup_pw_ent(): no passwd entry provided.\n"
2N/A#define ERR_NOINIT "%s: init_cache() failed.\n"
2N/A#define ERR_MALLOC "%s: malloc(%d) failed for %s.\n"
2N/A
2N/Astatic Cache *pwnam_cache = (Cache *) NULL;
2N/Astatic Cache *grnam_cache = (Cache *) NULL;
2N/Astatic Cache *pwuid_cache = (Cache *) NULL;
2N/Astatic Cache *grgid_cache = (Cache *) NULL;
2N/A
2N/Astatic int dup_gr_ent(struct group *grp);
2N/Astatic int dup_pw_ent(struct passwd *pwp);
2N/A
2N/A/*
2N/A * These indicate whether the hash table has been initialized for the four
2N/A * categories.
2N/A */
2N/Astatic int is_a_pwnam_cache;
2N/Astatic int is_a_grnam_cache;
2N/Astatic int is_a_pwuid_cache;
2N/Astatic int is_a_grgid_cache;
2N/A
2N/Aextern char *get_install_root(void);
2N/A
2N/A/*
2N/A * If there's a grnam cache, then update it with this new
2N/A * group, otherwise, skip it.
2N/A */
2N/Astatic Item *
2N/Acache_alloc(char *fname, int len, size_t struct_size)
2N/A{
2N/A Item *itemp;
2N/A
2N/A /*
2N/A * Allocate space for the Item pointer, key and data.
2N/A */
2N/A if ((itemp = (Item *) malloc(sizeof (*itemp))) ==
2N/A Null_Item) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_MALLOC), fname,
2N/A sizeof (*itemp), "itemp");
2N/A } else if ((itemp->key = (char *)malloc(len)) == NULL) {
2N/A (void) fprintf(stderr, pkg_gt(ERR_MALLOC), fname, len,
2N/A "itemp->key");
2N/A free(itemp);
2N/A } else if ((itemp->data = malloc(struct_size)) == NULL) {
2N/A (void) fprintf(stderr, pkg_gt(ERR_MALLOC), fname,
2N/A struct_size, "itemp->data");
2N/A free(itemp->key);
2N/A free(itemp);
2N/A } else {
2N/A /* Set length parameters. */
2N/A itemp->keyl = len;
2N/A itemp->datal = struct_size;
2N/A
2N/A return (itemp);
2N/A }
2N/A
2N/A return ((Item *) NULL);
2N/A}
2N/A
2N/A/* Get the required group structure based upon the group name. */
2N/Astruct group *
2N/Acgrnam(char *nam)
2N/A{
2N/A struct group *grp;
2N/A Item *itemp;
2N/A int len;
2N/A static int cache_failed;
2N/A
2N/A /* Attempt to initialize the grname cache. */
2N/A if (!is_a_grnam_cache && !cache_failed) {
2N/A if (init_cache(&grnam_cache, HASHSIZE, BSZ,
2N/A (int (*)())NULL, (int (*)())NULL) == -1) {
2N/A (void) fprintf(stderr, pkg_gt(ERR_NOINIT), "cgrnam()");
2N/A grnam_cache = (Cache *) NULL;
2N/A cache_failed = 1;
2N/A } else
2N/A is_a_grnam_cache = 1;
2N/A }
2N/A
2N/A len = strlen(nam) + 1;
2N/A
2N/A /* First look in the cache. Failing that, do it the hard way. */
2N/A if ((itemp = lookup_cache(grnam_cache, nam, len)) == Null_Item) {
2N/A
2N/A /* Get the group by name. */
2N/A if ((grp = clgrnam(nam)) != NULL ||
2N/A (grp = getgrnam(nam)) != NULL) {
2N/A /* A group by that name exists on this machine. */
2N/A if (dup_gr_ent(grp))
2N/A /*
2N/A * Effectively no such group since struct
2N/A * couldn't be duplicated.
2N/A */
2N/A grp = (struct group *)NULL;
2N/A /*
2N/A * If there's a grnam cache, then update it with this
2N/A * new group, otherwise, skip it.
2N/A */
2N/A else if (is_a_grnam_cache) {
2N/A if ((itemp = cache_alloc("cgrnam()", len,
2N/A sizeof (struct group))) != Null_Item) {
2N/A /*
2N/A * With that allocated, insert the
2N/A * group name as key and set the key
2N/A * length.
2N/A */
2N/A (void) memmove(itemp->key, nam, len);
2N/A
2N/A /*
2N/A * Insert the data associated with
2N/A * the key and the data length.
2N/A */
2N/A (void) memmove(itemp->data, grp,
2N/A sizeof (struct group));
2N/A
2N/A /* Insert the Item into the cache. */
2N/A if (add_cache(grnam_cache, itemp) == -1)
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_ADDFAIL),
2N/A "cgrnam()");
2N/A }
2N/A }
2N/A }
2N/A return (grp);
2N/A } else /* Found it in the cache. */
2N/A return ((struct group *)itemp->data);
2N/A}
2N/A
2N/Astruct passwd *
2N/Acpwnam(char *nam)
2N/A{
2N/A struct passwd *pwd;
2N/A Item *itemp;
2N/A int len;
2N/A static int cache_failed;
2N/A
2N/A if (!is_a_pwnam_cache && !cache_failed) {
2N/A if (init_cache(&pwnam_cache, HASHSIZE, BSZ,
2N/A (int (*)())NULL, (int (*)())NULL) == -1) {
2N/A (void) fprintf(stderr, pkg_gt(ERR_NOINIT), "cpwnam()");
2N/A pwnam_cache = (Cache *) NULL;
2N/A cache_failed = 1;
2N/A } else
2N/A is_a_pwnam_cache = 1;
2N/A }
2N/A
2N/A len = strlen(nam) + 1;
2N/A
2N/A /* First look in the cache. Failing that, do it the hard way. */
2N/A if ((itemp = lookup_cache(pwnam_cache, nam, len)) == Null_Item) {
2N/A
2N/A /* Get the passwd by name. */
2N/A if ((pwd = clpwnam(nam)) != NULL ||
2N/A (pwd = getpwnam(nam)) != NULL) {
2N/A /* A passwd by that name exists on this machine. */
2N/A if (dup_pw_ent(pwd))
2N/A /*
2N/A * Effectively no such passwd since struct
2N/A * couldn't be duplicated.
2N/A */
2N/A pwd = (struct passwd *)NULL;
2N/A /*
2N/A * If there's a pwnam cache, then update it with this
2N/A * new passwd, otherwise, skip it.
2N/A */
2N/A else if (is_a_pwnam_cache) {
2N/A /*
2N/A * Allocate space for the Item pointer, key
2N/A * and data.
2N/A */
2N/A if ((itemp = cache_alloc("cpwnam()", len,
2N/A sizeof (struct passwd))) != Null_Item) {
2N/A /*
2N/A * With that allocated, insert the
2N/A * group name as key and set the key
2N/A * length.
2N/A */
2N/A (void) memmove(itemp->key, nam, len);
2N/A
2N/A /*
2N/A * Insert the data associated with
2N/A * the key and the data length.
2N/A */
2N/A (void) memmove(itemp->data, pwd,
2N/A sizeof (struct passwd));
2N/A
2N/A if (add_cache(pwnam_cache, itemp) == -1)
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_ADDFAIL),
2N/A "cpwnam()");
2N/A }
2N/A }
2N/A }
2N/A return (pwd);
2N/A } else /* Found it in the cache. */
2N/A return ((struct passwd *)itemp->data);
2N/A}
2N/A
2N/Astatic int
2N/Auid_hash(void *datap, int datalen, int hsz)
2N/A{
2N/A#ifdef lint
2N/A int i = datalen;
2N/A datalen = i;
2N/A#endif /* lint */
2N/A
2N/A return (*((uid_t *)datap) % hsz);
2N/A}
2N/A
2N/Astatic int
2N/Auid_comp(void *datap1, void *datap2, int datalen)
2N/A{
2N/A#ifdef lint
2N/A int i = datalen;
2N/A datalen = i;
2N/A#endif /* lint */
2N/A
2N/A return (*((uid_t *)datap1) - *((uid_t *)datap2));
2N/A}
2N/A
2N/Astruct group *
2N/Acgrgid(gid_t gid)
2N/A{
2N/A struct group *grp;
2N/A Item *itemp;
2N/A int len;
2N/A static int cache_failed;
2N/A
2N/A if (!is_a_grgid_cache && !cache_failed) {
2N/A if (init_cache(&grgid_cache, HASHSIZE, BSZ,
2N/A uid_hash, uid_comp) == -1) {
2N/A (void) fprintf(stderr, pkg_gt(ERR_NOINIT), "cgrgid()");
2N/A grgid_cache = (Cache *) NULL;
2N/A cache_failed = 1;
2N/A } else
2N/A is_a_grgid_cache = 1;
2N/A }
2N/A
2N/A len = sizeof (uid_t);
2N/A
2N/A /* First look in the cache. Failing that, do it the hard way. */
2N/A if ((itemp = lookup_cache(grgid_cache, &gid, len)) == Null_Item) {
2N/A if ((grp = clgrgid(gid)) != NULL ||
2N/A (grp = getgrgid(gid)) != NULL) {
2N/A /* A group by that number exists on this machine. */
2N/A if (dup_gr_ent(grp))
2N/A /*
2N/A * Effectively no such group since struct
2N/A * couldn't be duplicated.
2N/A */
2N/A grp = (struct group *)NULL;
2N/A /*
2N/A * If there's a grnam cache, then update it with this
2N/A * new group, otherwise, skip it.
2N/A */
2N/A else if (is_a_grgid_cache) {
2N/A if ((itemp = cache_alloc("cgrgid()", len,
2N/A sizeof (struct group))) != Null_Item) {
2N/A /*
2N/A * With that allocated, insert the
2N/A * group name as key and set the key
2N/A * length.
2N/A */
2N/A (void) memmove(itemp->key, &gid, len);
2N/A
2N/A /*
2N/A * Insert the data associated with
2N/A * the key and the data length.
2N/A */
2N/A (void) memmove(itemp->data, grp,
2N/A sizeof (struct group));
2N/A
2N/A if (add_cache(grgid_cache, itemp) == -1)
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_ADDFAIL),
2N/A "cgrgid()");
2N/A }
2N/A }
2N/A }
2N/A return (grp);
2N/A } else /* Found it in the cache. */
2N/A return ((struct group *)itemp->data);
2N/A}
2N/A
2N/Astruct passwd *
2N/Acpwuid(uid_t uid)
2N/A{
2N/A struct passwd *pwd;
2N/A Item *itemp;
2N/A int len;
2N/A static int cache_failed;
2N/A
2N/A if (!is_a_pwuid_cache && !cache_failed) {
2N/A if (init_cache(&pwuid_cache, HASHSIZE, BSZ,
2N/A uid_hash, uid_comp) == -1) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_NOINIT), "cpwuid()");
2N/A pwuid_cache = (Cache *) NULL;
2N/A cache_failed = 1;
2N/A } else
2N/A is_a_pwuid_cache = 1;
2N/A }
2N/A
2N/A len = sizeof (uid_t);
2N/A
2N/A /* First look in the cache. Failing that, do it the hard way. */
2N/A if ((itemp = lookup_cache(pwuid_cache, &uid, len)) == Null_Item) {
2N/A
2N/A /* Get the passwd by number. */
2N/A if ((pwd = clpwuid(uid)) != NULL ||
2N/A (pwd = getpwuid(uid)) != NULL) {
2N/A /* A passwd by that user ID exists on this machine. */
2N/A if (dup_pw_ent(pwd))
2N/A /*
2N/A * Effectively no such passwd since struct
2N/A * couldn't be duplicated.
2N/A */
2N/A pwd = (struct passwd *)NULL;
2N/A /*
2N/A * If there's a pwuid cache, then update it with this
2N/A * new passwd, otherwise, skip it.
2N/A */
2N/A else if (is_a_pwuid_cache) {
2N/A if ((itemp = cache_alloc("cpwuid()", len,
2N/A sizeof (struct passwd))) != Null_Item) {
2N/A /*
2N/A * With that allocated, insert the
2N/A * group name as key and set the key
2N/A * length.
2N/A */
2N/A (void) memmove(itemp->key, &uid, len);
2N/A
2N/A /*
2N/A * Insert the data associated with
2N/A * the key and the data length.
2N/A */
2N/A (void) memmove(itemp->data, pwd,
2N/A sizeof (struct passwd));
2N/A
2N/A if (add_cache(pwuid_cache, itemp) == -1)
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_ADDFAIL),
2N/A "cpwuid()");
2N/A }
2N/A }
2N/A }
2N/A return (pwd);
2N/A } else /* Found it in the cache. */
2N/A return ((struct passwd *)itemp->data);
2N/A}
2N/A
2N/A/*
2N/A * This function duplicates the group structure provided from kernel static
2N/A * memory. There is a lot of defensive coding here because there have been
2N/A * problems with the getgr*() functions. They will sometimes provide NULL
2N/A * values instead of pointers to NULL values. There has been no explanation
2N/A * for the reason behind this; but, this function takes a NULL to be an
2N/A * invalid (char *) and returns an error.
2N/A */
2N/Astatic int
2N/Adup_gr_ent(struct group *grp)
2N/A{
2N/A char **tp = NULL;
2N/A char **memp = NULL;
2N/A int nent = 0; /* Number of entries in the member list. */
2N/A
2N/A if (grp) {
2N/A if (grp->gr_name == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_BADMEMB), "dup_gr_ent()", "gr_name",
2N/A "unknown", "group");
2N/A return (-1);
2N/A } else if ((grp->gr_name = strdup(grp->gr_name)) == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_DUPFAIL), "dup_gr_ent()", "gr_name");
2N/A return (-1);
2N/A }
2N/A if (grp->gr_passwd == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_BADMEMB), "dup_gr_ent()", "gr_passwd",
2N/A grp->gr_name, "group");
2N/A return (-1);
2N/A } else if ((grp->gr_passwd = strdup(grp->gr_passwd)) == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_DUPFAIL), "dup_gr_ent()", "gr_passwd");
2N/A return (-1);
2N/A }
2N/A /*
2N/A * Allocate space for the member list and move the members
2N/A * into it.
2N/A */
2N/A if (grp->gr_mem) {
2N/A /*
2N/A * First count the members. The nent variable will be
2N/A * the number of members + 1 for the terminator.
2N/A */
2N/A for (tp = grp->gr_mem; *tp; nent++, tp++);
2N/A
2N/A /* Now allocate room for the pointers. */
2N/A memp = malloc(sizeof (char **)* (nent+1));
2N/A
2N/A if (memp == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_MALLOC), "dup_gr_ent()",
2N/A (sizeof (char **)* (nent+1)),
2N/A "memp");
2N/A return (-1);
2N/A }
2N/A
2N/A /*
2N/A * Now copy over the pointers and entries. It should
2N/A * be noted that if the structure is messed up here,
2N/A * the resulting member list will be truncated at the
2N/A * NULL entry.
2N/A */
2N/A for (nent = 0, tp = grp->gr_mem; *tp; tp++) {
2N/A if ((memp[nent++] = strdup(*tp)) == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_DUPFAIL), "dup_gr_ent()",
2N/A "gr_mem");
2N/A return (-1);
2N/A }
2N/A }
2N/A } else {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_BADMEMB), "dup_gr_ent()", "gr_mem",
2N/A grp->gr_name, "group");
2N/A return (-1);
2N/A }
2N/A } else {
2N/A (void) fprintf(stderr, pkg_gt(ERR_NOGRP));
2N/A return (-1);
2N/A }
2N/A memp[nent++] = '\0';
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * This function duplicates the passwd structure provided from kernel static
2N/A * memory. As in the above function, since there have been problems with the
2N/A * getpw*() functions, the structure provided is rigorously scrubbed. This
2N/A * function takes a NULL to be an invalid (char *) and returns an error if
2N/A * one is detected.
2N/A */
2N/Astatic int
2N/Adup_pw_ent(struct passwd *pwd)
2N/A{
2N/A if (pwd) {
2N/A if (pwd->pw_name == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_BADMEMB), "dup_pw_ent()", "pw_name",
2N/A "unknown", "passwd");
2N/A return (-1);
2N/A } else if ((pwd->pw_name = strdup(pwd->pw_name)) == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_DUPFAIL), "dup_pw_ent()", "pw_name");
2N/A return (-1);
2N/A }
2N/A
2N/A if (pwd->pw_passwd == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_BADMEMB), "dup_pw_ent()", "pw_passwd",
2N/A pwd->pw_name, "passwd");
2N/A return (-1);
2N/A } else if ((pwd->pw_passwd = strdup(pwd->pw_passwd)) == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_DUPFAIL), "dup_pw_ent()", "pw_passwd");
2N/A return (-1);
2N/A }
2N/A
2N/A if (pwd->pw_age == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_BADMEMB), "dup_pw_ent()", "pw_age",
2N/A pwd->pw_name, "passwd");
2N/A return (-1);
2N/A } else if ((pwd->pw_age = strdup(pwd->pw_age)) == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_DUPFAIL), "dup_pw_ent()", "pw_age");
2N/A return (-1);
2N/A }
2N/A
2N/A if (pwd->pw_comment == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_BADMEMB), "dup_pw_ent()", "pw_comment",
2N/A pwd->pw_name, "passwd");
2N/A return (-1);
2N/A } else if ((pwd->pw_comment = strdup(pwd->pw_comment)) ==
2N/A NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_DUPFAIL), "dup_pw_ent()", "pw_comment");
2N/A return (-1);
2N/A }
2N/A
2N/A if (pwd->pw_gecos == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_BADMEMB), "dup_pw_ent()", "pw_gecos",
2N/A pwd->pw_name, "passwd");
2N/A return (-1);
2N/A } else if ((pwd->pw_gecos = strdup(pwd->pw_gecos)) == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_DUPFAIL), "dup_pw_ent()", "pw_gecos");
2N/A return (-1);
2N/A }
2N/A
2N/A if (pwd->pw_dir == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_BADMEMB), "dup_pw_ent()", "pw_dir",
2N/A pwd->pw_name, "passwd");
2N/A return (-1);
2N/A } else if ((pwd->pw_dir = strdup(pwd->pw_dir)) == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_DUPFAIL), "dup_pw_ent()", "pw_dir");
2N/A return (-1);
2N/A }
2N/A
2N/A if (pwd->pw_shell == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_BADMEMB), "dup_pw_ent()", "pw_shell",
2N/A pwd->pw_name, "passwd");
2N/A return (-1);
2N/A } else if ((pwd->pw_shell = strdup(pwd->pw_shell)) == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_DUPFAIL), "dup_pw_ent()", "pw_shell");
2N/A return (-1);
2N/A }
2N/A } else {
2N/A (void) fprintf(stderr, pkg_gt(ERR_NOPWD));
2N/A return (-1);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Check the client's etc/group file for the group name
2N/A *
2N/A * returns a pointer to the group structure if the group is found
2N/A * returns NULL if not found
2N/A */
2N/Astruct group *
2N/Aclgrnam(char *nam)
2N/A{
2N/A struct group *gr;
2N/A char *instroot, *buf;
2N/A FILE *gr_ptr;
2N/A
2N/A if ((instroot = get_install_root()) != NULL) {
2N/A if ((buf = (char *)malloc(strlen(instroot) +
2N/A strlen(GROUP) + 1)) == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_MALLOC), "clgrnam()",
2N/A strlen(instroot) + strlen(GROUP), "buf");
2N/A }
2N/A (void) sprintf(buf, "%s%s", instroot, GROUP);
2N/A if ((gr_ptr = fopen(buf, "r")) == NULL) {
2N/A free(buf);
2N/A return (NULL);
2N/A } else {
2N/A while ((gr = fgetgrent(gr_ptr)) != NULL) {
2N/A if (strcmp(gr->gr_name, nam) == 0) {
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A free(buf);
2N/A (void) fclose(gr_ptr);
2N/A return (gr);
2N/A } else {
2N/A return (NULL);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Check the client's etc/passwd file for the user name
2N/A *
2N/A * returns a pointer to the passwd structure if the passwd is found
2N/A * returns NULL if not found
2N/A */
2N/Astruct passwd *
2N/Aclpwnam(char *nam)
2N/A{
2N/A struct passwd *pw;
2N/A char *instroot, *buf;
2N/A FILE *pw_ptr;
2N/A
2N/A if ((instroot = get_install_root()) != NULL) {
2N/A if ((buf = (char *)malloc(strlen(instroot) +
2N/A strlen(PASSWD) + 1)) == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_MALLOC), "clpwnam()",
2N/A strlen(instroot) + strlen(PASSWD), "buf");
2N/A }
2N/A (void) sprintf(buf, "%s%s", instroot, PASSWD);
2N/A if ((pw_ptr = fopen(buf, "r")) == NULL) {
2N/A free(buf);
2N/A return (NULL);
2N/A } else {
2N/A while ((pw = fgetpwent(pw_ptr)) != NULL) {
2N/A if (strcmp(pw->pw_name, nam) == 0) {
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A free(buf);
2N/A (void) fclose(pw_ptr);
2N/A return (pw);
2N/A } else {
2N/A return (NULL);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Check the client's etc/group file for the group id
2N/A *
2N/A * returns a pointer to the group structure if the group id is found
2N/A * returns NULL if not found
2N/A */
2N/Astruct group *
2N/Aclgrgid(gid_t gid)
2N/A{
2N/A struct group *gr;
2N/A char *instroot, *buf;
2N/A FILE *gr_ptr;
2N/A
2N/A if ((instroot = get_install_root()) != NULL) {
2N/A if ((buf = (char *)malloc(strlen(instroot) +
2N/A strlen(GROUP) + 1)) == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_MALLOC), "clgrgid()",
2N/A strlen(instroot) + strlen(GROUP), "buf");
2N/A }
2N/A (void) sprintf(buf, "%s%s", instroot, GROUP);
2N/A if ((gr_ptr = fopen(buf, "r")) == NULL) {
2N/A free(buf);
2N/A return (NULL);
2N/A } else {
2N/A while ((gr = fgetgrent(gr_ptr)) != NULL) {
2N/A if (gr->gr_gid == gid) {
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A free(buf);
2N/A (void) fclose(gr_ptr);
2N/A return (gr);
2N/A } else {
2N/A return (NULL);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Check the client's etc/passwd file for the user id
2N/A *
2N/A * returns a pointer to the passwd structure if the user id is found
2N/A * returns NULL if not found
2N/A */
2N/Astruct passwd *
2N/Aclpwuid(uid_t uid)
2N/A{
2N/A struct passwd *pw;
2N/A char *instroot, *buf;
2N/A FILE *pw_ptr;
2N/A
2N/A if ((instroot = get_install_root()) != NULL) {
2N/A if ((buf = (char *)malloc(strlen(instroot) +
2N/A strlen(PASSWD) + 1)) == NULL) {
2N/A (void) fprintf(stderr,
2N/A pkg_gt(ERR_MALLOC), "clpwuid()",
2N/A strlen(instroot) + strlen(PASSWD), "buf");
2N/A }
2N/A (void) sprintf(buf, "%s%s", instroot, PASSWD);
2N/A if ((pw_ptr = fopen(buf, "r")) == NULL) {
2N/A free(buf);
2N/A return (NULL);
2N/A } else {
2N/A while ((pw = fgetpwent(pw_ptr)) != NULL) {
2N/A if (pw->pw_uid == uid) {
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A free(buf);
2N/A (void) fclose(pw_ptr);
2N/A return (pw);
2N/A } else {
2N/A return (NULL);
2N/A }
2N/A}