xref: /csrg-svn/old/mkpasswd/mkpasswd.c (revision 21151)
1*21151Sdist /*
2*21151Sdist  * Copyright (c) 1983 Regents of the University of California.
3*21151Sdist  * All rights reserved.  The Berkeley software License Agreement
4*21151Sdist  * specifies the terms and conditions for redistribution.
5*21151Sdist  */
6*21151Sdist 
715664Sralph #ifndef lint
8*21151Sdist char copyright[] =
9*21151Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
10*21151Sdist  All rights reserved.\n";
11*21151Sdist #endif not lint
1215664Sralph 
13*21151Sdist #ifndef lint
14*21151Sdist static char sccsid[] = "@(#)mkpasswd.c	5.1 (Berkeley) 05/28/85";
15*21151Sdist #endif not lint
16*21151Sdist 
1715664Sralph #include <sys/file.h>
1815664Sralph #include <stdio.h>
1915664Sralph #include <pwd.h>
2015664Sralph #include <ndbm.h>
2115664Sralph 
2215664Sralph char	buf[BUFSIZ];
2315664Sralph 
2415893Sralph struct	passwd *fgetpwent();
2515893Sralph 
2615664Sralph main(argc, argv)
2715664Sralph 	char *argv[];
2815664Sralph {
2915664Sralph 	DBM *dp;
3015664Sralph 	datum key, content;
3115664Sralph 	register char *cp, *tp;
3215664Sralph 	register struct passwd *pwd;
3315664Sralph 	int verbose = 0, entries = 0, maxlen = 0;
3415664Sralph 
3515664Sralph 	if (argc > 1 && strcmp(argv[1], "-v") == 0) {
3615664Sralph 		verbose++;
3715664Sralph 		argv++, argc--;
3815664Sralph 	}
3915664Sralph 	if (argc != 2) {
4015664Sralph 		fprintf(stderr, "usage: mkpasswd [ -v ] file\n");
4115664Sralph 		exit(1);
4215664Sralph 	}
4316509Sralph 	if (access(argv[1], R_OK) < 0) {
4415893Sralph 		fprintf(stderr, "mkpasswd: ");
4515893Sralph 		perror(argv[1]);
4615893Sralph 		exit(1);
4715893Sralph 	}
4815664Sralph 	umask(0);
4917308Sralph 	dp = dbm_open(argv[1], O_WRONLY|O_CREAT|O_EXCL, 0644);
5015664Sralph 	if (dp == NULL) {
5115893Sralph 		fprintf(stderr, "mkpasswd: ");
5215664Sralph 		perror(argv[1]);
5315664Sralph 		exit(1);
5415664Sralph 	}
5516509Sralph 	setpwfile(argv[1]);
5616509Sralph 	while (pwd = getpwent()) {
5715664Sralph 		cp = buf;
5815664Sralph #define	COMPACT(e)	tp = pwd->pw_/**/e; while (*cp++ = *tp++);
5915664Sralph 		COMPACT(name);
6015664Sralph 		COMPACT(passwd);
6117688Sralph 		bcopy((char *)&pwd->pw_uid, cp, sizeof (int));
6217688Sralph 		cp += sizeof (int);
6317688Sralph 		bcopy((char *)&pwd->pw_gid, cp, sizeof (int));
6417688Sralph 		cp += sizeof (int);
6517688Sralph 		bcopy((char *)&pwd->pw_quota, cp, sizeof (int));
6617688Sralph 		cp += sizeof (int);
6715664Sralph 		COMPACT(comment);
6815664Sralph 		COMPACT(gecos);
6915664Sralph 		COMPACT(dir);
7015664Sralph 		COMPACT(shell);
7115664Sralph 		content.dptr = buf;
7215664Sralph 		content.dsize = cp - buf;
7315664Sralph 		if (verbose)
7415664Sralph 			printf("store %s, uid %d\n", pwd->pw_name, pwd->pw_uid);
7515664Sralph 		key.dptr = pwd->pw_name;
7615664Sralph 		key.dsize = strlen(pwd->pw_name);
7717308Sralph 		if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
7817308Sralph 			fprintf(stderr, "mkpasswd: ");
7917308Sralph 			perror("dbm_store failed");
8017308Sralph 			exit(1);
8117308Sralph 		}
8215664Sralph 		key.dptr = (char *)&pwd->pw_uid;
8315664Sralph 		key.dsize = sizeof (int);
8417308Sralph 		if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
8517308Sralph 			fprintf(stderr, "mkpasswd: ");
8617308Sralph 			perror("dbm_store failed");
8717308Sralph 			exit(1);
8817308Sralph 		}
8915664Sralph 		entries++;
9015664Sralph 		if (cp - buf > maxlen)
9115664Sralph 			maxlen = cp - buf;
9215664Sralph 	}
9317308Sralph 	dbm_close(dp);
9415664Sralph 	printf("%d password entries, maximum length %d\n", entries, maxlen);
9515664Sralph 	exit(0);
9615664Sralph }
97