Annotation of src/usr.bin/top/username.c, Revision 1.7
1.7 ! deraadt 1: /* $OpenBSD: username.c,v 1.6 2002/06/23 03:07:22 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.2 downsj 59: uid_t uid;
1.1 downsj 60: char name[9];
61: };
62:
1.5 millert 63: static int enter_user(uid_t, char *, int);
64: static int get_user(uid_t);
1.2 downsj 65:
1.1 downsj 66: #define is_empty_hash(x) (hash_table[x].name[0] == 0)
67:
68: /* simple minded hashing function */
69: /* Uid "nobody" is -2 results in hashit(-2) = -2 which is out of bounds for
70: the hash_table. Applied abs() function to fix. 2/16/96 tpugh
71: */
72: #define hashit(i) (abs(i) % Table_size)
73:
74: /* K&R requires that statically declared tables be initialized to zero. */
75: /* We depend on that for hash_table and YOUR compiler had BETTER do it! */
76: struct hash_el hash_table[Table_size];
77:
1.2 downsj 78: void init_hash()
1.1 downsj 79:
80: {
81: /*
82: * There used to be some steps we had to take to initialize things.
83: * We don't need to do that anymore, but we will leave this stub in
84: * just in case future changes require initialization steps.
85: */
86: }
87:
88: char *username(uid)
89:
1.4 mpech 90: uid_t uid;
1.1 downsj 91:
92: {
1.4 mpech 93: int hashindex;
1.1 downsj 94:
95: hashindex = hashit(uid);
96: if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid))
97: {
98: /* not here or not right -- get it out of passwd */
99: hashindex = get_user(uid);
100: }
101: return(hash_table[hashindex].name);
102: }
103:
1.2 downsj 104: uid_t userid(username)
1.1 downsj 105:
106: char *username;
107:
108: {
109: struct passwd *pwd;
110:
111: /* Eventually we want this to enter everything in the hash table,
112: but for now we just do it simply and remember just the result.
113: */
114:
115: if ((pwd = getpwnam(username)) == NULL)
116: {
117: return(-1);
118: }
119:
120: /* enter the result in the hash table */
121: enter_user(pwd->pw_uid, username, 1);
122:
123: /* return our result */
124: return(pwd->pw_uid);
125: }
126:
1.2 downsj 127: static int enter_user(uid, name, wecare)
1.1 downsj 128:
1.4 mpech 129: uid_t uid;
130: char *name;
1.1 downsj 131: int wecare; /* 1 = enter it always, 0 = nice to have */
132:
133: {
1.4 mpech 134: int hashindex;
1.1 downsj 135:
136: #ifdef DEBUG
1.6 deraadt 137: fprintf(stderr, "enter_hash(%u, %s, %d)\n", uid, name, wecare);
1.1 downsj 138: #endif
139:
140: hashindex = hashit(uid);
141:
142: if (!is_empty_hash(hashindex))
143: {
144: if (!wecare)
145: return 0; /* Don't clobber a slot for trash */
146: if (hash_table[hashindex].uid == uid)
147: return(hashindex); /* Fortuitous find */
148: }
149:
150: /* empty or wrong slot -- fill it with new value */
151: hash_table[hashindex].uid = uid;
1.3 deraadt 152: (void) strlcpy(hash_table[hashindex].name, name,
153: sizeof(hash_table[hashindex].name));
1.1 downsj 154: return(hashindex);
155: }
156:
157: /*
158: * Get a userid->name mapping from the system.
159: * If the passwd database is hashed (#define RANDOM_PW), we
160: * just handle this uid. Otherwise we scan the passwd file
161: * and cache any entries we pass over while looking.
162: */
163:
1.2 downsj 164: static int get_user(uid)
1.1 downsj 165:
1.4 mpech 166: uid_t uid;
1.1 downsj 167:
168: {
169: struct passwd *pwd;
170:
171: #ifdef RANDOM_PW
172: /* no performance penalty for using getpwuid makes it easy */
173: if ((pwd = getpwuid(uid)) != NULL)
174: {
175: return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
176: }
177: #else
178:
179: int from_start = 0;
180:
181: /*
182: * If we just called getpwuid each time, things would be very slow
183: * since that just iterates through the passwd file each time. So,
184: * we walk through the file instead (using getpwent) and cache each
185: * entry as we go. Once the right record is found, we cache it and
186: * return immediately. The next time we come in, getpwent will get
187: * the next record. In theory, we never have to read the passwd file
188: * a second time (because we cache everything we read). But in
189: * practice, the cache may not be large enough, so if we don't find
190: * it the first time we have to scan the file a second time. This
191: * is not very efficient, but it will do for now.
192: */
193:
194: while (from_start++ < 2)
195: {
196: while ((pwd = getpwent()) != NULL)
197: {
198: if (pwd->pw_uid == uid)
199: {
200: return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
201: }
202: (void) enter_user(pwd->pw_uid, pwd->pw_name, 0);
203: }
204: /* try again */
205: setpwent();
206: }
207: #endif
208: /* if we can't find the name at all, then use the uid as the name */
209: return(enter_user(uid, itoa7(uid), 1));
210: }