]> diplodocus.org Git - nmh/blob - docs/historical/mh-6.8.5/uip/RCS/aliasbr.c,v
sbr/mts.c: Delete mmdlm2; use same-valued mmdlm1 instead.
[nmh] / docs / historical / mh-6.8.5 / uip / RCS / aliasbr.c,v
1 head 1.14;
2 access;
3 symbols;
4 locks; strict;
5 comment @ * @;
6
7
8 1.14
9 date 93.02.26.21.58.38; author jromine; state Exp;
10 branches;
11 next 1.13;
12
13 1.13
14 date 92.12.15.00.20.22; author jromine; state Exp;
15 branches;
16 next 1.12;
17
18 1.12
19 date 92.10.26.22.56.54; author jromine; state Exp;
20 branches;
21 next 1.11;
22
23 1.11
24 date 92.10.20.22.43.10; author jromine; state Exp;
25 branches;
26 next 1.10;
27
28 1.10
29 date 92.02.05.20.38.34; author jromine; state Exp;
30 branches;
31 next 1.9;
32
33 1.9
34 date 92.02.03.17.57.22; author jromine; state Exp;
35 branches;
36 next 1.8;
37
38 1.8
39 date 92.01.31.22.06.52; author jromine; state Exp;
40 branches;
41 next 1.7;
42
43 1.7
44 date 90.04.05.14.59.03; author sources; state Exp;
45 branches;
46 next 1.6;
47
48 1.6
49 date 90.04.02.14.42.27; author sources; state Exp;
50 branches;
51 next 1.5;
52
53 1.5
54 date 90.03.22.19.31.36; author sources; state Exp;
55 branches;
56 next 1.4;
57
58 1.4
59 date 90.02.06.13.14.52; author sources; state Exp;
60 branches;
61 next 1.3;
62
63 1.3
64 date 90.01.30.11.12.03; author sources; state Exp;
65 branches;
66 next 1.2;
67
68 1.2
69 date 90.01.29.15.19.04; author sources; state Exp;
70 branches;
71 next 1.1;
72
73 1.1
74 date 90.01.29.15.18.42; author sources; state Exp;
75 branches;
76 next ;
77
78
79 desc
80 @@
81
82
83 1.14
84 log
85 @BSD44
86 @
87 text
88 @/* aliasbr.c - new aliasing mechanism */
89 #ifndef lint
90 static char ident[] = "@@(#)$Id: aliasbr.c,v 1.13 1992/12/15 00:20:22 jromine Exp jromine $";
91 #endif /* lint */
92
93 #include "../h/mh.h"
94 #include "../h/aliasbr.h"
95 #ifdef BSD44
96 #include <sys/types.h>
97 #endif
98 #include <ctype.h>
99 #include <grp.h>
100 #include <pwd.h>
101 #include <stdio.h>
102
103 static int akvis;
104 static char *akerrst;
105
106 struct aka *akahead = NULL;
107 struct aka *akatail = NULL;
108
109 struct home *homehead = NULL;
110 struct home *hometail = NULL;
111
112 static char *scanp (), *getp (), *seekp (), *akval (), *getalias ();
113 static struct aka *akalloc ();
114 static struct home *hmalloc ();
115
116 static add_aka();
117 static int aleq(), addfile(), addgroup(), addmember(), addall();
118
119 #ifndef __STDC__
120 #ifdef SYS5
121 struct passwd *getpwent ();
122 struct group *getgrnam (), *getgrgid ();
123 #endif
124 #endif
125
126 /* \f */
127
128 char *akvalue (s)
129 register char *s;
130 {
131 register char *v;
132
133 if (akahead == NULL)
134 (void) alias (AliasFile);
135
136 akvis = -1;
137 v = akval (akahead, s);
138 if (akvis == -1)
139 akvis = 0;
140 return v;
141 }
142
143
144 int akvisible () {
145 return akvis;
146 }
147
148 /* \f */
149
150 char *akresult (ak)
151 register struct aka *ak;
152 {
153 register char *cp = NULL,
154 *dp,
155 *pp;
156 register struct adr *ad;
157
158 for (ad = ak -> ak_addr; ad; ad = ad -> ad_next) {
159 pp = ad -> ad_local ? akval (ak -> ak_next, ad -> ad_text)
160 : getcpy (ad -> ad_text);
161
162 if (dp = cp) {
163 cp = concat (cp, ",", pp, NULLCP);
164 free (dp);
165 free (pp);
166 }
167 else
168 cp = pp;
169 }
170
171 if (akvis == -1)
172 akvis = ak -> ak_visible;
173 return cp;
174 }
175
176
177 static char *akval (ak, s)
178 register struct aka *ak;
179 register char *s;
180 {
181 if (!s)
182 return s; /* XXX */
183
184 for (; ak; ak = ak -> ak_next)
185 if (aleq (s, ak -> ak_name))
186 return akresult (ak);
187
188 return getcpy (s);
189 }
190
191
192 static int aleq (string, aliasent)
193 register char *string,
194 *aliasent;
195 {
196 register char c;
197
198 while (c = *string++)
199 if (*aliasent == '*')
200 return 1;
201 else
202 if ((c | 040) != (*aliasent | 040))
203 return 0;
204 else
205 aliasent++;
206
207 return (*aliasent == 0 || *aliasent == '*');
208 }
209
210 /* \f */
211
212 int alias (file)
213 register char *file;
214 {
215 int i;
216 register char *bp,
217 *cp,
218 *pp;
219 char lc,
220 *ap;
221 register struct aka *ak = NULL;
222 register FILE *fp;
223
224 if (*file != '/'
225 && (strncmp (file, "./", 2) && strncmp (file, "../", 3)))
226 file = libpath (file);
227 if ((fp = fopen (file, "r")) == NULL) {
228 akerrst = file;
229 return AK_NOFILE;
230 }
231
232 while (vfgets (fp, &ap) == OK) {
233 bp = ap;
234 switch (*(pp = scanp (bp))) {
235 case '<': /* recurse a level */
236 if (!*(cp = getp (pp + 1))) {
237 akerrst = "'<' without alias-file";
238 (void) fclose (fp);
239 return AK_ERROR;
240 }
241 if ((i = alias (cp)) != AK_OK) {
242 (void) fclose (fp);
243 return i;
244 }
245
246 case ':': /* comment */
247 case ';':
248 case '#':
249 case 0:
250 continue;
251 }
252
253 akerrst = bp;
254 if (!*(cp = seekp (pp, &lc, &ap))) {
255 (void) fclose (fp);
256 return AK_ERROR;
257 }
258 if (!(ak = akalloc (cp))) {
259 (void) fclose (fp);
260 return AK_LIMIT;
261 }
262 switch (lc) {
263 case ':':
264 ak -> ak_visible = 0;
265 break;
266
267 case ';':
268 ak -> ak_visible = 1;
269 break;
270
271 default:
272 (void) fclose (fp);
273 return AK_ERROR;
274 }
275
276 switch (*(pp = scanp (ap))) {
277 case 0: /* EOL */
278 (void) fclose (fp);
279 return AK_ERROR;
280
281 case '<': /* read values from file */
282 if (!*(cp = getp (pp + 1))) {
283 (void) fclose (fp);
284 return AK_ERROR;
285 }
286 if (!addfile (ak, cp)) {
287 (void) fclose (fp);
288 return AK_NOFILE;
289 }
290 break;
291
292 case '=': /* UNIX group */
293 if (!*(cp = getp (pp + 1))) {
294 (void) fclose (fp);
295 return AK_ERROR;
296 }
297 if (!addgroup (ak, cp)) {
298 (void) fclose (fp);
299 return AK_NOGROUP;
300 }
301 break;
302
303 case '+': /* UNIX group members */
304 if (!*(cp = getp (pp + 1))) {
305 (void) fclose (fp);
306 return AK_ERROR;
307 }
308 if (!addmember (ak, cp)) {
309 (void) fclose (fp);
310 return AK_NOGROUP;
311 }
312 break;
313
314 case '*': /* Everyone */
315 (void) addall (ak);
316 break;
317
318 default: /* list */
319 while (cp = getalias (pp))
320 add_aka (ak, cp);
321 break;
322 }
323 }
324
325 (void) fclose (fp);
326 return AK_OK;
327 }
328
329 /* \f */
330
331 char *akerror (i)
332 int i;
333 {
334 static char buffer[BUFSIZ];
335
336 switch (i) {
337 case AK_NOFILE:
338 (void) sprintf (buffer, "unable to read '%s'", akerrst);
339 break;
340
341 case AK_ERROR:
342 (void) sprintf (buffer, "error in line '%s'", akerrst);
343 break;
344
345 case AK_LIMIT:
346 (void) sprintf (buffer, "out of memory while on '%s'", akerrst);
347 break;
348
349 case AK_NOGROUP:
350 (void) sprintf (buffer, "no such group as '%s'", akerrst);
351 break;
352
353 default:
354 (void) sprintf (buffer, "unknown error (%d)", i);
355 break;
356 }
357
358 return buffer;
359 }
360
361 /* \f */
362
363 static char *scanp (p)
364 register char *p;
365 {
366 while (isspace (*p))
367 p++;
368 return p;
369 }
370
371
372 static char *getp (p)
373 register char *p;
374 {
375 register char *cp = scanp (p);
376
377 p = cp;
378 while (!isspace (*cp) && *cp)
379 cp++;
380 *cp = 0;
381
382 return p;
383 }
384
385
386 static char *seekp (p, c, a)
387 register char *p,
388 *c,
389 **a;
390 {
391 register char *cp = scanp (p);
392
393 p = cp;
394 while (!isspace (*cp) && *cp && *cp != ':' && *cp != ';')
395 cp++;
396 *c = *cp;
397 *cp++ = 0;
398 *a = cp;
399
400 return p;
401 }
402
403 /* \f */
404
405 static int addfile (ak, file)
406 register struct aka *ak;
407 register char *file;
408 {
409 register char *cp;
410 char buffer[BUFSIZ];
411 register FILE *fp;
412
413 if ((fp = fopen (libpath (file), "r")) == NULL) {
414 akerrst = file;
415 return 0;
416 }
417
418 while (fgets (buffer, sizeof buffer, fp) != NULL)
419 while (cp = getalias (buffer))
420 add_aka (ak, cp);
421
422 (void) fclose (fp);
423 return 1;
424 }
425
426 /* \f */
427
428 static int addgroup (ak, grp)
429 register struct aka *ak;
430 register char *grp;
431 {
432 register char *gp;
433 register struct group *gr = getgrnam (grp);
434 register struct home *hm = NULL;
435
436 if (!gr)
437 gr = getgrgid (atoi (grp));
438 if (!gr) {
439 akerrst = grp;
440 return 0;
441 }
442
443 #ifndef DBMPWD
444 if (homehead == NULL)
445 init_pw ();
446 #endif /* DBMPWD */
447
448 while (gp = *gr -> gr_mem++)
449 #ifdef DBMPWD
450 {
451 struct passwd *pw;
452 #endif /* DBMPWD */
453 for (hm = homehead; hm; hm = hm -> h_next)
454 if (!strcmp (hm -> h_name, gp)) {
455 add_aka (ak, hm -> h_name);
456 break;
457 }
458 #ifdef DBMPWD
459 if (pw = getpwnam(gp))
460 {
461 hmalloc(pw);
462 add_aka (ak, gp);
463 }
464 }
465 #endif /* DBMPWD */
466
467 return 1;
468 }
469
470 /* \f */
471
472 static int addmember (ak, grp)
473 register struct aka *ak;
474 register char *grp;
475 {
476 int gid;
477 register struct group *gr = getgrnam (grp);
478 register struct home *hm = NULL;
479
480 if (gr)
481 gid = gr -> gr_gid;
482 else {
483 gid = atoi (grp);
484 gr = getgrgid (gid);
485 }
486 if (!gr) {
487 akerrst = grp;
488 return 0;
489 }
490
491 #ifndef DBMPWD
492 if (homehead == NULL)
493 #endif /* DBMPWD */
494 init_pw ();
495
496 for (hm = homehead; hm; hm = hm -> h_next)
497 if (hm -> h_gid == gid)
498 add_aka (ak, hm -> h_name);
499
500 return 1;
501 }
502
503 /* \f */
504
505 static int addall (ak)
506 register struct aka *ak;
507 {
508 int noshell = NoShell == NULLCP || *NoShell == 0;
509 register struct home *hm;
510
511 #ifndef DBMPWD
512 if (homehead == NULL)
513 #endif /* DBMPWD */
514 init_pw ();
515 if (Everyone < 0)
516 Everyone = EVERYONE;
517
518 for (hm = homehead; hm; hm = hm -> h_next)
519 if (hm -> h_uid > Everyone
520 && (noshell || strcmp (hm -> h_shell, NoShell)))
521 add_aka (ak, hm -> h_name);
522
523 return homehead != NULL;
524 }
525
526 /* \f */
527
528 static char *getalias (addrs)
529 register char *addrs;
530 {
531 register char *pp,
532 *qp;
533 static char *cp = NULL;
534
535 if (cp == NULL)
536 cp = addrs;
537 else
538 if (*cp == 0)
539 return (cp = NULL);
540
541 for (pp = cp; isspace (*pp); pp++)
542 continue;
543 if (*pp == 0)
544 return (cp = NULL);
545 for (qp = pp; *qp != 0 && *qp != ','; qp++)
546 continue;
547 if (*qp == ',')
548 *qp++ = 0;
549 for (cp = qp, qp--; qp > pp; qp--)
550 if (*qp != 0)
551 if (isspace (*qp))
552 *qp = 0;
553 else
554 break;
555
556 return pp;
557 }
558
559 /* \f */
560
561 static add_aka (ak, pp)
562 register struct aka *ak;
563 register char *pp;
564 {
565 register struct adr *ad,
566 *ld;
567
568 for (ad = ak -> ak_addr, ld = NULL; ad; ld = ad, ad = ad -> ad_next)
569 if (!strcmp (pp, ad -> ad_text))
570 return;
571
572 ad = (struct adr *) malloc (sizeof *ad);
573 if (ad == NULL)
574 return;
575 ad -> ad_text = getcpy (pp);
576 ad -> ad_local = index (pp, '@@') == NULL && index (pp, '!') == NULL;
577 ad -> ad_next = NULL;
578 if (ak -> ak_addr)
579 ld -> ad_next = ad;
580 else
581 ak -> ak_addr = ad;
582 }
583
584
585 init_pw () {
586 register struct passwd *pw;
587 #ifdef DBMPWD
588 static int init;
589
590 if (!init)
591 {
592 /* if the list has yet to be initialized */
593 /* zap the list, and rebuild from scratch */
594 homehead=NULL;
595 hometail=NULL;
596 init++;
597 #endif /* DBMPWD */
598
599 (void) setpwent ();
600
601 while (pw = getpwent ())
602 if (!hmalloc (pw))
603 break;
604
605 (void) endpwent ();
606 #ifdef DBMPWD
607 }
608 #endif /* DBMPWD */
609 }
610
611 /* \f */
612
613 static struct aka *akalloc (id)
614 register char *id;
615 {
616 register struct aka *p = (struct aka *) malloc (sizeof *p);
617
618 if (!p)
619 return NULL;
620
621 p -> ak_name = getcpy (id);
622 p -> ak_visible = 0;
623 p -> ak_addr = NULL;
624 p -> ak_next = NULL;
625 if (akatail != NULL)
626 akatail -> ak_next = p;
627 if (akahead == NULL)
628 akahead = p;
629 akatail = p;
630
631 return p;
632 }
633
634
635 static struct home *hmalloc (pw)
636 struct passwd *pw;
637 {
638 register struct home *p = (struct home *) malloc (sizeof *p);
639
640 if (!p)
641 return NULL;
642
643 p -> h_name = getcpy (pw -> pw_name);
644 p -> h_uid = pw -> pw_uid;
645 p -> h_gid = pw -> pw_gid;
646 p -> h_home = getcpy (pw -> pw_dir);
647 p -> h_shell = getcpy (pw -> pw_shell);
648 #ifdef BSD42
649 p -> h_ngrps = 0;
650 #endif /* BSD42 */
651 p -> h_next = NULL;
652 if (hometail != NULL)
653 hometail -> h_next = p;
654 if (homehead == NULL)
655 homehead = p;
656 hometail = p;
657
658 return p;
659 }
660
661 /* \f */
662
663 #ifndef MMDFMTS
664 struct home *seek_home (name)
665 register char *name;
666 {
667 register struct home *hp;
668 #ifdef DBMPWD
669 struct passwd *pw;
670 char lname[32];
671 char *c,*c1;
672 #else /* DBMPWD */
673
674 if (homehead == NULL)
675 init_pw ();
676 #endif /* DBMPWD */
677
678 for (hp = homehead; hp; hp = hp -> h_next)
679 if (uleq (name, hp -> h_name))
680 return hp;
681
682 #ifdef DBMPWD /* The only place where there might be problems */
683 /* This assumes that ALL usernames are kept in lowercase */
684 for (c = name,c1 = lname; *c; c++, c1++)
685 if (isalpha(*c) && isupper(*c))
686 *c1 = tolower(*c);
687 else
688 *c1 = *c;
689 *c1 = NULL;
690 if (pw = getpwnam(lname))
691 return(hmalloc(pw));
692 #endif /* DBMPWD */
693
694 return NULL;
695 }
696 #endif /* MMDFMTS */
697 @
698
699
700 1.13
701 log
702 @endif sugar
703 @
704 text
705 @d3 1
706 a3 1
707 static char ident[] = "@@(#)$Id: aliasbr.c,v 1.12 1992/10/26 22:56:54 jromine Exp jromine $";
708 d8 3
709 @
710
711
712 1.12
713 log
714 @use isalpha with isupper
715 @
716 text
717 @d3 2
718 a4 2
719 static char ident[] = "@@(#)$Id: aliasbr.c,v 1.11 1992/10/20 22:43:10 jromine Exp jromine $";
720 #endif lint
721 d560 1
722 a560 1
723 #endif BSD42
724 d606 1
725 a606 1
726 #endif MMDFMTS
727 @
728
729
730 1.11
731 log
732 @DBM->DBMPWD (conflict with ndbm.h)
733 @
734 text
735 @d3 1
736 a3 1
737 static char ident[] = "@@(#)$Id: aliasbr.c,v 1.10 1992/02/05 20:38:34 jromine Exp jromine $";
738 d595 1
739 a595 1
740 if (isupper(*c))
741 @
742
743
744 1.10
745 log
746 @remove #ifdef around EVERYONE
747 @
748 text
749 @d3 1
750 a3 1
751 static char ident[] = "@@(#)$Id: aliasbr.c,v 1.9 1992/02/03 17:57:22 jromine Exp jromine $";
752 d353 1
753 a353 1
754 #ifndef DBM
755 d356 1
756 a356 1
757 #endif /* DBM */
758 d359 1
759 a359 1
760 #ifdef DBM
761 d362 1
762 a362 1
763 #endif /* DBM */
764 d368 1
765 a368 1
766 #ifdef DBM
767 d375 1
768 a375 1
769 #endif /* DBM */
770 d401 1
771 a401 1
772 #ifndef DBM
773 d403 1
774 a403 1
775 #endif /* DBM */
776 d421 1
777 a421 1
778 #ifndef DBM
779 d423 1
780 a423 1
781 #endif /* DBM */
782 d497 1
783 a497 1
784 #ifdef DBM
785 d507 1
786 a507 1
787 #endif /* DBM */
788 d516 1
789 a516 1
790 #ifdef DBM
791 d518 1
792 a518 1
793 #endif /* DBM */
794 d578 1
795 a578 1
796 #ifdef DBM
797 d582 1
798 a582 1
799 #else /* DBM */
800 d586 1
801 a586 1
802 #endif /* DBM */
803 d592 1
804 a592 1
805 #ifdef DBM /* The only place where there might be problems */
806 d602 1
807 a602 1
808 #endif /* DBM */
809 @
810
811
812 1.9
813 log
814 @STDC/SYS5/getpw
815 @
816 text
817 @d3 1
818 a3 1
819 static char ident[] = "@@(#)$Id: aliasbr.c,v 1.8 1992/01/31 22:06:52 jromine Exp jromine $";
820 a424 1
821 #ifndef DBM
822 a425 1
823 #endif /* DBM */
824 @
825
826
827 1.8
828 log
829 @kerberos
830 @
831 text
832 @d3 1
833 a3 1
834 static char ident[] = "@@(#)$Id: aliasbr.c,v 1.7 1990/04/05 14:59:03 sources Exp jromine $";
835 d29 2
836 d33 2
837 @
838
839
840 1.7
841 log
842 @add ID
843 @
844 text
845 @d3 1
846 a3 1
847 static char ident[] = "@@(#)$Id:$";
848 d113 1
849 a113 1
850 return (*aliasent == NULL || *aliasent == '*');
851 d155 1
852 a155 1
853 case NULL:
854 d183 1
855 a183 1
856 case NULL: /* EOL */
857 d286 1
858 a286 1
859 *cp = NULL;
860 d303 1
861 a303 1
862 *cp++ = NULL;
863 d321 1
864 a321 1
865 return NULL;
866 d346 1
867 a346 1
868 return NULL;
869 d394 1
870 a394 1
871 return NULL;
872 d414 1
873 a414 1
874 int noshell = NoShell == NULLCP || *NoShell == NULL;
875 d446 1
876 a446 1
877 if (*cp == NULL)
878 d451 1
879 a451 1
880 if (*pp == NULL)
881 d453 1
882 a453 1
883 for (qp = pp; *qp != NULL && *qp != ','; qp++)
884 d456 1
885 a456 1
886 *qp++ = NULL;
887 d458 1
888 a458 1
889 if (*qp != NULL)
890 d460 1
891 a460 1
892 *qp = NULL;
893 @
894
895
896 1.6
897 log
898 @RAND fixes
899 @
900 text
901 @d2 3
902 @
903
904
905 1.5
906 log
907 @add "#" as a comment character
908 @
909 text
910 @a9 1
911
912 d346 1
913 d349 1
914 d352 4
915 d361 8
916 d394 1
917 d396 1
918 d414 1
919 d416 1
920 d418 1
921 d420 1
922 d492 11
923 d511 3
924 d573 5
925 d581 1
926 d586 12
927 @
928
929
930 1.4
931 log
932 @ANSI Compilance
933 @
934 text
935 @d152 1
936 @
937
938
939 1.3
940 log
941 @mismatched parens
942 @
943 text
944 @d20 3
945 a22 3
946 char *scanp (), *getp (), *seekp (), *akval (), *getalias ();
947 struct aka *akalloc ();
948 struct home *hmalloc ();
949 d24 2
950 @
951
952
953 1.2
954 log
955 @fix for NULL pointer
956 @
957 text
958 @d143 1
959 a143 1
960 if ((i = alias (cp) != AK_OK)) {
961 @
962
963
964 1.1
965 log
966 @Initial revision
967 @
968 text
969 @d83 3
970 @