xref: /original-bsd/usr.bin/uucp/libuu/versys.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1985, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)versys.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include "uucp.h"
13 #include <stdio.h>
14 #include <ctype.h>
15 
16 /*LINTLIBRARY*/
17 
18 char PhoneNumber[MAXPH];
19 
20 /*
21  *	verify system names n1 and n2
22  *	return codes:  SUCCESS  |  FAIL
23  *
24  *	NOTE:
25  *		the old calling sequence was versys(name) but is
26  *	now versys(&name) so that we can perform aliasing!!!!
27  *	See accompanying changes in uucp.c and uux.c
28  *		-- Ray Essick, April 27, 1984
29  */
30 versys(nameptr)
31 register char **nameptr;
32 {
33 	register FILE *fp;
34 	char line[BUFSIZ];
35 	char *name;
36 
37 	DEBUG (11, "Before Alias: %s\n", *nameptr);
38 	uualias (nameptr);			/* alias expansion */
39 	DEBUG (11, "After Alias: %s\n", *nameptr);
40 	name = *nameptr;			/* dereference */
41 
42 	if (name[0] == '\0' || strncmp(name, Myname, MAXBASENAME) == 0)
43 		return SUCCESS;
44 
45 	fp = fopen(SYSFILE, "r");
46 	if (fp == NULL) {
47 		syslog(LOG_ERR, "fopen(%s) failed: %m", SYSFILE);
48 		cleanup(FAIL);
49 	}
50 	PhoneNumber[0] = '\0';
51 	while (cfgets(line, sizeof(line), fp) != NULL) {
52 		char *targs[100];
53 
54 		getargs(line, targs, 100);
55 		if (strncmp(name, targs[0], MAXBASENAME) == SAME) {
56 			fclose(fp);
57 			if (targs[F_PHONE])
58 				strncpy(PhoneNumber, targs[F_PHONE], MAXPH);
59 			return SUCCESS;
60 		}
61 	}
62 	fclose(fp);
63 	return FAIL;
64 }
65 
66 /*
67  *	Works (sort of) like rhost(3) on 4.1[abc] Bsd systems.
68  *
69  *	Looks for the host in the L.aliases file and returns the
70  *	"standard" name by modifying the pointer. The returned
71  *	value is saved with malloc(3) so it isn't zapped by
72  *	subsequent calls.
73  *
74  *	Returns:
75  *		FAIL		No L.aliases file
76  *		SUCCESS		Anything else
77  */
78 
79 uualias(hostptr)
80 char  **hostptr;			  /* we change it */
81 {
82 	FILE *Aliases;			  /* list of aliases */
83 	char buf[BUFSIZ];
84 	int atend;
85 	char *p, *q;
86 	char *koshername;		 /* "official" name */
87 
88 	if ((Aliases = fopen(ALIASFILE, "r")) == NULL) {
89 		DEBUG(11, "No %s file\n", ALIASFILE);
90 		return FAIL;			  /* no alias file */
91 	}
92 
93 	DEBUG (11, "Alias expansion for %s\n", *hostptr);
94 	while (cfgets(buf, sizeof (buf), Aliases)) {
95 		p = &buf[0];
96 		atend = 0;
97 		DEBUG(11, "Alias line: %s\n", buf);
98 
99 		while (!atend) {
100 			while (isspace(*p) && *p != '\n')
101 				p++;			  /* skip white space */
102 			q = p;
103 			while (!isspace(*q) && *q != '\n')
104 				q++;			  /* find end */
105 			if (*q == '\n')
106 				atend++;		  /* last entry */
107 			*q = '\0';
108 			DEBUG(11, "Compare against: %s\n", p);
109 			if (strcmp(*hostptr, p) == 0)/* match? */ {
110 				koshername = malloc((unsigned)strlen(buf) + 1);
111 				strcpy(koshername, buf); /* save it */
112 				fclose(Aliases);
113 				DEBUG(4, "Alias: %s to ", *hostptr);
114 				DEBUG(4, "%s\n", koshername);
115 				*hostptr = koshername;	  /* correct one */
116 				return SUCCESS;		  /* all is well */
117 			}
118 			p = q + 1;			  /* try next entry */
119 		}
120 
121 	}
122 	fclose(Aliases);
123 	DEBUG(11, "Alias doesn't match %s, remains unchanged\n", *hostptr);
124 	return SUCCESS;				  /* unchanged host */
125 }
126