2001-10-18 00:43:36 +08:00
|
|
|
/*@-mods@*/
|
2000-08-28 03:18:25 +08:00
|
|
|
/** \ingroup rpmbuild
|
|
|
|
* \file build/names.c
|
2000-01-25 04:02:32 +08:00
|
|
|
* Simple user/group name/id cache (plus hostname and buildtime)
|
|
|
|
*/
|
|
|
|
|
1996-06-08 02:32:10 +08:00
|
|
|
|
1998-07-26 05:00:26 +08:00
|
|
|
#include "system.h"
|
|
|
|
|
1998-07-31 06:09:42 +08:00
|
|
|
#include "rpmbuild.h"
|
2000-12-13 04:03:45 +08:00
|
|
|
#include "debug.h"
|
1996-06-08 02:32:10 +08:00
|
|
|
|
2001-09-21 23:07:11 +08:00
|
|
|
typedef /*@owned@*/ /*@null@*/ const char * ugstr_t;
|
|
|
|
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@unchecked@*/
|
1996-06-08 02:32:10 +08:00
|
|
|
static uid_t uids[1024];
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@unchecked@*/
|
2001-09-21 23:07:11 +08:00
|
|
|
static ugstr_t unames[1024];
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@unchecked@*/
|
1996-06-08 02:32:10 +08:00
|
|
|
static int uid_used = 0;
|
|
|
|
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@unchecked@*/
|
1996-06-08 02:32:10 +08:00
|
|
|
static gid_t gids[1024];
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@unchecked@*/
|
2001-09-21 23:07:11 +08:00
|
|
|
static ugstr_t gnames[1024];
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@unchecked@*/
|
1996-06-08 02:32:10 +08:00
|
|
|
static int gid_used = 0;
|
|
|
|
|
1999-10-30 00:06:01 +08:00
|
|
|
void freeNames(void)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
for (x = 0; x < uid_used; x++)
|
2001-04-29 09:05:43 +08:00
|
|
|
unames[x] = _free(unames[x]);
|
1999-10-30 00:06:01 +08:00
|
|
|
for (x = 0; x < gid_used; x++)
|
2001-04-29 09:05:43 +08:00
|
|
|
gnames[x] = _free(gnames[x]);
|
1999-10-30 00:06:01 +08:00
|
|
|
}
|
|
|
|
|
1999-10-05 03:40:03 +08:00
|
|
|
const char *getUname(uid_t uid)
|
1996-06-08 02:32:10 +08:00
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
int x;
|
|
|
|
|
1998-11-21 04:18:22 +08:00
|
|
|
for (x = 0; x < uid_used; x++) {
|
2001-09-21 23:07:11 +08:00
|
|
|
if (unames[x] == NULL) continue;
|
2001-04-29 09:05:43 +08:00
|
|
|
if (uids[x] == uid)
|
1996-06-08 02:32:10 +08:00
|
|
|
return unames[x];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX - This is the other hard coded limit */
|
2000-09-02 05:15:40 +08:00
|
|
|
if (x == 1024)
|
|
|
|
rpmlog(RPMLOG_CRIT, _("getUname: too many uid's\n"));
|
2001-04-29 09:05:43 +08:00
|
|
|
uid_used++;
|
1996-06-08 02:32:10 +08:00
|
|
|
|
|
|
|
pw = getpwuid(uid);
|
|
|
|
uids[x] = uid;
|
2001-04-29 09:05:43 +08:00
|
|
|
unames[x] = (pw ? xstrdup(pw->pw_name) : NULL);
|
1996-06-08 02:32:10 +08:00
|
|
|
return unames[x];
|
|
|
|
}
|
|
|
|
|
1999-10-05 03:40:03 +08:00
|
|
|
const char *getUnameS(const char *uname)
|
1996-06-08 02:32:10 +08:00
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
int x;
|
|
|
|
|
1998-11-21 04:18:22 +08:00
|
|
|
for (x = 0; x < uid_used; x++) {
|
2001-09-21 23:07:11 +08:00
|
|
|
if (unames[x] == NULL) continue;
|
2001-04-29 09:05:43 +08:00
|
|
|
if (!strcmp(unames[x],uname))
|
1996-06-08 02:32:10 +08:00
|
|
|
return unames[x];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX - This is the other hard coded limit */
|
2000-09-02 05:15:40 +08:00
|
|
|
if (x == 1024)
|
|
|
|
rpmlog(RPMLOG_CRIT, _("getUnameS: too many uid's\n"));
|
2001-04-29 09:05:43 +08:00
|
|
|
uid_used++;
|
1996-06-08 02:32:10 +08:00
|
|
|
|
|
|
|
pw = getpwnam(uname);
|
2001-04-29 09:05:43 +08:00
|
|
|
uids[x] = (pw ? pw->pw_uid : -1);
|
|
|
|
unames[x] = (pw ? xstrdup(pw->pw_name) : xstrdup(uname));
|
1996-06-08 02:32:10 +08:00
|
|
|
return unames[x];
|
|
|
|
}
|
|
|
|
|
2001-07-04 03:26:58 +08:00
|
|
|
uid_t getUidS(const char *uname)
|
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
int x;
|
|
|
|
|
|
|
|
for (x = 0; x < uid_used; x++) {
|
2001-09-21 23:07:11 +08:00
|
|
|
if (unames[x] == NULL) continue;
|
2001-07-04 03:26:58 +08:00
|
|
|
if (!strcmp(unames[x],uname))
|
|
|
|
return uids[x];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX - This is the other hard coded limit */
|
|
|
|
if (x == 1024)
|
|
|
|
rpmlog(RPMLOG_CRIT, _("getUidS: too many uid's\n"));
|
|
|
|
uid_used++;
|
|
|
|
|
|
|
|
pw = getpwnam(uname);
|
|
|
|
uids[x] = (pw ? pw->pw_uid : -1);
|
|
|
|
unames[x] = (pw ? xstrdup(pw->pw_name) : xstrdup(uname));
|
|
|
|
return uids[x];
|
|
|
|
}
|
|
|
|
|
1999-10-05 03:40:03 +08:00
|
|
|
const char *getGname(gid_t gid)
|
1996-06-08 02:32:10 +08:00
|
|
|
{
|
|
|
|
struct group *gr;
|
|
|
|
int x;
|
|
|
|
|
1998-11-21 04:18:22 +08:00
|
|
|
for (x = 0; x < gid_used; x++) {
|
2001-09-21 23:07:11 +08:00
|
|
|
if (gnames[x] == NULL) continue;
|
2001-04-29 09:05:43 +08:00
|
|
|
if (gids[x] == gid)
|
1996-06-08 02:32:10 +08:00
|
|
|
return gnames[x];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX - This is the other hard coded limit */
|
2000-09-02 05:15:40 +08:00
|
|
|
if (x == 1024)
|
|
|
|
rpmlog(RPMLOG_CRIT, _("getGname: too many gid's\n"));
|
2001-04-29 09:05:43 +08:00
|
|
|
gid_used++;
|
1996-06-08 02:32:10 +08:00
|
|
|
|
|
|
|
gr = getgrgid(gid);
|
|
|
|
gids[x] = gid;
|
2001-04-29 09:05:43 +08:00
|
|
|
gnames[x] = (gr ? xstrdup(gr->gr_name) : NULL);
|
1996-06-08 02:32:10 +08:00
|
|
|
return gnames[x];
|
|
|
|
}
|
|
|
|
|
1999-10-05 03:40:03 +08:00
|
|
|
const char *getGnameS(const char *gname)
|
1996-06-08 02:32:10 +08:00
|
|
|
{
|
|
|
|
struct group *gr;
|
|
|
|
int x;
|
|
|
|
|
1998-11-21 04:18:22 +08:00
|
|
|
for (x = 0; x < gid_used; x++) {
|
2001-09-21 23:07:11 +08:00
|
|
|
if (gnames[x] == NULL) continue;
|
2001-04-29 09:05:43 +08:00
|
|
|
if (!strcmp(gnames[x], gname))
|
1996-06-08 02:32:10 +08:00
|
|
|
return gnames[x];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX - This is the other hard coded limit */
|
2000-09-02 05:15:40 +08:00
|
|
|
if (x == 1024)
|
|
|
|
rpmlog(RPMLOG_CRIT, _("getGnameS: too many gid's\n"));
|
2001-04-29 09:05:43 +08:00
|
|
|
gid_used++;
|
1996-06-08 02:32:10 +08:00
|
|
|
|
|
|
|
gr = getgrnam(gname);
|
2001-04-29 09:05:43 +08:00
|
|
|
gids[x] = (gr ? gr->gr_gid : -1);
|
|
|
|
gnames[x] = (gr ? xstrdup(gr->gr_name) : xstrdup(gname));
|
1996-06-08 02:32:10 +08:00
|
|
|
return gnames[x];
|
|
|
|
}
|
2001-07-04 03:26:58 +08:00
|
|
|
|
|
|
|
gid_t getGidS(const char *gname)
|
|
|
|
{
|
|
|
|
struct group *gr;
|
|
|
|
int x;
|
|
|
|
|
|
|
|
for (x = 0; x < gid_used; x++) {
|
2001-09-21 23:07:11 +08:00
|
|
|
if (gnames[x] == NULL) continue;
|
2001-07-04 03:26:58 +08:00
|
|
|
if (!strcmp(gnames[x], gname))
|
|
|
|
return gids[x];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX - This is the other hard coded limit */
|
|
|
|
if (x == 1024)
|
|
|
|
rpmlog(RPMLOG_CRIT, _("getGidS: too many gid's\n"));
|
|
|
|
gid_used++;
|
|
|
|
|
|
|
|
gr = getgrnam(gname);
|
|
|
|
gids[x] = (gr ? gr->gr_gid : -1);
|
|
|
|
gnames[x] = (gr ? xstrdup(gr->gr_name) : xstrdup(gname));
|
|
|
|
return gids[x];
|
|
|
|
}
|
1996-06-08 02:32:10 +08:00
|
|
|
|
2001-06-19 19:38:51 +08:00
|
|
|
int_32 *const getBuildTime(void)
|
1996-06-08 02:32:10 +08:00
|
|
|
{
|
2001-06-19 19:38:51 +08:00
|
|
|
static int_32 buildTime[1];
|
1998-01-13 05:31:29 +08:00
|
|
|
|
2001-04-29 09:05:43 +08:00
|
|
|
if (buildTime[0] == 0)
|
2001-08-03 13:04:13 +08:00
|
|
|
buildTime[0] = (int_32) time(NULL);
|
2001-04-29 09:05:43 +08:00
|
|
|
return buildTime;
|
1996-06-08 02:32:10 +08:00
|
|
|
}
|
|
|
|
|
1999-10-05 03:40:03 +08:00
|
|
|
const char *const buildHost(void)
|
1996-06-08 02:32:10 +08:00
|
|
|
{
|
|
|
|
static char hostname[1024];
|
|
|
|
static int gotit = 0;
|
|
|
|
struct hostent *hbn;
|
|
|
|
|
|
|
|
if (! gotit) {
|
2001-05-01 06:32:22 +08:00
|
|
|
(void) gethostname(hostname, sizeof(hostname));
|
2001-09-21 23:07:11 +08:00
|
|
|
/*@-unrecog -multithreaded @*/
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@-globs@*/ /* FIX: h_errno access */
|
2001-09-21 23:07:11 +08:00
|
|
|
hbn = gethostbyname(hostname);
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@=globs@*/
|
2001-09-21 23:07:11 +08:00
|
|
|
/*@=unrecog =multithreaded @*/
|
|
|
|
if (hbn)
|
1996-06-08 02:32:10 +08:00
|
|
|
strcpy(hostname, hbn->h_name);
|
2001-04-29 09:05:43 +08:00
|
|
|
else
|
|
|
|
rpmMessage(RPMMESS_WARNING,
|
|
|
|
_("Could not canonicalize hostname: %s\n"), hostname);
|
1996-06-08 02:32:10 +08:00
|
|
|
gotit = 1;
|
|
|
|
}
|
|
|
|
return(hostname);
|
|
|
|
}
|
2001-10-18 00:43:36 +08:00
|
|
|
/*@=mods@*/
|