xref: /original-bsd/usr.bin/uucp/libuu/uucpname.c (revision 93ab02a6)
1 /*-
2  * Copyright (c) 1985 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)uucpname.c	5.6 (Berkeley) 04/24/91";
10 #endif /* not lint */
11 
12 #include "uucp.h"
13 #include <sys/stat.h>
14 
15 /*LINTLIBRARY*/
16 
17 #ifdef	GETMYHNAME
18 #include <UNET/unetio.h>
19 #endif
20 
21 #ifdef	UNAME
22 /* Use USG uname() library routine */
23 #include <sys/utsname.h>
24 #endif
25 
26 #ifdef	CCWHOAMI
27 /* Compile in 'sysname' as found in standard(!) include file */
28 #include <whoami.h>
29 #endif
30 char Myfullname[64];
31 
32 /*
33  *	uucpname(name)		get the uucp name
34  *
35  *	return code - none
36  */
37 uucpname(name)
38 register char *name;
39 {
40 	register char *s;
41 
42 	/*
43 	 * Since some UNIX systems do not honor the set-user-id bit
44 	 * when the invoking user is root, we must change the uid here.
45 	 * So uucp files are created with the correct owner.
46 	 */
47 	if (geteuid() == 0 && getuid() == 0) {
48 		struct stat stbuf;
49 		stbuf.st_uid = 0;	/* In case the stat fails */
50 		stbuf.st_gid = 0;
51 		stat(UUCICO, &stbuf);	/* Assume uucico is correctly owned */
52 		setgid(stbuf.st_gid);
53 		setuid(stbuf.st_uid);
54 	}
55 
56 	s = NULL;	/* system name unknown, so far */
57 
58 #ifdef GETHOSTNAME
59 	if (s == NULL || *s == '\0') {
60 #ifdef VMS
61 		int i = sizeof(Myfullname);
62 #endif VMS
63 
64 		s = Myfullname;
65 #ifdef VMS
66 		if(gethostname(Myfullname, &i) == -1) {
67 #else !VMS
68 		if(gethostname(Myfullname, sizeof(Myfullname)) == -1) {
69 #endif !VMS
70 			DEBUG(1, "gethostname", _FAILED);
71 			s = NULL;
72 		}
73 	}
74 #endif GETHOSTNAME
75 
76 #ifdef	UNAME
77 	/* Use USG library routine */
78 	if (s == NULL || *s == '\0') {
79 		struct utsname utsn;
80 
81 		if (uname(&utsn) == -1) {
82 			DEBUG(1, "uname", _FAILED);
83 			s = NULL;
84 		} else {
85 			strncpy(Myfullname, utsn.nodename, sizeof Myfullname);
86 			s = Myfullname;
87 		}
88 	}
89 #endif
90 
91 #ifdef	WHOAMI
92 	/* Use fake gethostname() routine */
93 	if (s == NULL || *s == '\0') {
94 
95 		s = Myfullname;
96 		if (fakegethostname(Myfullname, sizeof(Myfullname)) == -1) {
97 			DEBUG(1, "whoami search", _FAILED);
98 			s = NULL;
99 		}
100 	}
101 #endif
102 
103 #ifdef	CCWHOAMI
104 	/* compile sysname right into uucp */
105 	if (s == NULL || *s == '\0') {
106 		s = sysname;
107 		strncpy(Myfullname, s, sizeof Myfullname);
108 	}
109 #endif
110 
111 #ifdef	UUNAME
112 	/* uucp name is stored in /etc/uucpname or /local/uucpname */
113 	if (s == NULL || *s == '\0') {
114 		FILE *uucpf;
115 
116 		s = Myfullname;
117 		if (((uucpf = fopen("/etc/uucpname", "r")) == NULL &&
118 		     (uucpf = fopen("/local/uucpname", "r")) == NULL) ||
119 			fgets(Myfullname, sizeof Myfullname, uucpf) == NULL) {
120 				DEBUG(1, "uuname search", _FAILED);
121 				s = NULL;
122 		}
123 		if (s == Myfullname) {
124 			register char *p;
125 			p = index(s, '\n');
126 			if (p)
127 				*p = '\0';
128   		}
129 		if (uucpf != NULL)
130 			fclose(uucpf);
131 	}
132 #endif
133 
134 #ifdef	GETMYHNAME
135 	/* Use 3Com's getmyhname() routine */
136 	if (s == NULL || *s == '\0') {
137 		if ((s = getmyhname()) == NULL)
138 			DEBUG(1, "getmyhname", _FAILED);
139 		else
140 			strncpy(Myfullname, s, sizeof Myfullname);
141 	}
142 #endif
143 
144 #ifdef	MYNAME
145 	if (s == NULL || *s == '\0') {
146 		s = MYNAME;
147 		strncpy(Myfullname, s, sizeof Myfullname);
148 	}
149 #endif
150 
151 	if (s == NULL || *s == '\0') {
152 		/*
153 		 * As a last ditch thing, we *could* search Spool
154 		 * for D.<uucpname> and use that,
155 		 * but that is too much trouble, isn't it?
156 		 */
157 		logent("SYSTEM NAME", "CANNOT DETERMINE");
158 		strcpy(Myfullname, "unknown");
159 	}
160 
161 	/*
162 	 * copy uucpname back to caller-supplied buffer,
163 	 * truncating to MAXBASENAME characters.
164 	 * Also set up subdirectory names
165 	 * Truncate names at '.' if found to handle cases like
166 	 * seismo.css.gov being returned by gethostname().
167 	 * uucp sites should not have '.' in their name anyway
168 	 */
169 	s = index(Myfullname, '.');
170 	if (s != NULL)
171 		*s = '\0';
172 	strncpy(name, Myfullname, MAXBASENAME);
173 	name[MAXBASENAME] = '\0';
174 	DEBUG(1, "My uucpname = %s\n", name);
175 
176 	sprintf(DLocal, "D.%.*s", SYSNSIZE, name);
177 	sprintf(DLocalX, "D.%.*sX", SYSNSIZE, name);
178 }
179 
180 #ifdef	WHOAMI
181 /*
182  * simulate the 4.2 bsd system call by reading /usr/include/whoami.h
183  * and looking for the #define sysname
184  */
185 
186 #define	HDRFILE "/usr/include/whoami.h"
187 
188 fakegethostname(name, len)
189 char *name;
190 int len;
191 {
192 	char buf[BUFSIZ];
193 	char bname[32];
194 	char hname[32];
195 	char nname[128];
196 	register char *p, *q, *nptr;
197 	int i;
198 	register FILE *fd;
199 
200 	fd = fopen(HDRFILE, "r");
201 	if (fd == NULL)
202 		return(-1);
203 
204 	hname[0] = 0;
205 	nname[0] = 0;
206 	nptr = nname;
207 
208 	while (fgets(buf, sizeof buf, fd) != NULL) { /* each line in the file */
209 		if (sscanf(buf, "#define sysname \"%[^\"]\"", bname) == 1) {
210 			strcpy(hname, bname);
211 		} else if (sscanf(buf, "#define nickname \"%[^\"]\"", bname) == 1) {
212 			strcpy(nptr, bname);
213 			nptr += strlen(bname) + 1;
214 		} else if (sscanf(buf, "#define nickname%d \"%[^\"]\"", &i, bname) == 2) {
215 			strcpy(nptr, bname);
216 			nptr += strlen(bname) + 1;
217 		}
218 	}
219 	fclose(fd);
220 	if (hname[0] == 0)
221 		return FAIL;
222 	strncpy(name, hname, len);
223 	p = nname;
224 	i = strlen(hname) + 1;
225 	q = name + i;
226 	while (i < len && (p[0] != 0 || p[1] != 0)) {
227 		*q++ = *p++;
228 		i++;
229 	}
230 	*q++ = 0;
231 	return SUCCESS;
232 }
233 #endif
234