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