2012-03-02 17:59:36 +05:30
|
|
|
/*
|
|
|
|
* pwcache.c - memory cache passwd file handling
|
|
|
|
*
|
2023-02-07 15:39:48 +05:30
|
|
|
* Copyright © 2011-2023 Jim Warner <james.warner@comcast.net>
|
|
|
|
* Copyright © 2015-2023 Craig Small <csmall@dropbear.xyz>
|
|
|
|
* Copyright © 2002 Albert Cahalan
|
2012-03-02 17:59:36 +05:30
|
|
|
*
|
2023-02-07 15:39:48 +05:30
|
|
|
* Older version:
|
|
|
|
* Copyright © 1992-1998 Michael K. Johnson <johnsonm@redhat.com>
|
|
|
|
* Note: most likely none of his code remains
|
2012-03-02 17:59:36 +05:30
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
#include <stdio.h>
|
2004-08-10 10:00:27 +05:30
|
|
|
#include <string.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pwd.h>
|
2015-06-24 18:16:06 +05:30
|
|
|
#include <grp.h>
|
|
|
|
|
2002-12-09 12:30:07 +05:30
|
|
|
#include "pwcache.h"
|
2015-06-24 18:16:06 +05:30
|
|
|
#include "procps-private.h"
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-10-14 02:49:26 +05:30
|
|
|
// might as well fill cache lines... else we waste memory anyway
|
|
|
|
|
2017-11-15 10:30:00 +05:30
|
|
|
#define HASHSIZE 64 /* power of 2 */
|
|
|
|
#define HASH(x) ((x) & (HASHSIZE - 1))
|
|
|
|
|
|
|
|
static char ERRname[] = "?";
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2021-10-22 10:30:00 +05:30
|
|
|
static __thread struct pwbuf {
|
2002-10-14 02:49:26 +05:30
|
|
|
struct pwbuf *next;
|
2002-02-02 04:17:29 +05:30
|
|
|
uid_t uid;
|
2004-07-21 05:01:12 +05:30
|
|
|
char name[P_G_SZ];
|
2002-02-02 04:17:29 +05:30
|
|
|
} *pwhash[HASHSIZE];
|
|
|
|
|
2016-06-18 03:47:45 +05:30
|
|
|
char *pwcache_get_user(uid_t uid) {
|
2002-02-02 04:17:29 +05:30
|
|
|
struct pwbuf **p;
|
|
|
|
struct passwd *pw;
|
|
|
|
|
|
|
|
p = &pwhash[HASH(uid)];
|
|
|
|
while (*p) {
|
2017-11-15 10:30:00 +05:30
|
|
|
if ((*p)->uid == uid)
|
|
|
|
return((*p)->name);
|
|
|
|
p = &(*p)->next;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
2017-11-15 10:30:00 +05:30
|
|
|
if (!(*p = (struct pwbuf *)malloc(sizeof(struct pwbuf))))
|
|
|
|
return ERRname;
|
2002-02-02 04:17:29 +05:30
|
|
|
(*p)->uid = uid;
|
2004-07-21 04:21:22 +05:30
|
|
|
pw = getpwuid(uid);
|
2004-08-10 10:17:07 +05:30
|
|
|
if(!pw || strlen(pw->pw_name) >= P_G_SZ)
|
2017-11-15 10:30:00 +05:30
|
|
|
sprintf((*p)->name, "%u", uid);
|
2002-02-02 04:17:29 +05:30
|
|
|
else
|
2004-07-21 05:01:12 +05:30
|
|
|
strcpy((*p)->name, pw->pw_name);
|
2004-08-10 10:17:07 +05:30
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
(*p)->next = NULL;
|
|
|
|
return((*p)->name);
|
|
|
|
}
|
|
|
|
|
2021-10-22 10:30:00 +05:30
|
|
|
static __thread struct grpbuf {
|
2002-10-14 02:49:26 +05:30
|
|
|
struct grpbuf *next;
|
2002-02-02 04:17:29 +05:30
|
|
|
gid_t gid;
|
2004-07-21 05:01:12 +05:30
|
|
|
char name[P_G_SZ];
|
2002-02-02 04:17:29 +05:30
|
|
|
} *grphash[HASHSIZE];
|
|
|
|
|
2016-06-18 03:47:45 +05:30
|
|
|
char *pwcache_get_group(gid_t gid) {
|
2002-02-02 04:17:29 +05:30
|
|
|
struct grpbuf **g;
|
|
|
|
struct group *gr;
|
|
|
|
|
|
|
|
g = &grphash[HASH(gid)];
|
|
|
|
while (*g) {
|
2004-07-21 05:01:12 +05:30
|
|
|
if ((*g)->gid == gid)
|
|
|
|
return((*g)->name);
|
|
|
|
g = &(*g)->next;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
2017-11-15 10:30:00 +05:30
|
|
|
if (!(*g = (struct grpbuf *)malloc(sizeof(struct grpbuf))))
|
|
|
|
return ERRname;;
|
2002-02-02 04:17:29 +05:30
|
|
|
(*g)->gid = gid;
|
2004-07-21 04:21:22 +05:30
|
|
|
gr = getgrgid(gid);
|
2004-08-10 10:17:07 +05:30
|
|
|
if (!gr || strlen(gr->gr_name) >= P_G_SZ)
|
2004-07-21 05:01:12 +05:30
|
|
|
sprintf((*g)->name, "%u", gid);
|
2002-02-02 04:17:29 +05:30
|
|
|
else
|
2004-07-21 05:01:12 +05:30
|
|
|
strcpy((*g)->name, gr->gr_name);
|
2002-02-02 04:17:29 +05:30
|
|
|
(*g)->next = NULL;
|
|
|
|
return((*g)->name);
|
|
|
|
}
|