Commit | Line | Data |
---|---|---|
3db081fa ES |
1 | /* Copyright (c) 1979 Regents of the University of California */ |
2 | /* | |
3 | This file is meant to handle all the machine | |
4 | dependencies in the network code. | |
5 | Everything is conditionally compiled. | |
6 | ||
7 | It can be uses w/o network stuff to simulate | |
8 | v7 for other programs, too. | |
9 | ||
10 | Table of machine dependencies (not isolated in mach.h, mach.c) | |
11 | ||
12 | VAX CC CORY | |
13 | --- -- ---- | |
14 | M_CC stuff mail -r | |
15 | ||
16 | ||
17 | ||
18 | */ | |
19 | # include <stdio.h> | |
20 | # include "mach.h" | |
21 | ||
22 | # ifndef CC | |
23 | submit(a) {} | |
24 | # endif | |
25 | ||
26 | # ifdef FUID | |
27 | setgid() {}; | |
28 | # endif | |
29 | ||
30 | char vaxtovax; | |
31 | long fixuplong(a) | |
32 | long a; { | |
33 | # ifdef VAX | |
34 | register char *p,c1,c2; | |
35 | char c3,c4; | |
36 | if(!vaxtovax){ | |
37 | p = (char*) &a; | |
38 | c1 = *p++; | |
39 | c2 = *p++; | |
40 | c3 = *p++; | |
41 | c4 = *p++; | |
42 | p = (char*) &a; | |
43 | *p++ = c3; | |
44 | *p++ = c4; | |
45 | *p++ = c1; | |
46 | *p++ = c2; | |
47 | } | |
48 | # endif | |
49 | return(a); | |
50 | } | |
51 | /* always returns a string */ | |
52 | char *getun(uid){ | |
53 | struct passwd *pwd; | |
54 | static int ouid = -1; | |
55 | static char oresult[20] = ""; | |
56 | if(uid != ouid){ | |
57 | # ifdef HPASSWD | |
58 | if(getname(uid,oresult) != 0) | |
59 | # endif | |
60 | { | |
61 | pwd = getpwuid(uid); | |
62 | strcpy(oresult,pwd == NULL ? "UNKNOWN" : pwd->pw_name); | |
63 | } | |
64 | } | |
65 | ouid = uid; | |
66 | return(oresult); | |
67 | } | |
68 | /* handle the regular unix and local mods difference for user id's */ | |
69 | /* this call returns the 1 word uid = to what getuid will return */ | |
70 | guid(uid,gid){ | |
71 | # ifdef FUID | |
72 | return((uid & 0377) | (gid << 8)); | |
73 | # else | |
74 | return(uid); | |
75 | # endif | |
76 | } | |
77 | ||
78 | # ifdef OLDTTY | |
79 | isatty(i){ | |
80 | return(ttyn(i) != 'x'); | |
81 | } | |
82 | char *ttyname(i){ /* return NULL if not TTY */ | |
83 | char c; | |
84 | static char ttystr[] = "/dev/ttyx"; | |
85 | c = ttyn(i); | |
86 | ttystr[8] = c; | |
87 | return(c == 'x' ? NULL : ttystr); | |
88 | } | |
89 | # endif | |
90 | ||
91 | # ifdef CCTTY | |
92 | # undef ttyname() | |
93 | myttyname(i){ /* return NULL for non tty */ | |
94 | static char s[15],*p; | |
95 | p = ttyname(i); | |
96 | if(p == NULL)return(NULL); | |
97 | strcpy(s,"/dev/"); | |
98 | strcat(s,p); | |
99 | return(s); | |
100 | } | |
101 | # define ttyname(S) myttyname(S) | |
102 | # endif | |
103 | ||
104 | /* get passwd from passwdf */ | |
105 | getpwdf(pwd) | |
106 | struct passwd *pwd; { | |
107 | # ifdef PASSWDF | |
108 | # ifndef TESTING | |
109 | register char *p, *q; | |
110 | char buf1[BUFSIZ], found; | |
111 | FILE *pw; | |
112 | pwd->pw_passwd[0] = 0; | |
113 | pw = fopen("/etc/passwdf","r"); | |
114 | if(pw == NULL){ | |
115 | /* | |
116 | error("/etc/passwdf: %s",sys_errlist[errno]); | |
117 | */ | |
118 | return; | |
119 | } | |
120 | found = 0; | |
121 | while(fgets(buf1,BUFSIZ,pw) != NULL){ | |
122 | for(p=buf1; *p && *p != ':'; p++); | |
123 | *p = 0; | |
124 | if(strcmp(buf1,pwd->pw_name) == 0){ | |
125 | found = 1; | |
126 | break; | |
127 | } | |
128 | } | |
129 | fclose(pw); | |
130 | if(!found)return; | |
131 | q = ++p; | |
132 | for(;*p && *p != ':';p++); | |
133 | *p = 0; | |
134 | strcpy(pwd->pw_passwd,q); | |
135 | /* | |
136 | debug("user %s passwd %s %s",pwd->pw_name,pwd->pw_passwd); | |
137 | */ | |
138 | # endif | |
139 | # endif | |
140 | } | |
141 | /* | |
142 | these are all the v7 routines not available on the v6 machines | |
143 | */ | |
144 | ||
145 | # ifndef V7 | |
146 | ||
147 | char **environ; /* global environment pointer */ | |
148 | ||
149 | ioctl(a,b,c){ | |
150 | return(0); /* always succeeds */ | |
151 | } | |
152 | long atol(s) | |
153 | register char *s; { | |
154 | long i = 0; | |
155 | while('0' <= *s && *s <= '9') | |
156 | i = i * 10 + (*s++ - '0'); | |
157 | return(i); | |
158 | } | |
159 | long gettime(){ | |
160 | long tt; | |
161 | time(&tt); | |
162 | return(tt); | |
163 | } | |
164 | long getsize(str) | |
165 | struct stat *str; { | |
166 | long wk; | |
167 | wk = ((long)(str->st_size0 & 0377)) << 16; | |
168 | wk += (long)((unsigned)str->st_size1); | |
169 | return(wk); | |
170 | } | |
171 | char *getenv(){ /* always returns home directory */ | |
172 | char *hdir = 0, tstr[20]; | |
173 | struct passwd *pwd; | |
174 | int t; | |
175 | t = ttyn(2); | |
176 | # ifdef OLDTTY | |
177 | if(t == 'x')t = ttyn(1); | |
178 | if(t == 'x')t = ttyn(0); | |
179 | if(t != 'x' && hget(t) == 0)hdir = hgethome(); | |
180 | # endif | |
181 | # ifdef CCTTY | |
182 | if(t == -1)t = ttyn(1); | |
183 | if(t == -1)t = ttyn(0); | |
184 | if(t != -1 && hget(t) == 0)hdir = hgethome(); | |
185 | # endif | |
186 | if(hdir == 0){ | |
187 | pwd = getpwuid(getuid()); | |
188 | if(pwd != NULL)hdir = pwd->pw_shell; | |
189 | } | |
190 | return(hdir); | |
191 | } | |
192 | ||
193 | /* doesn't handle split passwd files */ | |
194 | struct passwd * | |
195 | getpwuid(uid) | |
196 | register uid; | |
197 | { | |
198 | register struct passwd *p; | |
199 | struct passwd *getpwent(); | |
200 | ||
201 | setpwent(); | |
202 | while( (p = getpwent()) && guid(p->pw_uid,p->pw_gid) != uid ); | |
203 | endpwent(); | |
204 | return(p); | |
205 | } | |
206 | ||
207 | static char PASSWD[] = "/etc/passwd"; | |
208 | static char EMPTY[] = ""; | |
209 | static FILE *pwf = NULL; | |
210 | static char line[BUFSIZ+1]; | |
211 | static struct passwd passwd; | |
212 | ||
213 | setpwent() | |
214 | { | |
215 | if( pwf == NULL ) | |
216 | pwf = fopen( PASSWD, "r" ); | |
217 | else | |
218 | rewind( pwf ); | |
219 | } | |
220 | ||
221 | endpwent() | |
222 | { | |
223 | if( pwf != NULL ){ | |
224 | fclose( pwf ); | |
225 | pwf = NULL; | |
226 | } | |
227 | } | |
228 | ||
229 | static char * | |
230 | pwskip(p) | |
231 | register char *p; | |
232 | { | |
233 | while( *p && *p != ':' ) | |
234 | ++p; | |
235 | if( *p ) *p++ = 0; | |
236 | return(p); | |
237 | } | |
238 | ||
239 | struct passwd * | |
240 | getpwent() | |
241 | { | |
242 | register char *p; | |
243 | ||
244 | if (pwf == NULL) { | |
245 | if( (pwf = fopen( PASSWD, "r" )) == NULL ) | |
246 | return(0); | |
247 | } | |
248 | p = fgets(line, BUFSIZ, pwf); | |
249 | if (p==NULL) | |
250 | return(0); | |
251 | passwd.pw_name = p; | |
252 | p = pwskip(p); | |
253 | passwd.pw_passwd = p; | |
254 | p = pwskip(p); | |
255 | passwd.pw_uid = atoi(p); | |
256 | p = pwskip(p); | |
257 | passwd.pw_gid = atoi(p); | |
258 | passwd.pw_quota = 0; | |
259 | passwd.pw_comment = EMPTY; | |
260 | p = pwskip(p); | |
261 | passwd.pw_gecos = p; | |
262 | p = pwskip(p); | |
263 | passwd.pw_dir = p; | |
264 | p = pwskip(p); | |
265 | passwd.pw_shell = p; | |
266 | while(*p && *p != '\n') p++; | |
267 | *p = '\0'; | |
268 | return(&passwd); | |
269 | } | |
270 | ||
271 | struct passwd * | |
272 | getpwnam(name) | |
273 | char *name; | |
274 | { | |
275 | register struct passwd *p; | |
276 | struct passwd *getpwent(); | |
277 | ||
278 | setpwent(); | |
279 | while( (p = getpwent()) && strcmp(name,p->pw_name) ); | |
280 | endpwent(); | |
281 | return(p); | |
282 | } | |
283 | /* returns NULL if not found */ | |
284 | char *getlogin(){ /* get login name of current person */ | |
285 | static struct utmp utmpstr; | |
286 | char *s, *res; | |
287 | FILE *fp; | |
288 | res = NULL; | |
289 | s = ttyname(2); | |
290 | if(s == NULL)s = ttyname(1); | |
291 | if(s == NULL)s = ttyname(0); | |
292 | if(s == NULL)return(NULL); | |
293 | fp = fopen("/etc/utmp","r"); | |
294 | if(fp == NULL)return(NULL); | |
295 | while(fread(&utmpstr,1,sizeof utmpstr,fp) == sizeof utmpstr) | |
296 | # ifdef OLDTTY | |
297 | if(utmpstr.ut_tty == s[8]) | |
298 | # else | |
299 | if(strcmp(utmpstr.ut_line,s+5) == 0) | |
300 | # endif | |
301 | res = utmpstr.ut_name; | |
302 | fclose(fp); | |
303 | return(res); | |
304 | } | |
305 | /* | |
306 | * Unix routine to do an "fopen" on file descriptor | |
307 | * The mode has to be repeated because you can't query its | |
308 | * status | |
309 | */ | |
310 | ||
311 | FILE * | |
312 | fdopen(fd, mode) | |
313 | register char *mode; | |
314 | { | |
315 | extern int errno; | |
316 | register FILE *iop; | |
317 | extern FILE *_lastbuf; | |
318 | ||
319 | for (iop = _iob; iop->_flag&(_IOREAD|_IOWRT); iop++) | |
320 | if (iop >= _lastbuf) | |
321 | return(NULL); | |
322 | iop->_cnt = 0; | |
323 | iop->_file = fd; | |
324 | if (*mode != 'r') { | |
325 | iop->_flag |= _IOWRT; | |
326 | if (*mode == 'a') | |
327 | lseek(fd, 0L, 2); | |
328 | } else | |
329 | iop->_flag |= _IOREAD; | |
330 | return(iop); | |
331 | } | |
332 | system(s) | |
333 | char *s; | |
334 | { | |
335 | int status, pid, w; | |
336 | register int (*istat)(), (*qstat)(); | |
337 | ||
338 | if ((pid = fork()) == 0) { | |
339 | execl("/bin/sh", "sh", "-c", s, 0); | |
340 | _exit(127); | |
341 | } | |
342 | istat = signal(SIGINT, SIG_IGN); | |
343 | qstat = signal(SIGQUIT, SIG_IGN); | |
344 | while ((w = wait(&status)) != pid && w != -1) | |
345 | ; | |
346 | if (w == -1) | |
347 | status = -1; | |
348 | signal(SIGINT, istat); | |
349 | signal(SIGQUIT, qstat); | |
350 | return(status); | |
351 | } | |
352 | /* getpw - one in -lS doesn't work at Berkeley for gid > 127 */ | |
353 | getpw(uid, buf) | |
354 | int uid; | |
355 | char buf[]; | |
356 | { | |
357 | static FILE *pwf; | |
358 | register n, c; | |
359 | register char *bp; | |
360 | int n1; | |
361 | int id; | |
362 | ||
363 | if(pwf == 0) | |
364 | pwf = fopen("/etc/passwd", "r"); | |
365 | if(pwf == NULL) | |
366 | return(1); | |
367 | rewind(pwf); | |
368 | ||
369 | for (;;) { | |
370 | bp = buf; | |
371 | while((c=getc(pwf)) != '\n') { | |
372 | if(c <= 0) | |
373 | return(1); | |
374 | *bp++ = c; | |
375 | } | |
376 | *bp++ = '\0'; | |
377 | bp = buf; | |
378 | n = 3; | |
379 | while(--n) | |
380 | while((c = *bp++) != ':') | |
381 | if(c == '\n') | |
382 | return(1); | |
383 | while((c = *bp++) != ':') { | |
384 | if(!isdigit(c)) | |
385 | continue; | |
386 | n = n*10+c-'0'; | |
387 | } | |
388 | n1 = 0; | |
389 | while((c = *bp++) != ':') { | |
390 | if(!isdigit(c)) | |
391 | continue; | |
392 | n1 = n1*10 + c - '0'; | |
393 | } | |
394 | id = (n1 << 8) | (n&0377); | |
395 | if(id == uid) | |
396 | return(0); | |
397 | } | |
398 | return(1); | |
399 | } | |
400 | char * | |
401 | getpass(prompt) | |
402 | char *prompt; | |
403 | { | |
404 | struct sgttyb ttyb; | |
405 | int flags; | |
406 | register char *p; | |
407 | register c; | |
408 | FILE *fi = NULL; | |
409 | static char pbuf[9]; | |
410 | int (*signal())(); | |
411 | int (*sig)(); | |
412 | ||
413 | /* modified because Cory needs super-user to stty /dev/tty */ | |
414 | # ifndef CORY | |
415 | if ((fi = fopen("/dev/tty", "r")) == NULL) | |
416 | fi = stdin; | |
417 | else | |
418 | setbuf(fi, (char *)NULL); | |
419 | gtty(fileno(fi), &ttyb); | |
420 | # else | |
421 | if(gtty(0,&ttyb) >= 0)fi = stdin; | |
422 | else if(gtty(2,&ttyb) >= 0)fi = stderr; | |
423 | else { | |
424 | pbuf[0] = 0; | |
425 | return(pbuf); | |
426 | } | |
427 | # endif | |
428 | sig = signal(SIGINT, SIG_IGN); | |
429 | flags = ttyb.sg_flags; | |
430 | ttyb.sg_flags &= ~ECHO; | |
431 | if(stty(fileno(fi), &ttyb) < 0) perror("stty:"); | |
432 | fprintf(stderr, prompt); | |
433 | for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) { | |
434 | if (p < &pbuf[8]) | |
435 | *p++ = c; | |
436 | } | |
437 | *p = '\0'; | |
438 | fprintf(stderr, "\n"); | |
439 | ttyb.sg_flags = flags; | |
440 | stty(fileno(fi), &ttyb); | |
441 | signal(SIGINT, sig); | |
442 | # ifndef CORY | |
443 | if (fi != stdin) | |
444 | fclose(fi); | |
445 | # endif | |
446 | return(pbuf); | |
447 | } | |
448 | /* end of non-vax v7 routines */ | |
449 | # endif |