3 * getbbent.c -- subroutines for accessing the BBoards file
37 #define NCOLON 9 /* currently 10 fields per entry */
43 #define ARCHIVE "archive"
44 #define CNTFILE ".cnt"
45 #define DSTFILE ".dist"
46 #define MAPFILE ".map"
48 static int BBuid = -1;
50 static unsigned int BBflags = SB_NULL;
52 static char BBName[BUFSIZ] = BBOARDS;
53 static char BBDir[BUFSIZ] = "";
54 static char BBData[BUFSIZ] = "";
56 static FILE *BBfile = NULL;
58 static struct bboard BB;
59 static struct bboard *bb = &BB;
61 static int BBload = 1;
63 static char BBFile[BUFSIZ];
64 static char BBArchive[BUFSIZ];
65 static char BBInfo[BUFSIZ];
66 static char BBMap[BUFSIZ];
67 static char *BBAkas[MaxBBAka];
68 static char *BBLeaders[MaxBBLdr];
69 static char *BBDists[MaxBBDist];
70 static char BBAddr[BUFSIZ];
71 static char BBRequest[BUFSIZ];
72 static char BBDate[BUFSIZ];
73 static char BBErrors[BUFSIZ];
88 static int setbbaux (char *, char *);
89 static int setpwaux (struct passwd *, char *);
90 static void BBread (void);
91 static int getbbitem (struct bboard *, char *, int (*)());
92 static int bblose (char *, ...);
93 static char *bbskip (char *, char);
94 static char *getcpy (char *);
98 setbbfile (char *file, int f)
101 return setbbinfo (BBOARDS, file, f);
103 strncpy (BBData, file, sizeof(BBData));
113 setbbinfo (char *user, char *file, int f)
115 register struct passwd *pw;
117 if ((pw = getpwnam (user)) == NULL) {
118 snprintf (BBErrors, sizeof(BBErrors), "unknown user: %s", user);
122 return setpwinfo (pw, file, f);
127 setpwinfo (struct passwd *pw, char *file, int f)
129 if (!setpwaux (pw, file))
140 setbbaux (char *name, char *file)
142 register struct passwd *pw;
144 if ((pw = getpwnam (name)) == NULL) {
145 snprintf (BBErrors, sizeof(BBErrors), "unknown user: %s", name);
149 return setpwaux (pw, file);
154 setpwaux (struct passwd *pw, char *file)
156 strncpy (BBName, pw->pw_name, sizeof(BBName));
158 strncpy (BBDir, pw->pw_dir, sizeof(BBDir));
159 snprintf (BBData, sizeof(BBData), "%s/%s",
160 *file != '/' ? BBDir : "",
161 *file != '/' ? file : file + 1);
172 if (BBfile == NULL) {
173 if (BBuid == -1 && !setbbaux (BBOARDS, BBDB))
176 if ((BBfile = fopen (BBData, "r")) == NULL) {
177 snprintf (BBErrors, sizeof(BBErrors), "unable to open: %s", BBData);
185 return (BBfile != NULL);
192 if (BBfile != NULL && !(BBflags & SB_STAY)) {
206 if (BBfile == NULL) {
207 if (BBuid == -1 && !setbbaux (BBOARDS, BBDB))
210 if (stat (BBData, &st) == NOTOK) {
211 snprintf (BBErrors, sizeof(BBErrors), "unable to stat: %s", BBData);
215 if (fstat (fileno (BBfile), &st) == NOTOK) {
216 snprintf (BBErrors, sizeof(BBErrors), "unable to fstat: %s", BBData);
221 return ((long) st.st_mtime);
229 register char *p, *q, *r, *d, *f, **s;
230 static char line[BUFSIZ];
232 if (BBfile == NULL && !setbbent (SB_NULL))
236 if ((p = fgets (line, sizeof line, BBfile)) == NULL)
239 for (q = p, count = 0; *q != 0 && *q != NEWLINE; q++)
243 if (count != NCOLON) {
245 if (q = strchr(p, NEWLINE))
247 ll_log (logptr, LLOGTMP, "bad entry in %s: %s", BBData, p);
248 #endif /* MMDFONLY */
253 p = q = bbskip (p, COLON);
254 p = bb->bb_file = bbskip (p, COLON);
255 bb->bb_archive = bb->bb_info = bb->bb_map = "";
256 p = bb->bb_passwd = bbskip (p, COLON);
257 p = r = bbskip (p, COLON);
258 p = bb->bb_addr = bbskip (p, COLON);
259 p = bb->bb_request = bbskip (p, COLON);
260 p = bb->bb_relay = bbskip (p, COLON);
261 p = d = bbskip (p, COLON);
262 p = f = bbskip (p, COLON);
265 s = bb->bb_aka = BBAkas;
268 q = bbskip (q, COMMA);
272 s = bb->bb_leader = BBLeaders;
274 if (!(BBflags & SB_FAST)) {
282 r = bbskip (r, COMMA);
287 s = bb->bb_dist = BBDists;
290 d = bbskip (d, COMMA);
295 sscanf (f, "%o", &bb->bb_flags);
297 bb->bb_flags = BB_NULL;
298 bb->bb_count = bb->bb_maxima = 0;
300 bb->bb_next = bb->bb_link = bb->bb_chain = NULL;
304 * Only do a BBread on bboards that the user has expressed an
305 * interest in, if we were called by bbc.
308 register char **ap, *cp;
314 for (bbp = 0; cp = bbs[bbp]; bbp++) {
315 if (!strcmp(bb->bb_name, cp)) {
319 for (ap = bb->bb_aka; *ap; ap++)
320 if (!strcmp(*ap, cp)) {
337 getbbnam (char *name)
339 register struct bboard *b = NULL;
341 if (!setbbent (SB_NULL))
344 while ((b = getbbent ()) && strcmp (name, b->bb_name))
360 register struct bboard *b = NULL;
362 if (!setbbent (SB_NULL))
365 while ((b = getbbent ()) != NULL)
366 for (ap = b->bb_aka; *ap; ap++)
367 if (strcmp (aka, *ap) == 0)
384 register char *cp, *dp, *p, *r;
386 static char line[BUFSIZ];
387 register FILE * info;
389 if (BBflags & SB_FAST)
392 p = strchr(bb->bb_request, '@');
393 r = strchr(bb->bb_addr, '@');
396 if (*bb->bb_request == '-')
397 if (p == NULL && r && *r == '@')
398 snprintf (BBRequest, sizeof(BBRequest), "%s%s%s", bb->bb_name, bb->bb_request, r);
400 snprintf (BBRequest, sizeof(BBRequest), "%s%s", bb->bb_name, bb->bb_request);
402 if (p == NULL && r && *r == '@' && *bb->bb_request)
403 snprintf (BBRequest, sizeof(BBRequest), "%s%s", bb->bb_request, r);
406 bb->bb_request = BBRequest;
408 if (*bb->bb_request == 0)
409 bb->bb_request = *bb->bb_addr ? bb->bb_addr
412 if (*bb->bb_addr == '@') {
413 snprintf (BBAddr, sizeof(BBAddr), "%s%s", bb->bb_name, bb->bb_addr);
414 bb->bb_addr = BBAddr;
417 if (*bb->bb_addr == 0)
418 bb->bb_addr = bb->bb_name;
420 if (*bb->bb_file == 0)
422 if (*bb->bb_file != '/') {
423 snprintf (BBFile, sizeof(BBFile), "%s/%s", BBDir, bb->bb_file);
424 bb->bb_file = BBFile;
427 if ((cp = strrchr(bb->bb_file, '/')) == NULL || *++cp == 0) {
431 snprintf (prf, sizeof(prf), "%.*s", cp - bb->bb_file, bb->bb_file);
433 if ((dp = strchr(cp, '.')) == NULL)
434 dp = cp + strlen (cp);
436 snprintf (BBArchive, sizeof(BBArchive), "%s%s/%s", prf, ARCHIVE, cp);
437 bb->bb_archive = BBArchive;
438 snprintf (BBInfo, sizeof(BBInfo), "%s.%.*s%s", prf, dp - cp, cp, CNTFILE);
439 bb->bb_info = BBInfo;
440 snprintf (BBMap, sizeof(BBMap), "%s.%.*s%s", prf, dp - cp, cp, MAPFILE);
443 if ((info = fopen (bb->bb_info, "r")) == NULL)
446 if (fgets (line, sizeof line, info) && (i = atoi (line)) > 0)
447 bb->bb_maxima = (unsigned) i;
448 if (!feof (info) && fgets (line, sizeof line, info)) {
449 strncpy (BBDate, line, sizeof(BBData));
450 if ((cp = strchr(BBDate, NEWLINE)))
452 bb->bb_date = BBDate;
460 ldrbb (struct bboard *b)
462 register char *p, **q, **r;
463 static uid_t uid = 0;
464 static gid_t gid = 0;
465 static char username[10] = "";
466 register struct passwd *pw;
467 register struct group *gr;
471 if (BBuid == -1 && !setbbaux (BBOARDS, BBDB))
474 if (username[0] == 0) {
475 if ((pw = getpwuid (uid = getuid ())) == NULL)
478 strncpy (username, pw->pw_name, sizeof(username));
487 if ((gr = getgrnam (++p)) == NULL)
489 if (gid == gr->gr_gid)
493 if (strcmp (username, p) == 0)
497 if (strcmp (username, p) == 0)
505 ldrchk (struct bboard *b)
510 if (*b->bb_passwd == 0)
513 if (strcmp (b->bb_passwd,
514 crypt (getpass ("Password: "), b->bb_passwd)) == 0)
517 fprintf (stderr, "Sorry\n");
523 getbbcpy (struct bboard *bp)
525 register char **p, **q;
526 register struct bboard *b;
531 b = (struct bboard *) malloc ((unsigned) sizeof *b);
535 b->bb_name = getcpy (bp->bb_name);
536 b->bb_file = getcpy (bp->bb_file);
537 b->bb_archive = getcpy (bp->bb_archive);
538 b->bb_info = getcpy (bp->bb_info);
539 b->bb_map = getcpy (bp->bb_map);
540 b->bb_passwd = getcpy (bp->bb_passwd);
541 b->bb_flags = bp->bb_flags;
542 b->bb_count = bp->bb_count;
543 b->bb_maxima = bp->bb_maxima;
544 b->bb_date = getcpy (bp->bb_date);
545 b->bb_addr = getcpy (bp->bb_addr);
546 b->bb_request = getcpy (bp->bb_request);
547 b->bb_relay = getcpy (bp->bb_relay);
549 for (p = bp->bb_aka; *p; p++)
552 q = (char **) calloc ((unsigned) (p - bp->bb_aka + 1), sizeof *q);
555 for (p = bp->bb_aka; *p; *q++ = getcpy (*p++))
559 for (p = bp->bb_leader; *p; p++)
562 q = (char **) calloc ((unsigned) (p - bp->bb_leader + 1), sizeof *q);
565 for (p = bp->bb_leader; *p; *q++ = getcpy (*p++))
569 for (p = bp->bb_dist; *p; p++)
572 q = (char **) calloc ((unsigned) (p - bp->bb_dist + 1), sizeof *q);
575 for (p = bp->bb_dist; *p; *q++ = getcpy (*p++))
579 b->bb_next = bp->bb_next;
580 b->bb_link = bp->bb_link;
581 b->bb_chain = bp->bb_chain;
588 getbbdist (struct bboard *bb, int (*action)())
594 for (dp = bb->bb_dist; *dp; dp++)
595 if ((result = getbbitem (bb, *dp, action)))
604 return (BBErrors[0] ? BBErrors : NULL);
609 getbbitem (struct bboard *bb, char *item, int (*action)())
612 register char *cp, *dp, *hp, *np;
628 if ((cp = strrchr(bb->bb_file, '/')) == NULL || *++cp == 0) {
632 snprintf (prf, sizeof(prf), "%.*s", cp - bb->bb_file, bb->bb_file);
634 if ((dp = strchr(cp, '.')) == NULL)
635 dp = cp + strlen (cp);
636 snprintf (file, sizeof(file), "%s.%.*s%s", prf, dp - cp, cp, DSTFILE);
641 snprintf (file, sizeof(file), "%s/%s", BBDir, item);
646 if ((fp = fopen (hp, "r")) == NULL)
647 return bblose ("unable to read file %s", hp);
648 while (fgets (buffer, sizeof buffer, fp)) {
649 if ((np = strchr(buffer, '\n')))
651 if ((result = getbbitem (bb, buffer, action))) {
653 bblose ("error with file %s, item %s", hp, buffer);
661 if ((hp = strrchr(item, '@'))) {
663 strncpy (mbox, item, sizeof(mbox));
664 strncpy (host, hp, sizeof(host));
668 snprintf (mbox, sizeof(mbox), "%s%s", DISTADR, bb->bb_name);
669 strncpy (host, item, sizeof(host));
671 if ((result = (*action) (mbox, host)))
672 bblose ("action (%s, %s) returned 0%o", mbox, host, result);
679 bblose (char *fmt, ...)
684 if (BBErrors[0] == 0)
685 vsnprintf (BBErrors, sizeof(BBErrors), fmt, ap);
693 make_lower (char *s1, char *s2)
699 *s1++ = isupper (*s2) ? tolower (*s2) : *s2;
705 bbskip (char *p, char c)
710 while (*p && *p != c)
728 len = strlen (s) + 1;
729 if ((p = malloc (len)))