]> diplodocus.org Git - nmh/blob - docs/historical/mh-nov-1983/cmds/aliascheck.c
Removed --depth 1 from git clone invocation.
[nmh] / docs / historical / mh-nov-1983 / cmds / aliascheck.c
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <grp.h>
4 #include <sys/param.h>
5 #include <dir.h>
6
7 char *AliasFile = "/usr/local/lib/MailAliases";
8 char *Password = "/etc/passwd";
9 char *MailDir = "/usr/spool/mail";
10
11 char *termptr;
12
13 struct mailname {
14 struct mailname *m_next;
15 char *m_name;
16 int m_seen;
17 } users, bad;
18
19
20 char *parse(ptr, buf)
21 register char *ptr;
22 char *buf;
23 {
24 register char *cp;
25
26 cp = buf;
27 while(isspace(*ptr) || *ptr == ',' || *ptr == ':')
28 ptr++;
29 while(isalnum(*ptr) || *ptr == '/' || *ptr == '-' || *ptr == '.')
30 *cp++ = *ptr++;
31 if(cp == buf) {
32 switch(*ptr) {
33 case '<':
34 case '*':
35 case '=':
36 *cp++ = *ptr++;
37 }
38 }
39 *cp = 0;
40 if(cp == buf)
41 return 0;
42 termptr = ptr;
43 return buf;
44 }
45
46
47 char *mail = 0;
48 FILE *out;
49 int donecd = 0;
50
51 main(argc, argv)
52 int argc;
53 char *argv[];
54 {
55 register char *cp, *pp, **cpp;
56 register struct mailname *lp;
57 register struct group *gp;
58 struct direct *dir;
59 DIR *dirp;
60 char line[256], pbuf[64];
61 FILE *a;
62
63 if(argc == 3 && strcmp(argv[1], "-mail") == 0)
64 mail = argv[2];
65 if(!mail)
66 out = stdout;
67 if((a = fopen(Password, "r")) == NULL) {
68 om();
69 fprintf(out, "Can't open password file ");
70 perror(Password);
71 done(1);
72 }
73 while(fgets(line, sizeof line, a)) {
74 if(line[0] == '\n' || line[0] == ';')
75 continue;
76 cp = parse(line, pbuf);
77 add(cp, &users);
78 }
79 fclose(a);
80 if((a = fopen(AliasFile, "r")) == NULL) {
81 om();
82 fprintf(out, "Can't open alias file: %s\n", AliasFile);
83 donecd = 1;
84 } else {
85 while(fgets(line, sizeof line, a)) {
86 if(line[0] == '\n' || line[0] == ';')
87 continue;
88 cp = parse(line, pbuf);
89 if(check(cp, 0)) {
90 add(line, &bad);
91 donecd = 1;
92 }
93 }
94 fclose(a);
95 if(donecd < 1) {
96 if(out)
97 fprintf(out, "No Alias Inconsistencies.\n");
98 } else {
99 om();
100 fprintf(out, "%s :: %s Collisions:\n",
101 Password, AliasFile);
102 fprintf(out, "Colliding alias lines:\n");
103 for(lp = bad.m_next; lp; lp = lp->m_next)
104 fprintf(out, "%s", lp->m_name);
105 donecd = 1;
106 }
107 }
108 while(gp = getgrent()) {
109 for(cpp = gp->gr_mem; *cpp; cpp++)
110 if(!check(*cpp, 1)) {
111 om();
112 fprintf(out, "Group: %s--User: %s not in /etc/passwd\n",
113 gp->gr_name, *cpp);
114 donecd = 2;
115 }
116 }
117 if(donecd < 2 && out)
118 fprintf(out, "No extraneous group entries.\n");
119 #ifdef RAND
120 for(lp = users.m_next; lp; lp = lp->m_next)
121 if(lp->m_seen == 0) {
122 om();
123 fprintf(out, "User: %s not in a group.\n", lp->m_name);
124 donecd = 3;
125 }
126 if(donecd < 3 && out)
127 fprintf(out, "No Users not in any group.\n");
128 #endif
129 if ((dirp = opendir(MailDir)) == 0) {
130 om();
131 fprintf(out, "Can't open mail directory: %s\n", MailDir);
132 donecd = 4;
133 } else {
134 (void) readdir(dirp); /* skip . */
135 (void) readdir(dirp); /* skip .. */
136 while (dir = readdir(dirp)) {
137 if (!check(dir->d_name, 0)) {
138 om();
139 fprintf(out, "Mail drop: %s--Nonexistent user.\n",
140 dir->d_name);
141 donecd = 4;
142 }
143 }
144 closedir(dirp);
145 }
146 if(donecd < 4 && out)
147 fprintf(out, "No Extra mail drops.\n");
148
149 done(donecd);
150 }
151
152
153 add(name, list)
154 char *name;
155 struct mailname *list;
156 {
157 register struct mailname *mp;
158 char *getcpy();
159
160 for(mp = list; mp->m_next; mp = mp->m_next)
161 ;
162 mp->m_next = (struct mailname *) malloc(sizeof *mp->m_next);
163 mp = mp->m_next;
164 mp->m_next = 0;
165 mp->m_name = getcpy(name);
166 }
167
168 check(name, mark)
169 char *name;
170 int mark;
171 {
172 register struct mailname *mp;
173
174 for(mp = users.m_next; mp; mp = mp->m_next)
175 if(uleq(name, mp->m_name)) {
176 if(mark)
177 mp->m_seen = 1;
178 return 1;
179 }
180 return 0;
181 }
182
183
184 om()
185 {
186 int pipes[2], child;
187
188 if(out)
189 return;
190 if(mail) {
191 pipe(pipes);
192 out = fdopen(pipes[1], "w");
193 if((child = fork()) == -1) {
194 fprintf(stderr, "Aliascheck: no forks!\n");
195 done(1);
196 }
197 if(child == 0) {
198 close(pipes[1]);
199 close(0);
200 dup(pipes[0]);
201 close(pipes[0]);
202 execl("/bin/mail", "mail", mail, 0);
203 execl("/usr/bin/mail", "mail", mail, 0);
204 perror("mail");
205 done(1);
206 }
207 fprintf(out, "Aliascheck: ");
208 }
209 }