Skip to content

Commit

Permalink
Initialize all the name services we need
Browse files Browse the repository at this point in the history
- A long time ago in a far away commit, "somebody" had this less than
  brilliant idea that calling gethostbyname() loads all the required
  name service libraries (commit 39b75d2).
  This is of course only true if hosts db happens to use the same
  database as passwd and group... which often is the case, but by
  no means guaranteed or always the case.
- Add an explicit initializer for rpmug to force loading the name service
  libraries early in initialization so we dont need to worry about going
  chroot later on. Of course, the "host" name service configuration might
  not actually make sense for the chroot, but this commit doesn't make
  that any worse than it already was.
  • Loading branch information
pmatilai committed Jan 28, 2014
1 parent ee5a500 commit abbf489
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/rpmrc.c
Expand Up @@ -12,7 +12,6 @@
#if HAVE_SYS_UTSNAME_H
#include <sys/utsname.h>
#endif
#include <netdb.h>
#include <ctype.h> /* XXX for /etc/rpm/platform contents */

#if HAVE_SYS_SYSTEMCFG_H
Expand All @@ -36,6 +35,7 @@
#include "rpmio/rpmio_internal.h" /* XXX for rpmioSlurp */
#include "lib/misc.h"
#include "lib/rpmliblua.h"
#include "lib/rpmug.h"

#include "debug.h"

Expand Down Expand Up @@ -1520,7 +1520,9 @@ int rpmReadConfigFiles(const char * file, const char * target)
rpmrcCtx ctx = rpmrcCtxAcquire(1);

/* Force preloading of dlopen()'ed libraries in case we go chrooting */
(void) gethostbyname("localhost");
if (rpmugInit())
goto exit;

if (rpmInitCrypto())
goto exit;

Expand Down
19 changes: 19 additions & 0 deletions lib/rpmug.c
@@ -1,7 +1,9 @@
#include "system.h"

#include <pthread.h>
#include <pwd.h>
#include <grp.h>
#include <netdb.h>
#include <rpm/rpmlog.h>
#include <rpm/rpmstring.h>

Expand Down Expand Up @@ -166,6 +168,23 @@ const char * rpmugGname(gid_t gid)
}
}

static void loadLibs(void)
{
(void) getpwnam("root");
endpwent();
(void) getgrnam("root");
endgrent();
(void) gethostbyname("localhost");
}

int rpmugInit(void)
{
static pthread_once_t libsLoaded = PTHREAD_ONCE_INIT;

pthread_once(&libsLoaded, loadLibs);
return 0;
}

void rpmugFree(void)
{
rpmugUid(NULL, NULL);
Expand Down
2 changes: 2 additions & 0 deletions lib/rpmug.h
Expand Up @@ -11,6 +11,8 @@ const char * rpmugUname(uid_t uid);

const char * rpmugGname(gid_t gid);

int rpmugInit(void);

void rpmugFree(void);

#endif /* _RPMUG_H */

0 comments on commit abbf489

Please sign in to comment.