1 /* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7 #ifndef lint 8 char copyright[] = 9 "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 10 All rights reserved.\n"; 11 #endif not lint 12 13 #ifndef lint 14 static char sccsid[] = "@(#)mkpasswd.c 5.1 (Berkeley) 05/28/85"; 15 #endif not lint 16 17 #include <sys/file.h> 18 #include <stdio.h> 19 #include <pwd.h> 20 #include <ndbm.h> 21 22 char buf[BUFSIZ]; 23 24 struct passwd *fgetpwent(); 25 26 main(argc, argv) 27 char *argv[]; 28 { 29 DBM *dp; 30 datum key, content; 31 register char *cp, *tp; 32 register struct passwd *pwd; 33 int verbose = 0, entries = 0, maxlen = 0; 34 35 if (argc > 1 && strcmp(argv[1], "-v") == 0) { 36 verbose++; 37 argv++, argc--; 38 } 39 if (argc != 2) { 40 fprintf(stderr, "usage: mkpasswd [ -v ] file\n"); 41 exit(1); 42 } 43 if (access(argv[1], R_OK) < 0) { 44 fprintf(stderr, "mkpasswd: "); 45 perror(argv[1]); 46 exit(1); 47 } 48 umask(0); 49 dp = dbm_open(argv[1], O_WRONLY|O_CREAT|O_EXCL, 0644); 50 if (dp == NULL) { 51 fprintf(stderr, "mkpasswd: "); 52 perror(argv[1]); 53 exit(1); 54 } 55 setpwfile(argv[1]); 56 while (pwd = getpwent()) { 57 cp = buf; 58 #define COMPACT(e) tp = pwd->pw_/**/e; while (*cp++ = *tp++); 59 COMPACT(name); 60 COMPACT(passwd); 61 bcopy((char *)&pwd->pw_uid, cp, sizeof (int)); 62 cp += sizeof (int); 63 bcopy((char *)&pwd->pw_gid, cp, sizeof (int)); 64 cp += sizeof (int); 65 bcopy((char *)&pwd->pw_quota, cp, sizeof (int)); 66 cp += sizeof (int); 67 COMPACT(comment); 68 COMPACT(gecos); 69 COMPACT(dir); 70 COMPACT(shell); 71 content.dptr = buf; 72 content.dsize = cp - buf; 73 if (verbose) 74 printf("store %s, uid %d\n", pwd->pw_name, pwd->pw_uid); 75 key.dptr = pwd->pw_name; 76 key.dsize = strlen(pwd->pw_name); 77 if (dbm_store(dp, key, content, DBM_INSERT) < 0) { 78 fprintf(stderr, "mkpasswd: "); 79 perror("dbm_store failed"); 80 exit(1); 81 } 82 key.dptr = (char *)&pwd->pw_uid; 83 key.dsize = sizeof (int); 84 if (dbm_store(dp, key, content, DBM_INSERT) < 0) { 85 fprintf(stderr, "mkpasswd: "); 86 perror("dbm_store failed"); 87 exit(1); 88 } 89 entries++; 90 if (cp - buf > maxlen) 91 maxlen = cp - buf; 92 } 93 dbm_close(dp); 94 printf("%d password entries, maximum length %d\n", entries, maxlen); 95 exit(0); 96 } 97