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