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