]>
diplodocus.org Git - nmh/blob - zotnet/bboards/getbbent.c
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
);
403 if (p
== NULL
&& r
&& *r
== '@' && *bb
->bb_request
)
404 snprintf (BBRequest
, sizeof(BBRequest
), "%s%s", bb
->bb_request
, r
);
407 bb
->bb_request
= BBRequest
;
409 if (*bb
->bb_request
== 0)
410 bb
->bb_request
= *bb
->bb_addr
? bb
->bb_addr
413 if (*bb
->bb_addr
== '@') {
414 snprintf (BBAddr
, sizeof(BBAddr
), "%s%s", bb
->bb_name
, bb
->bb_addr
);
415 bb
->bb_addr
= BBAddr
;
418 if (*bb
->bb_addr
== 0)
419 bb
->bb_addr
= bb
->bb_name
;
421 if (*bb
->bb_file
== 0)
423 if (*bb
->bb_file
!= '/') {
424 snprintf (BBFile
, sizeof(BBFile
), "%s/%s", BBDir
, bb
->bb_file
);
425 bb
->bb_file
= BBFile
;
428 if ((cp
= strrchr(bb
->bb_file
, '/')) == NULL
|| *++cp
== 0) {
432 snprintf (prf
, sizeof(prf
), "%.*s", cp
- bb
->bb_file
, bb
->bb_file
);
434 if ((dp
= strchr(cp
, '.')) == NULL
)
435 dp
= cp
+ strlen (cp
);
437 snprintf (BBArchive
, sizeof(BBArchive
), "%s%s/%s", prf
, ARCHIVE
, cp
);
438 bb
->bb_archive
= BBArchive
;
439 snprintf (BBInfo
, sizeof(BBInfo
), "%s.%.*s%s", prf
, dp
- cp
, cp
, CNTFILE
);
440 bb
->bb_info
= BBInfo
;
441 snprintf (BBMap
, sizeof(BBMap
), "%s.%.*s%s", prf
, dp
- cp
, cp
, MAPFILE
);
444 if ((info
= fopen (bb
->bb_info
, "r")) == NULL
)
447 if (fgets (line
, sizeof line
, info
) && (i
= atoi (line
)) > 0)
448 bb
->bb_maxima
= (unsigned) i
;
449 if (!feof (info
) && fgets (line
, sizeof line
, info
)) {
450 strncpy (BBDate
, line
, sizeof(BBData
));
451 if ((cp
= strchr(BBDate
, NEWLINE
)))
453 bb
->bb_date
= BBDate
;
461 ldrbb (struct bboard
*b
)
463 register char *p
, **q
, **r
;
464 static uid_t uid
= 0;
465 static gid_t gid
= 0;
466 static char username
[10] = "";
467 register struct passwd
*pw
;
468 register struct group
*gr
;
472 if (BBuid
== -1 && !setbbaux (BBOARDS
, BBDB
))
475 if (username
[0] == 0) {
476 if ((pw
= getpwuid (uid
= getuid ())) == NULL
)
479 strncpy (username
, pw
->pw_name
, sizeof(username
));
488 if ((gr
= getgrnam (++p
)) == NULL
)
490 if (gid
== gr
->gr_gid
)
494 if (strcmp (username
, p
) == 0)
498 if (strcmp (username
, p
) == 0)
506 ldrchk (struct bboard
*b
)
511 if (*b
->bb_passwd
== 0)
514 if (strcmp (b
->bb_passwd
,
515 crypt (getpass ("Password: "), b
->bb_passwd
)) == 0)
518 fprintf (stderr
, "Sorry\n");
524 getbbcpy (struct bboard
*bp
)
526 register char **p
, **q
;
527 register struct bboard
*b
;
532 b
= (struct bboard
*) malloc ((unsigned) sizeof *b
);
536 b
->bb_name
= getcpy (bp
->bb_name
);
537 b
->bb_file
= getcpy (bp
->bb_file
);
538 b
->bb_archive
= getcpy (bp
->bb_archive
);
539 b
->bb_info
= getcpy (bp
->bb_info
);
540 b
->bb_map
= getcpy (bp
->bb_map
);
541 b
->bb_passwd
= getcpy (bp
->bb_passwd
);
542 b
->bb_flags
= bp
->bb_flags
;
543 b
->bb_count
= bp
->bb_count
;
544 b
->bb_maxima
= bp
->bb_maxima
;
545 b
->bb_date
= getcpy (bp
->bb_date
);
546 b
->bb_addr
= getcpy (bp
->bb_addr
);
547 b
->bb_request
= getcpy (bp
->bb_request
);
548 b
->bb_relay
= getcpy (bp
->bb_relay
);
550 for (p
= bp
->bb_aka
; *p
; p
++)
553 q
= (char **) calloc ((unsigned) (p
- bp
->bb_aka
+ 1), sizeof *q
);
556 for (p
= bp
->bb_aka
; *p
; *q
++ = getcpy (*p
++))
560 for (p
= bp
->bb_leader
; *p
; p
++)
563 q
= (char **) calloc ((unsigned) (p
- bp
->bb_leader
+ 1), sizeof *q
);
566 for (p
= bp
->bb_leader
; *p
; *q
++ = getcpy (*p
++))
570 for (p
= bp
->bb_dist
; *p
; p
++)
573 q
= (char **) calloc ((unsigned) (p
- bp
->bb_dist
+ 1), sizeof *q
);
576 for (p
= bp
->bb_dist
; *p
; *q
++ = getcpy (*p
++))
580 b
->bb_next
= bp
->bb_next
;
581 b
->bb_link
= bp
->bb_link
;
582 b
->bb_chain
= bp
->bb_chain
;
589 getbbdist (struct bboard
*bb
, int (*action
)())
595 for (dp
= bb
->bb_dist
; *dp
; dp
++)
596 if ((result
= getbbitem (bb
, *dp
, action
)))
605 return (BBErrors
[0] ? BBErrors
: NULL
);
610 getbbitem (struct bboard
*bb
, char *item
, int (*action
)())
613 register char *cp
, *dp
, *hp
, *np
;
629 if ((cp
= strrchr(bb
->bb_file
, '/')) == NULL
|| *++cp
== 0) {
633 snprintf (prf
, sizeof(prf
), "%.*s", cp
- bb
->bb_file
, bb
->bb_file
);
635 if ((dp
= strchr(cp
, '.')) == NULL
)
636 dp
= cp
+ strlen (cp
);
637 snprintf (file
, sizeof(file
), "%s.%.*s%s", prf
, dp
- cp
, cp
, DSTFILE
);
642 snprintf (file
, sizeof(file
), "%s/%s", BBDir
, item
);
647 if ((fp
= fopen (hp
, "r")) == NULL
)
648 return bblose ("unable to read file %s", hp
);
649 while (fgets (buffer
, sizeof buffer
, fp
)) {
650 if ((np
= strchr(buffer
, '\n')))
652 if ((result
= getbbitem (bb
, buffer
, action
))) {
654 bblose ("error with file %s, item %s", hp
, buffer
);
662 if ((hp
= strrchr(item
, '@'))) {
664 strncpy (mbox
, item
, sizeof(mbox
));
665 strncpy (host
, hp
, sizeof(host
));
669 snprintf (mbox
, sizeof(mbox
), "%s%s", DISTADR
, bb
->bb_name
);
670 strncpy (host
, item
, sizeof(host
));
672 if ((result
= (*action
) (mbox
, host
)))
673 bblose ("action (%s, %s) returned 0%o", mbox
, host
, result
);
680 bblose (char *fmt
, ...)
685 if (BBErrors
[0] == 0)
686 vsnprintf (BBErrors
, sizeof(BBErrors
), fmt
, ap
);
694 make_lower (char *s1
, char *s2
)
700 *s1
++ = isupper (*s2
) ? tolower (*s2
) : *s2
;
706 bbskip (char *p
, char c
)
711 while (*p
&& *p
!= c
)
729 len
= strlen (s
) + 1;
730 if ((p
= malloc (len
)))