Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / support / pop / RCS / popauth.c,v
1 head    1.7;
2 access;
3 symbols;
4 locks; strict;
5 comment @ * @;
6
7
8 1.7
9 date    92.12.15.00.20.22;      author jromine; state Exp;
10 branches;
11 next    1.6;
12
13 1.6
14 date    92.05.19.20.59.48;      author jromine; state Exp;
15 branches;
16 next    1.5;
17
18 1.5
19 date    92.02.08.05.39.47;      author jromine; state Exp;
20 branches;
21 next    1.4;
22
23 1.4
24 date    92.02.08.00.09.16;      author jromine; state Exp;
25 branches;
26 next    1.3;
27
28 1.3
29 date    92.02.05.17.56.15;      author jromine; state Exp;
30 branches;
31 next    1.2;
32
33 1.2
34 date    92.02.04.21.48.55;      author jromine; state Exp;
35 branches;
36 next    1.1;
37
38 1.1
39 date    92.02.04.21.48.15;      author jromine; state Exp;
40 branches;
41 next    ;
42
43
44 desc
45 @from mrose for APOP
46 @
47
48
49 1.7
50 log
51 @endif sugar
52 @
53 text
54 @/* popauth.c - manipulate POP authorization DB */
55 #ifndef lint
56 static char ident[] = "@@(#)$Id: popauth.c,v 1.6 1992/05/19 20:59:48 jromine Exp jromine $";
57 #endif  /* lint */
58
59 #include "../h/mh.h"
60 #include "popauth.h"
61 #undef  DBM             /* used by mts.c and ndbm.h */
62 #include <ndbm.h>
63 #include <pwd.h>
64 #include <stdio.h>
65 #include <sys/types.h>
66 #include <sys/stat.h>
67 #include <sys/file.h>
68 #ifdef  SYS5
69 #include <fcntl.h>
70 #endif
71 #include "../zotnet/bboards.h"
72 #include "../zotnet/mts.h"
73
74 /* \f */
75
76 static struct swit switches[] = {
77 #define INITSW  0
78     "init", 0,
79 #define LISTSW  1
80     "list", 0,
81 #define USERSW  2
82     "user name", 0,
83
84 #define HELPSW  3
85     "help", 4,
86
87     NULL, 0
88 };
89
90 /* \f */
91
92 char   *getpass ();
93
94 /* \f */
95
96 /* ARGSUSED */
97
98 main (argc, argv)
99 int     argc;
100 char   *argv[];
101 {
102     int     flags,
103             i,
104             initsw = 0,
105             insist,
106             listsw = 0;
107     long    clock;
108     char   *bp,
109            *cp,
110            *usersw = NULL,
111             buf[100],
112           **ap,
113           **argp,
114            *arguments[MAXARGS];
115     datum   key,
116             value;
117     DBM    *db;
118     struct authinfo auth;
119
120     invo_name = r1bindex (argv[0], '/');
121     m_foil (NULLCP);
122     if ((cp = m_find (invo_name)) != NULL) {
123         ap = brkstring (cp = getcpy (cp), " ", "\n");
124         ap = copyip (ap, arguments);
125     }
126     else
127         ap = arguments;
128     (void) copyip (argv + 1, ap);
129     argp = arguments;
130
131 /* \f */
132
133     while (cp = *argp++) {
134         if (*cp == '-')
135             switch (smatch (++cp, switches)) {
136                 case AMBIGSW: 
137                     ambigsw (cp, switches);
138                     done (1);
139                 case UNKWNSW: 
140                     adios (NULLCP, "-%s unknown", cp);
141                 case HELPSW: 
142                     (void) sprintf (buf, "%s [switches]", invo_name);
143                     help (buf, switches);
144                     done (1);
145
146                 case INITSW:
147                     initsw = 1, listsw = 0;
148                     continue;
149                 case LISTSW:
150                     listsw = 1, initsw = 0;
151                     continue;
152                 case USERSW:
153                     if (!(usersw = *argp++) || *usersw == '-')
154                         adios (NULLCP, "missing argument to %s", argp[-2]);
155                     continue;
156             }
157         adios (NULLCP, "usage: %s [switches]", invo_name);
158     }
159
160 /* \f */
161
162 #ifndef APOP
163     adios (NULLCP, "not compiled with APOP option");
164 #else
165     if (getuid ())
166         initsw = listsw = 0, usersw = NULL;
167
168     if (initsw) {
169         struct passwd *pw;
170         struct stat st;
171
172         if ((pw = getpwnam (POPUID)) == NULL)
173             adios (NULLCP, "POP user-id unknown");
174
175         (void) sprintf (buf, "%s.dir", APOP);
176         if (stat (buf, &st) != NOTOK) {
177             if (!getanswer ("Really initialize POP authorization DB? "))
178                 done (1);
179             (void) unlink (buf);
180             (void) sprintf (buf, "%s.pag", APOP);           
181             (void) unlink (buf);
182         }
183         if ((db = dbm_open (APOP, O_RDWR | O_CREAT, 0600)) == NULL)
184             adios (APOP, "unable to create POP authorization DB");
185         if (fchown (dbm_dirfno (db), pw -> pw_uid, pw -> pw_gid) == NOTOK
186                 || fchown (dbm_pagfno (db), pw -> pw_uid, pw -> pw_gid)
187                         == NOTOK)
188             advise (" ", "error setting ownership of POP authorization DB");
189
190         done (0);
191     }
192
193     if ((db = dbm_open (APOP, O_RDONLY, 0)) == NULL)
194         adios (APOP, "unable to open POP authorization DB");
195
196     if (flock (dbm_pagfno (db), LOCK_SH) == NOTOK)
197         adios (APOP, "unable to lock POP authorization DB");
198
199     if (listsw) {
200         if (usersw) {
201             key.dsize = strlen (key.dptr = usersw) + 1;
202             value = dbm_fetch (db, key);
203             if (value.dptr == NULL)
204                 adios (NULLCP, "no such entry in POP authorization DB");
205             bcopy (value.dptr, (char *) &auth, sizeof auth);
206             printf ("%s\n", key.dptr);
207         }
208         else
209             for (key = dbm_firstkey (db); key.dptr; key = dbm_nextkey (db)) {
210                 printf ("%s", key.dptr);
211                 value = dbm_fetch (db, key);
212                 if (value.dptr == NULL)
213                     printf (" - no information?!?\n");
214                 else {
215                     bcopy (value.dptr, (char *) &auth, sizeof auth);
216                     printf ("\n");
217                 }
218             }
219
220         dbm_close (db);
221
222         done (0);
223     }
224
225     if (usersw == NULL)
226         usersw = getusr ();
227
228     fprintf (stderr, "Changing POP password for %s.\n", usersw);
229
230     key.dsize = strlen (key.dptr = usersw) + 1;
231     value = dbm_fetch (db, key);
232     if (value.dptr != NULL) {
233         bcopy (value.dptr, (char *) &auth, sizeof auth);
234         dbm_close (db);
235
236         if ((i = strlen (strcpy (buf, getpass ("Old password:")))) == 0
237                 || auth.auth_secretlen != i
238                 || bcmp (buf, auth.auth_secret, i))
239             fprintf (stderr, "Sorry.\n"), exit (1);
240     }
241     else
242         dbm_close (db);
243
244 #ifdef  lint
245     flags = 0;
246 #endif  /* lint */
247     for (insist = 0; insist < 2; insist++) {
248         int     i;
249         char    c;
250
251         if (insist)
252             printf ("Please use %s.\n",
253                     flags == 1 ? "at least one non-numeric character"
254                     : "a longer password");
255
256         if ((i = strlen (strcpy (buf, getpass ("New password:")))) == 0) {
257             fprintf (stderr, "Password unchanged.\n");
258             exit (1);
259         }
260
261         flags = 0;
262         for (cp = buf; c = *cp++;)
263             if (c >= 'a' && c <= 'z')
264                 flags |= 2;
265             else
266                 if (c >= 'A' && c <= 'Z')
267                     flags |= 4;
268                 else
269                     if (c >= '0' && c <= '9')
270                         flags |= 1;
271                     else
272                         flags |= 8;
273
274         if ((flags >= 7 && i >= 4)
275                 || ((flags == 2 || flags == 4) && i >= 6)
276                 || ((flags == 3 || flags == 5 || flags == 6) && i >= 5))
277             break;
278     }
279
280     if (strcmp (buf, getpass ("Retype new password:"))) {
281         fprintf (stderr, "Mismatch - password unchanged.\n");
282         exit (1);
283     }
284
285     if ((db = dbm_open (APOP, O_RDWR, 0)) == NULL)
286         adios (APOP, "unable to open POP authorization DB");
287
288     if (flock (dbm_pagfno (db), LOCK_EX) == NOTOK)
289         adios (APOP, "unable to lock POP authorization DB");
290
291     key.dsize = strlen (key.dptr = usersw) + 1;
292
293     buf[sizeof auth.auth_secret] = NULL;
294     bcopy (buf, auth.auth_secret, auth.auth_secretlen = strlen (buf));
295     value.dptr = (char *) &auth, value.dsize = sizeof auth;
296
297     if (dbm_store (db, key, value, DBM_REPLACE))
298         adios (NULLCP, "POP authorization DB may be corrupt?!?");
299     dbm_close (db);
300 #endif
301
302     done (0);
303     /* NOTREACHED */
304 }
305 @
306
307
308 1.6
309 log
310 @NULL->0
311 @
312 text
313 @d3 2
314 a4 2
315 static char ident[] = "@@(#)$Id: popauth.c,v 1.5 1992/02/08 05:39:47 jromine Exp jromine $";
316 #endif  lint
317 d193 1
318 a193 1
319 #endif  lint
320 @
321
322
323 1.5
324 log
325 @fix from mrose
326 @
327 text
328 @d3 1
329 a3 1
330 static char ident[] = "@@(#)$Id: popauth.c,v 1.4 1992/02/08 00:09:16 jromine Exp jromine $";
331 d34 1
332 a34 1
333     NULL, NULL
334 @
335
336
337 1.4
338 log
339 @fix from mrose
340 @
341 text
342 @d3 1
343 a3 1
344 static char ident[] = "@@(#)$Id: popauth.c,v 1.3 1992/02/05 17:56:15 jromine Exp jromine $";
345 d109 3
346 a111 1
347 #ifdef  APOP
348 @
349
350
351 1.3
352 log
353 @undef DBM
354 @
355 text
356 @d3 1
357 a3 1
358 static char ident[] = "@@(#)$Id: popauth.c,v 1.2 1992/02/04 21:48:55 jromine Exp jromine $";
359 d50 1
360 a112 4
361     if ((db = dbm_open (APOP, listsw ? O_RDONLY : O_RDWR, 0))
362             == NULL)
363         adios (APOP, "unable to open POP authorization DB");
364
365 d138 6
366 d172 1
367 d174 15
368 d229 3
369 @
370
371
372 1.2
373 log
374 @contributed patch
375 @
376 text
377 @d3 1
378 a3 1
379 static char ident[] = "@@(#)$Id: scan.c,v 1.11 1992/02/03 22:45:20 jromine Exp $";
380 d8 1
381 @
382
383
384 1.1
385 log
386 @Initial revision
387 @
388 text
389 @d2 3
390 d7 1
391 a37 7
392 struct authinfo {
393     long    auth_clock;
394     char    auth_secret[16];
395     int     auth_secretlen;
396 };
397
398
399 d132 4
400 a135 2
401         (void) fchown (dbm_dirfno (db), pw -> pw_uid, pw -> pw_gid);
402         (void) fchown (dbm_pagfno (db), pw -> pw_uid, pw -> pw_gid);
403 d147 1
404 a147 1
405             printf ("%s %ld\n", key.dptr, auth.auth_clock);
406 d151 1
407 a151 1
408                 printf ("%s ", key.dptr);
409 d154 1
410 a154 1
411                     printf ("- no information?!?\n");
412 d157 1
413 a157 1
414                     printf ("%ld\n", auth.auth_clock);
415 a214 1
416     (void) time (&auth.auth_clock);
417 @