Add endianness support.

While version 4 entries are architecture-independent, we
also store old (version 3) entries in native byte order.
Also, the hash itself is created in a native byte order.

With this change, pwd_mkdb(8) can be used to cross-build
*pwd.db files for another architecture.

Tested on:	i386, amd64, alpha, sparc64
This commit is contained in:
Ruslan Ermilov 2005-02-28 21:05:30 +00:00
parent ab16a51bbb
commit e2db8d3732
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=142832
2 changed files with 47 additions and 41 deletions

View File

@ -28,7 +28,7 @@
.\" @(#)pwd_mkdb.8 8.1 (Berkeley) 6/6/93 .\" @(#)pwd_mkdb.8 8.1 (Berkeley) 6/6/93
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.Dd June 6, 1993 .Dd February 28, 2005
.Dt PWD_MKDB 8 .Dt PWD_MKDB 8
.Os .Os
.Sh NAME .Sh NAME
@ -36,10 +36,7 @@
.Nd "generate the password databases" .Nd "generate the password databases"
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl C .Op Fl BCiLNp
.Op Fl N
.Op Fl p
.Op Fl i
.Op Fl d Ar directory .Op Fl d Ar directory
.Op Fl s Ar cachesize .Op Fl s Ar cachesize
.Op Fl u Ar username .Op Fl u Ar username
@ -64,10 +61,14 @@ different from the historic Version 7 style format.
.Pp .Pp
The options are as follows: The options are as follows:
.Bl -tag -width flag .Bl -tag -width flag
.It Fl B
Store data in big-endian format.
.It Fl C .It Fl C
Check if the password file is in the correct format. Check if the password file is in the correct format.
Do not Do not
change, add, or remove any files. change, add, or remove any files.
.It Fl L
Store data in little-endian format.
.It Fl N .It Fl N
Tell Tell
.Nm .Nm

View File

