2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1991, 1993, 1994 The Regents of the University of California. All rights reserved.
30 * @(#)pwd_mkdb.c 8.5 (Berkeley) 4/20/94
31 * $FreeBSD: src/usr.sbin/pwd_mkdb/pwd_mkdb.c,v 1.51 2005/06/15 10:13:04 dd Exp $
32 * $DragonFly: src/usr.sbin/pwd_mkdb/pwd_mkdb.c,v 1.5 2005/12/05 02:40:27 swildner Exp $
35 #include <sys/param.h>
36 #include <sys/endian.h>
38 #include <arpa/inet.h>
56 #define PERM_INSECURE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
57 #define PERM_SECURE (S_IRUSR|S_IWUSR)
58 #define LEGACY_VERSION(x) _PW_VERSIONED(x, 3)
59 #define CURRENT_VERSION(x) _PW_VERSIONED(x, 4)
65 2048 * 1024, /* cachesize */
67 BYTE_ORDER
/* lorder */
70 static enum state
{ FILE_INSECURE
, FILE_SECURE
, FILE_ORIG
} clean
;
71 static struct passwd pwd
; /* password structure */
72 static char *pname
; /* password file name */
73 static char prefix
[MAXPATHLEN
];
75 static int is_comment
; /* flag for comments */
76 static char line
[LINE_MAX
];
79 void error(const char *);
80 void cp(char *, char *, mode_t mode
);
81 void mv(char *, char *);
82 int scan(FILE *, struct passwd
*);
83 static void usage(void);
86 main(int argc
, char *argv
[])
88 static char verskey
[] = _PWD_VERSION_KEY
;
89 char version
= _PWD_CURRENT_VERSION
;
94 int ch
, cnt
, ypcnt
, makeold
, tfd
, yp_enabled
= 0;
99 char buf
[MAX(MAXPATHLEN
, LINE_MAX
* 2)], tbuf
[1024];
100 char sbuf
[MAX(MAXPATHLEN
, LINE_MAX
* 2)];
101 char buf2
[MAXPATHLEN
];
102 char sbuf2
[MAXPATHLEN
];
104 u_int method
, methoduid
;
105 int Cflag
, dflag
, iflag
;
108 iflag
= dflag
= Cflag
= 0;
109 strcpy(prefix
, _PATH_PWD
);
113 while ((ch
= getopt(argc
, argv
, "BCLNd:ips:u:v")) != -1)
115 case 'B': /* big-endian output */
116 openinfo
.lorder
= BIG_ENDIAN
;
118 case 'C': /* verify only */
121 case 'L': /* little-endian output */
122 openinfo
.lorder
= LITTLE_ENDIAN
;
124 case 'N': /* do not wait for lock */
125 nblock
= LOCK_NB
; /* will fail if locked */
129 strlcpy(prefix
, optarg
, sizeof(prefix
));
134 case 'p': /* create V7 "file.orig" */
137 case 's': /* change default cachesize */
138 openinfo
.cachesize
= atoi(optarg
) * 1024 * 1024;
140 case 'u': /* only update this record */
143 case 'v': /* backward compatible */
151 if (argc
!= 1 || (username
&& (*username
== '+' || *username
== '-')))
155 * This could be changed to allow the user to interrupt.
156 * Probably not worth the effort.
159 sigaddset(&set
, SIGTSTP
);
160 sigaddset(&set
, SIGHUP
);
161 sigaddset(&set
, SIGINT
);
162 sigaddset(&set
, SIGQUIT
);
163 sigaddset(&set
, SIGTERM
);
164 sigprocmask(SIG_BLOCK
, &set
, NULL
);
166 /* We don't care what the user wants. */
172 * Open and lock the original password file. We have to check
173 * the hardlink count after we get the lock to handle any potential
174 * unlink/rename race.
176 * This lock is necessary when someone runs pwd_mkdb manually, directly
177 * on master.passwd, to handle the case where a user might try to
178 * change his password while pwd_mkdb is running.
183 if (!(fp
= fopen(pname
, "r")))
185 if (flock(fileno(fp
), LOCK_EX
|nblock
) < 0 && !(dflag
&& iflag
))
187 if (fstat(fileno(fp
), &st
) < 0)
189 if (st
.st_nlink
!= 0)
195 /* check only if password database is valid */
197 for (cnt
= 1; scan(fp
, &pwd
); ++cnt
);
201 /* Open the temporary insecure password database. */
202 snprintf(buf
, sizeof(buf
), "%s/%s.tmp", prefix
, _MP_DB
);
203 snprintf(sbuf
, sizeof(sbuf
), "%s/%s.tmp", prefix
, _SMP_DB
);
207 snprintf(buf2
, sizeof(buf2
), "%s/%s", prefix
, _MP_DB
);
208 snprintf(sbuf2
, sizeof(sbuf2
), "%s/%s", prefix
, _SMP_DB
);
210 clean
= FILE_INSECURE
;
211 cp(buf2
, buf
, PERM_INSECURE
);
213 O_RDWR
|O_EXCL
, PERM_INSECURE
, DB_HASH
, &openinfo
);
218 cp(sbuf2
, sbuf
, PERM_SECURE
);
220 O_RDWR
|O_EXCL
, PERM_SECURE
, DB_HASH
, &openinfo
);
225 * Do some trouble to check if we should store this users
226 * uid. Don't use getpwnam/getpwuid as that interferes
229 pw_db
= dbopen(_PATH_MP_DB
, O_RDONLY
, 0, DB_HASH
, NULL
);
234 key
.size
= sizeof(verskey
)-1;
235 if ((pw_db
->get
)(pw_db
, &key
, &data
, 0) == 0)
236 use_version
= *(unsigned char *)data
.data
;
239 buf
[0] = _PW_VERSIONED(_PW_KEYBYNAME
, use_version
);
240 len
= strlen(username
);
242 /* Only check that username fits in buffer */
243 memmove(buf
+ 1, username
, MIN(len
, sizeof(buf
) - 1));
244 key
.data
= (u_char
*)buf
;
246 if ((pw_db
->get
)(pw_db
, &key
, &data
, 0) == 0) {
247 p
= (char *)data
.data
;
249 /* jump over pw_name and pw_passwd, to get to pw_uid */
255 buf
[0] = _PW_VERSIONED(_PW_KEYBYUID
, use_version
);
256 memmove(buf
+ 1, p
, sizeof(store
));
257 key
.data
= (u_char
*)buf
;
258 key
.size
= sizeof(store
) + 1;
260 if ((pw_db
->get
)(pw_db
, &key
, &data
, 0) == 0) {
261 /* First field of data.data holds pw_pwname */
262 if (!strcmp(data
.data
, username
))
265 methoduid
= R_NOOVERWRITE
;
267 methoduid
= R_NOOVERWRITE
;
270 methoduid
= R_NOOVERWRITE
;
272 if ((pw_db
->close
)(pw_db
))
273 error("close pw_db");
277 O_RDWR
|O_CREAT
|O_EXCL
, PERM_INSECURE
, DB_HASH
, &openinfo
);
280 clean
= FILE_INSECURE
;
283 O_RDWR
|O_CREAT
|O_EXCL
, PERM_SECURE
, DB_HASH
, &openinfo
);
288 method
= R_NOOVERWRITE
;
289 methoduid
= R_NOOVERWRITE
;
293 * Open file for old password file. Minor trickiness -- don't want to
294 * chance the file already existing, since someone (stupidly) might
295 * still be using this for permission checking. So, open it first and
296 * fdopen the resulting fd. The resulting file should be readable by
300 snprintf(buf
, sizeof(buf
), "%s.orig", pname
);
302 O_WRONLY
|O_CREAT
|O_EXCL
, PERM_INSECURE
)) < 0)
304 if ((oldfp
= fdopen(tfd
, "w")) == NULL
)
310 * The databases actually contain three copies of the original data.
311 * Each password file entry is converted into a rough approximation
312 * of a ``struct passwd'', with the strings placed inline. This
313 * object is then stored as the data for three separate keys. The
314 * first key * is the pw_name field prepended by the _PW_KEYBYNAME
315 * character. The second key is the pw_uid field prepended by the
316 * _PW_KEYBYUID character. The third key is the line number in the
317 * original file prepended by the _PW_KEYBYNUM character. (The special
318 * characters are prepended to ensure that the keys do not collide.)
320 /* In order to transition this file into a machine-independent
321 * form, we have to change the format of entries. However, since
322 * older binaries will still expect the old MD format entries, we
323 * create those as usual and use versioned tags for the new entries.
325 if (username
== NULL
) {
326 /* Do not add the VERSION tag when updating a single
327 * user. When operating on `old format' databases, this
328 * would result in applications `seeing' only the updated
332 key
.size
= sizeof(verskey
)-1;
333 data
.data
= &version
;
335 if ((dp
->put
)(dp
, &key
, &data
, 0) == -1)
337 if ((dp
->put
)(sdp
, &key
, &data
, 0) == -1)
341 data
.data
= (u_char
*)buf
;
342 sdata
.data
= (u_char
*)sbuf
;
343 key
.data
= (u_char
*)tbuf
;
344 for (cnt
= 1; scan(fp
, &pwd
); ++cnt
) {
346 (pwd
.pw_name
[0] == '+' || pwd
.pw_name
[0] == '-'))
350 #define COMPACT(e) t = e; while ((*p++ = *t++));
351 #define SCALAR(e) store = htonl((uint32_t)(e)); \
352 memmove(p, &store, sizeof(store)); \
354 #define LSCALAR(e) store = HTOL((uint32_t)(e)); \
355 memmove(p, &store, sizeof(store)); \
357 #define HTOL(e) (openinfo.lorder == BYTE_ORDER ? \
359 bswap32((uint32_t)(e)))
361 (!username
|| (strcmp(username
, pwd
.pw_name
) == 0))) {
362 /* Create insecure data. */
364 COMPACT(pwd
.pw_name
);
368 SCALAR(pwd
.pw_change
);
369 COMPACT(pwd
.pw_class
);
370 COMPACT(pwd
.pw_gecos
);
372 COMPACT(pwd
.pw_shell
);
373 SCALAR(pwd
.pw_expire
);
374 SCALAR(pwd
.pw_fields
);
377 /* Create secure data. */
379 COMPACT(pwd
.pw_name
);
380 COMPACT(pwd
.pw_passwd
);
383 SCALAR(pwd
.pw_change
);
384 COMPACT(pwd
.pw_class
);
385 COMPACT(pwd
.pw_gecos
);
387 COMPACT(pwd
.pw_shell
);
388 SCALAR(pwd
.pw_expire
);
389 SCALAR(pwd
.pw_fields
);
390 sdata
.size
= p
- sbuf
;
392 /* Store insecure by name. */
393 tbuf
[0] = CURRENT_VERSION(_PW_KEYBYNAME
);
394 len
= strlen(pwd
.pw_name
);
395 memmove(tbuf
+ 1, pwd
.pw_name
, len
);
397 if ((dp
->put
)(dp
, &key
, &data
, method
) == -1)
400 /* Store insecure by number. */
401 tbuf
[0] = CURRENT_VERSION(_PW_KEYBYNUM
);
403 memmove(tbuf
+ 1, &store
, sizeof(store
));
404 key
.size
= sizeof(store
) + 1;
405 if ((dp
->put
)(dp
, &key
, &data
, method
) == -1)
408 /* Store insecure by uid. */
409 tbuf
[0] = CURRENT_VERSION(_PW_KEYBYUID
);
410 store
= htonl(pwd
.pw_uid
);
411 memmove(tbuf
+ 1, &store
, sizeof(store
));
412 key
.size
= sizeof(store
) + 1;
413 if ((dp
->put
)(dp
, &key
, &data
, methoduid
) == -1)
416 /* Store secure by name. */
417 tbuf
[0] = CURRENT_VERSION(_PW_KEYBYNAME
);
418 len
= strlen(pwd
.pw_name
);
419 memmove(tbuf
+ 1, pwd
.pw_name
, len
);
421 if ((sdp
->put
)(sdp
, &key
, &sdata
, method
) == -1)
424 /* Store secure by number. */
425 tbuf
[0] = CURRENT_VERSION(_PW_KEYBYNUM
);
427 memmove(tbuf
+ 1, &store
, sizeof(store
));
428 key
.size
= sizeof(store
) + 1;
429 if ((sdp
->put
)(sdp
, &key
, &sdata
, method
) == -1)
432 /* Store secure by uid. */
433 tbuf
[0] = CURRENT_VERSION(_PW_KEYBYUID
);
434 store
= htonl(pwd
.pw_uid
);
435 memmove(tbuf
+ 1, &store
, sizeof(store
));
436 key
.size
= sizeof(store
) + 1;
437 if ((sdp
->put
)(sdp
, &key
, &sdata
, methoduid
) == -1)
440 /* Store insecure and secure special plus and special minus */
441 if (pwd
.pw_name
[0] == '+' || pwd
.pw_name
[0] == '-') {
442 tbuf
[0] = CURRENT_VERSION(_PW_KEYYPBYNUM
);
443 store
= htonl(ypcnt
);
444 memmove(tbuf
+ 1, &store
, sizeof(store
));
446 key
.size
= sizeof(store
) + 1;
447 if ((dp
->put
)(dp
, &key
, &data
, method
) == -1)
449 if ((sdp
->put
)(sdp
, &key
, &sdata
, method
) == -1)
453 /* Create insecure data. (legacy version) */
455 COMPACT(pwd
.pw_name
);
459 LSCALAR(pwd
.pw_change
);
460 COMPACT(pwd
.pw_class
);
461 COMPACT(pwd
.pw_gecos
);
463 COMPACT(pwd
.pw_shell
);
464 LSCALAR(pwd
.pw_expire
);
465 LSCALAR(pwd
.pw_fields
);
468 /* Create secure data. (legacy version) */
470 COMPACT(pwd
.pw_name
);
471 COMPACT(pwd
.pw_passwd
);
474 LSCALAR(pwd
.pw_change
);
475 COMPACT(pwd
.pw_class
);
476 COMPACT(pwd
.pw_gecos
);
478 COMPACT(pwd
.pw_shell
);
479 LSCALAR(pwd
.pw_expire
);
480 LSCALAR(pwd
.pw_fields
);
481 sdata
.size
= p
- sbuf
;
483 /* Store insecure by name. */
484 tbuf
[0] = LEGACY_VERSION(_PW_KEYBYNAME
);
485 len
= strlen(pwd
.pw_name
);
486 memmove(tbuf
+ 1, pwd
.pw_name
, len
);
488 if ((dp
->put
)(dp
, &key
, &data
, method
) == -1)
491 /* Store insecure by number. */
492 tbuf
[0] = LEGACY_VERSION(_PW_KEYBYNUM
);
494 memmove(tbuf
+ 1, &store
, sizeof(store
));
495 key
.size
= sizeof(store
) + 1;
496 if ((dp
->put
)(dp
, &key
, &data
, method
) == -1)
499 /* Store insecure by uid. */
500 tbuf
[0] = LEGACY_VERSION(_PW_KEYBYUID
);
501 store
= HTOL(pwd
.pw_uid
);
502 memmove(tbuf
+ 1, &store
, sizeof(store
));
503 key
.size
= sizeof(store
) + 1;
504 if ((dp
->put
)(dp
, &key
, &data
, methoduid
) == -1)
507 /* Store secure by name. */
508 tbuf
[0] = LEGACY_VERSION(_PW_KEYBYNAME
);
509 len
= strlen(pwd
.pw_name
);
510 memmove(tbuf
+ 1, pwd
.pw_name
, len
);
512 if ((sdp
->put
)(sdp
, &key
, &sdata
, method
) == -1)
515 /* Store secure by number. */
516 tbuf
[0] = LEGACY_VERSION(_PW_KEYBYNUM
);
518 memmove(tbuf
+ 1, &store
, sizeof(store
));
519 key
.size
= sizeof(store
) + 1;
520 if ((sdp
->put
)(sdp
, &key
, &sdata
, method
) == -1)
523 /* Store secure by uid. */
524 tbuf
[0] = LEGACY_VERSION(_PW_KEYBYUID
);
525 store
= HTOL(pwd
.pw_uid
);
526 memmove(tbuf
+ 1, &store
, sizeof(store
));
527 key
.size
= sizeof(store
) + 1;
528 if ((sdp
->put
)(sdp
, &key
, &sdata
, methoduid
) == -1)
531 /* Store insecure and secure special plus and special minus */
532 if (pwd
.pw_name
[0] == '+' || pwd
.pw_name
[0] == '-') {
533 tbuf
[0] = LEGACY_VERSION(_PW_KEYYPBYNUM
);
535 memmove(tbuf
+ 1, &store
, sizeof(store
));
537 key
.size
= sizeof(store
) + 1;
538 if ((dp
->put
)(dp
, &key
, &data
, method
) == -1)
540 if ((sdp
->put
)(sdp
, &key
, &sdata
, method
) == -1)
544 /* Create original format password file entry */
545 if (is_comment
&& makeold
){ /* copy comments */
546 if (fprintf(oldfp
, "%s\n", line
) < 0)
548 } else if (makeold
) {
552 snprintf(uidstr
, sizeof(uidstr
), "%u", pwd
.pw_uid
);
553 snprintf(gidstr
, sizeof(gidstr
), "%u", pwd
.pw_gid
);
555 if (fprintf(oldfp
, "%s:*:%s:%s:%s:%s:%s\n",
556 pwd
.pw_name
, pwd
.pw_fields
& _PWF_UID
? uidstr
: "",
557 pwd
.pw_fields
& _PWF_GID
? gidstr
: "",
558 pwd
.pw_gecos
, pwd
.pw_dir
, pwd
.pw_shell
) < 0)
562 /* If YP enabled, set flag. */
564 buf
[0] = yp_enabled
+ 2;
567 tbuf
[0] = CURRENT_VERSION(_PW_KEYYPENABLED
);
568 if ((dp
->put
)(dp
, &key
, &data
, method
) == -1)
570 if ((sdp
->put
)(sdp
, &key
, &data
, method
) == -1)
572 tbuf
[0] = LEGACY_VERSION(_PW_KEYYPENABLED
);
574 if ((dp
->put
)(dp
, &key
, &data
, method
) == -1)
576 if ((sdp
->put
)(sdp
, &key
, &data
, method
) == -1)
580 if ((dp
->close
)(dp
) == -1)
582 if ((sdp
->close
)(sdp
) == -1)
586 if (fclose(oldfp
) == EOF
)
590 /* Set master.passwd permissions, in case caller forgot. */
591 fchmod(fileno(fp
), S_IRUSR
|S_IWUSR
);
593 /* Install as the real password files. */
594 snprintf(buf
, sizeof(buf
), "%s/%s.tmp", prefix
, _MP_DB
);
595 snprintf(buf2
, sizeof(buf2
), "%s/%s", prefix
, _MP_DB
);
597 snprintf(buf
, sizeof(buf
), "%s/%s.tmp", prefix
, _SMP_DB
);
598 snprintf(buf2
, sizeof(buf2
), "%s/%s", prefix
, _SMP_DB
);
601 snprintf(buf2
, sizeof(buf2
), "%s/%s", prefix
, _PASSWD
);
602 snprintf(buf
, sizeof(buf
), "%s.orig", pname
);
606 * Move the master password LAST -- chpass(1), passwd(1) and vipw(8)
607 * all use flock(2) on it to block other incarnations of themselves.
608 * The rename means that everything is unlocked, as the original file
609 * can no longer be accessed.
611 snprintf(buf
, sizeof(buf
), "%s/%s", prefix
, _MASTERPASSWD
);
615 * Close locked password file after rename()
617 if (fclose(fp
) == EOF
)
624 scan(FILE *fp
, struct passwd
*pw
)
630 p
= fgetln(fp
, &len
);
635 * ``... if I swallow anything evil, put your fingers down my
639 if (len
> 0 && p
[len
- 1] == '\n')
641 if (len
>= sizeof(line
) - 1) {
642 warnx("line #%d too long", lcnt
);
645 memcpy(line
, p
, len
);
649 * Ignore comments: ^[ \t]*#
651 for (p
= line
; *p
!= '\0'; p
++)
652 if (*p
!= ' ' && *p
!= '\t')
654 if (*p
== '#' || *p
== '\0') {
660 if (!__pw_scan(line
, pw
, _PWSCAN_WARN
|_PWSCAN_MASTER
)) {
661 warnx("at line #%d", lcnt
);
662 fmt
: errno
= EFTYPE
; /* XXX */
670 cp(char *from
, char *to
, mode_t mode
)
672 static char buf
[MAXBSIZE
];
673 int from_fd
, rcount
, to_fd
, wcount
;
675 if ((from_fd
= open(from
, O_RDONLY
, 0)) < 0)
677 if ((to_fd
= open(to
, O_WRONLY
|O_CREAT
|O_EXCL
, mode
)) < 0)
679 while ((rcount
= read(from_fd
, buf
, MAXBSIZE
)) > 0) {
680 wcount
= write(to_fd
, buf
, rcount
);
681 if (rcount
!= wcount
|| wcount
== -1) {
684 snprintf(buf
, sizeof(buf
), "%s to %s", from
, to
);
692 snprintf(buf
, sizeof(buf
), "%s to %s", from
, to
);
700 mv(char *from
, char *to
)
702 char buf
[MAXPATHLEN
];
704 if (rename(from
, to
)) {
706 snprintf(buf
, sizeof(buf
), "%s to %s", from
, to
);
713 error(const char *name
)
724 char buf
[MAXPATHLEN
];
728 snprintf(buf
, sizeof(buf
), "%s.orig", pname
);
732 snprintf(buf
, sizeof(buf
), "%s/%s.tmp", prefix
, _SMP_DB
);
736 snprintf(buf
, sizeof(buf
), "%s/%s.tmp", prefix
, _MP_DB
);
746 "usage: pwd_mkdb [-BCiLNp] [-d directory] [-s cachesize] [-u username] file\n");