1997-03-23 18:51:21 +00:00
|
|
|
/*
|
|
|
|
* Top users/processes display for Unix
|
|
|
|
*
|
|
|
|
* This program may be freely redistributed,
|
|
|
|
* but this entire comment MUST remain intact.
|
|
|
|
*
|
|
|
|
* Copyright (c) 1984, 1989, William LeFebvre, Rice University
|
|
|
|
* Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
|
2002-06-09 19:29:55 +00:00
|
|
|
*
|
|
|
|
* $FreeBSD$
|
1997-03-23 18:51:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Username translation code for top.
|
|
|
|
*
|
|
|
|
* These routines handle uid to username mapping.
|
|
|
|
* They use a hashing table scheme to reduce reading overhead.
|
|
|
|
* For the time being, these are very straightforward hashing routines.
|
|
|
|
* Maybe someday I'll put in something better. But with the advent of
|
|
|
|
* "random access" password files, it might not be worth the effort.
|
|
|
|
*
|
|
|
|
* Changes to these have been provided by John Gilmore (gnu@toad.com).
|
|
|
|
*
|
|
|
|
* The hash has been simplified in this release, to avoid the
|
|
|
|
* table overflow problems of previous releases. If the value
|
|
|
|
* at the initial hash location is not right, it is replaced
|
|
|
|
* by the right value. Collisions will cause us to call getpw*
|
|
|
|
* but hey, this is a cache, not the Library of Congress.
|
|
|
|
* This makes the table size independent of the passwd file size.
|
|
|
|
*/
|
|
|
|
|
2009-12-25 09:02:41 +00:00
|
|
|
#include <sys/param.h>
|
2016-05-22 04:17:00 +00:00
|
|
|
|
1997-03-23 18:51:21 +00:00
|
|
|
#include <pwd.h>
|
2018-06-02 00:02:15 +00:00
|
|
|
#include <stdbool.h>
|
2016-05-22 04:17:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
#include "utils.h"
|
2016-05-22 04:17:00 +00:00
|
|
|
#include "username.h"
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
struct hash_el {
|
2018-06-03 05:20:11 +00:00
|
|
|
int uid;
|
2009-12-25 09:02:41 +00:00
|
|
|
char name[MAXLOGNAME];
|
1997-03-23 18:51:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define is_empty_hash(x) (hash_table[x].name[0] == 0)
|
|
|
|
|
|
|
|
/* simple minded hashing function */
|
2018-06-03 05:20:11 +00:00
|
|
|
#define hashit(i) (abs(i) % Table_size)
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
/* K&R requires that statically declared tables be initialized to zero. */
|
|
|
|
/* We depend on that for hash_table and YOUR compiler had BETTER do it! */
|
2018-05-21 09:25:21 +00:00
|
|
|
static struct hash_el hash_table[Table_size];
|
1997-03-23 18:51:21 +00:00
|
|
|
|
2016-05-22 04:17:00 +00:00
|
|
|
|
2018-06-13 08:52:22 +00:00
|
|
|
char *
|
|
|
|
username(int uid)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-05-20 01:32:27 +00:00
|
|
|
int hashindex;
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
hashindex = hashit(uid);
|
|
|
|
if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid))
|
|
|
|
{
|
|
|
|
/* not here or not right -- get it out of passwd */
|
|
|
|
hashindex = get_user(uid);
|
|
|
|
}
|
|
|
|
return(hash_table[hashindex].name);
|
|
|
|
}
|
|
|
|
|
2018-06-13 08:52:22 +00:00
|
|
|
int
|
2019-02-10 13:34:21 +00:00
|
|
|
userid(char username_[])
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
struct passwd *pwd;
|
|
|
|
|
|
|
|
/* Eventually we want this to enter everything in the hash table,
|
|
|
|
but for now we just do it simply and remember just the result.
|
|
|
|
*/
|
|
|
|
|
2019-02-10 13:34:21 +00:00
|
|
|
if ((pwd = getpwnam(username_)) == NULL)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enter the result in the hash table */
|
2019-02-10 13:34:21 +00:00
|
|
|
enter_user(pwd->pw_uid, username_, 1);
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
/* return our result */
|
|
|
|
return(pwd->pw_uid);
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:02:15 +00:00
|
|
|
/* wecare 1 = enter it always, 0 = nice to have */
|
2018-06-13 08:52:22 +00:00
|
|
|
int
|
|
|
|
enter_user(int uid, char name[], bool wecare)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-05-20 01:32:27 +00:00
|
|
|
int hashindex;
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "enter_hash(%d, %s, %d)\n", uid, name, wecare);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
hashindex = hashit(uid);
|
|
|
|
|
|
|
|
if (!is_empty_hash(hashindex))
|
|
|
|
{
|
|
|
|
if (!wecare)
|
2018-06-08 01:55:47 +00:00
|
|
|
return (0); /* Don't clobber a slot for trash */
|
1997-03-23 18:51:21 +00:00
|
|
|
if (hash_table[hashindex].uid == uid)
|
|
|
|
return(hashindex); /* Fortuitous find */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* empty or wrong slot -- fill it with new value */
|
|
|
|
hash_table[hashindex].uid = uid;
|
2009-12-25 09:02:41 +00:00
|
|
|
(void) strncpy(hash_table[hashindex].name, name, MAXLOGNAME - 1);
|
1997-03-23 18:51:21 +00:00
|
|
|
return(hashindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a userid->name mapping from the system.
|
|
|
|
*/
|
|
|
|
|
2018-05-20 23:37:30 +00:00
|
|
|
int
|
2018-06-03 05:20:11 +00:00
|
|
|
get_user(int uid)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
struct passwd *pwd;
|
|
|
|
|
|
|
|
/* no performance penalty for using getpwuid makes it easy */
|
|
|
|
if ((pwd = getpwuid(uid)) != NULL)
|
|
|
|
{
|
2018-06-10 06:21:51 +00:00
|
|
|
return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if we can't find the name at all, then use the uid as the name */
|
|
|
|
return(enter_user(uid, itoa7(uid), 1));
|
|
|
|
}
|