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
5*f48205beScasper * Common Development and Distribution License (the "License").
6*f48205beScasper * 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 */
217c478bd9Sstevel@tonic-gate /*
22*f48205beScasper * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
267c478bd9Sstevel@tonic-gate /* All Rights Reserved */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * logins.c
307c478bd9Sstevel@tonic-gate *
317c478bd9Sstevel@tonic-gate * This file contains the source for the administrative command
327c478bd9Sstevel@tonic-gate * "logins" (available to the administrator) that produces a report
337c478bd9Sstevel@tonic-gate * containing login-IDs and other requested information.
347c478bd9Sstevel@tonic-gate */
357c478bd9Sstevel@tonic-gate
3620d7339fSgww #include <sys/types.h>
3720d7339fSgww #include <stdio.h>
3820d7339fSgww #include <stdlib.h>
3920d7339fSgww #include <unistd.h>
4020d7339fSgww #include <string.h>
4120d7339fSgww #include <ctype.h>
4220d7339fSgww #include <grp.h>
4320d7339fSgww #include <pwd.h>
4420d7339fSgww #include <shadow.h>
4520d7339fSgww #include <time.h>
4620d7339fSgww #include <stdarg.h>
4720d7339fSgww #include <fmtmsg.h>
487c478bd9Sstevel@tonic-gate #include <locale.h>
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate * Local constant definitions
527c478bd9Sstevel@tonic-gate * TRUE Boolean constant
537c478bd9Sstevel@tonic-gate * FALSE Boolean constant
547c478bd9Sstevel@tonic-gate * USAGE_MSG Message used to display a usage error
557c478bd9Sstevel@tonic-gate * MAXLOGINSIZE Maximum length of a valid login-ID
567c478bd9Sstevel@tonic-gate * MAXSYSTEMLOGIN Maximum value of a system user-ID.
577c478bd9Sstevel@tonic-gate * OPTSTR Options to this command
587c478bd9Sstevel@tonic-gate * ROOT_ID The user-ID of an administrator
597c478bd9Sstevel@tonic-gate */
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate #ifndef FALSE
627c478bd9Sstevel@tonic-gate #define FALSE 0
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate #ifndef TRUE
6620d7339fSgww #define TRUE ((int)'t')
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate
6920d7339fSgww #define USAGE_MSG "usage: logins [-admopstux] [-g groups] [-l logins]"
7020d7339fSgww #define MAXLOGINSIZE 14
7120d7339fSgww #define MAXSYSTEMLOGIN 99
7220d7339fSgww #define OPTSTR "adg:l:mopstux"
7320d7339fSgww #define ROOT_ID 0
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate * The following macros do their function for now but will probably have
777c478bd9Sstevel@tonic-gate * to be replaced by functions sometime in the near future. The maximum
787c478bd9Sstevel@tonic-gate * system login value may someday be administerable, in which case these
797c478bd9Sstevel@tonic-gate * will have to be changed to become functions
807c478bd9Sstevel@tonic-gate *
817c478bd9Sstevel@tonic-gate * isasystemlogin Returns TRUE if the user-ID in the "struct passwd"
827c478bd9Sstevel@tonic-gate * structure referenced by the function's argument is
837c478bd9Sstevel@tonic-gate * less than or equal to the maximum value for a system
847c478bd9Sstevel@tonic-gate * user-ID, FALSE otherwise.
857c478bd9Sstevel@tonic-gate * isauserlogin Returns TRUE if the user-ID in the "struct passwd"
867c478bd9Sstevel@tonic-gate * structure referenced by the function's argument is
877c478bd9Sstevel@tonic-gate * greater than the maximum value for a system user-ID,
887c478bd9Sstevel@tonic-gate * FALSE otherwise.
897c478bd9Sstevel@tonic-gate */
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate #define isauserlogin(pw) (pw->pw_uid > MAXSYSTEMLOGIN)
9220d7339fSgww #define isasystemlogin(pw) (pw->pw_uid <= MAXSYSTEMLOGIN)
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate * Local datatype definitions
977c478bd9Sstevel@tonic-gate * struct reqgrp Describes a group as requested through the
987c478bd9Sstevel@tonic-gate * -g option
997c478bd9Sstevel@tonic-gate * struct reqlogin Describes a login-ID as requested through
1007c478bd9Sstevel@tonic-gate * the -l option
1017c478bd9Sstevel@tonic-gate * struct pwdinfo Describes a password's aging information,
1027c478bd9Sstevel@tonic-gate * as extracted from /etc/shadow
1037c478bd9Sstevel@tonic-gate * struct secgrp Describes a login-ID's secondary group
1047c478bd9Sstevel@tonic-gate */
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate /* Describes a specified group name (from the -g groups option) */
1077c478bd9Sstevel@tonic-gate struct reqgrp {
10820d7339fSgww char *groupname; /* Requested group name */
10920d7339fSgww struct reqgrp *next; /* Next item in the list */
1107c478bd9Sstevel@tonic-gate gid_t groupID; /* Group's ID */
1117c478bd9Sstevel@tonic-gate };
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate /* Describes a specified login name (from the -l logins option) */
1147c478bd9Sstevel@tonic-gate struct reqlogin {
11520d7339fSgww char *loginname; /* Requested login name */
11620d7339fSgww struct reqlogin *next; /* Next item in the list */
11720d7339fSgww int found; /* TRUE if login in /etc/passwd */
1187c478bd9Sstevel@tonic-gate };
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate * This structure describes a password's information
1227c478bd9Sstevel@tonic-gate */
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate struct pwdinfo {
12520d7339fSgww long datechg; /* Date the password was changed (mmddyy) */
12620d7339fSgww char *passwdstatus; /* Password status */
12720d7339fSgww long mindaystilchg; /* Min days b4 pwd can change again */
12820d7339fSgww long maxdaystilchg; /* Max days b4 pwd can change again */
12920d7339fSgww long warninterval; /* Days before expire to warn user */
13020d7339fSgww long inactive; /* Lapsed days of inactivity before lock */
13120d7339fSgww long expdate; /* Date of expiration (mmddyy) */
1327c478bd9Sstevel@tonic-gate };
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate /* This structure describes secondary groups that a user belongs to */
1357c478bd9Sstevel@tonic-gate struct secgrp {
13620d7339fSgww char *groupname; /* Name of the group */
13720d7339fSgww struct secgrp *next; /* Next item in the list */
13820d7339fSgww gid_t groupID; /* Group-ID */
1397c478bd9Sstevel@tonic-gate };
14020d7339fSgww
14120d7339fSgww
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate * These functions handle error and warning message writing.
1447c478bd9Sstevel@tonic-gate * (This deals with UNIX(r) standard message generation, so
1457c478bd9Sstevel@tonic-gate * the rest of the code doesn't have to.)
1467c478bd9Sstevel@tonic-gate *
1477c478bd9Sstevel@tonic-gate * Functions included:
1487c478bd9Sstevel@tonic-gate * initmsg Initialize the message handling functions.
1497c478bd9Sstevel@tonic-gate * wrtmsg Write the message using fmtmsg().
1507c478bd9Sstevel@tonic-gate *
1517c478bd9Sstevel@tonic-gate * Static data included:
1527c478bd9Sstevel@tonic-gate * fcnlbl The label for standard messages
1537c478bd9Sstevel@tonic-gate * msgbuf A buffer to contain the edited message
1547c478bd9Sstevel@tonic-gate */
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate static char fcnlbl[MM_MXLABELLN+1]; /* Buffer for message label */
1577c478bd9Sstevel@tonic-gate static char msgbuf[MM_MXTXTLN+1]; /* Buffer for message text */
15820d7339fSgww
15920d7339fSgww
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate * void initmsg(p)
1627c478bd9Sstevel@tonic-gate *
1637c478bd9Sstevel@tonic-gate * This function initializes the message handling functions.
1647c478bd9Sstevel@tonic-gate *
1657c478bd9Sstevel@tonic-gate * Arguments:
1667c478bd9Sstevel@tonic-gate * p A pointer to a character string that is the name of the
1677c478bd9Sstevel@tonic-gate * function, used to generate the label on messages. If this
1687c478bd9Sstevel@tonic-gate * string contains a slash ('/'), it only uses the characters
1697c478bd9Sstevel@tonic-gate * beyond the last slash in the string (this permits argv[0]
1707c478bd9Sstevel@tonic-gate * to be used).
1717c478bd9Sstevel@tonic-gate *
1727c478bd9Sstevel@tonic-gate * Returns: Void
1737c478bd9Sstevel@tonic-gate */
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate static void
initmsg(char * p)17620d7339fSgww initmsg(char *p)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate char *q; /* Local multi-use pointer */
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate /* Use only the simple filename if there is a slash in the name */
18120d7339fSgww if (!(q = strrchr(p, '/'))) {
18220d7339fSgww q = p;
18320d7339fSgww } else {
18420d7339fSgww q++;
18520d7339fSgww }
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate /* Build the label for messages */
18820d7339fSgww (void) snprintf(fcnlbl, MM_MXLABELLN, "UX:%s", q);
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate /* Restrict messages to the text-component */
1917c478bd9Sstevel@tonic-gate (void) putenv("MSGVERB=text");
1927c478bd9Sstevel@tonic-gate }
19320d7339fSgww
19420d7339fSgww
1957c478bd9Sstevel@tonic-gate /*
1967c478bd9Sstevel@tonic-gate * void wrtmsg(severity, action, tag, text[, txtarg1[, txtarg2[, ...]]])
1977c478bd9Sstevel@tonic-gate *
1987c478bd9Sstevel@tonic-gate * This function writes a message using fmtmsg()
1997c478bd9Sstevel@tonic-gate *
2007c478bd9Sstevel@tonic-gate * Arguments:
2017c478bd9Sstevel@tonic-gate * severity The severity-component of the message
2027c478bd9Sstevel@tonic-gate * action The action-string used to generate the
2037c478bd9Sstevel@tonic-gate * action-component of the message
2047c478bd9Sstevel@tonic-gate * tag Tag-component of the message
2057c478bd9Sstevel@tonic-gate * text The text-string used to generate the text-
2067c478bd9Sstevel@tonic-gate * component of the message
2077c478bd9Sstevel@tonic-gate * txtarg Arguments to be inserted into the "text"
2087c478bd9Sstevel@tonic-gate * string using vsprintf()
2097c478bd9Sstevel@tonic-gate *
2107c478bd9Sstevel@tonic-gate * Returns: Void
2117c478bd9Sstevel@tonic-gate */
21220d7339fSgww /*PRINTFLIKE4*/
2137c478bd9Sstevel@tonic-gate static void
wrtmsg(int severity,char * action,char * tag,char * text,...)2147c478bd9Sstevel@tonic-gate wrtmsg(int severity, char *action, char *tag, char *text, ...)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate int errorflg; /* TRUE if problem generating message */
2177c478bd9Sstevel@tonic-gate va_list argp; /* Pointer into vararg list */
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate /* No problems yet */
2217c478bd9Sstevel@tonic-gate errorflg = FALSE;
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate /* Generate the error message */
2247c478bd9Sstevel@tonic-gate va_start(argp, text);
22520d7339fSgww if (text != MM_NULLTXT) {
22620d7339fSgww errorflg = vsnprintf(msgbuf,
22720d7339fSgww MM_MXTXTLN, text, argp) > MM_MXTXTLN;
22820d7339fSgww }
2297c478bd9Sstevel@tonic-gate (void) fmtmsg(MM_PRINT, fcnlbl, severity,
23020d7339fSgww (text == MM_NULLTXT) ? MM_NULLTXT : msgbuf, action, tag);
2317c478bd9Sstevel@tonic-gate va_end(argp);
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate * If there was a buffer overflow generating the error message,
2357c478bd9Sstevel@tonic-gate * write a message and quit (things are probably corrupt in the
2367c478bd9Sstevel@tonic-gate * static data space now
2377c478bd9Sstevel@tonic-gate */
2387c478bd9Sstevel@tonic-gate if (errorflg) {
23920d7339fSgww (void) fmtmsg(MM_PRINT, fcnlbl, MM_WARNING,
24020d7339fSgww gettext("Internal message buffer overflow"),
24120d7339fSgww MM_NULLACT, MM_NULLTAG);
24220d7339fSgww exit(100);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate }
24520d7339fSgww
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate * These functions control the group membership list, as found in
2487c478bd9Sstevel@tonic-gate * the /etc/group file.
2497c478bd9Sstevel@tonic-gate *
2507c478bd9Sstevel@tonic-gate * Functions included:
2517c478bd9Sstevel@tonic-gate * addmember Adds a member to the membership list
2527c478bd9Sstevel@tonic-gate * isamember Looks for a particular login-ID in the
2537c478bd9Sstevel@tonic-gate * list of members
2547c478bd9Sstevel@tonic-gate *
2557c478bd9Sstevel@tonic-gate * Datatype Definitions:
2567c478bd9Sstevel@tonic-gate * struct grpmember Describes a group member
2577c478bd9Sstevel@tonic-gate *
2587c478bd9Sstevel@tonic-gate * Static Data:
2597c478bd9Sstevel@tonic-gate * membershead Pointer to the head of the list of
2607c478bd9Sstevel@tonic-gate * group members
2617c478bd9Sstevel@tonic-gate */
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate struct grpmember {
26420d7339fSgww char *membername;
26520d7339fSgww struct grpmember *next;
2667c478bd9Sstevel@tonic-gate };
2677c478bd9Sstevel@tonic-gate
26820d7339fSgww static struct grpmember *membershead;
26920d7339fSgww
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate * void addmember(p)
2727c478bd9Sstevel@tonic-gate * char *p
2737c478bd9Sstevel@tonic-gate *
2747c478bd9Sstevel@tonic-gate * This function adds a member to the group member's list. The
2757c478bd9Sstevel@tonic-gate * group members list is a list of structures containing a pointer
2767c478bd9Sstevel@tonic-gate * to the member-name and a pointer to the next item in the
2777c478bd9Sstevel@tonic-gate * structure. The structure is not ordered in any particular way.
2787c478bd9Sstevel@tonic-gate *
2797c478bd9Sstevel@tonic-gate * Arguments:
2807c478bd9Sstevel@tonic-gate * p Pointer to the member name
2817c478bd9Sstevel@tonic-gate *
2827c478bd9Sstevel@tonic-gate * Returns: Void
2837c478bd9Sstevel@tonic-gate */
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate static void
addmember(char * p)28620d7339fSgww addmember(char *p)
2877c478bd9Sstevel@tonic-gate {
28820d7339fSgww struct grpmember *new; /* Member being added */
2897c478bd9Sstevel@tonic-gate
290b816ddf8Sgww new = malloc(sizeof (struct grpmember));
291b816ddf8Sgww new->membername = strdup(p);
2927c478bd9Sstevel@tonic-gate new->next = membershead;
2937c478bd9Sstevel@tonic-gate membershead = new;
2947c478bd9Sstevel@tonic-gate }
29520d7339fSgww
29620d7339fSgww
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate * init isamember(p)
2997c478bd9Sstevel@tonic-gate * char *p
3007c478bd9Sstevel@tonic-gate *
3017c478bd9Sstevel@tonic-gate * This function examines the list of group-members for the string
3027c478bd9Sstevel@tonic-gate * referenced by 'p'. If 'p' is a member of the members list, the
3037c478bd9Sstevel@tonic-gate * function returns TRUE. Otherwise it returns FALSE.
3047c478bd9Sstevel@tonic-gate *
3057c478bd9Sstevel@tonic-gate * Arguments:
3067c478bd9Sstevel@tonic-gate * p Pointer to the name to search for.
3077c478bd9Sstevel@tonic-gate *
3087c478bd9Sstevel@tonic-gate * Returns: int
3097c478bd9Sstevel@tonic-gate * TRUE If 'p' is found in the members list,
3107c478bd9Sstevel@tonic-gate * FALSE otherwise
3117c478bd9Sstevel@tonic-gate */
3127c478bd9Sstevel@tonic-gate
3137c478bd9Sstevel@tonic-gate static int
isamember(char * p)31420d7339fSgww isamember(char *p)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate int found; /* TRUE if login found in list */
31720d7339fSgww struct grpmember *pmem; /* Group member being examined */
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate /* Search the membership list for 'p' */
3217c478bd9Sstevel@tonic-gate found = FALSE;
32220d7339fSgww for (pmem = membershead; !found && pmem; pmem = pmem->next) {
32320d7339fSgww if (strcmp(p, pmem->membername) == 0)
32420d7339fSgww found = TRUE;
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate return (found);
3287c478bd9Sstevel@tonic-gate }
32920d7339fSgww
33020d7339fSgww
3317c478bd9Sstevel@tonic-gate /*
3327c478bd9Sstevel@tonic-gate * These functions handle the display list. The display list contains
3337c478bd9Sstevel@tonic-gate * all of the information we're to display. The list contains a pointer
3347c478bd9Sstevel@tonic-gate * to the login-name, a pointer to the free-field (comment), and a
3357c478bd9Sstevel@tonic-gate * pointer to the next item in the list. The list is ordered alpha-
3367c478bd9Sstevel@tonic-gate * betically (ascending) on the login-name field. The list initially
3377c478bd9Sstevel@tonic-gate * contains a dummy field (to make insertion easier) that contains a
3387c478bd9Sstevel@tonic-gate * login-name of "".
3397c478bd9Sstevel@tonic-gate *
3407c478bd9Sstevel@tonic-gate * Functions included:
3417c478bd9Sstevel@tonic-gate * initdisp Initializes the display list
3427c478bd9Sstevel@tonic-gate * adddisp Adds information to the display list
3437c478bd9Sstevel@tonic-gate * isuidindisp Looks to see if a particular user-ID is in the
3447c478bd9Sstevel@tonic-gate * display list
3457c478bd9Sstevel@tonic-gate * genreport Generates a report from the items in the display
3467c478bd9Sstevel@tonic-gate * list
3477c478bd9Sstevel@tonic-gate * applygroup Add group information to the items in the display
3487c478bd9Sstevel@tonic-gate * list
3497c478bd9Sstevel@tonic-gate * applypasswd Add extended password information to the items
3507c478bd9Sstevel@tonic-gate * in the display list
3517c478bd9Sstevel@tonic-gate *
3527c478bd9Sstevel@tonic-gate * Datatypes Defined:
3537c478bd9Sstevel@tonic-gate * struct display Describes the structure that contains the information
3547c478bd9Sstevel@tonic-gate * to be displayed. Includes pointers to the login-ID,
3557c478bd9Sstevel@tonic-gate * free-field (comment), and the next structure in the
3567c478bd9Sstevel@tonic-gate * list.
3577c478bd9Sstevel@tonic-gate *
3587c478bd9Sstevel@tonic-gate * Static Data:
3597c478bd9Sstevel@tonic-gate * displayhead Pointer to the head of the display list. Initially
3607c478bd9Sstevel@tonic-gate * references the null-item on the head of the list.
3617c478bd9Sstevel@tonic-gate */
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate struct display {
36420d7339fSgww char *loginID; /* Login name */
36520d7339fSgww char *freefield; /* Free (comment) field */
36620d7339fSgww char *groupname; /* Name of the primary group */
36720d7339fSgww char *iwd; /* Initial working directory */
36820d7339fSgww char *shell; /* Shell after login (may be null) */
36920d7339fSgww struct pwdinfo *passwdinfo; /* Password information structure */
37020d7339fSgww struct secgrp *secgrplist; /* Head of the secondary group list */
3717c478bd9Sstevel@tonic-gate uid_t userID; /* User ID */
3727c478bd9Sstevel@tonic-gate gid_t groupID; /* Group ID of primary group */
37320d7339fSgww struct display *nextlogin; /* Next login in the list */
37420d7339fSgww struct display *nextuid; /* Next user-ID in the list */
3757c478bd9Sstevel@tonic-gate };
3767c478bd9Sstevel@tonic-gate
37720d7339fSgww static struct display *displayhead;
37820d7339fSgww
37920d7339fSgww
3807c478bd9Sstevel@tonic-gate /*
3817c478bd9Sstevel@tonic-gate * void initdisp()
3827c478bd9Sstevel@tonic-gate *
3837c478bd9Sstevel@tonic-gate * Initializes the display list. An empty display list contains
3847c478bd9Sstevel@tonic-gate * a single element, the dummy element.
3857c478bd9Sstevel@tonic-gate *
3867c478bd9Sstevel@tonic-gate * Arguments: None
3877c478bd9Sstevel@tonic-gate *
3887c478bd9Sstevel@tonic-gate * Returns: Void
3897c478bd9Sstevel@tonic-gate */
3907c478bd9Sstevel@tonic-gate
3917c478bd9Sstevel@tonic-gate static void
initdisp(void)39220d7339fSgww initdisp(void)
3937c478bd9Sstevel@tonic-gate {
394b816ddf8Sgww displayhead = malloc(sizeof (struct display));
39520d7339fSgww displayhead->nextlogin = NULL;
39620d7339fSgww displayhead->nextuid = NULL;
3977c478bd9Sstevel@tonic-gate displayhead->loginID = "";
3987c478bd9Sstevel@tonic-gate displayhead->freefield = "";
399*f48205beScasper displayhead->userID = (uid_t)-1;
4007c478bd9Sstevel@tonic-gate }
40120d7339fSgww
40220d7339fSgww
4037c478bd9Sstevel@tonic-gate /*
4047c478bd9Sstevel@tonic-gate * void adddisp(pwent)
4057c478bd9Sstevel@tonic-gate * struct passwd *pwent
4067c478bd9Sstevel@tonic-gate *
4077c478bd9Sstevel@tonic-gate * This function adds the appropriate information from the login
4087c478bd9Sstevel@tonic-gate * description referenced by 'pwent' to the list if information
4097c478bd9Sstevel@tonic-gate * to be displayed. It only adds the information if the login-ID
4107c478bd9Sstevel@tonic-gate * (user-name) is unique. It inserts the information in the list
4117c478bd9Sstevel@tonic-gate * in such a way that the list remains ordered alphabetically
4127c478bd9Sstevel@tonic-gate * (ascending) according to the login-ID (user-name).
4137c478bd9Sstevel@tonic-gate *
4147c478bd9Sstevel@tonic-gate * Arguments:
4157c478bd9Sstevel@tonic-gate * pwent Structure that contains all of the login information
4167c478bd9Sstevel@tonic-gate * of the login being added to the list. The only
4177c478bd9Sstevel@tonic-gate * information that this function uses is the login-ID
4187c478bd9Sstevel@tonic-gate * (user-name) and the free-field (comment field).
4197c478bd9Sstevel@tonic-gate *
4207c478bd9Sstevel@tonic-gate * Returns: Void
4217c478bd9Sstevel@tonic-gate */
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate static void
adddisp(struct passwd * pwent)42420d7339fSgww adddisp(struct passwd *pwent)
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate struct display *new; /* Item being added to the list */
4277c478bd9Sstevel@tonic-gate struct display *prev; /* Previous item in the list */
4287c478bd9Sstevel@tonic-gate struct display *current; /* Next item in the list */
4297c478bd9Sstevel@tonic-gate int found; /* FLAG, insertion point found */
4307c478bd9Sstevel@tonic-gate int compare = 1; /* strcmp() compare value */
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate /* Find where this value belongs in the list */
4347c478bd9Sstevel@tonic-gate prev = displayhead;
4357c478bd9Sstevel@tonic-gate found = FALSE;
43620d7339fSgww while (!found && (current = prev->nextlogin)) {
43720d7339fSgww if ((compare = strcmp(current->loginID, pwent->pw_name)) >= 0) {
43820d7339fSgww found = TRUE;
43920d7339fSgww } else {
44020d7339fSgww prev = current;
44120d7339fSgww }
4427c478bd9Sstevel@tonic-gate
44320d7339fSgww }
4447c478bd9Sstevel@tonic-gate /* Insert this value in the list, only if it is unique though */
4457c478bd9Sstevel@tonic-gate if (compare != 0) {
446b816ddf8Sgww new = malloc(sizeof (struct display));
447b816ddf8Sgww new->loginID = strdup(pwent->pw_name);
44820d7339fSgww if (pwent->pw_comment && pwent->pw_comment[0] != '\0') {
449b816ddf8Sgww new->freefield = strdup(pwent->pw_comment);
45020d7339fSgww } else {
451b816ddf8Sgww new->freefield = strdup(pwent->pw_gecos);
45220d7339fSgww }
45320d7339fSgww if (!pwent->pw_shell || !(*pwent->pw_shell)) {
45420d7339fSgww new->shell = "/sbin/sh";
45520d7339fSgww } else {
456b816ddf8Sgww new->shell = strdup(pwent->pw_shell);
45720d7339fSgww }
458b816ddf8Sgww new->iwd = strdup(pwent->pw_dir);
45920d7339fSgww new->userID = pwent->pw_uid;
46020d7339fSgww new->groupID = pwent->pw_gid;
46120d7339fSgww new->secgrplist = NULL;
46220d7339fSgww new->passwdinfo = NULL;
46320d7339fSgww new->groupname = NULL;
46420d7339fSgww
46520d7339fSgww /* Add new display item to the list ordered by login-ID */
46620d7339fSgww new->nextlogin = current;
46720d7339fSgww prev->nextlogin = new;
46820d7339fSgww
46920d7339fSgww /*
47020d7339fSgww * Find the appropriate place for the new item in the list
47120d7339fSgww * ordered by userID
47220d7339fSgww */
47320d7339fSgww prev = displayhead;
47420d7339fSgww found = FALSE;
47520d7339fSgww while (!found && (current = prev->nextuid)) {
47620d7339fSgww if (current->userID > pwent->pw_uid) {
47720d7339fSgww found = TRUE;
47820d7339fSgww } else {
47920d7339fSgww prev = current;
48020d7339fSgww }
48120d7339fSgww }
48220d7339fSgww
48320d7339fSgww /* Add the item into the list that is ordered by user-ID */
48420d7339fSgww new->nextuid = current;
48520d7339fSgww prev->nextuid = new;
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate }
48820d7339fSgww
48920d7339fSgww
4907c478bd9Sstevel@tonic-gate /*
4917c478bd9Sstevel@tonic-gate * int isuidindisp(pwent)
4927c478bd9Sstevel@tonic-gate * struct passwd *pwent
4937c478bd9Sstevel@tonic-gate *
4947c478bd9Sstevel@tonic-gate * This function examines the display list to see if the uid in
4957c478bd9Sstevel@tonic-gate * the (struct passwd) referenced by "pwent" is already in the
4967c478bd9Sstevel@tonic-gate * display list. It returns TRUE if it is in the list, FALSE
4977c478bd9Sstevel@tonic-gate * otherwise.
4987c478bd9Sstevel@tonic-gate *
4997c478bd9Sstevel@tonic-gate * Since the display list is ordered by user-ID, the search continues
5007c478bd9Sstevel@tonic-gate * until a match is found or a user-ID is found that is larger than
5017c478bd9Sstevel@tonic-gate * the one we're searching for.
5027c478bd9Sstevel@tonic-gate *
5037c478bd9Sstevel@tonic-gate * Arguments:
5047c478bd9Sstevel@tonic-gate * pwent Structure that contains the user-ID we're to
5057c478bd9Sstevel@tonic-gate * look for
5067c478bd9Sstevel@tonic-gate *
5077c478bd9Sstevel@tonic-gate * Returns: int
5087c478bd9Sstevel@tonic-gate * TRUE if the user-ID was found, FALSE otherwise.
5097c478bd9Sstevel@tonic-gate */
5107c478bd9Sstevel@tonic-gate
5117c478bd9Sstevel@tonic-gate static int
isuidindisp(struct passwd * pwent)51220d7339fSgww isuidindisp(struct passwd *pwent)
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate struct display *dp;
5157c478bd9Sstevel@tonic-gate
5167c478bd9Sstevel@tonic-gate
5177c478bd9Sstevel@tonic-gate /*
5187c478bd9Sstevel@tonic-gate * Search the list, beginning at the beginning (where else?)
5197c478bd9Sstevel@tonic-gate * and stopping when the user-ID is found or one is found that
5207c478bd9Sstevel@tonic-gate * is greater than the user-ID we're searching for. Recall
5217c478bd9Sstevel@tonic-gate * that this list is ordered by user-ID
5227c478bd9Sstevel@tonic-gate */
5237c478bd9Sstevel@tonic-gate
52420d7339fSgww for (dp = displayhead->nextuid; dp && (dp->userID < pwent->pw_uid);
52520d7339fSgww dp = dp->nextuid) {
52620d7339fSgww continue;
52720d7339fSgww }
5287c478bd9Sstevel@tonic-gate
52920d7339fSgww /*
53020d7339fSgww * If the pointer "dp" points to a structure that has a
53120d7339fSgww * matching user-ID, return TRUE. Otherwise FALSE
53220d7339fSgww */
5337c478bd9Sstevel@tonic-gate return (dp && (dp->userID == pwent->pw_uid));
5347c478bd9Sstevel@tonic-gate }
53520d7339fSgww
53620d7339fSgww
5377c478bd9Sstevel@tonic-gate /*
5387c478bd9Sstevel@tonic-gate * void applygroup(allgroups)
5397c478bd9Sstevel@tonic-gate * int allgroups
5407c478bd9Sstevel@tonic-gate *
5417c478bd9Sstevel@tonic-gate * This function applies group information to the login-IDs in the
5427c478bd9Sstevel@tonic-gate * display list. It always applies the primary group information.
5437c478bd9Sstevel@tonic-gate * If "allgroups" is TRUE, it applies secondary information as well.
5447c478bd9Sstevel@tonic-gate *
5457c478bd9Sstevel@tonic-gate * Arguments:
5467c478bd9Sstevel@tonic-gate * allgroups FLAG. TRUE if secondary group info is to be
5477c478bd9Sstevel@tonic-gate * applied -- FALSE otherwise.
5487c478bd9Sstevel@tonic-gate *
5497c478bd9Sstevel@tonic-gate * Returns: void
5507c478bd9Sstevel@tonic-gate */
5517c478bd9Sstevel@tonic-gate
5527c478bd9Sstevel@tonic-gate static void
applygroup(int allgroups)55320d7339fSgww applygroup(int allgroups)
5547c478bd9Sstevel@tonic-gate {
55520d7339fSgww struct display *dp; /* Display list running ptr */
55620d7339fSgww struct group *grent; /* Group info, from getgrent() */
55720d7339fSgww char *p; /* Temp char pointer */
55820d7339fSgww char **pp; /* Temp char * pointer */
5597c478bd9Sstevel@tonic-gate int compare; /* Value from strcmp() */
5607c478bd9Sstevel@tonic-gate int done; /* TRUE if finished, FALSE otherwise */
56120d7339fSgww struct secgrp *psecgrp; /* Block allocated for this info */
56220d7339fSgww struct secgrp *psrch; /* Secondary group -- for searching */
5637c478bd9Sstevel@tonic-gate
564b816ddf8Sgww if (!allgroups) {
565b816ddf8Sgww /* short circute getting all the groups */
566b816ddf8Sgww for (dp = displayhead->nextuid; dp; dp = dp->nextuid) {
567b816ddf8Sgww if ((grent = getgrgid(dp->groupID)) != NULL) {
568b816ddf8Sgww dp->groupname = strdup(grent->gr_name);
569b816ddf8Sgww }
570b816ddf8Sgww }
571b816ddf8Sgww return;
572b816ddf8Sgww }
5737c478bd9Sstevel@tonic-gate
5747c478bd9Sstevel@tonic-gate /* For each group-ID in the /etc/group file ... */
5757c478bd9Sstevel@tonic-gate while (grent = getgrent()) {
57620d7339fSgww /*
57720d7339fSgww * Set the primary group for the login-IDs in the display
57820d7339fSgww * list. For each group-ID we get, leaf through the display
57920d7339fSgww * list and set the group-name if the group-IDs match
58020d7339fSgww */
58120d7339fSgww
58220d7339fSgww p = NULL;
58320d7339fSgww for (dp = displayhead->nextuid; dp; dp = dp->nextuid) {
58420d7339fSgww if ((dp->groupID == grent->gr_gid) && !dp->groupname) {
58520d7339fSgww if (!p) {
586b816ddf8Sgww p = strdup(grent->gr_name);
58720d7339fSgww }
58820d7339fSgww dp->groupname = p;
58920d7339fSgww }
59020d7339fSgww }
59120d7339fSgww
59220d7339fSgww /*
59320d7339fSgww * If we're to be displaying secondary group membership,
59420d7339fSgww * leaf through the list of group members. Then, attempt
59520d7339fSgww * to find that member in the display list. When found,
59620d7339fSgww * attach secondary group info to the user.
59720d7339fSgww */
5987c478bd9Sstevel@tonic-gate
59920d7339fSgww for (pp = grent->gr_mem; *pp; pp++) {
60020d7339fSgww done = FALSE;
60120d7339fSgww for (dp = displayhead->nextlogin; !done && dp;
60220d7339fSgww dp = dp->nextlogin) {
60320d7339fSgww if (((compare = strcmp(dp->loginID,
60420d7339fSgww *pp)) == 0) &&
60520d7339fSgww !(grent->gr_gid == dp->groupID)) {
60620d7339fSgww if (!p) {
607b816ddf8Sgww p = strdup(grent->gr_name);
60820d7339fSgww }
609b816ddf8Sgww psecgrp = malloc(
61020d7339fSgww sizeof (struct secgrp));
61120d7339fSgww psecgrp->groupID = grent->gr_gid;
61220d7339fSgww psecgrp->groupname = p;
61320d7339fSgww psecgrp->next = NULL;
61420d7339fSgww if (!dp->secgrplist) {
61520d7339fSgww dp->secgrplist = psecgrp;
61620d7339fSgww } else {
61720d7339fSgww for (psrch = dp->secgrplist;
61820d7339fSgww psrch->next;
61920d7339fSgww psrch = psrch->next) {
62020d7339fSgww continue;
62120d7339fSgww }
62220d7339fSgww psrch->next = psecgrp;
62320d7339fSgww }
62420d7339fSgww done = TRUE;
62520d7339fSgww } else if (compare > 0) {
62620d7339fSgww done = TRUE;
62720d7339fSgww }
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate }
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate /* Close the /etc/group file */
6337c478bd9Sstevel@tonic-gate endgrent();
6347c478bd9Sstevel@tonic-gate }
63520d7339fSgww
63620d7339fSgww
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate * void applypasswd()
6397c478bd9Sstevel@tonic-gate *
6407c478bd9Sstevel@tonic-gate * This function applies extended password information to an item
6417c478bd9Sstevel@tonic-gate * to be displayed. It allocates space for a structure describing
6427c478bd9Sstevel@tonic-gate * the password, then fills in that structure from the information
6437c478bd9Sstevel@tonic-gate * in the /etc/shadow file.
6447c478bd9Sstevel@tonic-gate *
6457c478bd9Sstevel@tonic-gate * Arguments: None
6467c478bd9Sstevel@tonic-gate *
6477c478bd9Sstevel@tonic-gate * Returns: Void
6487c478bd9Sstevel@tonic-gate */
6497c478bd9Sstevel@tonic-gate
6507c478bd9Sstevel@tonic-gate static void
applypasswd(void)65120d7339fSgww applypasswd(void)
6527c478bd9Sstevel@tonic-gate {
65320d7339fSgww struct pwdinfo *ppasswd; /* Ptr to pwd desc in current element */
65420d7339fSgww struct display *dp; /* Ptr to current element */
65520d7339fSgww struct spwd *psp; /* Pointer to a shadow-file entry */
65620d7339fSgww struct tm *ptm; /* Pointer to a time-of-day structure */
65720d7339fSgww time_t pwchg; /* System-time of last pwd chg */
65820d7339fSgww time_t pwexp; /* System-time of password expiration */
6597c478bd9Sstevel@tonic-gate
6607c478bd9Sstevel@tonic-gate
6617c478bd9Sstevel@tonic-gate /* Make sure the shadow file is rewound */
6627c478bd9Sstevel@tonic-gate setspent();
6637c478bd9Sstevel@tonic-gate
6647c478bd9Sstevel@tonic-gate
6657c478bd9Sstevel@tonic-gate /*
6667c478bd9Sstevel@tonic-gate * For each item in the display list...
6677c478bd9Sstevel@tonic-gate */
6687c478bd9Sstevel@tonic-gate
66920d7339fSgww for (dp = displayhead->nextuid; dp; dp = dp->nextuid) {
6707c478bd9Sstevel@tonic-gate
67120d7339fSgww /* Allocate structure space for the password description */
672b816ddf8Sgww ppasswd = malloc(sizeof (struct pwdinfo));
67320d7339fSgww dp->passwdinfo = ppasswd;
6747c478bd9Sstevel@tonic-gate
67520d7339fSgww /*
67620d7339fSgww * If there's no entry in the /etc/shadow file, assume
67720d7339fSgww * that the login is locked
67820d7339fSgww */
6797c478bd9Sstevel@tonic-gate
680b816ddf8Sgww if ((psp = getspnam(dp->loginID)) == NULL) {
68120d7339fSgww pwchg = 0L; /* Epoch */
68220d7339fSgww ppasswd->passwdstatus = "LK"; /* LK, Locked */
68320d7339fSgww ppasswd->mindaystilchg = 0L;
68420d7339fSgww ppasswd->maxdaystilchg = 0L;
68520d7339fSgww ppasswd->warninterval = 0L;
68620d7339fSgww ppasswd->inactive = 0L;
68720d7339fSgww pwexp = 0L;
68820d7339fSgww } else {
68920d7339fSgww /*
69020d7339fSgww * Otherwise, fill in the password information from the
691b816ddf8Sgww * info in the shadow entry
69220d7339fSgww */
693b816ddf8Sgww if (psp->sp_pwdp == NULL || (*psp->sp_pwdp) == '\0')
69420d7339fSgww ppasswd->passwdstatus = "NP";
695b816ddf8Sgww else if (strncmp(psp->sp_pwdp, LOCKSTRING,
696b816ddf8Sgww sizeof (LOCKSTRING)-1) == 0)
69720d7339fSgww ppasswd->passwdstatus = "LK";
698b816ddf8Sgww else if (strncmp(psp->sp_pwdp, NOLOGINSTRING,
699b816ddf8Sgww sizeof (NOLOGINSTRING)-1) == 0)
700b816ddf8Sgww ppasswd->passwdstatus = "NL";
701b816ddf8Sgww else if ((strlen(psp->sp_pwdp) == 13 &&
702b816ddf8Sgww psp->sp_pwdp[0] != '$') ||
703b816ddf8Sgww psp->sp_pwdp[0] == '$')
70420d7339fSgww ppasswd->passwdstatus = "PS";
705b816ddf8Sgww else
706b816ddf8Sgww ppasswd->passwdstatus = "UN";
70720d7339fSgww /*
70820d7339fSgww * Set up the last-changed date,
70920d7339fSgww * the minimum days between changes,
71020d7339fSgww * the maximum life of a password,
71120d7339fSgww * the interval before expiration that the user
71220d7339fSgww * is warned,
71320d7339fSgww * the number of days a login can be inactive before
71420d7339fSgww * it expires, and the login expiration date
71520d7339fSgww */
7167c478bd9Sstevel@tonic-gate
71720d7339fSgww pwchg = psp->sp_lstchg;
71820d7339fSgww ppasswd->mindaystilchg = psp->sp_min;
71920d7339fSgww ppasswd->maxdaystilchg = psp->sp_max;
72020d7339fSgww ppasswd->warninterval = psp->sp_warn;
72120d7339fSgww ppasswd->inactive = psp->sp_inact;
72220d7339fSgww pwexp = psp->sp_expire;
72320d7339fSgww }
7247c478bd9Sstevel@tonic-gate
7257c478bd9Sstevel@tonic-gate /*
72620d7339fSgww * Convert the date of the last password change from days-
72720d7339fSgww * since-epoch to mmddyy (integer) form. Involves the
72820d7339fSgww * intermediate step of converting the date from days-
72920d7339fSgww * since-epoch to seconds-since-epoch. We'll set this to
73020d7339fSgww * somewhere near the middle of the day, since there are not
73120d7339fSgww * always 24*60*60 seconds in a year. (Yeech)
73220d7339fSgww *
73320d7339fSgww * Note: The form mmddyy should probably be subject to
73420d7339fSgww * internationalization -- Non-Americans will think that
73520d7339fSgww * 070888 is 07 August 88 when the software is trying to say
73620d7339fSgww * 08 July 88. Systems Engineers seem to think that this isn't
73720d7339fSgww * a problem though...
7387c478bd9Sstevel@tonic-gate */
7397c478bd9Sstevel@tonic-gate
74020d7339fSgww if (pwchg != -1L) {
74120d7339fSgww pwchg = (pwchg * DAY) + (DAY/2);
74220d7339fSgww ptm = localtime(&pwchg);
74320d7339fSgww ppasswd->datechg = ((long)(ptm->tm_mon+1) * 10000L) +
74420d7339fSgww (long)((ptm->tm_mday * 100) +
74520d7339fSgww (ptm->tm_year % 100));
74620d7339fSgww } else {
74720d7339fSgww ppasswd->datechg = 0L;
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate
7507c478bd9Sstevel@tonic-gate /*
75120d7339fSgww * Convert the passwd expiration date from days-since-epoch
75220d7339fSgww * to mmddyy (long integer) form using the same algorithm and
75320d7339fSgww * comments as above.
7547c478bd9Sstevel@tonic-gate */
7557c478bd9Sstevel@tonic-gate
75620d7339fSgww if (pwexp != -1L) {
75720d7339fSgww pwexp = (pwexp * DAY) + (DAY/2);
75820d7339fSgww ptm = localtime(&pwexp);
75920d7339fSgww ppasswd->expdate = ((long)(ptm->tm_mon+1) * 10000L) +
76020d7339fSgww (long)((ptm->tm_mday * 100) +
76120d7339fSgww (ptm->tm_year % 100));
76220d7339fSgww } else {
76320d7339fSgww ppasswd->expdate = 0L;
76420d7339fSgww }
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate
7677c478bd9Sstevel@tonic-gate /* Close the shadow password file */
7687c478bd9Sstevel@tonic-gate endspent();
7697c478bd9Sstevel@tonic-gate }
77020d7339fSgww
77120d7339fSgww
7727c478bd9Sstevel@tonic-gate /*
7737c478bd9Sstevel@tonic-gate * int hasnopasswd(pwent)
7747c478bd9Sstevel@tonic-gate * struct passwd *pwent
7757c478bd9Sstevel@tonic-gate *
7767c478bd9Sstevel@tonic-gate * This function examines a login's password-file entry
7777c478bd9Sstevel@tonic-gate * and, if necessary, its shadow password-file entry and
7787c478bd9Sstevel@tonic-gate * returns TRUE if that user-ID has no password, meaning
7797c478bd9Sstevel@tonic-gate * that the user-ID can be used to log into the system
7807c478bd9Sstevel@tonic-gate * without giving a password. The function returns FALSE
7817c478bd9Sstevel@tonic-gate * otherwise.
7827c478bd9Sstevel@tonic-gate *
7837c478bd9Sstevel@tonic-gate * Arguments:
7847c478bd9Sstevel@tonic-gate * pwent Login to examine.
7857c478bd9Sstevel@tonic-gate *
7867c478bd9Sstevel@tonic-gate * Returns: int
7877c478bd9Sstevel@tonic-gate * TRUE if the login can be used without a password, FALSE
7887c478bd9Sstevel@tonic-gate * otherwise.
7897c478bd9Sstevel@tonic-gate */
7907c478bd9Sstevel@tonic-gate
7917c478bd9Sstevel@tonic-gate static int
hasnopasswd(struct passwd * pwent)79220d7339fSgww hasnopasswd(struct passwd *pwent)
7937c478bd9Sstevel@tonic-gate {
7947c478bd9Sstevel@tonic-gate struct spwd *psp; /* /etc/shadow file struct */
7957c478bd9Sstevel@tonic-gate int nopwflag; /* TRUE if login has no passwd */
7967c478bd9Sstevel@tonic-gate
7977c478bd9Sstevel@tonic-gate /*
7987c478bd9Sstevel@tonic-gate * A login has no password if:
7997c478bd9Sstevel@tonic-gate * 1. There exists an entry for that login in the
8007c478bd9Sstevel@tonic-gate * shadow password-file (/etc/passwd), and
8017c478bd9Sstevel@tonic-gate * 2. The encrypted password in the structure describing
80220d7339fSgww * that entry is either: NULL or a null string ("")
8037c478bd9Sstevel@tonic-gate */
8047c478bd9Sstevel@tonic-gate
8057c478bd9Sstevel@tonic-gate /* Get the login's entry in the shadow password file */
8067c478bd9Sstevel@tonic-gate if (psp = getspnam(pwent->pw_name)) {
8077c478bd9Sstevel@tonic-gate
80820d7339fSgww /* Look at the encrypted password in that entry */
80920d7339fSgww if (psp->sp_pwdp == (char *)0 ||
81020d7339fSgww *psp->sp_pwdp == '\0') {
81120d7339fSgww nopwflag = TRUE;
81220d7339fSgww } else {
81320d7339fSgww nopwflag = FALSE;
81420d7339fSgww }
81520d7339fSgww } else {
8167c478bd9Sstevel@tonic-gate nopwflag = FALSE;
8177c478bd9Sstevel@tonic-gate }
8187c478bd9Sstevel@tonic-gate
8197c478bd9Sstevel@tonic-gate /* Done ... */
82020d7339fSgww return (nopwflag);
8217c478bd9Sstevel@tonic-gate }
82220d7339fSgww
82320d7339fSgww
8247c478bd9Sstevel@tonic-gate /*
8257c478bd9Sstevel@tonic-gate * void writeunformatted(current, xtndflag, expflag)
8267c478bd9Sstevel@tonic-gate * struct display *current
8277c478bd9Sstevel@tonic-gate * int xtndflag
8287c478bd9Sstevel@tonic-gate * int expflag
8297c478bd9Sstevel@tonic-gate *
8307c478bd9Sstevel@tonic-gate * This function writes the data in the display structure "current"
8317c478bd9Sstevel@tonic-gate * to the standard output file. It writes the information in the
8327c478bd9Sstevel@tonic-gate * form of a colon-list. It writes secondary group information if
8337c478bd9Sstevel@tonic-gate * that information is in the structure, it writes extended
8347c478bd9Sstevel@tonic-gate * (initial working directory, shell, and password-aging) info
83520d7339fSgww * if the "xtndflag" is TRUE, and it writes password expiration
8367c478bd9Sstevel@tonic-gate * information if "expflag" is TRUE.
8377c478bd9Sstevel@tonic-gate *
8387c478bd9Sstevel@tonic-gate * Arguments:
8397c478bd9Sstevel@tonic-gate * current Structure containing information to write.
8407c478bd9Sstevel@tonic-gate * xtndflag TRUE if extended information is to be written,
8417c478bd9Sstevel@tonic-gate * FALSE otherwise
8427c478bd9Sstevel@tonic-gate * expflag TRUE if password expiration information is to
8437c478bd9Sstevel@tonic-gate * be written, FALSE otherwise
8447c478bd9Sstevel@tonic-gate *
8457c478bd9Sstevel@tonic-gate * Returns: void
8467c478bd9Sstevel@tonic-gate */
8477c478bd9Sstevel@tonic-gate
8487c478bd9Sstevel@tonic-gate static void
writeunformatted(struct display * current,int xtndflag,int expflag)84920d7339fSgww writeunformatted(struct display *current, int xtndflag, int expflag)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate struct secgrp *psecgrp; /* Secondary group info */
8527c478bd9Sstevel@tonic-gate struct pwdinfo *pwdinfo; /* Password aging info */
8537c478bd9Sstevel@tonic-gate
8547c478bd9Sstevel@tonic-gate /* Write the general information */
855*f48205beScasper (void) fprintf(stdout, "%s:%u:%s:%u:%s",
85620d7339fSgww current->loginID,
85720d7339fSgww current->userID,
85820d7339fSgww current->groupname == NULL ? "" : current->groupname,
85920d7339fSgww current->groupID,
86020d7339fSgww current->freefield);
8617c478bd9Sstevel@tonic-gate
8627c478bd9Sstevel@tonic-gate /*
8637c478bd9Sstevel@tonic-gate * If the group information is there, write it (it's only
8647c478bd9Sstevel@tonic-gate * there if it's supposed to be written)
8657c478bd9Sstevel@tonic-gate */
86620d7339fSgww for (psecgrp = current->secgrplist; psecgrp; psecgrp = psecgrp->next) {
867*f48205beScasper (void) fprintf(stdout, ":%s:%u",
86820d7339fSgww psecgrp->groupname, psecgrp->groupID);
86920d7339fSgww }
8707c478bd9Sstevel@tonic-gate
8717c478bd9Sstevel@tonic-gate /* If the extended info flag is TRUE, write the extended information */
8727c478bd9Sstevel@tonic-gate if (xtndflag) {
87320d7339fSgww pwdinfo = current->passwdinfo;
87420d7339fSgww (void) fprintf(stdout, ":%s:%s:%s:%6.6ld:%ld:%ld:%ld",
87520d7339fSgww current->iwd, current->shell,
87620d7339fSgww pwdinfo->passwdstatus,
87720d7339fSgww pwdinfo->datechg,
87820d7339fSgww pwdinfo->mindaystilchg, pwdinfo->maxdaystilchg,
87920d7339fSgww pwdinfo->warninterval);
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate
8827c478bd9Sstevel@tonic-gate /* If the password expiration information is requested, write it. */
8837c478bd9Sstevel@tonic-gate if (expflag) {
88420d7339fSgww pwdinfo = current->passwdinfo;
88520d7339fSgww (void) fprintf(stdout, ":%ld:%ld",
88620d7339fSgww pwdinfo->inactive, pwdinfo->expdate);
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate
8897c478bd9Sstevel@tonic-gate /* Terminate the information with a new-line */
8907c478bd9Sstevel@tonic-gate (void) putc('\n', stdout);
8917c478bd9Sstevel@tonic-gate }
89220d7339fSgww
89320d7339fSgww
8947c478bd9Sstevel@tonic-gate /*
8957c478bd9Sstevel@tonic-gate * void writeformatted(current, xtndflag, expflag)
8967c478bd9Sstevel@tonic-gate * struct display *current
8977c478bd9Sstevel@tonic-gate * int xtndflag
8987c478bd9Sstevel@tonic-gate * int expflag
8997c478bd9Sstevel@tonic-gate *
9007c478bd9Sstevel@tonic-gate * This function writes the data in the display structure "current"
9017c478bd9Sstevel@tonic-gate * to the standard output file. It writes the information in an
9027c478bd9Sstevel@tonic-gate * easily readable format. It writes secondary group information
9037c478bd9Sstevel@tonic-gate * if that information is in the structure, it writes extended
9047c478bd9Sstevel@tonic-gate * (initial working directory, shell, and password-aging) info if
9057c478bd9Sstevel@tonic-gate * "xtndflag" is TRUE, and it write password expiration information
9067c478bd9Sstevel@tonic-gate * if "expflag" is TRUE.
9077c478bd9Sstevel@tonic-gate *
9087c478bd9Sstevel@tonic-gate * Arguments:
9097c478bd9Sstevel@tonic-gate * current Structure containing info to write.
91020d7339fSgww * xtndflag TRUE if extended information to be written,
9117c478bd9Sstevel@tonic-gate * FALSE otherwise
91220d7339fSgww * expflag TRUE if password expiration information to be written,
9137c478bd9Sstevel@tonic-gate * FALSE otherwise
9147c478bd9Sstevel@tonic-gate *
9157c478bd9Sstevel@tonic-gate * Returns: void
9167c478bd9Sstevel@tonic-gate */
9177c478bd9Sstevel@tonic-gate
9187c478bd9Sstevel@tonic-gate static void
writeformatted(struct display * current,int xtndflag,int expflag)91920d7339fSgww writeformatted(struct display *current, int xtndflag, int expflag)
9207c478bd9Sstevel@tonic-gate {
9217c478bd9Sstevel@tonic-gate struct secgrp *psecgrp; /* Secondary group info */
9227c478bd9Sstevel@tonic-gate struct pwdinfo *pwdinfo; /* Password aging info */
9237c478bd9Sstevel@tonic-gate
9247c478bd9Sstevel@tonic-gate /* Write general information */
925*f48205beScasper (void) fprintf(stdout, "%-14s %-6u %-14s %-6u %s\n",
92620d7339fSgww current->loginID, current->userID,
92720d7339fSgww current->groupname == NULL ? "" : current->groupname,
92820d7339fSgww current->groupID, current->freefield);
9297c478bd9Sstevel@tonic-gate
9307c478bd9Sstevel@tonic-gate /*
9317c478bd9Sstevel@tonic-gate * Write information about secondary groups if the info exists
9327c478bd9Sstevel@tonic-gate * (it only exists if it is to be written)
9337c478bd9Sstevel@tonic-gate */
93420d7339fSgww for (psecgrp = current->secgrplist; psecgrp; psecgrp = psecgrp->next) {
935*f48205beScasper (void) fprintf(stdout, " %-14s %-6u\n",
93620d7339fSgww psecgrp->groupname, psecgrp->groupID);
93720d7339fSgww }
9387c478bd9Sstevel@tonic-gate
93920d7339fSgww /*
94020d7339fSgww * If the extended information flag is TRUE,
94120d7339fSgww * write the extended information
94220d7339fSgww */
9437c478bd9Sstevel@tonic-gate
9447c478bd9Sstevel@tonic-gate if (xtndflag) {
94520d7339fSgww pwdinfo = current->passwdinfo;
94620d7339fSgww (void) fprintf(stdout, " %s\n",
94720d7339fSgww current->iwd);
94820d7339fSgww (void) fprintf(stdout, " %s\n",
94920d7339fSgww current->shell);
95020d7339fSgww (void) fprintf(stdout, " %s "
95120d7339fSgww "%6.6ld %ld %ld %ld\n",
95220d7339fSgww pwdinfo->passwdstatus,
95320d7339fSgww pwdinfo->datechg, pwdinfo->mindaystilchg,
95420d7339fSgww pwdinfo->maxdaystilchg,
95520d7339fSgww pwdinfo->warninterval);
9567c478bd9Sstevel@tonic-gate }
9577c478bd9Sstevel@tonic-gate
95820d7339fSgww /*
95920d7339fSgww * If the password expiration info flag is TRUE,
96020d7339fSgww * write that information
96120d7339fSgww */
9627c478bd9Sstevel@tonic-gate if (expflag) {
96320d7339fSgww pwdinfo = current->passwdinfo;
96420d7339fSgww (void) fprintf(stdout, " %ld %6.6ld\n",
96520d7339fSgww pwdinfo->inactive, pwdinfo->expdate);
96620d7339fSgww }
9677c478bd9Sstevel@tonic-gate }
96820d7339fSgww
96920d7339fSgww
9707c478bd9Sstevel@tonic-gate /*
9717c478bd9Sstevel@tonic-gate * void genuidreport(pipeflag, xtndflag, expflag)
9727c478bd9Sstevel@tonic-gate * int pipeflag
9737c478bd9Sstevel@tonic-gate * int xtndflag
9747c478bd9Sstevel@tonic-gate * int expflag
9757c478bd9Sstevel@tonic-gate *
9767c478bd9Sstevel@tonic-gate * This function generates a report on the standard output
9777c478bd9Sstevel@tonic-gate * stream (stdout) containing the login-IDs in the list of
9787c478bd9Sstevel@tonic-gate * logins built by this command. The list is ordered based
9797c478bd9Sstevel@tonic-gate * on user-ID. If the <pipeflag> variable is not zero, it
9807c478bd9Sstevel@tonic-gate * will generate a report containing parsable records.
9817c478bd9Sstevel@tonic-gate * Otherwise, it will generate a columnarized report. If
9827c478bd9Sstevel@tonic-gate * the <xtndflag> variable is not zero, it will include the
9837c478bd9Sstevel@tonic-gate * extended set of information (password aging info, home
9847c478bd9Sstevel@tonic-gate * directory, shell process, etc.). If <expflag> is not
9857c478bd9Sstevel@tonic-gate * zero, it will display password expiration information.
9867c478bd9Sstevel@tonic-gate *
9877c478bd9Sstevel@tonic-gate * Arguments:
9887c478bd9Sstevel@tonic-gate * pipeflag int
9897c478bd9Sstevel@tonic-gate * TRUE if a parsable report is needed,
9907c478bd9Sstevel@tonic-gate * FALSE if a columnar report is needed
9917c478bd9Sstevel@tonic-gate * xtndflag int
9927c478bd9Sstevel@tonic-gate * TRUE if extended set of info is to be displayed,
9937c478bd9Sstevel@tonic-gate * FALSE otherwise
9947c478bd9Sstevel@tonic-gate * expflag int
9957c478bd9Sstevel@tonic-gate * TRUE if password expiration information is to be
9967c478bd9Sstevel@tonic-gate * displayed, FALSE otherwise
9977c478bd9Sstevel@tonic-gate *
9987c478bd9Sstevel@tonic-gate * Returns: void
9997c478bd9Sstevel@tonic-gate */
10007c478bd9Sstevel@tonic-gate
10017c478bd9Sstevel@tonic-gate static void
genuidreport(int pipeflag,int xtndflag,int expflag)100220d7339fSgww genuidreport(int pipeflag, int xtndflag, int expflag)
10037c478bd9Sstevel@tonic-gate {
10047c478bd9Sstevel@tonic-gate
10057c478bd9Sstevel@tonic-gate struct display *current; /* Data being displayed */
10067c478bd9Sstevel@tonic-gate
10077c478bd9Sstevel@tonic-gate
10087c478bd9Sstevel@tonic-gate /*
10097c478bd9Sstevel@tonic-gate * Initialization for loop.
101020d7339fSgww * (NOTE: The first element in the list of logins to display is
101120d7339fSgww * a dummy element.)
10127c478bd9Sstevel@tonic-gate */
10137c478bd9Sstevel@tonic-gate current = displayhead;
10147c478bd9Sstevel@tonic-gate
10157c478bd9Sstevel@tonic-gate /*
10167c478bd9Sstevel@tonic-gate * Display elements in the list
10177c478bd9Sstevel@tonic-gate */
101820d7339fSgww if (pipeflag) {
101920d7339fSgww for (current = displayhead->nextuid; current;
102020d7339fSgww current = current->nextuid) {
102120d7339fSgww writeunformatted(current, xtndflag, expflag);
102220d7339fSgww }
102320d7339fSgww } else {
102420d7339fSgww for (current = displayhead->nextuid; current;
102520d7339fSgww current = current->nextuid) {
102620d7339fSgww writeformatted(current, xtndflag, expflag);
102720d7339fSgww }
102820d7339fSgww }
10297c478bd9Sstevel@tonic-gate }
103020d7339fSgww
103120d7339fSgww
10327c478bd9Sstevel@tonic-gate /*
10337c478bd9Sstevel@tonic-gate * void genlogreport(pipeflag, xtndflag, expflag)
10347c478bd9Sstevel@tonic-gate * int pipeflag
10357c478bd9Sstevel@tonic-gate * int xtndflag
10367c478bd9Sstevel@tonic-gate * int expflag
10377c478bd9Sstevel@tonic-gate *
10387c478bd9Sstevel@tonic-gate * This function generates a report on the standard output
10397c478bd9Sstevel@tonic-gate * stream (stdout) containing the login-IDs in the list of
10407c478bd9Sstevel@tonic-gate * logins built by this command. The list is ordered based
10417c478bd9Sstevel@tonic-gate * on user name. If the <pipeflag> variable is not zero, it
10427c478bd9Sstevel@tonic-gate * will generate a report containing parsable records.
10437c478bd9Sstevel@tonic-gate * Otherwise, it will generate a columnarized report. If
10447c478bd9Sstevel@tonic-gate * the <xtndflag> variable is not zero, it will include the
10457c478bd9Sstevel@tonic-gate * extended set of information (password aging info, home
10467c478bd9Sstevel@tonic-gate * directory, shell process, etc.). If <expflag> is not
10477c478bd9Sstevel@tonic-gate * zero, it will include password expiration information.
10487c478bd9Sstevel@tonic-gate *
10497c478bd9Sstevel@tonic-gate * Arguments:
10507c478bd9Sstevel@tonic-gate * pipeflag int
10517c478bd9Sstevel@tonic-gate * TRUE if a parsable report is needed,
10527c478bd9Sstevel@tonic-gate * FALSE if a columnar report is needed
10537c478bd9Sstevel@tonic-gate * xtndflag int
10547c478bd9Sstevel@tonic-gate * TRUE if extended set of info is to be displayed,
10557c478bd9Sstevel@tonic-gate * FALSE otherwise
10567c478bd9Sstevel@tonic-gate * expflag int
10577c478bd9Sstevel@tonic-gate * TRUE if password expiration information is to
10587c478bd9Sstevel@tonic-gate * be displayed, FALSE otherwise
10597c478bd9Sstevel@tonic-gate *
10607c478bd9Sstevel@tonic-gate * Returns: void
10617c478bd9Sstevel@tonic-gate */
10627c478bd9Sstevel@tonic-gate
10637c478bd9Sstevel@tonic-gate static void
genlogreport(int pipeflag,int xtndflag,int expflag)106420d7339fSgww genlogreport(int pipeflag, int xtndflag, int expflag)
10657c478bd9Sstevel@tonic-gate {
10667c478bd9Sstevel@tonic-gate struct display *p; /* Value being displayed */
10677c478bd9Sstevel@tonic-gate
10687c478bd9Sstevel@tonic-gate
10697c478bd9Sstevel@tonic-gate /*
10707c478bd9Sstevel@tonic-gate * Initialization for loop.
107120d7339fSgww * (NOTE: The first element in the list of logins to display is
107220d7339fSgww * a dummy element.)
10737c478bd9Sstevel@tonic-gate */
10747c478bd9Sstevel@tonic-gate p = displayhead;
10757c478bd9Sstevel@tonic-gate
10767c478bd9Sstevel@tonic-gate /*
10777c478bd9Sstevel@tonic-gate * Display elements in the list
10787c478bd9Sstevel@tonic-gate */
107920d7339fSgww if (pipeflag) {
108020d7339fSgww for (p = displayhead->nextlogin; p; p = p->nextlogin) {
108120d7339fSgww writeunformatted(p, xtndflag, expflag);
108220d7339fSgww }
108320d7339fSgww } else {
108420d7339fSgww for (p = displayhead->nextlogin; p; p = p->nextlogin) {
108520d7339fSgww writeformatted(p, xtndflag, expflag);
108620d7339fSgww }
108720d7339fSgww }
10887c478bd9Sstevel@tonic-gate }
108920d7339fSgww
10907c478bd9Sstevel@tonic-gate struct localpw {
10917c478bd9Sstevel@tonic-gate struct localpw *next;
10927c478bd9Sstevel@tonic-gate struct passwd pw;
10937c478bd9Sstevel@tonic-gate };
10947c478bd9Sstevel@tonic-gate
10957c478bd9Sstevel@tonic-gate struct localpw *pwtable = NULL;
10967c478bd9Sstevel@tonic-gate
109720d7339fSgww /* Local passwd pointer for getpwent() -- -1 means not in use, NULL for EOF */
109820d7339fSgww struct localpw *pwptr;
10997c478bd9Sstevel@tonic-gate
11007c478bd9Sstevel@tonic-gate int in_localgetpwent = 0; /* Set if in local_getpwent */
11017c478bd9Sstevel@tonic-gate
1102b816ddf8Sgww static struct localpw *
fill_localpw(struct localpw * lpw,struct passwd * pw)1103b816ddf8Sgww fill_localpw(struct localpw *lpw, struct passwd *pw) {
1104b816ddf8Sgww struct localpw *cur;
1105b816ddf8Sgww
1106b816ddf8Sgww /*
1107b816ddf8Sgww * Copy the data -- we have to alloc areas for it all
1108b816ddf8Sgww */
1109b816ddf8Sgww lpw->pw.pw_name = strdup(pw->pw_name);
1110b816ddf8Sgww lpw->pw.pw_passwd = strdup(pw->pw_passwd);
1111b816ddf8Sgww lpw->pw.pw_uid = pw->pw_uid;
1112b816ddf8Sgww lpw->pw.pw_gid = pw->pw_gid;
1113b816ddf8Sgww lpw->pw.pw_age = strdup(pw->pw_age);
1114b816ddf8Sgww lpw->pw.pw_comment = strdup(pw->pw_comment);
1115b816ddf8Sgww lpw->pw.pw_gecos = strdup(pw->pw_gecos);
1116b816ddf8Sgww lpw->pw.pw_dir = strdup(pw->pw_dir);
1117b816ddf8Sgww lpw->pw.pw_shell = strdup(pw->pw_shell);
1118b816ddf8Sgww
1119b816ddf8Sgww cur = lpw;
1120b816ddf8Sgww lpw->next = malloc(sizeof (struct localpw));
1121b816ddf8Sgww return (cur);
1122b816ddf8Sgww }
1123b816ddf8Sgww
11247c478bd9Sstevel@tonic-gate void
build_localpw(struct reqlogin * req_head)1125b816ddf8Sgww build_localpw(struct reqlogin *req_head)
11267c478bd9Sstevel@tonic-gate {
11277c478bd9Sstevel@tonic-gate struct localpw *next, *cur;
11287c478bd9Sstevel@tonic-gate struct passwd *pw;
1129b816ddf8Sgww struct reqlogin *req_next;
11307c478bd9Sstevel@tonic-gate
1131b816ddf8Sgww next = malloc(sizeof (struct localpw));
11327c478bd9Sstevel@tonic-gate
11337c478bd9Sstevel@tonic-gate pwtable = next;
11347c478bd9Sstevel@tonic-gate
1135b816ddf8Sgww req_next = req_head;
1136b816ddf8Sgww
1137b816ddf8Sgww while (req_next != NULL) {
1138b816ddf8Sgww if ((pw = getpwnam(req_next->loginname)) != NULL) {
1139b816ddf8Sgww /*
1140b816ddf8Sgww * Copy the data -- we have to alloc areas for it all
1141b816ddf8Sgww */
1142b816ddf8Sgww cur = fill_localpw(next, pw);
1143b816ddf8Sgww req_next->found = TRUE;
1144b816ddf8Sgww next = cur->next;
1145b816ddf8Sgww }
1146b816ddf8Sgww
1147b816ddf8Sgww req_next = req_next->next;
11487c478bd9Sstevel@tonic-gate }
11497c478bd9Sstevel@tonic-gate
1150b816ddf8Sgww if (req_head == NULL) {
1151b816ddf8Sgww while ((pw = getpwent()) != NULL) {
1152b816ddf8Sgww /*
1153b816ddf8Sgww * Copy the data -- we have to alloc areas for it all
1154b816ddf8Sgww */
1155b816ddf8Sgww cur = fill_localpw(next, pw);
1156b816ddf8Sgww next = cur->next;
1157b816ddf8Sgww }
1158b816ddf8Sgww }
11597c478bd9Sstevel@tonic-gate
116020d7339fSgww if (pwtable == next) {
11617c478bd9Sstevel@tonic-gate pwtable = NULL;
116220d7339fSgww } else {
1163b816ddf8Sgww free(next);
11647c478bd9Sstevel@tonic-gate cur->next = NULL;
116520d7339fSgww }
11667c478bd9Sstevel@tonic-gate
11677c478bd9Sstevel@tonic-gate endpwent();
11687c478bd9Sstevel@tonic-gate }
11697c478bd9Sstevel@tonic-gate
11707c478bd9Sstevel@tonic-gate struct passwd *
local_getpwent(void)117120d7339fSgww local_getpwent(void)
11727c478bd9Sstevel@tonic-gate {
11737c478bd9Sstevel@tonic-gate if (!in_localgetpwent) {
11747c478bd9Sstevel@tonic-gate in_localgetpwent = 1;
11757c478bd9Sstevel@tonic-gate pwptr = pwtable;
117620d7339fSgww } else if (pwptr != NULL) {
11777c478bd9Sstevel@tonic-gate pwptr = pwptr->next;
117820d7339fSgww }
11797c478bd9Sstevel@tonic-gate
11807c478bd9Sstevel@tonic-gate if (pwptr != NULL)
118120d7339fSgww return (&(pwptr->pw));
11827c478bd9Sstevel@tonic-gate else
118320d7339fSgww return (NULL);
11847c478bd9Sstevel@tonic-gate }
11857c478bd9Sstevel@tonic-gate
11867c478bd9Sstevel@tonic-gate void
local_endpwent(void)118720d7339fSgww local_endpwent(void)
11887c478bd9Sstevel@tonic-gate {
11897c478bd9Sstevel@tonic-gate in_localgetpwent = 0;
11907c478bd9Sstevel@tonic-gate }
11917c478bd9Sstevel@tonic-gate
11927c478bd9Sstevel@tonic-gate long
local_pwtell(void)119320d7339fSgww local_pwtell(void)
11947c478bd9Sstevel@tonic-gate {
119520d7339fSgww return ((long)pwptr);
11967c478bd9Sstevel@tonic-gate }
11977c478bd9Sstevel@tonic-gate
11987c478bd9Sstevel@tonic-gate void
local_pwseek(long ptr)119920d7339fSgww local_pwseek(long ptr)
12007c478bd9Sstevel@tonic-gate {
12017c478bd9Sstevel@tonic-gate pwptr = (struct localpw *)ptr;
12027c478bd9Sstevel@tonic-gate }
120320d7339fSgww
12047c478bd9Sstevel@tonic-gate /*
12057c478bd9Sstevel@tonic-gate * logins [-admopstux] [-l logins] [-g groups]
12067c478bd9Sstevel@tonic-gate *
12077c478bd9Sstevel@tonic-gate * This command generates a report of logins administered on
12087c478bd9Sstevel@tonic-gate * the system. The list will contain logins that meet criteria
12097c478bd9Sstevel@tonic-gate * described by the options in the list. If there are no options,
12107c478bd9Sstevel@tonic-gate * it will list all logins administered. It is intended to be used
12117c478bd9Sstevel@tonic-gate * only by administrators.
12127c478bd9Sstevel@tonic-gate *
12137c478bd9Sstevel@tonic-gate * Options:
12147c478bd9Sstevel@tonic-gate * -a Display password expiration information.
12157c478bd9Sstevel@tonic-gate * -d list all logins that share user-IDs with another
12167c478bd9Sstevel@tonic-gate * login.
12177c478bd9Sstevel@tonic-gate * -g groups specifies the names of the groups to which a login
12187c478bd9Sstevel@tonic-gate * must belong before it is included in the generated
12197c478bd9Sstevel@tonic-gate * list. "groups" is a comma-list of group names.
12207c478bd9Sstevel@tonic-gate * -l logins specifies the logins to display. "logins" is a
12217c478bd9Sstevel@tonic-gate * comma-list of login names.
12227c478bd9Sstevel@tonic-gate * -m in addition to the usual information, for each
12237c478bd9Sstevel@tonic-gate * login displayed, list all groups to which that
12247c478bd9Sstevel@tonic-gate * login is member.
12257c478bd9Sstevel@tonic-gate * -o generate a report as a colon-list instead of in a
12267c478bd9Sstevel@tonic-gate * columnar format
12277c478bd9Sstevel@tonic-gate * -p list all logins that have no password.
12287c478bd9Sstevel@tonic-gate * -s list all system logins
12297c478bd9Sstevel@tonic-gate * -t sort the report lexicographically by login name
12307c478bd9Sstevel@tonic-gate * instead of by user-ID
12317c478bd9Sstevel@tonic-gate * -u list all user logins
12327c478bd9Sstevel@tonic-gate * -x in addition to the usual information, display an
12337c478bd9Sstevel@tonic-gate * extended set of information that includes the home
12347c478bd9Sstevel@tonic-gate * directory, initial process, and password status and
12357c478bd9Sstevel@tonic-gate * aging information
12367c478bd9Sstevel@tonic-gate *
12377c478bd9Sstevel@tonic-gate * Exit Codes:
12387c478bd9Sstevel@tonic-gate * 0 All's well that ends well
12397c478bd9Sstevel@tonic-gate * 1 Usage error
12407c478bd9Sstevel@tonic-gate */
12417c478bd9Sstevel@tonic-gate
124220d7339fSgww int
main(int argc,char * argv[])124320d7339fSgww main(int argc, char *argv[])
12447c478bd9Sstevel@tonic-gate {
124520d7339fSgww struct passwd *plookpwd; /* Ptr to searcher pw (-d) */
124620d7339fSgww struct reqgrp *reqgrphead; /* Head of the req'd group list */
124720d7339fSgww struct reqgrp *pgrp; /* Current item in req'd group list */
124820d7339fSgww struct reqgrp *qgrp; /* Prev item in the req'd group list */
124920d7339fSgww struct reqlogin *reqloginhead; /* Head of req'd login list */
125020d7339fSgww struct reqlogin *plogin; /* Current item in req'd login list */
125120d7339fSgww struct reqlogin *qlogin; /* Prev item in req'd login list */
125220d7339fSgww struct passwd *pwent; /* /etc/passwd entry */
125320d7339fSgww struct group *grent; /* /etc/group entry */
125420d7339fSgww char *token; /* Token extracted by strtok() */
125520d7339fSgww char **pp; /* Group member */
125620d7339fSgww char *g_arg; /* -g option's argument */
125720d7339fSgww char *l_arg; /* -l option's argument */
125820d7339fSgww long lookpos; /* File pos'n, rec we're looking for */
125920d7339fSgww int a_seen; /* Is -a requested? */
126020d7339fSgww int d_seen; /* Is -d requested? */
126120d7339fSgww int g_seen; /* Is -g requested? */
126220d7339fSgww int l_seen; /* Is -l requested? */
126320d7339fSgww int m_seen; /* Is -m requested? */
126420d7339fSgww int o_seen; /* Is -o requested? */
126520d7339fSgww int p_seen; /* Is -p requested? */
126620d7339fSgww int s_seen; /* Is -s requested? */
126720d7339fSgww int t_seen; /* Is -t requested? */
126820d7339fSgww int u_seen; /* Is -u requested? */
126920d7339fSgww int x_seen; /* Is -x requested? */
127020d7339fSgww int errflg; /* Is there a command-line problem */
127120d7339fSgww int done; /* Is the process (?) is complete */
127220d7339fSgww int groupcount; /* Number of groups specified */
127320d7339fSgww int doall; /* Are all logins to be reported */
127420d7339fSgww int c; /* Character returned from getopt() */
12757c478bd9Sstevel@tonic-gate
12767c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
12777c478bd9Sstevel@tonic-gate
12787c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
127920d7339fSgww #define TEXT_DOMAIN "SYS_TEST"
12807c478bd9Sstevel@tonic-gate #endif
12817c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
12827c478bd9Sstevel@tonic-gate
12837c478bd9Sstevel@tonic-gate /* Initializations */
12847c478bd9Sstevel@tonic-gate initmsg(argv[0]);
12857c478bd9Sstevel@tonic-gate
12867c478bd9Sstevel@tonic-gate
12877c478bd9Sstevel@tonic-gate
128820d7339fSgww /* Command-line processing */
12897c478bd9Sstevel@tonic-gate
12907c478bd9Sstevel@tonic-gate /* Initializations */
12917c478bd9Sstevel@tonic-gate a_seen = FALSE;
12927c478bd9Sstevel@tonic-gate d_seen = FALSE;
12937c478bd9Sstevel@tonic-gate g_seen = FALSE;
12947c478bd9Sstevel@tonic-gate l_seen = FALSE;
12957c478bd9Sstevel@tonic-gate m_seen = FALSE;
12967c478bd9Sstevel@tonic-gate o_seen = FALSE;
12977c478bd9Sstevel@tonic-gate p_seen = FALSE;
12987c478bd9Sstevel@tonic-gate s_seen = FALSE;
12997c478bd9Sstevel@tonic-gate t_seen = FALSE;
13007c478bd9Sstevel@tonic-gate u_seen = FALSE;
13017c478bd9Sstevel@tonic-gate x_seen = FALSE;
13027c478bd9Sstevel@tonic-gate errflg = FALSE;
13037c478bd9Sstevel@tonic-gate opterr = 0;
13047c478bd9Sstevel@tonic-gate while (!errflg && ((c = getopt(argc, argv, OPTSTR)) != EOF)) {
13057c478bd9Sstevel@tonic-gate
130620d7339fSgww /* Case on the option character */
130720d7339fSgww switch (c) {
130820d7339fSgww
130920d7339fSgww /*
131020d7339fSgww * -a option:
131120d7339fSgww * Display password expiration information
131220d7339fSgww */
131320d7339fSgww
131420d7339fSgww case 'a':
131520d7339fSgww if (a_seen)
131620d7339fSgww errflg = TRUE;
131720d7339fSgww else
131820d7339fSgww a_seen = TRUE;
131920d7339fSgww break;
132020d7339fSgww
132120d7339fSgww /*
132220d7339fSgww * -d option:
132320d7339fSgww * Display logins which share user-IDs with other logins
132420d7339fSgww */
132520d7339fSgww
132620d7339fSgww case 'd':
132720d7339fSgww if (d_seen)
132820d7339fSgww errflg = TRUE;
132920d7339fSgww else
133020d7339fSgww d_seen = TRUE;
133120d7339fSgww break;
133220d7339fSgww
133320d7339fSgww /*
133420d7339fSgww * -g <groups> option:
133520d7339fSgww * Display the specified groups
133620d7339fSgww */
133720d7339fSgww
133820d7339fSgww case 'g':
133920d7339fSgww if (g_seen) {
134020d7339fSgww errflg = TRUE;
134120d7339fSgww } else {
134220d7339fSgww g_seen = TRUE;
134320d7339fSgww g_arg = optarg;
134420d7339fSgww }
134520d7339fSgww break;
134620d7339fSgww
134720d7339fSgww /*
134820d7339fSgww * -l <logins> option:
134920d7339fSgww * Display the specified logins
135020d7339fSgww */
135120d7339fSgww
135220d7339fSgww case 'l':
135320d7339fSgww if (l_seen) {
135420d7339fSgww errflg = TRUE;
135520d7339fSgww } else {
135620d7339fSgww l_seen = TRUE;
135720d7339fSgww l_arg = optarg;
135820d7339fSgww }
135920d7339fSgww break;
136020d7339fSgww
136120d7339fSgww /*
136220d7339fSgww * -m option:
136320d7339fSgww * Display multiple group information
136420d7339fSgww */
136520d7339fSgww
136620d7339fSgww case 'm':
136720d7339fSgww if (m_seen)
136820d7339fSgww errflg = TRUE;
136920d7339fSgww else
137020d7339fSgww m_seen = TRUE;
137120d7339fSgww break;
137220d7339fSgww
137320d7339fSgww /*
137420d7339fSgww * -o option:
137520d7339fSgww * Display information as a colon-list
137620d7339fSgww */
137720d7339fSgww
137820d7339fSgww case 'o':
137920d7339fSgww if (o_seen)
138020d7339fSgww errflg = TRUE;
138120d7339fSgww else
138220d7339fSgww o_seen = TRUE;
138320d7339fSgww break;
138420d7339fSgww
138520d7339fSgww /*
138620d7339fSgww * -p option:
138720d7339fSgww * Select logins that have no password
138820d7339fSgww */
138920d7339fSgww
139020d7339fSgww case 'p':
139120d7339fSgww if (p_seen)
139220d7339fSgww errflg = TRUE;
139320d7339fSgww else
139420d7339fSgww p_seen = TRUE;
139520d7339fSgww break;
139620d7339fSgww
139720d7339fSgww /*
139820d7339fSgww * -s option:
139920d7339fSgww * Select system logins
140020d7339fSgww */
140120d7339fSgww
140220d7339fSgww case 's':
140320d7339fSgww if (s_seen)
140420d7339fSgww errflg = TRUE;
140520d7339fSgww else
140620d7339fSgww s_seen = TRUE;
140720d7339fSgww break;
140820d7339fSgww
140920d7339fSgww /*
141020d7339fSgww * -t option:
141120d7339fSgww * Sort alphabetically by login-ID instead of numerically
141220d7339fSgww * by user-ID
141320d7339fSgww */
141420d7339fSgww
141520d7339fSgww case 't':
141620d7339fSgww if (t_seen)
141720d7339fSgww errflg = TRUE;
141820d7339fSgww else
141920d7339fSgww t_seen = TRUE;
142020d7339fSgww break;
142120d7339fSgww
142220d7339fSgww /*
142320d7339fSgww * -u option:
142420d7339fSgww * Select user logins
142520d7339fSgww */
142620d7339fSgww
142720d7339fSgww case 'u':
142820d7339fSgww if (u_seen)
142920d7339fSgww errflg = TRUE;
143020d7339fSgww else
143120d7339fSgww u_seen = TRUE;
143220d7339fSgww break;
143320d7339fSgww
143420d7339fSgww /*
143520d7339fSgww * -x option:
143620d7339fSgww * Display extended info (init working dir, shell, pwd info)
143720d7339fSgww */
143820d7339fSgww
143920d7339fSgww case 'x':
144020d7339fSgww if (x_seen)
144120d7339fSgww errflg = TRUE;
144220d7339fSgww else
144320d7339fSgww x_seen = TRUE;
144420d7339fSgww break;
144520d7339fSgww
144620d7339fSgww default: /* Oops.... */
144720d7339fSgww errflg = TRUE;
14487c478bd9Sstevel@tonic-gate }
14497c478bd9Sstevel@tonic-gate }
14507c478bd9Sstevel@tonic-gate
14517c478bd9Sstevel@tonic-gate /* Write out a usage message if necessary and quit */
14527c478bd9Sstevel@tonic-gate if (errflg || (optind != argc)) {
145320d7339fSgww wrtmsg(MM_ERROR, MM_NULLACT, MM_NULLTAG, gettext(USAGE_MSG));
145420d7339fSgww exit(1);
14557c478bd9Sstevel@tonic-gate }
14567c478bd9Sstevel@tonic-gate
14577c478bd9Sstevel@tonic-gate /*
14587c478bd9Sstevel@tonic-gate * The following section does preparation work, setting up for
14597c478bd9Sstevel@tonic-gate * building the list of logins to display
14607c478bd9Sstevel@tonic-gate */
14617c478bd9Sstevel@tonic-gate
1462b816ddf8Sgww
14637c478bd9Sstevel@tonic-gate /*
1464b816ddf8Sgww * If -l logins is on the command line, build a list of
1465b816ddf8Sgww * logins we're to generate reports for.
14667c478bd9Sstevel@tonic-gate */
14677c478bd9Sstevel@tonic-gate
1468b816ddf8Sgww if (l_seen) {
1469b816ddf8Sgww reqloginhead = NULL;
1470b816ddf8Sgww if (token = strtok(l_arg, ",")) {
1471b816ddf8Sgww plogin = malloc(sizeof (struct reqlogin));
1472b816ddf8Sgww plogin->loginname = token;
1473b816ddf8Sgww plogin->found = FALSE;
1474b816ddf8Sgww plogin->next = NULL;
1475b816ddf8Sgww reqloginhead = plogin;
1476b816ddf8Sgww qlogin = plogin;
1477b816ddf8Sgww while (token = strtok(NULL, ",")) {
1478b816ddf8Sgww plogin = malloc(sizeof (struct reqlogin));
1479b816ddf8Sgww plogin->loginname = token;
1480b816ddf8Sgww plogin->found = FALSE;
1481b816ddf8Sgww plogin->next = NULL;
1482b816ddf8Sgww qlogin->next = plogin;
1483b816ddf8Sgww qlogin = plogin;
1484b816ddf8Sgww }
1485b816ddf8Sgww }
1486b816ddf8Sgww /*
1487b816ddf8Sgww * Build an in-core structure of just the passwd database
1488b816ddf8Sgww * entries requested. This greatly reduces the time
1489b816ddf8Sgww * to get all entries and filter later.
1490b816ddf8Sgww */
1491b816ddf8Sgww build_localpw(reqloginhead);
1492b816ddf8Sgww } else {
1493b816ddf8Sgww /*
1494b816ddf8Sgww * Build an in-core structure of all passwd database
1495b816ddf8Sgww * entries. This is important since we have to assume that
1496b816ddf8Sgww * getpwent() is going out to one or more network name
1497b816ddf8Sgww * services that could be changing on the fly. This will
1498b816ddf8Sgww * limit us to one pass through the network data.
1499b816ddf8Sgww */
1500b816ddf8Sgww build_localpw(NULL);
1501b816ddf8Sgww }
15027c478bd9Sstevel@tonic-gate
15037c478bd9Sstevel@tonic-gate /*
15047c478bd9Sstevel@tonic-gate * If the -g groups option was on the command line, build a
15057c478bd9Sstevel@tonic-gate * list containing groups we're to list logins for.
15067c478bd9Sstevel@tonic-gate */
15077c478bd9Sstevel@tonic-gate
15087c478bd9Sstevel@tonic-gate if (g_seen) {
150920d7339fSgww groupcount = 0;
151020d7339fSgww reqgrphead = NULL;
151120d7339fSgww if (token = strtok(g_arg, ",")) {
1512b816ddf8Sgww pgrp = malloc(sizeof (struct reqgrp));
151320d7339fSgww pgrp->groupname = token;
151420d7339fSgww pgrp->next = NULL;
151520d7339fSgww groupcount++;
151620d7339fSgww reqgrphead = pgrp;
151720d7339fSgww qgrp = pgrp;
151820d7339fSgww while (token = strtok(NULL, ",")) {
1519b816ddf8Sgww pgrp = malloc(sizeof (struct reqgrp));
152020d7339fSgww pgrp->groupname = token;
152120d7339fSgww pgrp->next = NULL;
152220d7339fSgww groupcount++;
152320d7339fSgww qgrp->next = pgrp;
152420d7339fSgww qgrp = pgrp;
152520d7339fSgww }
15267c478bd9Sstevel@tonic-gate }
15277c478bd9Sstevel@tonic-gate }
15287c478bd9Sstevel@tonic-gate
15297c478bd9Sstevel@tonic-gate
15307c478bd9Sstevel@tonic-gate /*
15317c478bd9Sstevel@tonic-gate * Generate the list of login information to display
15327c478bd9Sstevel@tonic-gate */
15337c478bd9Sstevel@tonic-gate
15347c478bd9Sstevel@tonic-gate /* Initialize the login list */
1535b816ddf8Sgww membershead = NULL;
15367c478bd9Sstevel@tonic-gate
15377c478bd9Sstevel@tonic-gate
15387c478bd9Sstevel@tonic-gate /*
15397c478bd9Sstevel@tonic-gate * If -g groups was specified, generate a list of members
15407c478bd9Sstevel@tonic-gate * of the specified groups
15417c478bd9Sstevel@tonic-gate */
15427c478bd9Sstevel@tonic-gate
15437c478bd9Sstevel@tonic-gate if (g_seen) {
1544b816ddf8Sgww /* For each group mentioned with the -g option ... */
1545b816ddf8Sgww for (pgrp = reqgrphead; (groupcount > 0) && pgrp;
1546b816ddf8Sgww pgrp = pgrp->next) {
1547b816ddf8Sgww if ((grent = getgrnam(pgrp->groupname)) != NULL) {
1548b816ddf8Sgww /*
1549b816ddf8Sgww * Remembering the group-ID for later
1550b816ddf8Sgww */
15517c478bd9Sstevel@tonic-gate
1552b816ddf8Sgww groupcount--;
1553b816ddf8Sgww pgrp->groupID = grent->gr_gid;
1554b816ddf8Sgww for (pp = grent->gr_mem; *pp; pp++) {
1555b816ddf8Sgww addmember(*pp);
155620d7339fSgww }
1557b816ddf8Sgww } else {
155820d7339fSgww wrtmsg(MM_WARNING, MM_NULLACT, MM_NULLTAG,
155920d7339fSgww gettext("%s was not found"),
156020d7339fSgww pgrp->groupname);
156120d7339fSgww }
15627c478bd9Sstevel@tonic-gate }
15637c478bd9Sstevel@tonic-gate }
15647c478bd9Sstevel@tonic-gate
15657c478bd9Sstevel@tonic-gate
15667c478bd9Sstevel@tonic-gate /* Initialize the list of logins to display */
15677c478bd9Sstevel@tonic-gate initdisp();
15687c478bd9Sstevel@tonic-gate
15697c478bd9Sstevel@tonic-gate
15707c478bd9Sstevel@tonic-gate /*
15717c478bd9Sstevel@tonic-gate * Add logins that have user-IDs that are used more than once,
15727c478bd9Sstevel@tonic-gate * if requested. This command is pretty slow, since the algorithm
15737c478bd9Sstevel@tonic-gate * reads from the /etc/passwd file 1+2+3+...+n times where n is the
15747c478bd9Sstevel@tonic-gate * number of login-IDs in the /etc/passwd file. (Actually, this
15757c478bd9Sstevel@tonic-gate * can be optimized so it's not quite that bad, but the order or
15767c478bd9Sstevel@tonic-gate * magnitude stays the same.)
15777c478bd9Sstevel@tonic-gate *
15787c478bd9Sstevel@tonic-gate * Note: This processing needs to be done before any other options
157920d7339fSgww * are processed -- the algorithm contains an optimization
15807c478bd9Sstevel@tonic-gate * that insists on the display list being empty before this
15817c478bd9Sstevel@tonic-gate * option is processed.
15827c478bd9Sstevel@tonic-gate */
15837c478bd9Sstevel@tonic-gate
15847c478bd9Sstevel@tonic-gate if (d_seen) {
15857c478bd9Sstevel@tonic-gate
15867c478bd9Sstevel@tonic-gate /*
158720d7339fSgww * The following code is a quick&dirty reimplementation of the
158820d7339fSgww * original algorithm, which opened the password file twice (to
158920d7339fSgww * get two file pointer into the data) and then used fgetpwent()
159020d7339fSgww * in undocumented ways to scan through the file, checking for
159120d7339fSgww * duplicates. This does not work when getpwent() is used to
159220d7339fSgww * go out over the network, since there is not file pointer.
159320d7339fSgww *
159420d7339fSgww * Instead an in-memory list of passwd structures is built,
159520d7339fSgww * and then this list is scanned. The routines
159620d7339fSgww * Local_getpwent(), etc., are designed to mimic the standard
159720d7339fSgww * library routines, so this code does not have to be
159820d7339fSgww * extensively modified.
159920d7339fSgww */
160020d7339fSgww
160120d7339fSgww /*
160220d7339fSgww * For reference, here is the original comment about the next
160320d7339fSgww * section of code. Some of the code has changed, but the
160420d7339fSgww * algorithm is the same:
160520d7339fSgww *
160620d7339fSgww * Open the system password file once. This instance will be
160720d7339fSgww * used to leaf through the file once, reading each entry once,
160820d7339fSgww * and searching the remainder of the file for another login-ID
160920d7339fSgww * that has the same user-ID. Note that there are lots of
161020d7339fSgww * contortions one has to go through when reading two instances
161120d7339fSgww * of the /etc/passwd file. That's why there's some seeking,
161220d7339fSgww * re-reading of the same record, and other junk. Luckily, this
161320d7339fSgww * feature won't be requested very often, and still isn't too
161420d7339fSgww * slow...
16157c478bd9Sstevel@tonic-gate */
16167c478bd9Sstevel@tonic-gate
161720d7339fSgww /* For each entry in the passwd database ... */
161820d7339fSgww while (plookpwd = local_getpwent()) {
16197c478bd9Sstevel@tonic-gate /*
162020d7339fSgww * Optimization -- If the login's user-ID is already
162120d7339fSgww * in the display list, there's no reason to process
162220d7339fSgww * this entry -- it's already there.
16237c478bd9Sstevel@tonic-gate */
162420d7339fSgww if (!isuidindisp(plookpwd)) {
162520d7339fSgww /*
162620d7339fSgww * Rememeber the current entry's position,
162720d7339fSgww * so when we finish scanning through the
162820d7339fSgww * database looking for duplicates we can
162920d7339fSgww * return to the current place, so that the
163020d7339fSgww * enclosing loop will march in an orderly
163120d7339fSgww * fashion through the passwd database.
163220d7339fSgww */
163320d7339fSgww done = FALSE;
163420d7339fSgww lookpos = local_pwtell();
163520d7339fSgww
163620d7339fSgww /*
163720d7339fSgww * For each record in the passwd database
163820d7339fSgww * beyond the searching record ...
163920d7339fSgww */
164020d7339fSgww while (pwent = local_getpwent()) {
164120d7339fSgww
164220d7339fSgww /*
164320d7339fSgww * If there's a match between the
164420d7339fSgww * searcher's user-ID and the
164520d7339fSgww * searchee's user-ID ...
164620d7339fSgww */
164720d7339fSgww if (pwent->pw_uid == plookpwd->pw_uid) {
164820d7339fSgww /*
164920d7339fSgww * If this is the first
165020d7339fSgww * duplicate of this searcher
165120d7339fSgww * that we find,
165220d7339fSgww * add the searcher's
165320d7339fSgww * record to the display list
165420d7339fSgww * (It wants to be on the
165520d7339fSgww * list first to avoid
165620d7339fSgww * ordering "flakeyness")
165720d7339fSgww */
165820d7339fSgww if (done == FALSE) {
165920d7339fSgww adddisp(plookpwd);
166020d7339fSgww done = TRUE;
166120d7339fSgww }
166220d7339fSgww
166320d7339fSgww /*
166420d7339fSgww * Now add the searchee's
166520d7339fSgww * record
166620d7339fSgww */
166720d7339fSgww adddisp(pwent);
166820d7339fSgww
166920d7339fSgww }
167020d7339fSgww }
167120d7339fSgww /* Reposition to searcher record */
167220d7339fSgww local_pwseek(lookpos);
167320d7339fSgww }
16747c478bd9Sstevel@tonic-gate }
16757c478bd9Sstevel@tonic-gate
167620d7339fSgww local_endpwent();
16777c478bd9Sstevel@tonic-gate }
16787c478bd9Sstevel@tonic-gate
16797c478bd9Sstevel@tonic-gate
16807c478bd9Sstevel@tonic-gate /*
16817c478bd9Sstevel@tonic-gate * Loop through the passwd database squirelling away the
16827c478bd9Sstevel@tonic-gate * information we need for the display.
16837c478bd9Sstevel@tonic-gate *
16847c478bd9Sstevel@tonic-gate * NOTE: Once a login is added to the list, the rest of the
16857c478bd9Sstevel@tonic-gate * body of the loop is bypassed (via a continue statement).
16867c478bd9Sstevel@tonic-gate */
16877c478bd9Sstevel@tonic-gate
16887c478bd9Sstevel@tonic-gate doall = !(s_seen || u_seen || p_seen || d_seen || l_seen || g_seen);
16897c478bd9Sstevel@tonic-gate
16907c478bd9Sstevel@tonic-gate if (doall || s_seen || u_seen || p_seen || l_seen || g_seen) {
16917c478bd9Sstevel@tonic-gate
169220d7339fSgww while (pwent = local_getpwent()) {
169320d7339fSgww done = FALSE;
16947c478bd9Sstevel@tonic-gate
169520d7339fSgww /*
169620d7339fSgww * If no user-specific options were specified,
169720d7339fSgww * include this login-ID
169820d7339fSgww */
169920d7339fSgww if (doall) {
170020d7339fSgww adddisp(pwent);
170120d7339fSgww continue;
170220d7339fSgww }
17037c478bd9Sstevel@tonic-gate
170420d7339fSgww /*
170520d7339fSgww * If the user specified system login-IDs,
170620d7339fSgww * and this is a system ID, include it
170720d7339fSgww */
170820d7339fSgww if (s_seen) {
170920d7339fSgww if (isasystemlogin(pwent)) {
171020d7339fSgww adddisp(pwent);
171120d7339fSgww continue;
171220d7339fSgww }
171320d7339fSgww }
17147c478bd9Sstevel@tonic-gate
171520d7339fSgww /*
171620d7339fSgww * If the user specified user login-IDs,
171720d7339fSgww * and this is a user ID, include it
171820d7339fSgww */
171920d7339fSgww if (u_seen) {
172020d7339fSgww if (isauserlogin(pwent)) {
172120d7339fSgww adddisp(pwent);
172220d7339fSgww continue;
172320d7339fSgww }
172420d7339fSgww }
17257c478bd9Sstevel@tonic-gate
172620d7339fSgww /*
172720d7339fSgww * If the user is asking for login-IDs that have
172820d7339fSgww * no password, and this one has no password, include it
172920d7339fSgww */
173020d7339fSgww if (p_seen) {
173120d7339fSgww if (hasnopasswd(pwent)) {
173220d7339fSgww adddisp(pwent);
173320d7339fSgww continue;
173420d7339fSgww }
173520d7339fSgww }
17367c478bd9Sstevel@tonic-gate
173720d7339fSgww /*
173820d7339fSgww * If specific logins were requested, leaf through
173920d7339fSgww * the list of logins they requested. If this login
174020d7339fSgww * is on the list, include it.
174120d7339fSgww */
174220d7339fSgww if (l_seen) {
174320d7339fSgww for (plogin = reqloginhead; !done && plogin;
174420d7339fSgww plogin = plogin->next) {
174520d7339fSgww if (strcmp(pwent->pw_name,
174620d7339fSgww plogin->loginname) == 0) {
174720d7339fSgww plogin->found = TRUE;
174820d7339fSgww adddisp(pwent);
174920d7339fSgww done = TRUE;
175020d7339fSgww }
175120d7339fSgww }
175220d7339fSgww if (done)
175320d7339fSgww continue;
17547c478bd9Sstevel@tonic-gate }
17557c478bd9Sstevel@tonic-gate
175620d7339fSgww /*
175720d7339fSgww * If specific groups were requested, leaf through the
175820d7339fSgww * list of login-IDs that belong to those groups.
175920d7339fSgww * If this login-ID is in that list, or its primary
176020d7339fSgww * group is one of those requested, include it.
176120d7339fSgww */
17627c478bd9Sstevel@tonic-gate
176320d7339fSgww if (g_seen) {
176420d7339fSgww for (pgrp = reqgrphead; !done && pgrp;
176520d7339fSgww pgrp = pgrp->next) {
176620d7339fSgww if (pwent->pw_gid == pgrp->groupID) {
176720d7339fSgww adddisp(pwent);
176820d7339fSgww done = TRUE;
176920d7339fSgww }
177020d7339fSgww }
177120d7339fSgww if (!done && isamember(pwent->pw_name)) {
177220d7339fSgww adddisp(pwent);
177320d7339fSgww done = TRUE;
177420d7339fSgww }
177520d7339fSgww }
177620d7339fSgww if (done)
177720d7339fSgww continue;
17787c478bd9Sstevel@tonic-gate }
177920d7339fSgww
178020d7339fSgww local_endpwent();
17817c478bd9Sstevel@tonic-gate }
17827c478bd9Sstevel@tonic-gate
178320d7339fSgww /* Let the user know about logins they requested that don't exist */
178420d7339fSgww if (l_seen) {
178520d7339fSgww for (plogin = reqloginhead; plogin; plogin = plogin->next) {
178620d7339fSgww if (!plogin->found) {
178720d7339fSgww wrtmsg(MM_WARNING, MM_NULLACT, MM_NULLTAG,
178820d7339fSgww gettext("%s was not found"),
178920d7339fSgww plogin->loginname);
179020d7339fSgww }
179120d7339fSgww }
179220d7339fSgww }
17937c478bd9Sstevel@tonic-gate
179420d7339fSgww /* Apply group information */
17957c478bd9Sstevel@tonic-gate applygroup(m_seen);
17967c478bd9Sstevel@tonic-gate
17977c478bd9Sstevel@tonic-gate
17987c478bd9Sstevel@tonic-gate /*
17997c478bd9Sstevel@tonic-gate * Apply password information (only needed if the extended
18007c478bd9Sstevel@tonic-gate * set of information has been requested)
18017c478bd9Sstevel@tonic-gate */
180220d7339fSgww if (x_seen || a_seen)
180320d7339fSgww applypasswd();
18047c478bd9Sstevel@tonic-gate
18057c478bd9Sstevel@tonic-gate
18067c478bd9Sstevel@tonic-gate /*
180720d7339fSgww * Generate a report from this display items we've squirreled away
18087c478bd9Sstevel@tonic-gate */
18097c478bd9Sstevel@tonic-gate
181020d7339fSgww if (t_seen)
181120d7339fSgww genlogreport(o_seen, x_seen, a_seen);
181220d7339fSgww else
181320d7339fSgww genuidreport(o_seen, x_seen, a_seen);
18147c478bd9Sstevel@tonic-gate
181520d7339fSgww /* We're through! */
181620d7339fSgww return (0);
18177c478bd9Sstevel@tonic-gate }
1818