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