|
|
1.1 root 1: /* getbbent.c - subroutines for accessing the BBoards file */
2:
3: /* LINTLIBRARY */
4:
5: #include "bboards.h"
6: #ifndef MMDFONLY
7: #include "../h/strings.h"
8: #include <sys/types.h>
9: #else MMDFONLY
10: #include "util.h"
11: #include "mmdf.h"
12: #include "strings.h"
13: #endif MMDFONLY
14: #include <ctype.h>
15: #include <pwd.h>
16: #include <grp.h>
17: #include <stdio.h>
18: #include <sys/stat.h>
19:
20:
21: #ifndef MMDFONLY
22: #define NOTOK (-1)
23: #define OK 0
24: #endif not MMDFONLY
25:
26:
27: #define MaxBBAka 100
28: #define MaxBBLdr 100
29: #define MaxBBDist 100
30:
31:
32: #define NCOLON 9 /* currently 10 fields per entry */
33:
34: #define COLON ':'
35: #define COMMA ','
36: #define NEWLINE '\n'
37:
38:
39: #define ARCHIVE "archive"
40: #define CNTFILE ".cnt"
41: #define DSTFILE ".dist"
42: #define MAPFILE ".map"
43:
44: /* */
45:
46: static int BBuid = -1;
47:
48: static unsigned int BBflags = SB_NULL;
49:
50: static char BBName[BUFSIZ] = BBOARDS;
51: static char BBDir[BUFSIZ] = "";
52: static char BBData[BUFSIZ] = "";
53:
54: static FILE *BBfile = NULL;
55:
56:
57: static struct bboard BB;
58: static struct bboard *bb = &BB;
59:
60: static int BBload = 1;
61:
62: static char BBFile[BUFSIZ];
63: static char BBArchive[BUFSIZ];
64: static char BBInfo[BUFSIZ];
65: static char BBMap[BUFSIZ];
66: static char *BBAkas[MaxBBAka];
67: static char *BBLeaders[MaxBBLdr];
68: static char *BBDists[MaxBBDist];
69: static char BBAddr[BUFSIZ];
70: static char BBRequest[BUFSIZ];
71: static char BBDate[BUFSIZ];
72: static char BBErrors[BUFSIZ];
73:
74: #ifdef MMDFONLY
75: extern LLog *logptr;
76: #endif MMDFONLY
77:
78: char *crypt(), *getpass();
79: struct group *getgrnam();
80: struct passwd *getpwnam(), *getpwuid();
81:
82: static int setbbaux(), setpwaux(), BBread(), getbbitem(), bblose();
83: static char *bbskip(), *getcpy();
84:
85: /* */
86:
87: setbbfile(file, f)
88: register char *file;
89: register int f;
90: {
91: if (BBuid == -1)
92: return setbbinfo (BBOARDS, file, f);
93:
94: (void) strcpy (BBData, file);
95:
96: BBflags = SB_NULL;
97: (void) endbbent ();
98:
99: return setbbent (f);
100: }
101:
102: /* */
103:
104: setbbinfo(user, file, f)
105: register char *user, *file;
106: register int f;
107: {
108: register struct passwd *pw;
109:
110: if ((pw = getpwnam (user)) == NULL) {
111: (void) sprintf (BBErrors, "unknown user: %s", user);
112: return 0;
113: }
114:
115: return setpwinfo (pw, file, f);
116: }
117:
118:
119: setpwinfo(pw, file, f)
120: register struct passwd *pw;
121: register char *file;
122: register int f;
123: {
124: if (!setpwaux (pw, file))
125: return 0;
126:
127: BBflags = SB_NULL;
128: (void) endbbent ();
129:
130: return setbbent (f);
131: }
132:
133: /* */
134:
135: static int
136: setbbaux(name, file)
137: register char *name, *file;
138: {
139: register struct passwd *pw;
140:
141: if ((pw = getpwnam (name)) == NULL) {
142: (void) sprintf (BBErrors, "unknown user: %s", name);
143: return 0;
144: }
145:
146: return setpwaux (pw, file);
147: }
148:
149:
150: static int
151: setpwaux(pw, file)
152: register struct passwd *pw;
153: register char *file;
154: {
155: (void) strcpy (BBName, pw -> pw_name);
156: BBuid = pw -> pw_uid;
157: (void) strcpy (BBDir, pw -> pw_dir);
158: (void) sprintf (BBData, "%s/%s",
159: *file != '/' ? BBDir : "",
160: *file != '/' ? file : file + 1);
161:
162: BBflags = SB_NULL;
163:
164: return 1;
165: }
166:
167: /* */
168:
169: setbbent(f)
170: register int f;
171: {
172: if (BBfile == NULL) {
173: if (BBuid == -1 && !setbbaux (BBOARDS, BBDB))
174: return 0;
175:
176: if ((BBfile = fopen (BBData, "r")) == NULL) {
177: (void) sprintf (BBErrors, "unable to open: %s", BBData);
178: return 0;
179: }
180: }
181: else
182: rewind (BBfile);
183:
184: BBflags |= f;
185: return (BBfile != NULL);
186: }
187:
188:
189: int endbbent () {
190: if (BBfile != NULL && !(BBflags & SB_STAY)) {
191: (void) fclose (BBfile);
192: BBfile = NULL;
193: }
194:
195: return 1;
196: }
197:
198:
199: long
200: getbbtime()
201: {
202: struct stat st;
203:
204: if (BBfile == NULL) {
205: if (BBuid == -1 && !setbbaux (BBOARDS, BBDB))
206: return 0;
207:
208: if (stat (BBData, &st) == NOTOK) {
209: (void) sprintf (BBErrors, "unable to stat: %s", BBData);
210: return 0;
211: }
212: }
213: else
214: if (fstat (fileno (BBfile), &st) == NOTOK) {
215: (void) sprintf (BBErrors, "unable to fstat: %s", BBData);
216: return 0;
217: }
218:
219: return ((long) st.st_mtime);
220: }
221:
222: /* */
223:
224: struct bboard *
225: getbbent()
226: {
227: register int count;
228: register char *p,
229: *q,
230: *r,
231: *d,
232: *f,
233: **s;
234: static char line[BUFSIZ];
235:
236: if (BBfile == NULL && !setbbent (SB_NULL))
237: return NULL;
238:
239: retry: ;
240: if ((p = fgets (line, sizeof line, BBfile)) == NULL)
241: return NULL;
242:
243: for (q = p, count = 0; *q != NULL && *q != NEWLINE; q++)
244: if (*q == COLON)
245: count++;
246:
247: if (count != NCOLON) {
248: #ifdef MMDFONLY
249: if (q = index (p, NEWLINE))
250: *q = NULL;
251: ll_log (logptr, LLOGTMP, "bad entry in %s: %s", BBData, p);
252: #endif MMDFONLY
253: goto retry;
254: }
255:
256: bb -> bb_name = p;
257: p = q = bbskip (p, COLON);
258: p = bb -> bb_file = bbskip (p, COLON);
259: bb -> bb_archive = bb -> bb_info = bb -> bb_map = "";
260: p = bb -> bb_passwd = bbskip (p, COLON);
261: p = r = bbskip (p, COLON);
262: p = bb -> bb_addr = bbskip (p, COLON);
263: p = bb -> bb_request = bbskip (p, COLON);
264: p = bb -> bb_relay = bbskip (p, COLON);
265: p = d = bbskip (p, COLON);
266: p = f = bbskip (p, COLON);
267: (void) bbskip (p, NEWLINE);
268:
269: s = bb -> bb_aka = BBAkas;
270: while (*q) {
271: *s++ = q;
272: q = bbskip (q, COMMA);
273: }
274: *s = NULL;
275:
276: s = bb -> bb_leader = BBLeaders;
277: if (*r == NULL) {
278: if (!(BBflags & SB_FAST))
279: *s++ = BBName;
280: }
281: else
282: while (*r) {
283: *s++ = r;
284: r = bbskip (r, COMMA);
285: }
286: *s = NULL;
287:
288: s = bb -> bb_dist = BBDists;
289: while (*d) {
290: *s++ = d;
291: d = bbskip (d, COMMA);
292: }
293: *s = NULL;
294:
295: if (*f)
296: (void) sscanf (f, "%o", &bb -> bb_flags);
297: else
298: bb -> bb_flags = BB_NULL;
299: bb -> bb_count = bb -> bb_maxima = 0;
300: bb -> bb_date = NULL;
301: bb -> bb_next = bb -> bb_link = bb -> bb_chain = NULL;
302:
303: if (BBload)
304: BBread ();
305:
306: return bb;
307: }
308:
309: /* */
310:
311: struct bboard *
312: getbbnam(name)
313: register char *name;
314: {
315: register struct bboard *b = NULL;
316:
317: if (!setbbent (SB_NULL))
318: return NULL;
319: BBload = 0;
320: while ((b = getbbent ()) && strcmp (name, b -> bb_name))
321: continue;
322: BBload = 1;
323: (void) endbbent ();
324:
325: if (b != NULL)
326: BBread ();
327:
328: return b;
329: }
330:
331:
332: struct bboard *
333: getbbaka(aka)
334: register char *aka;
335: {
336: register char **ap;
337: register struct bboard *b = NULL;
338:
339: if (!setbbent (SB_NULL))
340: return NULL;
341: BBload = 0;
342: while ((b = getbbent ()) != NULL)
343: for (ap = b -> bb_aka; *ap; ap++)
344: if (strcmp (aka, *ap) == 0)
345: goto hit;
346: hit: ;
347: BBload = 1;
348: (void) endbbent ();
349:
350: if (b != NULL)
351: BBread ();
352:
353: return b;
354: }
355:
356: /* */
357:
358: static int
359: BBread()
360: {
361: register int i;
362: register char *cp,
363: *dp,
364: *p,
365: *r;
366: char prf[BUFSIZ];
367: static char line[BUFSIZ];
368: register FILE * info;
369:
370: if (BBflags & SB_FAST)
371: return;
372:
373: p = index (bb -> bb_request, '@');
374: r = index (bb -> bb_addr, '@');
375: BBRequest[0] = NULL;
376:
377: if (*bb -> bb_request == '-')
378: if (p == NULL && r && *r == '@')
379: (void) sprintf (BBRequest, "%s%s%s",
380: bb -> bb_name, bb -> bb_request, r);
381: else
382: (void) sprintf (BBRequest, "%s%s",
383: bb -> bb_name, bb -> bb_request);
384: else
385: if (p == NULL && r && *r == '@' && *bb -> bb_request)
386: (void) sprintf (BBRequest, "%s%s", bb -> bb_request, r);
387:
388: if (BBRequest[0])
389: bb -> bb_request = BBRequest;
390: else
391: if (*bb -> bb_request == NULL)
392: bb -> bb_request = *bb -> bb_addr ? bb -> bb_addr
393: : bb -> bb_leader[0];
394:
395: if (*bb -> bb_addr == '@') {
396: (void) sprintf (BBAddr, "%s%s", bb -> bb_name, bb -> bb_addr);
397: bb -> bb_addr = BBAddr;
398: }
399: else
400: if (*bb -> bb_addr == NULL)
401: bb -> bb_addr = bb -> bb_name;
402:
403: if (*bb -> bb_file == NULL)
404: return;
405: if (*bb -> bb_file != '/') {
406: (void) sprintf (BBFile, "%s/%s", BBDir, bb -> bb_file);
407: bb -> bb_file = BBFile;
408: }
409:
410: if ((cp = rindex (bb -> bb_file, '/')) == NULL || *++cp == NULL)
411: (void) strcpy (prf, ""), cp = bb -> bb_file;
412: else
413: (void) sprintf (prf, "%.*s", cp - bb -> bb_file, bb -> bb_file);
414: if ((dp = index (cp, '.')) == NULL)
415: dp = cp + strlen (cp);
416:
417: (void) sprintf (BBArchive, "%s%s/%s", prf, ARCHIVE, cp);
418: bb -> bb_archive = BBArchive;
419: (void) sprintf (BBInfo, "%s.%.*s%s", prf, dp - cp, cp, CNTFILE);
420: bb -> bb_info = BBInfo;
421: (void) sprintf (BBMap, "%s.%.*s%s", prf, dp - cp, cp, MAPFILE);
422: bb -> bb_map = BBMap;
423:
424: if ((info = fopen (bb -> bb_info, "r")) == NULL)
425: return;
426:
427: if (fgets (line, sizeof line, info) && (i = atoi (line)) > 0)
428: bb -> bb_maxima = (unsigned) i;
429: if (!feof (info) && fgets (line, sizeof line, info)) {
430: (void) strcpy (BBDate, line);
431: if (cp = index (BBDate, NEWLINE))
432: *cp = NULL;
433: bb -> bb_date = BBDate;
434: }
435:
436: (void) fclose (info);
437: }
438:
439: /* */
440:
441: ldrbb(b)
442: register struct bboard *b;
443: {
444: register char *p,
445: **q,
446: **r;
447: static int uid = 0,
448: gid = 0;
449: static char username[10] = "";
450: register struct passwd *pw;
451: register struct group *gr;
452:
453: if (b == NULL)
454: return 0;
455: if (BBuid == -1 && !setbbaux (BBOARDS, BBDB))
456: return 0;
457:
458: if (username[0] == NULL) {
459: if ((pw = getpwuid (uid = getuid ())) == NULL)
460: return 0;
461: gid = getgid ();
462: (void) strcpy (username, pw -> pw_name);
463: }
464:
465: if (uid == BBuid)
466: return 1;
467:
468: q = b -> bb_leader;
469: while (p = *q++)
470: if (*p == '=') {
471: if ((gr = getgrnam (++p)) == NULL)
472: continue;
473: if (gid == gr -> gr_gid)
474: return 1;
475: r = gr -> gr_mem;
476: while (p = *r++)
477: if (strcmp (username, p) == 0)
478: return 1;
479: }
480: else
481: if (strcmp (username, p) == 0)
482: return 1;
483:
484: return 0;
485: }
486:
487: /* */
488:
489: ldrchk(b)
490: register struct bboard *b;
491: {
492: if (b == NULL)
493: return 0;
494:
495: if (*b -> bb_passwd == NULL)
496: return 1;
497:
498: if (strcmp (b -> bb_passwd,
499: crypt (getpass ("Password: "), b -> bb_passwd)) == 0)
500: return 1;
501:
502: fprintf (stderr, "Sorry\n");
503: return 0;
504: }
505:
506: /* */
507:
508: struct bboard *
509: getbbcpy(bp)
510: register struct bboard *bp;
511: {
512: register char **p,
513: **q;
514: register struct bboard *b;
515:
516: if (bp == NULL)
517: return NULL;
518:
519: b = (struct bboard *) malloc ((unsigned) sizeof *b);
520: if (b == NULL)
521: return NULL;
522:
523: b -> bb_name = getcpy (bp -> bb_name);
524: b -> bb_file = getcpy (bp -> bb_file);
525: b -> bb_archive = getcpy (bp -> bb_archive);
526: b -> bb_info = getcpy (bp -> bb_info);
527: b -> bb_map = getcpy (bp -> bb_map);
528: b -> bb_passwd = getcpy (bp -> bb_passwd);
529: b -> bb_flags = bp -> bb_flags;
530: b -> bb_count = bp -> bb_count;
531: b -> bb_maxima = bp -> bb_maxima;
532: b -> bb_date = getcpy (bp -> bb_date);
533: b -> bb_addr = getcpy (bp -> bb_addr);
534: b -> bb_request = getcpy (bp -> bb_request);
535: b -> bb_relay = getcpy (bp -> bb_relay);
536:
537: for (p = bp -> bb_aka; *p; p++)
538: continue;
539: b -> bb_aka =
540: q = (char **) calloc ((unsigned) (p - bp -> bb_aka + 1), sizeof *q);
541: if (q == NULL)
542: return NULL;
543: for (p = bp -> bb_aka; *p; *q++ = getcpy (*p++))
544: continue;
545: *q = NULL;
546:
547: for (p = bp -> bb_leader; *p; p++)
548: continue;
549: b -> bb_leader =
550: q = (char **) calloc ((unsigned) (p - bp -> bb_leader + 1), sizeof *q);
551: if (q == NULL)
552: return NULL;
553: for (p = bp -> bb_leader; *p; *q++ = getcpy (*p++))
554: continue;
555: *q = NULL;
556:
557: for (p = bp -> bb_dist; *p; p++)
558: continue;
559: b -> bb_dist =
560: q = (char **) calloc ((unsigned) (p - bp -> bb_dist + 1), sizeof *q);
561: if (q == NULL)
562: return NULL;
563: for (p = bp -> bb_dist; *p; *q++ = getcpy (*p++))
564: continue;
565: *q = NULL;
566:
567: b -> bb_next = bp -> bb_next;
568: b -> bb_link = bp -> bb_link;
569: b -> bb_chain = bp -> bb_chain;
570:
571: return b;
572: }
573:
574: /* */
575:
576: getbbdist(bb, action)
577: register struct bboard *bb;
578: register int (*action)();
579: {
580: register int result;
581: register char **dp;
582:
583: BBErrors[0] = NULL;
584: for (dp = bb -> bb_dist; *dp; dp++)
585: if (result = getbbitem (bb, *dp, action))
586: return result;
587:
588: return result;
589: }
590:
591: char *getbberr () {
592: return (BBErrors[0] ? BBErrors : NULL);
593: };
594:
595: /* */
596:
597: static int
598: getbbitem(bb, item, action)
599: register struct bboard *bb;
600: register char *item;
601: register int (*action)();
602: {
603: register int result;
604: register char *cp,
605: *dp,
606: *hp,
607: *np;
608: char mbox[BUFSIZ],
609: buffer[BUFSIZ],
610: file[BUFSIZ],
611: host[BUFSIZ],
612: prf[BUFSIZ];
613: register FILE *fp;
614:
615: switch (*item) {
616: case '*':
617: switch (*++item) {
618: case '/':
619: hp = item;
620: break;
621:
622: case NULL:
623: if ((cp = rindex (bb -> bb_file, '/')) == NULL || *++cp == NULL)
624: (void) strcpy (prf, ""), cp = bb -> bb_file;
625: else
626: (void) sprintf (prf, "%.*s", cp - bb -> bb_file, bb -> bb_file);
627: if ((dp = index (cp, '.')) == NULL)
628: dp = cp + strlen (cp);
629: (void) sprintf (file, "%s.%.*s%s", prf, dp - cp, cp, DSTFILE);
630: hp = file;
631: break;
632:
633: default:
634: (void) sprintf (file, "%s/%s", BBDir, item);
635: hp = file;
636: break;
637: }
638:
639: if ((fp = fopen (hp, "r")) == NULL)
640: return bblose ("unable to read file %s", hp);
641: while (fgets (buffer, sizeof buffer, fp)) {
642: if (np = index (buffer, '\n'))
643: *np = NULL;
644: if (result = getbbitem (bb, buffer, action)) {
645: (void) fclose (fp);
646: (void) bblose ("error with file %s, item %s", hp, buffer);
647: return result;
648: }
649: }
650: (void) fclose (fp);
651: return OK;
652:
653: default:
654: if (hp = rindex (item, '@')) {
655: *hp++ = NULL;
656: (void) strcpy (mbox, item);
657: (void) strcpy (host, hp);
658: *--hp = '@';
659: }
660: else {
661: (void) sprintf (mbox, "%s%s", DISTADR, bb -> bb_name);
662: (void) strcpy (host, item);
663: }
664: if (result = (*action) (mbox, host))
665: (void) bblose ("action (%s, %s) returned 0%o", mbox, host, result);
666: return result;
667: }
668: }
669:
670: /* */
671:
672: /* VARARGS1 */
673:
674: static int
675: bblose(fmt, a, b, c)
676: char *fmt, *a, *b, *c;
677: {
678: if (BBErrors[0] == NULL)
679: (void) sprintf (BBErrors, fmt, a, b, c);
680:
681: return NOTOK;
682: }
683:
684: /* */
685:
686: void
687: make_lower(s1, s2)
688: register char *s1, *s2;
689: {
690: if (s1 == NULL || s2 == NULL)
691: return;
692:
693: for (; *s2; s2++)
694: *s1++ = isupper (*s2) ? tolower (*s2) : *s2;
695: *s1 = NULL;
696: }
697:
698: /* */
699:
700: static char *
701: bbskip(p, c)
702: register char *p, c;
703: {
704: if (p == NULL)
705: return NULL;
706:
707: while (*p && *p != c)
708: p++;
709: if (*p)
710: *p++ = NULL;
711:
712: return p;
713: }
714:
715:
716: static char *
717: getcpy(s)
718: register char *s;
719: {
720: register char *p;
721:
722: if (s == NULL)
723: return NULL;
724:
725: if (p = malloc ((unsigned) (strlen (s) + 1)))
726: (void) strcpy (p, s);
727: return p;
728: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.