@ -42,6 +42,7 @@ static char sccsid[] = "@(#)pwd_mkdb.c 8.5 (Berkeley) 4/20/94";
__FBSDID("$FreeBSD$"); __FBSDID("$FreeBSD$");
#include <sys/param.h> #include <sys/param.h>
#include <sys/endian.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <arpa/inet.h> #include <arpa/inet.h>
@ -72,7 +73,7 @@ HASHINFO openinfo = {
256, /* nelem */ 256, /* nelem */
2048 * 1024, /* cachesize */ 2048 * 1024, /* cachesize */
NULL, /* hash() */ NULL, /* hash() */
0 /* lorder */ BYTE_ORDER /* lorder */
}; };
static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean; static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
@ -101,7 +102,6 @@ main(int argc, char *argv[])
sigset_t set; sigset_t set;
int ch, cnt, ypcnt, makeold, tfd, yp_enabled = 0; int ch, cnt, ypcnt, makeold, tfd, yp_enabled = 0;
unsigned int len; unsigned int len;
int32_t pw_change, pw_expire;
uint32_t store; uint32_t store;
const char *t; const char *t;
char *p; char *p;
@ -118,11 +118,17 @@ main(int argc, char *argv[])
strcpy(prefix, _PATH_PWD); strcpy(prefix, _PATH_PWD);
makeold = 0; makeold = 0;
username = NULL; username = NULL;
while ((ch = getopt(argc, argv, "CNd:ips:u:v")) != -1) while ((ch = getopt(argc, argv, "BCLNd:ips:u:v")) != -1)
switch(ch) { switch(ch) {
case 'B': /* big-endian output */
openinfo.lorder = BIG_ENDIAN;
break;
case 'C': /* verify only */ case 'C': /* verify only */
Cflag = 1; Cflag = 1;
break; break;
case 'L': /* little-endian output */
openinfo.lorder = LITTLE_ENDIAN;
break;
case 'N': /* do not wait for lock */ case 'N': /* do not wait for lock */
nblock = LOCK_NB; /* will fail if locked */ nblock = LOCK_NB; /* will fail if locked */
break; break;
@ -353,10 +359,14 @@ main(int argc, char *argv[])
#define SCALAR(e) store = htonl((uint32_t)(e)); \ #define SCALAR(e) store = htonl((uint32_t)(e)); \
memmove(p, &store, sizeof(store)); \ memmove(p, &store, sizeof(store)); \
p += sizeof(store); p += sizeof(store);
#define LSCALAR(e) store = HTOL((uint32_t)(e)); \
memmove(p, &store, sizeof(store)); \
p += sizeof(store);
#define HTOL(e) (openinfo.lorder == BYTE_ORDER ? \
(uint32_t)(e) : \
bswap32((uint32_t)(e)))
if (!is_comment && if (!is_comment &&
(!username || (strcmp(username, pwd.pw_name) == 0))) { (!username || (strcmp(username, pwd.pw_name) == 0))) {
pw_change = pwd.pw_change;
pw_expire = pwd.pw_expire;
/* Create insecure data. */ /* Create insecure data. */
p = buf; p = buf;
COMPACT(pwd.pw_name); COMPACT(pwd.pw_name);
@ -452,40 +462,30 @@ main(int argc, char *argv[])
p = buf; p = buf;
COMPACT(pwd.pw_name); COMPACT(pwd.pw_name);
COMPACT("*"); COMPACT("*");
memmove(p, &pwd.pw_uid, sizeof(pwd.pw_uid)); LSCALAR(pwd.pw_uid);
p += sizeof(int); LSCALAR(pwd.pw_gid);
memmove(p, &pwd.pw_gid, sizeof(pwd.pw_gid)); LSCALAR(pwd.pw_change);
p += sizeof(int);
memmove(p, &pw_change, sizeof(pw_change));
p += sizeof(pw_change);
COMPACT(pwd.pw_class); COMPACT(pwd.pw_class);
COMPACT(pwd.pw_gecos); COMPACT(pwd.pw_gecos);
COMPACT(pwd.pw_dir); COMPACT(pwd.pw_dir);
COMPACT(pwd.pw_shell); COMPACT(pwd.pw_shell);
memmove(p, &pw_expire, sizeof(pw_expire)); LSCALAR(pwd.pw_expire);
p += sizeof(pw_expire); LSCALAR(pwd.pw_fields);
memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields);
p += sizeof pwd.pw_fields;
data.size = p - buf; data.size = p - buf;
/* Create secure data. (legacy version) */ /* Create secure data. (legacy version) */
p = sbuf; p = sbuf;
COMPACT(pwd.pw_name); COMPACT(pwd.pw_name);
COMPACT(pwd.pw_passwd); COMPACT(pwd.pw_passwd);
memmove(p, &pwd.pw_uid, sizeof(pwd.pw_uid)); LSCALAR(pwd.pw_uid);
p += sizeof(int); LSCALAR(pwd.pw_gid);
memmove(p, &pwd.pw_gid, sizeof(pwd.pw_gid)); LSCALAR(pwd.pw_change);
p += sizeof(int);
memmove(p, &pw_change, sizeof(pw_change));
p += sizeof(pw_change);
COMPACT(pwd.pw_class); COMPACT(pwd.pw_class);
COMPACT(pwd.pw_gecos); COMPACT(pwd.pw_gecos);
COMPACT(pwd.pw_dir); COMPACT(pwd.pw_dir);
COMPACT(pwd.pw_shell); COMPACT(pwd.pw_shell);
memmove(p, &pw_expire, sizeof(pw_expire)); LSCALAR(pwd.pw_expire);
p += sizeof(pw_expire); LSCALAR(pwd.pw_fields);
memmove(p, &pwd.pw_fields, sizeof pwd.pw_fields);
p += sizeof pwd.pw_fields;
sdata.size = p - sbuf; sdata.size = p - sbuf;
/* Store insecure by name. */ /* Store insecure by name. */
@ -498,15 +498,17 @@ main(int argc, char *argv[])
/* Store insecure by number. */ /* Store insecure by number. */
tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM); tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM);
memmove(tbuf + 1, &cnt, sizeof(cnt)); store = HTOL(cnt);
key.size = sizeof(cnt) + 1; memmove(tbuf + 1, &store, sizeof(store));
key.size = sizeof(store) + 1;
if ((dp->put)(dp, &key, &data, method) == -1) if ((dp->put)(dp, &key, &data, method) == -1)
error("put"); error("put");
/* Store insecure by uid. */ /* Store insecure by uid. */
tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID); tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID);
memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid)); store = HTOL(pwd.pw_uid);
key.size = sizeof(pwd.pw_uid) + 1; memmove(tbuf + 1, &store, sizeof(store));
key.size = sizeof(store) + 1;
if ((dp->put)(dp, &key, &data, methoduid) == -1) if ((dp->put)(dp, &key, &data, methoduid) == -1)
error("put"); error("put");
@ -520,24 +522,27 @@ main(int argc, char *argv[])
/* Store secure by number. */ /* Store secure by number. */
tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM); tbuf[0] = LEGACY_VERSION(_PW_KEYBYNUM);
memmove(tbuf + 1, &cnt, sizeof(cnt)); store = HTOL(cnt);
key.size = sizeof(cnt) + 1; memmove(tbuf + 1, &store, sizeof(store));
key.size = sizeof(store) + 1;
if ((sdp->put)(sdp, &key, &sdata, method) == -1) if ((sdp->put)(sdp, &key, &sdata, method) == -1)
error("put"); error("put");
/* Store secure by uid. */ /* Store secure by uid. */
tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID); tbuf[0] = LEGACY_VERSION(_PW_KEYBYUID);
memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid)); store = HTOL(pwd.pw_uid);
key.size = sizeof(pwd.pw_uid) + 1; memmove(tbuf + 1, &store, sizeof(store));
key.size = sizeof(store) + 1;
if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1) if ((sdp->put)(sdp, &key, &sdata, methoduid) == -1)
error("put"); error("put");
/* Store insecure and secure special plus and special minus */ /* Store insecure and secure special plus and special minus */
if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') { if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-') {
tbuf[0] = LEGACY_VERSION(_PW_KEYYPBYNUM); tbuf[0] = LEGACY_VERSION(_PW_KEYYPBYNUM);
memmove(tbuf + 1, &ypcnt, sizeof(cnt)); store = HTOL(ypcnt);
memmove(tbuf + 1, &store, sizeof(store));
ypcnt++; ypcnt++;
key.size = sizeof(cnt) + 1; key.size = sizeof(store) + 1;
if ((dp->put)(dp, &key, &data, method) == -1) if ((dp->put)(dp, &key, &data, method) == -1)
error("put"); error("put");
if ((sdp->put)(sdp, &key, &sdata, method) == -1) if ((sdp->put)(sdp, &key, &sdata, method) == -1)
@ -746,6 +751,6 @@ usage(void)
{ {
(void)fprintf(stderr, (void)fprintf(stderr,
"usage: pwd_mkdb [-C] [-N] [-i] [-p] [-d <dest dir>] [-s <cachesize>] [-u <local username>] file\n"); "usage: pwd_mkdb [-BCiLNp] [-d directory] [-s cachesize] [-u username] file\n");
exit(1); exit(1);
} }