[BACK]Return to username.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / top

Annotation of src/usr.bin/top/username.c, Revision 1.11

1.11    ! millert     1: /* $OpenBSD: username.c,v 1.10 2003/06/13 21:52:25 deraadt Exp $        */
1.1       downsj      2:
                      3: /*
                      4:  *  Top users/processes display for Unix
                      5:  *  Version 3
                      6:  *
1.7       deraadt     7:  * Copyright (c) 1984, 1989, William LeFebvre, Rice University
                      8:  * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
1.1       downsj      9:  *
1.7       deraadt    10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     20:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     21:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     22:  * IN NO EVENT SHALL THE AUTHOR OR HIS EMPLOYER BE LIABLE FOR ANY DIRECT, INDIRECT,
                     23:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     24:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     25:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     26:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     27:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     28:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       downsj     29:  */
                     30:
                     31: /*
                     32:  *  Username translation code for top.
                     33:  *
                     34:  *  These routines handle uid to username mapping.
                     35:  *  They use a hashing table scheme to reduce reading overhead.
                     36:  *  For the time being, these are very straightforward hashing routines.
                     37:  *  Maybe someday I'll put in something better.  But with the advent of
                     38:  *  "random access" password files, it might not be worth the effort.
                     39:  *
                     40:  *  Changes to these have been provided by John Gilmore (gnu@toad.com).
                     41:  *
                     42:  *  The hash has been simplified in this release, to avoid the
                     43:  *  table overflow problems of previous releases.  If the value
                     44:  *  at the initial hash location is not right, it is replaced
                     45:  *  by the right value.  Collisions will cause us to call getpw*
                     46:  *  but hey, this is a cache, not the Library of Congress.
                     47:  *  This makes the table size independent of the passwd file size.
                     48:  */
                     49:
1.2       downsj     50: #include <sys/types.h>
1.1       downsj     51: #include <stdio.h>
1.2       downsj     52: #include <string.h>
1.1       downsj     53: #include <pwd.h>
                     54:
                     55: #include "top.local.h"
                     56: #include "utils.h"
                     57:
                     58: struct hash_el {
1.11    ! millert    59:        uid_t   uid;
        !            60:        char    name[9];
1.1       downsj     61: };
                     62:
1.9       deraadt    63: static int      enter_user(uid_t, char *, int);
                     64: static int      get_user(uid_t);
1.2       downsj     65:
1.11    ! millert    66: #define        is_empty_hash(x)        (hash_table[x].name[0] == 0)
1.1       downsj     67:
                     68: /* simple minded hashing function */
1.9       deraadt    69: /*
                     70:  * Uid "nobody" is -2 results in hashit(-2) = -2 which is out of bounds for
                     71:  * the hash_table.  Applied abs() function to fix. 2/16/96 tpugh
                     72:  */
1.11    ! millert    73: #define        hashit(i)       (abs(i) % Table_size)
1.1       downsj     74:
                     75: /* K&R requires that statically declared tables be initialized to zero. */
                     76: /* We depend on that for hash_table and YOUR compiler had BETTER do it! */
1.9       deraadt    77: struct hash_el  hash_table[Table_size];
1.1       downsj     78:
1.8       pvalchev   79: char *
                     80: username(uid_t uid)
1.1       downsj     81: {
1.9       deraadt    82:        int hashindex;
1.1       downsj     83:
1.9       deraadt    84:        hashindex = hashit(uid);
                     85:        if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid)) {
                     86:                /* not here or not right -- get it out of passwd */
                     87:                hashindex = get_user(uid);
                     88:        }
                     89:        return (hash_table[hashindex].name);
1.1       downsj     90: }
                     91:
1.8       pvalchev   92: uid_t
                     93: userid(char *username)
