xref: /csrg-svn/old/mkpasswd/mkpasswd.c (revision 33137)
121151Sdist /*
2*33137Sbostic  * Copyright (c) 1980, 1983 Regents of the University of California.
3*33137Sbostic  * All rights reserved.
4*33137Sbostic  *
5*33137Sbostic  * Redistribution and use in source and binary forms are permitted
6*33137Sbostic  * provided that this notice is preserved and that due credit is given
7*33137Sbostic  * to the University of California at Berkeley. The name of the University
8*33137Sbostic  * may not be used to endorse or promote products derived from this
9*33137Sbostic  * software without specific prior written permission. This software
10*33137Sbostic  * is provided ``as is'' without express or implied warranty.
1121151Sdist  */
1221151Sdist 
1315664Sralph #ifndef lint
1421151Sdist char copyright[] =
15*33137Sbostic "@(#) Copyright (c) 1980, 1983 Regents of the University of California.\n\
1621151Sdist  All rights reserved.\n";
17*33137Sbostic #endif /* not lint */
1815664Sralph 
1921151Sdist #ifndef lint
20*33137Sbostic static char sccsid[] = "@(#)mkpasswd.c	5.2 (Berkeley) 12/26/87";
21*33137Sbostic #endif /* not lint */
2221151Sdist 
2315664Sralph #include <sys/file.h>
2415664Sralph #include <stdio.h>
2515664Sralph #include <pwd.h>
2615664Sralph #include <ndbm.h>
2715664Sralph 
2815664Sralph main(argc, argv)
29*33137Sbostic 	int argc;
3015664Sralph 	char *argv[];
3115664Sralph {
3215664Sralph 	DBM *dp;
3315664Sralph 	datum key, content;
3415664Sralph 	register char *cp, *tp;
3515664Sralph 	register struct passwd *pwd;
3615664Sralph 	int verbose = 0, entries = 0, maxlen = 0;
37*33137Sbostic 	char buf[BUFSIZ];
3815664Sralph 
3915664Sralph 	if (argc > 1 && strcmp(argv[1], "-v") == 0) {
4015664Sralph 		verbose++;
4115664Sralph 		argv++, argc--;
4215664Sralph 	}
4315664Sralph 	if (argc != 2) {
4415664Sralph 		fprintf(stderr, "usage: mkpasswd [ -v ] file\n");
4515664Sralph 		exit(1);
4615664Sralph 	}
4716509Sralph 	if (access(argv[1], R_OK) < 0) {
4815893Sralph 		fprintf(stderr, "mkpasswd: ");
4915893Sralph 		perror(argv[1]);
5015893Sralph 		exit(1);
5115893Sralph 	}
52*33137Sbostic 	(void)umask(0);
5317308Sralph 	dp = dbm_open(argv[1], O_WRONLY|O_CREAT|O_EXCL, 0644);
5415664Sralph 	if (dp == NULL) {
5515893Sralph 		fprintf(stderr, "mkpasswd: ");
5615664Sralph 		perror(argv[1]);
5715664Sralph 		exit(1);
5815664Sralph 	}
5916509Sralph 	setpwfile(argv[1]);
6016509Sralph 	while (pwd = getpwent()) {
6115664Sralph 		cp = buf;
62*33137Sbostic #define	COMPACT(e)	tp = pwd->e; while (*cp++ = *tp++);
63*33137Sbostic 		COMPACT(pw_name);
64*33137Sbostic 		COMPACT(pw_passwd);
6517688Sralph 		bcopy((char *)&pwd->pw_uid, cp, sizeof (int));
6617688Sralph 		cp += sizeof (int);
6717688Sralph 		bcopy((char *)&pwd->pw_gid, cp, sizeof (int));
6817688Sralph 		cp += sizeof (int);
6917688Sralph 		bcopy((char *)&pwd->pw_quota, cp, sizeof (int));
7017688Sralph 		cp += sizeof (int);
71*33137Sbostic 		COMPACT(pw_comment);
72*33137Sbostic 		COMPACT(pw_gecos);
73*33137Sbostic 		COMPACT(pw_dir);
74*33137Sbostic 		COMPACT(pw_shell);
7515664Sralph 		content.dptr = buf;
7615664Sralph 		content.dsize = cp - buf;
7715664Sralph 		if (verbose)
7815664Sralph 			printf("store %s, uid %d\n", pwd->pw_name, pwd->pw_uid);
7915664Sralph 		key.dptr = pwd->pw_name;
8015664Sralph 		key.dsize = strlen(pwd->pw_name);
8117308Sralph 		if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
8217308Sralph 			fprintf(stderr, "mkpasswd: ");
8317308Sralph 			perror("dbm_store failed");
8417308Sralph 			exit(1);
8517308Sralph 		}
8615664Sralph 		key.dptr = (char *)&pwd->pw_uid;
8715664Sralph 		key.dsize = sizeof (int);
8817308Sralph 		if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
8917308Sralph 			fprintf(stderr, "mkpasswd: ");
9017308Sralph 			perror("dbm_store failed");
9117308Sralph 			exit(1);
9217308Sralph 		}
9315664Sralph 		entries++;
9415664Sralph 		if (cp - buf > maxlen)
9515664Sralph 			maxlen = cp - buf;
9615664Sralph 	}
9717308Sralph 	dbm_close(dp);
9815664Sralph 	printf("%d password entries, maximum length %d\n", entries, maxlen);
9915664Sralph 	exit(0);
10015664Sralph }
101