dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / ypcmd / mkalias.c
blobf9969a6927cd03663edc0889ebf4005f8a8b4535
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
29 * mkmap - program to convert the mail.aliases map into an
30 * inverse map of <user@host> back to <preferred-alias>
33 #pragma ident "%Z%%M% %I% %E% SMI"
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <fcntl.h>
39 #include <ndbm.h>
40 #include <stdio.h>
41 #include <ctype.h>
42 #include <netdb.h>
43 #include <sys/systeminfo.h>
45 #include "ypdefs.h"
46 USE_YP_PREFIX
47 USE_YP_MASTER_NAME
48 USE_YP_LAST_MODIFIED
50 #define MKAL_INCLUDE ":include:"
52 void CopyName(char *dst, char *src, int len);
53 int HostCheck(char *h, char *a);
54 void DoName(char *cp);
55 void UpperCase(char *cp);
56 void AddYPEntries(void);
58 int Verbose = 0; /* to get the gory details */
59 int UucpOK = 0; /* pass all UUCP names right through */
60 int DomainOK = 0; /* pass all Domain names (with dots) */
61 int ErrorCheck = 0; /* check carefully for errors */
62 int NoOutput = 0; /* no output, just do the check */
63 int Simple = 0; /* Do not do the user name preference step */
64 int NameMode = 0; /* Try to capitalize as names */
66 DBM *Indbm = NULL, *Scandbm = NULL, *Outdbm = NULL;
68 int
69 IsMailingList(char *s)
72 * returns true if the given string is a mailing list
74 char *p;
76 if (strchr(s, ','))
77 return (1);
78 if (strchr(s, '|'))
79 return (1);
80 p = strchr(s, ':');
81 if (p && strncmp(p, MKAL_INCLUDE, sizeof (MKAL_INCLUDE)))
82 return (1);
83 return (0);
86 int
87 IsQualified(char *s, char *p, char *h)
90 * returns true if the given string is qualified with a host name
92 register char *middle;
94 middle = strchr(s, '@');
95 if (middle) {
96 for (middle = s; *middle != '@'; *p++ = *middle++)
97 continue;
98 *p = '\0';
99 CopyName(h, middle+1, strlen(middle + 1));
100 return (1);
102 middle = strrchr(s, '!');
103 if (middle) {
104 strcpy(p, middle+1);
105 *middle = '\0';
106 CopyName(h, s, strlen(s));
107 *middle = '!';
108 return (1);
110 return (0);
114 IsMaint(char *s)
117 * returns true if the given string is one of the maintenence
118 * strings used in sendmail or NIS.
120 if (*s == '@')
121 return (1);
122 if (strncmp(s, yp_prefix, yp_prefix_sz) == 0)
123 return (1);
124 return (0);
127 void
128 CopyName(char *dst, char *src, int len)
131 * copy a string, but ignore white space
133 while (*src && len--) {
134 if (isspace(*src))
135 src++;
136 else
137 *dst++ = *src++;
139 *dst = '\0';
143 Compare(char *s1, char *s2)
146 * compare strings, but ignore white space
148 while (*s1 != '\0' && isspace(*s1))
149 s1++;
150 while (*s2 != '\0' && isspace(*s2))
151 s2++;
152 return (strcmp(s1, s2));
155 void
156 ProcessMap(void)
158 datum key, value, part, partvalue;
159 char address[PBLKSIZ]; /* qualified version */
160 char user[PBLKSIZ]; /* unqualified version */
161 char userpart[PBLKSIZ]; /* unqualified part of qualified addr. */
162 char hostpart[PBLKSIZ]; /* rest of qualified addr. */
164 for (key = dbm_firstkey(Scandbm); key.dptr != NULL;
165 key = dbm_nextkey(Scandbm)) {
166 value = dbm_fetch(Indbm, key);
167 CopyName(address, value.dptr, value.dsize);
168 CopyName(user, key.dptr, key.dsize);
169 if (address == NULL) continue;
170 if (IsMailingList(address)) continue;
171 if (!IsQualified(address, userpart, hostpart)) continue;
172 if (IsMaint(user)) continue;
173 if (ErrorCheck && HostCheck(hostpart, address)) {
174 printf("Invalid host %s in %s:%s\n",
175 hostpart, user, address);
176 continue;
178 part.dptr = userpart;
179 part.dsize = strlen(userpart) + 1;
180 if (Simple)
181 partvalue.dptr = NULL;
182 else
183 partvalue = dbm_fetch(Indbm, part);
184 value.dptr = address;
185 value.dsize = strlen(address) + 1;
186 if (partvalue.dptr != NULL &&
187 Compare(partvalue.dptr, user) == 0) {
189 if (NameMode)
190 DoName(userpart);
191 if (!NoOutput)
192 dbm_store(Outdbm, value, part, DBM_REPLACE);
193 if (Verbose) printf("%s --> %s --> %s\n",
194 userpart, user, address);
195 } else {
196 if (NameMode)
197 DoName(user);
198 key.dptr = user;
199 key.dsize = strlen(user) + 1;
200 if (!NoOutput)
201 dbm_store(Outdbm, value, key, DBM_REPLACE);
202 if (Verbose)
203 printf("%s --> %s\n", user, address);
210 * Returns true if this is an invalid host
213 HostCheck(char *h, char *a)
215 struct hostent *hp;
217 if (DomainOK && strchr(a, '.'))
218 return (0);
220 if (UucpOK && strchr(a, '!'))
221 return (0);
223 hp = gethostbyname(h);
224 return (hp == NULL);
228 * Apply some Heurisitcs to upper case-ify the name
229 * If it has a dot in it.
231 void
232 DoName(char *cp)
234 if (strchr(cp, '.') == NULL)
235 return;
237 while (*cp) {
238 UpperCase(cp);
239 while (*cp && *cp != '-' && *cp != '.')
240 cp++;
241 if (*cp)
242 cp++; /* skip past punctuation */
247 * upper cases one name - stops at a .
249 void
250 UpperCase(char *cp)
252 int ch = cp[0];
254 if (isupper(ch))
255 ch = tolower(ch);
257 if (ch == 'f' && cp[1] == 'f')
258 return; /* handle ff */
260 if (ch == 'm' && cp[1] == 'c' && islower(cp[2]))
261 cp[2] = toupper(cp[2]);
262 if (islower(ch))
263 cp[0] = toupper(ch);
266 void
267 AddYPEntries(void)
269 datum key, value;
270 char last_modified[PBLKSIZ];
271 char host_name[PBLKSIZ];
272 time_t now;
275 * Add the special NIS entries.
277 key.dptr = yp_last_modified;
278 key.dsize = yp_last_modified_sz;
279 time(&now);
280 sprintf(last_modified, "%10.10d", now);
281 value.dptr = last_modified;
282 value.dsize = strlen(value.dptr);
283 dbm_store(Outdbm, key, value, DBM_REPLACE);
285 key.dptr = yp_master_name;
286 key.dsize = yp_master_name_sz;
287 sysinfo(SI_HOSTNAME, host_name, sizeof (host_name));
288 value.dptr = host_name;
289 value.dsize = strlen(value.dptr);
290 dbm_store(Outdbm, key, value, DBM_REPLACE);
294 main(int argc, char *argv[])
296 while (argc > 1 && argv[1][0] == '-') {
297 switch (argv[1][1]) {
298 case 'v':
299 Verbose = 1;
300 break;
302 case 'u':
303 UucpOK = 1;
304 break;
306 case 'd':
307 DomainOK = 1;
308 break;
310 case 'e':
311 ErrorCheck = 1;
312 break;
314 case 's':
315 Simple = 1;
316 break;
318 case 'n':
319 NameMode = 1;
320 break;
322 default:
323 printf("Unknown option %c\n", argv[1][1]);
324 break;
326 argc--; argv++;
328 if (argc < 2) {
329 printf("Usage: mkalias [-e] [-v] [-u] [-d] [-s] [-n] <input> <output>\n");
330 exit(1);
332 Indbm = dbm_open(argv[1], O_RDONLY, 0);
333 if (Indbm == NULL) {
334 printf("Unable to open input database %s\n", argv[1]);
335 exit(1);
337 Scandbm = dbm_open(argv[1], O_RDONLY, 0);
338 if (Scandbm == NULL) {
339 printf("Unable to open input database %s\n", argv[1]);
340 exit(1);
342 if (argv[2] == NULL)
343 NoOutput = 1;
344 else {
345 Outdbm = dbm_open(argv[2], O_RDWR|O_CREAT|O_TRUNC, 0644);
346 if (Outdbm == NULL) {
347 printf("Unable to open output database %s\n", argv[2]);
348 exit(1);
351 ProcessMap();
352 dbm_close(Indbm);
353 dbm_close(Scandbm);
354 if (!NoOutput) {
355 AddYPEntries();
356 dbm_close(Outdbm);
358 return (0);