1.1       downsj     94: {
1.9       deraadt    95:        struct passwd *pwd;
                     96:
                     97:        /*
                     98:         * Eventually we want this to enter everything in the hash table, but
                     99:         * for now we just do it simply and remember just the result.
                    100:         */
                    101:        if ((pwd = getpwnam(username)) == NULL)
1.11    ! millert   102:                return ((uid_t)-1);
1.1       downsj    103:
1.9       deraadt   104:        /* enter the result in the hash table */
                    105:        enter_user(pwd->pw_uid, username, 1);
1.1       downsj    106:
1.9       deraadt   107:        /* return our result */
                    108:        return (pwd->pw_uid);
1.1       downsj    109: }
                    110:
1.9       deraadt   111: /*
                    112:  * wecare: 1 = enter it always, 0 = nice to have
                    113:  */
1.8       pvalchev  114: static int
                    115: enter_user(uid_t uid, char *name, int wecare)
1.1       downsj    116: {
1.9       deraadt   117:        int hashindex;
1.1       downsj    118:
                    119: #ifdef DEBUG
1.9       deraadt   120:        fprintf(stderr, "enter_hash(%u, %s, %d)\n", uid, name, wecare);
1.1       downsj    121: #endif
                    122:
1.9       deraadt   123:        hashindex = hashit(uid);
1.1       downsj    124:
1.9       deraadt   125:        if (!is_empty_hash(hashindex)) {
                    126:                if (!wecare)
                    127:                        return 0;       /* Don't clobber a slot for trash */
                    128:                if (hash_table[hashindex].uid == uid)
                    129:                        return (hashindex);     /* Fortuitous find */
                    130:        }
                    131:        /* empty or wrong slot -- fill it with new value */
                    132:        hash_table[hashindex].uid = uid;
                    133:        (void) strlcpy(hash_table[hashindex].name, name,
                    134:            sizeof(hash_table[hashindex].name));
                    135:        return (hashindex);
1.1       downsj    136: }
                    137:
                    138: /*
                    139:  * Get a userid->name mapping from the system.
                    140:  * If the passwd database is hashed (#define RANDOM_PW), we
                    141:  * just handle this uid.  Otherwise we scan the passwd file
                    142:  * and cache any entries we pass over while looking.
                    143:  */
1.8       pvalchev  144: static int
                    145: get_user(uid_t uid)
1.1       downsj    146: {
1.9       deraadt   147:        struct passwd *pwd;
1.1       downsj    148:
                    149: #ifdef RANDOM_PW
1.9       deraadt   150:        /* no performance penalty for using getpwuid makes it easy */
                    151:        if ((pwd = getpwuid(uid)) != NULL)
                    152:                return (enter_user(pwd->pw_uid, pwd->pw_name, 1));
1.1       downsj    153: #else
                    154:
1.9       deraadt   155:        int from_start = 0;
1.1       downsj    156:
1.9       deraadt   157:        /*
                    158:         *  If we just called getpwuid each time, things would be very slow
                    159:         *  since that just iterates through the passwd file each time.  So,
                    160:         *  we walk through the file instead (using getpwent) and cache each
                    161:         *  entry as we go.  Once the right record is found, we cache it and
                    162:         *  return immediately.  The next time we come in, getpwent will get
                    163:         *  the next record.  In theory, we never have to read the passwd file
                    164:         *  a second time (because we cache everything we read).  But in
                    165:         *  practice, the cache may not be large enough, so if we don't find
                    166:         *  it the first time we have to scan the file a second time.  This
                    167:         *  is not very efficient, but it will do for now.
                    168:         */
                    169:        while (from_start++ < 2) {
                    170:                while ((pwd = getpwent()) != NULL) {
                    171:                        if (pwd->pw_uid == uid)
                    172:                                return (enter_user(pwd->pw_uid, pwd->pw_name, 1));
                    173:                        (void) enter_user(pwd->pw_uid, pwd->pw_name, 0);
                    174:                }
                    175:                /* try again */
                    176:                setpwent();
1.1       downsj    177:        }
                    178: #endif
1.9       deraadt   179:        /* if we can't find the name at all, then use the uid as the name */
                    180:        return (enter_user(uid, itoa7(uid), 1));
1.1       downsj    181: }