1 /* popauth.c - manipulate POP authorization DB */
3 static char ident[] = "@(#)$Id: popauth.c,v 1.7 1992/12/15 00:20:22 jromine Exp $";
8 #undef DBM /* used by mts.c and ndbm.h */
12 #include <sys/types.h>
18 #include "../zotnet/bboards.h"
19 #include "../zotnet/mts.h"
23 static struct swit switches[] = {
67 invo_name = r1bindex (argv[0], '/');
69 if ((cp = m_find (invo_name)) != NULL) {
70 ap = brkstring (cp = getcpy (cp), " ", "\n");
71 ap = copyip (ap, arguments);
75 (void) copyip (argv + 1, ap);
80 while (cp = *argp++) {
82 switch (smatch (++cp, switches)) {
84 ambigsw (cp, switches);
87 adios (NULLCP, "-%s unknown", cp);
89 (void) sprintf (buf, "%s [switches]", invo_name);
94 initsw = 1, listsw = 0;
97 listsw = 1, initsw = 0;
100 if (!(usersw = *argp++) || *usersw == '-')
101 adios (NULLCP, "missing argument to %s", argp[-2]);
104 adios (NULLCP, "usage: %s [switches]", invo_name);
110 adios (NULLCP, "not compiled with APOP option");
113 initsw = listsw = 0, usersw = NULL;
119 if ((pw = getpwnam (POPUID)) == NULL)
120 adios (NULLCP, "POP user-id unknown");
122 (void) sprintf (buf, "%s.dir", APOP);
123 if (stat (buf, &st) != NOTOK) {
124 if (!getanswer ("Really initialize POP authorization DB? "))
127 (void) sprintf (buf, "%s.pag", APOP);
130 if ((db = dbm_open (APOP, O_RDWR | O_CREAT, 0600)) == NULL)
131 adios (APOP, "unable to create POP authorization DB");
132 if (fchown (dbm_dirfno (db), pw -> pw_uid, pw -> pw_gid) == NOTOK
133 || fchown (dbm_pagfno (db), pw -> pw_uid, pw -> pw_gid)
135 advise (" ", "error setting ownership of POP authorization DB");
140 if ((db = dbm_open (APOP, O_RDONLY, 0)) == NULL)
141 adios (APOP, "unable to open POP authorization DB");
143 if (flock (dbm_pagfno (db), LOCK_SH) == NOTOK)
144 adios (APOP, "unable to lock POP authorization DB");
148 key.dsize = strlen (key.dptr = usersw) + 1;
149 value = dbm_fetch (db, key);
150 if (value.dptr == NULL)
151 adios (NULLCP, "no such entry in POP authorization DB");
152 bcopy (value.dptr, (char *) &auth, sizeof auth);
153 printf ("%s\n", key.dptr);
156 for (key = dbm_firstkey (db); key.dptr; key = dbm_nextkey (db)) {
157 printf ("%s", key.dptr);
158 value = dbm_fetch (db, key);
159 if (value.dptr == NULL)
160 printf (" - no information?!?\n");
162 bcopy (value.dptr, (char *) &auth, sizeof auth);
175 fprintf (stderr, "Changing POP password for %s.\n", usersw);
177 key.dsize = strlen (key.dptr = usersw) + 1;
178 value = dbm_fetch (db, key);
179 if (value.dptr != NULL) {
180 bcopy (value.dptr, (char *) &auth, sizeof auth);
183 if ((i = strlen (strcpy (buf, getpass ("Old password:")))) == 0
184 || auth.auth_secretlen != i
185 || bcmp (buf, auth.auth_secret, i))
186 fprintf (stderr, "Sorry.\n"), exit (1);
194 for (insist = 0; insist < 2; insist++) {
199 printf ("Please use %s.\n",
200 flags == 1 ? "at least one non-numeric character"
201 : "a longer password");
203 if ((i = strlen (strcpy (buf, getpass ("New password:")))) == 0) {
204 fprintf (stderr, "Password unchanged.\n");
209 for (cp = buf; c = *cp++;)
210 if (c >= 'a' && c <= 'z')
213 if (c >= 'A' && c <= 'Z')
216 if (c >= '0' && c <= '9')
221 if ((flags >= 7 && i >= 4)
222 || ((flags == 2 || flags == 4) && i >= 6)
223 || ((flags == 3 || flags == 5 || flags == 6) && i >= 5))
227 if (strcmp (buf, getpass ("Retype new password:"))) {
228 fprintf (stderr, "Mismatch - password unchanged.\n");
232 if ((db = dbm_open (APOP, O_RDWR, 0)) == NULL)
233 adios (APOP, "unable to open POP authorization DB");
235 if (flock (dbm_pagfno (db), LOCK_EX) == NOTOK)
236 adios (APOP, "unable to lock POP authorization DB");
238 key.dsize = strlen (key.dptr = usersw) + 1;
240 buf[sizeof auth.auth_secret] = NULL;
241 bcopy (buf, auth.auth_secret, auth.auth_secretlen = strlen (buf));
242 value.dptr = (char *) &auth, value.dsize = sizeof auth;
244 if (dbm_store (db, key, value, DBM_REPLACE))
245 adios (NULLCP, "POP authorization DB may be corrupt?!?");