1 /* $Header: doio.c,v 3.0.1.7 90/03/14 12:26:24 lwall Locked $
3 * Copyright (c) 1989, Larry Wall
5 * You may distribute under the terms of the GNU General Public License
6 * as specified in the README file that comes with the perl 3.0 kit.
9 * Revision 3.0.1.7 90/03/14 12:26:24 lwall
10 * patch15: commands involving execs could cause malloc arena corruption
12 * Revision 3.0.1.6 90/03/12 16:30:07 lwall
13 * patch13: system 'FOO=bar command' didn't invoke sh as it should
15 * Revision 3.0.1.5 90/02/28 17:01:36 lwall
16 * patch9: open(FOO,"$filename\0") will now protect trailing spaces in filename
17 * patch9: removed obsolete checks to avoid opening block devices
18 * patch9: removed references to acusec and modusec that some utime.h's have
19 * patch9: added pipe function
21 * Revision 3.0.1.4 89/12/21 19:55:10 lwall
22 * patch7: select now works on big-endian machines
23 * patch7: errno may now be a macro with an lvalue
24 * patch7: ANSI strerror() is now supported
25 * patch7: Configure now detects DG/UX thingies like [sg]etpgrp2 and utime.h
27 * Revision 3.0.1.3 89/11/17 15:13:06 lwall
28 * patch5: some systems have symlink() but not lstat()
29 * patch5: some systems have dirent.h but not readdir()
31 * Revision 3.0.1.2 89/11/11 04:25:51 lwall
32 * patch2: orthogonalized the file modes some so we can have <& +<& etc.
33 * patch2: do_open() now detects sockets passed to process from parent
34 * patch2: fd's above 2 are now closed on exec
35 * patch2: csh code can now use csh from other than /bin
36 * patch2: getsockopt, get{sock,peer}name didn't define result properly
37 * patch2: warn("shutdown") was replicated
38 * patch2: gethostbyname was misdeclared
39 * patch2: telldir() is sometimes a macro
41 * Revision 3.0.1.1 89/10/26 23:10:05 lwall
42 * patch1: Configure now checks for BSD shadow passwords
44 * Revision 3.0 89/10/18 15:10:54 lwall
53 #include <sys/socket.h>
68 do_open(stab,name,len)
74 register STIO *stio = stab_io(stab);
75 char *myname = savestr(name);
79 char mode[3]; /* stdio file mode ("r\0" or "r+\0") */
82 forkprocess = 1; /* assume true if no fork */
83 while (len && isspace(name[len-1]))
86 stio = stab_io(stab) = stio_new();
88 fd = fileno(stio->ifp);
89 if (stio->type == '|')
90 result = mypclose(stio->ifp);
91 else if (stio->ifp != stio->ofp) {
94 result = fclose(stio->ifp);
96 else if (stio->type != '-')
97 result = fclose(stio->ifp);
100 if (result == EOF && fd > 2)
101 fprintf(stderr,"Warning: unable to close filehandle %s properly.\n",
103 stio->ofp = stio->ifp = Nullfp;
105 if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */
116 for (name++; isspace(*name); name++) ;
119 taintproper("Insecure dependency in piped open");
121 fp = mypopen(name,"w");
124 else if (*name == '>') {
126 taintproper("Insecure dependency in open");
130 mode[0] = stio->type = 'a';
139 while (isspace(*name))
144 stab = stabent(name,FALSE);
145 if (!stab || !stab_io(stab))
147 if (stab_io(stab) && stab_io(stab)->ifp) {
148 fd = fileno(stab_io(stab)->ifp);
149 if (stab_io(stab)->type == 's')
155 fp = fdopen(dup(fd),mode);
158 while (isspace(*name))
160 if (strEQ(name,"-")) {
165 fp = fopen(name,mode);
173 while (isspace(*name))
177 if (strEQ(name,"-")) {
182 fp = fopen(name,mode);
184 else if (name[len-1] == '|') {
187 taintproper("Insecure dependency in piped open");
190 while (len && isspace(name[len-1]))
192 for (; isspace(*name); name++) ;
193 fp = mypopen(name,"r");
198 for (; isspace(*name); name++) ;
199 if (strEQ(name,"-")) {
204 fp = fopen(name,"r");
211 stio->type != '|' && stio->type != '-') {
212 if (fstat(fileno(fp),&statbuf) < 0) {
216 result = (statbuf.st_mode & S_IFMT);
218 if (result == S_IFSOCK || result == 0)
219 stio->type = 's'; /* in case a socket was passed in to us */
222 #if defined(FCNTL) && defined(F_SETFD)
229 if (stio->type != 's')
232 stio->ofp = fdopen(fileno(fp),"w");
243 int filemode,fileuid,filegid;
245 while (alen(stab_xarray(stab)) >= 0) {
246 str = ashift(stab_xarray(stab));
247 str_sset(stab_val(stab),str);
248 STABSET(stab_val(stab));
249 oldname = str_get(stab_val(stab));
250 if (do_open(stab,oldname,stab_val(stab)->str_cur)) {
253 taintproper("Insecure dependency in inplace open");
255 filemode = statbuf.st_mode;
256 fileuid = statbuf.st_uid;
257 filegid = statbuf.st_gid;
259 str_cat(str,inplace);
261 (void)rename(oldname,str->str_ptr);
263 (void)UNLINK(str->str_ptr);
264 (void)link(oldname,str->str_ptr);
265 (void)UNLINK(oldname);
269 (void)UNLINK(oldname);
273 str_cat(str,oldname);
274 errno = 0; /* in case sprintf set errno */
275 if (!do_open(argvoutstab,str->str_ptr,str->str_cur))
276 fatal("Can't do inplace edit");
277 defoutstab = argvoutstab;
279 (void)fchmod(fileno(stab_io(argvoutstab)->ifp),filemode);
281 (void)chmod(oldname,filemode);
284 (void)fchown(fileno(stab_io(argvoutstab)->ifp),fileuid,filegid);
286 (void)chown(oldname,fileuid,filegid);
290 return stab_io(stab)->ifp;
293 fprintf(stderr,"Can't open %s\n",str_get(str));
297 (void)do_close(argvoutstab,FALSE);
298 defoutstab = stabent("STDOUT",TRUE);
304 do_pipe(str, rstab, wstab)
309 register STIO *rstio;
310 register STIO *wstio;
318 rstio = stab_io(rstab);
319 wstio = stab_io(wstab);
322 rstio = stab_io(rstab) = stio_new();
324 do_close(rstab,FALSE);
326 wstio = stab_io(wstab) = stio_new();
328 do_close(wstab,FALSE);
332 rstio->ifp = fdopen(fd[0], "r");
333 wstio->ofp = fdopen(fd[1], "w");
334 wstio->ifp = wstio->ofp;
338 str_sset(str,&str_yes);
342 str_sset(str,&str_undef);
347 do_close(stab,explicit)
352 register STIO *stio = stab_io(stab);
355 if (!stio) { /* never opened */
356 if (dowarn && explicit)
357 warn("Close on unopened file <%s>",stab_name(stab));
361 if (stio->type == '|') {
362 status = mypclose(stio->ifp);
363 retval = (status >= 0);
364 statusvalue = (unsigned)status & 0xffff;
366 else if (stio->type == '-')
369 if (stio->ofp && stio->ofp != stio->ifp) /* a socket */
371 retval = (fclose(stio->ifp) != EOF);
373 stio->ofp = stio->ifp = Nullfp;
388 if (!stab) { /* eof() */
390 stio = stab_io(argvstab);
395 stio = stab_io(stab);
402 #ifdef STDSTDIO /* (the code works without this) */
403 if (stio->ifp->_cnt > 0) /* cheat a little, since */
404 return FALSE; /* this is the most usual case */
407 ch = getc(stio->ifp);
409 (void)ungetc(ch, stio->ifp);
412 if (!stab) { /* not necessarily a real EOF yet? */
413 if (!nextargv(argvstab)) /* get another fp handy */
417 return TRUE; /* normal fp, definitely end of file */
431 stio = stab_io(stab);
432 if (!stio || !stio->ifp)
436 (void)fseek (stio->ifp, 0L, 2); /* ultrix 1.2 workaround */
438 return ftell(stio->ifp);
442 warn("tell() on unopened file");
447 do_seek(stab, pos, whence)
457 stio = stab_io(stab);
458 if (!stio || !stio->ifp)
462 (void)fseek (stio->ifp, 0L, 2); /* ultrix 1.2 workaround */
464 return fseek(stio->ifp, pos, whence) >= 0;
468 warn("seek() on unopened file");
473 do_ctl(optype,stab,func,argstr)
483 if (!stab || !argstr)
485 stio = stab_io(stab);
489 if (argstr->str_pok || !argstr->str_nok) {
490 if (!argstr->str_pok)
495 #define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
499 retval = IOCPARM_LEN(func); /* on BSDish systes we're safe */
501 retval = 256; /* otherwise guess at what's safe */
503 if (argstr->str_cur < retval) {
504 str_grow(argstr,retval+1);
505 argstr->str_cur = retval;
509 s[argstr->str_cur] = 17; /* a little sanity check here */
512 retval = (int)str_gnum(argstr);
513 s = (char*)retval; /* ouch */
517 if (optype == O_IOCTL)
518 retval = ioctl(fileno(stio->ifp), func, s);
521 retval = fcntl(fileno(stio->ifp), func, s);
523 fatal("fcntl is not implemented");
529 if (argstr->str_pok) {
530 if (s[argstr->str_cur] != 17)
531 fatal("Return value overflowed string");
532 s[argstr->str_cur] = 0; /* put our null back */
538 do_stat(str,arg,gimme,arglast)
544 register ARRAY *ary = stack;
545 register int sp = arglast[0] + 1;
549 if ((arg[1].arg_type & A_MASK) == A_WORD) {
550 tmpstab = arg[1].arg_ptr.arg_stab;
551 if (tmpstab != defstab) {
553 str_set(statname,"");
554 if (!stab_io(tmpstab) ||
555 fstat(fileno(stab_io(tmpstab)->ifp),&statcache) < 0) {
561 str_sset(statname,ary->ary_array[sp]);
564 if (arg->arg_type == O_LSTAT)
565 i = lstat(str_get(statname),&statcache);
568 i = stat(str_get(statname),&statcache);
573 if (gimme != G_ARRAY) {
575 str_sset(str,&str_yes);
577 str_sset(str,&str_undef);
579 ary->ary_array[sp] = str;
585 (void)astore(ary,++sp,
586 str_2static(str_nmake((double)statcache.st_dev)));
587 (void)astore(ary,++sp,
588 str_2static(str_nmake((double)statcache.st_ino)));
589 (void)astore(ary,++sp,
590 str_2static(str_nmake((double)statcache.st_mode)));
591 (void)astore(ary,++sp,
592 str_2static(str_nmake((double)statcache.st_nlink)));
593 (void)astore(ary,++sp,
594 str_2static(str_nmake((double)statcache.st_uid)));
595 (void)astore(ary,++sp,
596 str_2static(str_nmake((double)statcache.st_gid)));
597 (void)astore(ary,++sp,
598 str_2static(str_nmake((double)statcache.st_rdev)));
599 (void)astore(ary,++sp,
600 str_2static(str_nmake((double)statcache.st_size)));
601 (void)astore(ary,++sp,
602 str_2static(str_nmake((double)statcache.st_atime)));
603 (void)astore(ary,++sp,
604 str_2static(str_nmake((double)statcache.st_mtime)));
605 (void)astore(ary,++sp,
606 str_2static(str_nmake((double)statcache.st_ctime)));
608 (void)astore(ary,++sp,
609 str_2static(str_nmake((double)statcache.st_blksize)));
610 (void)astore(ary,++sp,
611 str_2static(str_nmake((double)statcache.st_blocks)));
613 (void)astore(ary,++sp,
614 str_2static(str_make("",0)));
615 (void)astore(ary,++sp,
616 str_2static(str_make("",0)));
619 (void)astore(ary,++sp,str_nmake(0.0));
626 looks_like_number(str)
635 send = s + str->str_cur;
640 if (*s == '+' || *s == '-')
648 else if (s == str->str_ptr)
654 if (*s == 'e' || *s == 'E') {
656 if (*s == '+' || *s == '-')
677 warn("print to unopened file");
683 ((str->str_nok && str->str_u.str_nval != 0.0)
684 || (looks_like_number(str) && str_gnum(str) != 0.0) ) )
685 fprintf(fp, ofmt, str->str_u.str_nval);
688 if (*tmps == 'S' && tmps[1] == 't' && tmps[2] == 'a' && tmps[3] == 'b'
689 && str->str_cur == sizeof(STBP) && strlen(tmps) < str->str_cur) {
690 tmps = stab_name(((STAB*)str)); /* a stab value, be nice */
691 str = ((STAB*)str)->str_magic;
694 if (str->str_cur && fwrite(tmps,1,str->str_cur,fp) == 0)
701 do_aprint(arg,fp,arglast)
706 register STR **st = stack->ary_array;
707 register int sp = arglast[1];
709 register int items = arglast[2] - sp;
713 warn("print to unopened file");
717 if (arg->arg_type == O_PRTF) {
718 do_sprintf(arg->arg_ptr.arg_str,items,st);
719 retval = do_print(arg->arg_ptr.arg_str,fp);
722 retval = (items <= 0);
723 for (; items > 0; items--,st++) {
724 if (retval && ofslen) {
725 if (fwrite(ofs, 1, ofslen, fp) == 0) {
730 if (!(retval = do_print(*st, fp)))
733 if (retval && orslen)
734 if (fwrite(ors, 1, orslen, fp) == 0)
747 if (arg[1].arg_type & A_DONT) {
748 stio = stab_io(arg[1].arg_ptr.arg_stab);
749 if (stio && stio->ifp) {
750 statstab = arg[1].arg_ptr.arg_stab;
751 str_set(statname,"");
752 return fstat(fileno(stio->ifp), &statcache);
755 if (arg[1].arg_ptr.arg_stab == defstab)
758 warn("Stat on unopened file <%s>",
759 stab_name(arg[1].arg_ptr.arg_stab));
761 str_set(statname,"");
767 str_sset(statname,str);
768 return stat(str_get(str),&statcache);
784 if (arg[1].arg_type & A_DONT) {
785 if (arg[1].arg_ptr.arg_stab == defstab) {
787 stio = stab_io(statstab);
790 goto really_filename;
794 statstab = arg[1].arg_ptr.arg_stab;
795 str_set(statname,"");
796 stio = stab_io(statstab);
798 if (stio && stio->ifp) {
800 fstat(fileno(stio->ifp),&statcache);
801 if (stio->ifp->_cnt <= 0) {
804 (void)ungetc(i,stio->ifp);
806 if (stio->ifp->_cnt <= 0) /* null file is anything */
808 len = stio->ifp->_cnt + (stio->ifp->_ptr - stio->ifp->_base);
809 s = stio->ifp->_base;
811 fatal("-T and -B not implemented on filehandles\n");
816 warn("Test on unopened file <%s>",
817 stab_name(arg[1].arg_ptr.arg_stab));
823 str_sset(statname,str);
825 i = open(str_get(str),0);
829 len = read(i,tbuf,512);
830 if (len <= 0) /* null file is anything */
836 /* now scan s to look for textiness */
838 for (i = 0; i < len; i++,s++) {
839 if (!*s) { /* null never allowed in text */
846 *s != '\n' && *s != '\r' && *s != '\b' &&
847 *s != '\t' && *s != '\f' && *s != 27)
851 if ((odd * 10 > len) == (arg->arg_type == O_FTTEXT)) /* allow 10% odd */
858 do_aexec(really,arglast)
862 register STR **st = stack->ary_array;
863 register int sp = arglast[1];
864 register int items = arglast[2] - sp;
870 New(401,argv, items+1, char*);
872 for (st += ++sp; items > 0; items--,st++) {
880 if (*argv[0] != '/') /* will execvp use PATH? */
881 taintenv(); /* testing IFS here is overkill, probably */
883 if (really && *(tmps = str_get(really)))
886 execvp(argv[0],argv);
903 taintproper("Insecure dependency in exec");
906 /* save an extra exec if possible */
909 if (strnEQ(cmd,cshname,cshlen) && strnEQ(cmd+cshlen," -c",3)) {
927 execl(cshname,"csh", flags,ncmd,(char*)0);
935 /* see if there are shell metacharacters in it */
937 for (s = cmd; *s && isalpha(*s); s++) ; /* catch VAR=val gizmo */
940 for (s = cmd; *s; s++) {
941 if (*s != ' ' && !isalpha(*s) && index("$&*(){}[]'\";\\|?<>~`\n",*s)) {
942 if (*s == '\n' && !s[1]) {
947 execl("/bin/sh","sh","-c",cmd,(char*)0);
951 New(402,argv, (s - cmd) / 2 + 2, char*);
955 while (*s && isspace(*s)) s++;
958 while (*s && !isspace(*s)) s++;
964 execvp(argv[0],argv);
965 if (errno == ENOEXEC) /* for system V NIH syndrome */
974 do_socket(stab, arglast)
978 register STR **st = stack->ary_array;
979 register int sp = arglast[1];
981 int domain, type, protocol, fd;
986 stio = stab_io(stab);
988 stio = stab_io(stab) = stio_new();
990 do_close(stab,FALSE);
992 domain = (int)str_gnum(st[++sp]);
993 type = (int)str_gnum(st[++sp]);
994 protocol = (int)str_gnum(st[++sp]);
996 taintproper("Insecure dependency in socket");
998 fd = socket(domain,type,protocol);
1001 stio->ifp = fdopen(fd, "r"); /* stdio gets confused about sockets */
1002 stio->ofp = fdopen(fd, "w");
1009 do_bind(stab, arglast)
1013 register STR **st = stack->ary_array;
1014 register int sp = arglast[1];
1015 register STIO *stio;
1021 stio = stab_io(stab);
1022 if (!stio || !stio->ifp)
1025 addr = str_get(st[++sp]);
1027 taintproper("Insecure dependency in bind");
1029 return bind(fileno(stio->ifp), addr, st[sp]->str_cur) >= 0;
1033 warn("bind() on closed fd");
1039 do_connect(stab, arglast)
1043 register STR **st = stack->ary_array;
1044 register int sp = arglast[1];
1045 register STIO *stio;
1051 stio = stab_io(stab);
1052 if (!stio || !stio->ifp)
1055 addr = str_get(st[++sp]);
1057 taintproper("Insecure dependency in connect");
1059 return connect(fileno(stio->ifp), addr, st[sp]->str_cur) >= 0;
1063 warn("connect() on closed fd");
1069 do_listen(stab, arglast)
1073 register STR **st = stack->ary_array;
1074 register int sp = arglast[1];
1075 register STIO *stio;
1081 stio = stab_io(stab);
1082 if (!stio || !stio->ifp)
1085 backlog = (int)str_gnum(st[++sp]);
1086 return listen(fileno(stio->ifp), backlog) >= 0;
1090 warn("listen() on closed fd");
1095 do_accept(str, nstab, gstab)
1100 register STIO *nstio;
1101 register STIO *gstio;
1102 int len = sizeof buf;
1110 gstio = stab_io(gstab);
1111 nstio = stab_io(nstab);
1113 if (!gstio || !gstio->ifp)
1116 nstio = stab_io(nstab) = stio_new();
1117 else if (nstio->ifp)
1118 do_close(nstab,FALSE);
1120 fd = accept(fileno(gstio->ifp),buf,&len);
1123 nstio->ifp = fdopen(fd, "r");
1124 nstio->ofp = fdopen(fd, "w");
1127 str_nset(str, buf, len);
1132 warn("accept() on closed fd");
1134 str_sset(str,&str_undef);
1139 do_shutdown(stab, arglast)
1143 register STR **st = stack->ary_array;
1144 register int sp = arglast[1];
1145 register STIO *stio;
1151 stio = stab_io(stab);
1152 if (!stio || !stio->ifp)
1155 how = (int)str_gnum(st[++sp]);
1156 return shutdown(fileno(stio->ifp), how) >= 0;
1160 warn("shutdown() on closed fd");
1166 do_sopt(optype, stab, arglast)
1171 register STR **st = stack->ary_array;
1172 register int sp = arglast[1];
1173 register STIO *stio;
1181 stio = stab_io(stab);
1182 if (!stio || !stio->ifp)
1185 fd = fileno(stio->ifp);
1186 lvl = (int)str_gnum(st[sp+1]);
1187 optname = (int)str_gnum(st[sp+2]);
1190 st[sp] = str_2static(str_new(257));
1191 st[sp]->str_cur = 256;
1192 st[sp]->str_pok = 1;
1193 if (getsockopt(fd, lvl, optname, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
1198 if (setsockopt(fd, lvl, optname, st[sp]->str_ptr, st[sp]->str_cur) < 0)
1208 warn("[gs]etsockopt() on closed fd");
1209 st[sp] = &str_undef;
1215 do_getsockname(optype, stab, arglast)
1220 register STR **st = stack->ary_array;
1221 register int sp = arglast[1];
1222 register STIO *stio;
1228 stio = stab_io(stab);
1229 if (!stio || !stio->ifp)
1232 st[sp] = str_2static(str_new(257));
1233 st[sp]->str_cur = 256;
1234 st[sp]->str_pok = 1;
1235 fd = fileno(stio->ifp);
1238 if (getsockname(fd, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
1242 if (getpeername(fd, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
1251 warn("get{sock,peer}name() on closed fd");
1252 st[sp] = &str_undef;
1258 do_ghent(which,gimme,arglast)
1263 register ARRAY *ary = stack;
1264 register int sp = arglast[0];
1265 register char **elem;
1267 struct hostent *gethostbyname();
1268 struct hostent *gethostbyaddr();
1270 struct hostent *gethostent();
1272 struct hostent *hent;
1275 if (gimme != G_ARRAY) {
1276 astore(ary, ++sp, str_static(&str_undef));
1280 if (which == O_GHBYNAME) {
1281 char *name = str_get(ary->ary_array[sp+1]);
1283 hent = gethostbyname(name);
1285 else if (which == O_GHBYADDR) {
1286 STR *addrstr = ary->ary_array[sp+1];
1287 int addrtype = (int)str_gnum(ary->ary_array[sp+2]);
1288 char *addr = str_get(addrstr);
1290 hent = gethostbyaddr(addr,addrstr->str_cur,addrtype);
1294 hent = gethostent();
1296 fatal("gethostent not implemented");
1300 (void)astore(ary, ++sp, str = str_static(&str_no));
1301 str_set(str, hent->h_name);
1302 (void)astore(ary, ++sp, str = str_static(&str_no));
1303 for (elem = hent->h_aliases; *elem; elem++) {
1304 str_cat(str, *elem);
1306 str_ncat(str," ",1);
1308 (void)astore(ary, ++sp, str = str_static(&str_no));
1309 str_numset(str, (double)hent->h_addrtype);
1310 (void)astore(ary, ++sp, str = str_static(&str_no));
1311 len = hent->h_length;
1312 str_numset(str, (double)len);
1314 for (elem = hent->h_addr_list; *elem; elem++) {
1315 (void)astore(ary, ++sp, str = str_static(&str_no));
1316 str_nset(str, *elem, len);
1319 (void)astore(ary, ++sp, str = str_static(&str_no));
1320 str_nset(str, hent->h_addr, len);
1325 (void)astore(ary, ++sp, str_static(&str_no));
1333 do_gnent(which,gimme,arglast)
1338 register ARRAY *ary = stack;
1339 register int sp = arglast[0];
1340 register char **elem;
1342 struct netent *getnetbyname();
1343 struct netent *getnetbyaddr();
1344 struct netent *getnetent();
1345 struct netent *nent;
1347 if (gimme != G_ARRAY) {
1348 astore(ary, ++sp, str_static(&str_undef));
1352 if (which == O_GNBYNAME) {
1353 char *name = str_get(ary->ary_array[sp+1]);
1355 nent = getnetbyname(name);
1357 else if (which == O_GNBYADDR) {
1358 STR *addrstr = ary->ary_array[sp+1];
1359 int addrtype = (int)str_gnum(ary->ary_array[sp+2]);
1360 char *addr = str_get(addrstr);
1362 nent = getnetbyaddr(addr,addrtype);
1369 (void)astore(ary, ++sp, str = str_static(&str_no));
1370 str_set(str, nent->n_name);
1371 (void)astore(ary, ++sp, str = str_static(&str_no));
1372 for (elem = nent->n_aliases; *elem; elem++) {
1373 str_cat(str, *elem);
1375 str_ncat(str," ",1);
1377 (void)astore(ary, ++sp, str = str_static(&str_no));
1378 str_numset(str, (double)nent->n_addrtype);
1379 (void)astore(ary, ++sp, str = str_static(&str_no));
1380 str_numset(str, (double)nent->n_net);
1384 (void)astore(ary, ++sp, str_static(&str_no));
1392 do_gpent(which,gimme,arglast)
1397 register ARRAY *ary = stack;
1398 register int sp = arglast[0];
1399 register char **elem;
1401 struct protoent *getprotobyname();
1402 struct protoent *getprotobynumber();
1403 struct protoent *getprotoent();
1404 struct protoent *pent;
1406 if (gimme != G_ARRAY) {
1407 astore(ary, ++sp, str_static(&str_undef));
1411 if (which == O_GPBYNAME) {
1412 char *name = str_get(ary->ary_array[sp+1]);
1414 pent = getprotobyname(name);
1416 else if (which == O_GPBYNUMBER) {
1417 int proto = (int)str_gnum(ary->ary_array[sp+1]);
1419 pent = getprotobynumber(proto);
1422 pent = getprotoent();
1426 (void)astore(ary, ++sp, str = str_static(&str_no));
1427 str_set(str, pent->p_name);
1428 (void)astore(ary, ++sp, str = str_static(&str_no));
1429 for (elem = pent->p_aliases; *elem; elem++) {
1430 str_cat(str, *elem);
1432 str_ncat(str," ",1);
1434 (void)astore(ary, ++sp, str = str_static(&str_no));
1435 str_numset(str, (double)pent->p_proto);
1439 (void)astore(ary, ++sp, str_static(&str_no));
1447 do_gsent(which,gimme,arglast)
1452 register ARRAY *ary = stack;
1453 register int sp = arglast[0];
1454 register char **elem;
1456 struct servent *getservbyname();
1457 struct servent *getservbynumber();
1458 struct servent *getservent();
1459 struct servent *sent;
1461 if (gimme != G_ARRAY) {
1462 astore(ary, ++sp, str_static(&str_undef));
1466 if (which == O_GSBYNAME) {
1467 char *name = str_get(ary->ary_array[sp+1]);
1468 char *proto = str_get(ary->ary_array[sp+2]);
1470 if (proto && !*proto)
1473 sent = getservbyname(name,proto);
1475 else if (which == O_GSBYPORT) {
1476 int port = (int)str_gnum(ary->ary_array[sp+1]);
1477 char *proto = str_get(ary->ary_array[sp+2]);
1479 sent = getservbyport(port,proto);
1482 sent = getservent();
1485 (void)astore(ary, ++sp, str = str_static(&str_no));
1486 str_set(str, sent->s_name);
1487 (void)astore(ary, ++sp, str = str_static(&str_no));
1488 for (elem = sent->s_aliases; *elem; elem++) {
1489 str_cat(str, *elem);
1491 str_ncat(str," ",1);
1493 (void)astore(ary, ++sp, str = str_static(&str_no));
1495 str_numset(str, (double)ntohs(sent->s_port));
1497 str_numset(str, (double)(sent->s_port));
1499 (void)astore(ary, ++sp, str = str_static(&str_no));
1500 str_set(str, sent->s_proto);
1504 (void)astore(ary, ++sp, str_static(&str_no));
1512 do_select(gimme,arglast)
1516 register STR **st = stack->ary_array;
1517 register int sp = arglast[0];
1525 struct timeval timebuf;
1526 struct timeval *tbuf = &timebuf;
1528 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1534 #if BYTEORDER & 0xf0000
1535 #define ORDERBYTE (0x88888888 - BYTEORDER)
1537 #define ORDERBYTE (0x4444 - BYTEORDER)
1542 for (i = 1; i <= 3; i++) {
1543 j = st[sp+i]->str_cur;
1548 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
1549 growsize = maxlen; /* little endians can use vecs directly */
1557 masksize = NFDBITS / NBBY;
1559 masksize = sizeof(long); /* documented int, everyone seems to use long */
1561 growsize = maxlen + (masksize - (maxlen % masksize));
1562 Zero(&fd_sets[0], 4, char*);
1565 for (i = 1; i <= 3; i++) {
1570 str_grow(str,growsize);
1571 s = str_get(str) + j;
1572 while (++j <= growsize) {
1576 else if (str->str_ptr) {
1577 Safefree(str->str_ptr);
1578 str->str_ptr = Nullch;
1581 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1584 New(403, fd_sets[i], growsize, char);
1585 for (offset = 0; offset < growsize; offset += masksize) {
1586 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1587 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1593 if (str->str_nok || str->str_pok) {
1594 value = str_gnum(str);
1597 timebuf.tv_sec = (long)value;
1598 value -= (double)timebuf.tv_sec;
1599 timebuf.tv_usec = (long)(value * 1000000.0);
1602 tbuf = Null(struct timeval*);
1604 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
1618 for (i = 1; i <= 3; i++) {
1622 for (offset = 0; offset < growsize; offset += masksize) {
1623 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1624 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1630 st[++sp] = str_static(&str_no);
1631 str_numset(st[sp], (double)nfound);
1632 if (gimme == G_ARRAY && tbuf) {
1633 value = (double)(timebuf.tv_sec) +
1634 (double)(timebuf.tv_usec) / 1000000.0;
1635 st[++sp] = str_static(&str_no);
1636 str_numset(st[sp], value);
1642 do_spair(stab1, stab2, arglast)
1647 register STR **st = stack->ary_array;
1648 register int sp = arglast[2];
1649 register STIO *stio1;
1650 register STIO *stio2;
1651 int domain, type, protocol, fd[2];
1653 if (!stab1 || !stab2)
1656 stio1 = stab_io(stab1);
1657 stio2 = stab_io(stab2);
1659 stio1 = stab_io(stab1) = stio_new();
1660 else if (stio1->ifp)
1661 do_close(stab1,FALSE);
1663 stio2 = stab_io(stab2) = stio_new();
1664 else if (stio2->ifp)
1665 do_close(stab2,FALSE);
1667 domain = (int)str_gnum(st[++sp]);
1668 type = (int)str_gnum(st[++sp]);
1669 protocol = (int)str_gnum(st[++sp]);
1671 taintproper("Insecure dependency in socketpair");
1674 if (socketpair(domain,type,protocol,fd) < 0)
1677 fatal("Socketpair unimplemented");
1679 stio1->ifp = fdopen(fd[0], "r");
1680 stio1->ofp = fdopen(fd[0], "w");
1682 stio2->ifp = fdopen(fd[1], "r");
1683 stio2->ofp = fdopen(fd[1], "w");
1692 do_gpwent(which,gimme,arglast)
1698 register ARRAY *ary = stack;
1699 register int sp = arglast[0];
1700 register char **elem;
1702 struct passwd *getpwnam();
1703 struct passwd *getpwuid();
1704 struct passwd *getpwent();
1705 struct passwd *pwent;
1708 if (gimme != G_ARRAY) {
1709 astore(ary, ++sp, str_static(&str_undef));
1713 if (which == O_GPWNAM) {
1714 char *name = str_get(ary->ary_array[sp+1]);
1716 pwent = getpwnam(name);
1718 else if (which == O_GPWUID) {
1719 int uid = (int)str_gnum(ary->ary_array[sp+1]);
1721 pwent = getpwuid(uid);
1727 (void)astore(ary, ++sp, str = str_static(&str_no));
1728 str_set(str, pwent->pw_name);
1729 (void)astore(ary, ++sp, str = str_static(&str_no));
1730 str_set(str, pwent->pw_passwd);
1731 (void)astore(ary, ++sp, str = str_static(&str_no));
1732 str_numset(str, (double)pwent->pw_uid);
1733 (void)astore(ary, ++sp, str = str_static(&str_no));
1734 str_numset(str, (double)pwent->pw_gid);
1735 (void)astore(ary, ++sp, str = str_static(&str_no));
1737 str_numset(str, (double)pwent->pw_change);
1740 str_numset(str, (double)pwent->pw_quota);
1743 str_set(str, pwent->pw_age);
1747 (void)astore(ary, ++sp, str = str_static(&str_no));
1749 str_set(str,pwent->pw_class);
1751 str_set(str, pwent->pw_comment);
1753 (void)astore(ary, ++sp, str = str_static(&str_no));
1754 str_set(str, pwent->pw_gecos);
1755 (void)astore(ary, ++sp, str = str_static(&str_no));
1756 str_set(str, pwent->pw_dir);
1757 (void)astore(ary, ++sp, str = str_static(&str_no));
1758 str_set(str, pwent->pw_shell);
1760 (void)astore(ary, ++sp, str = str_static(&str_no));
1761 str_numset(str, (double)pwent->pw_expire);
1767 fatal("password routines not implemented");
1772 do_ggrent(which,gimme,arglast)
1778 register ARRAY *ary = stack;
1779 register int sp = arglast[0];
1780 register char **elem;
1782 struct group *getgrnam();
1783 struct group *getgrgid();
1784 struct group *getgrent();
1785 struct group *grent;
1788 if (gimme != G_ARRAY) {
1789 astore(ary, ++sp, str_static(&str_undef));
1793 if (which == O_GGRNAM) {
1794 char *name = str_get(ary->ary_array[sp+1]);
1796 grent = getgrnam(name);
1798 else if (which == O_GGRGID) {
1799 int gid = (int)str_gnum(ary->ary_array[sp+1]);
1801 grent = getgrgid(gid);
1807 (void)astore(ary, ++sp, str = str_static(&str_no));
1808 str_set(str, grent->gr_name);
1809 (void)astore(ary, ++sp, str = str_static(&str_no));
1810 str_set(str, grent->gr_passwd);
1811 (void)astore(ary, ++sp, str = str_static(&str_no));
1812 str_numset(str, (double)grent->gr_gid);
1813 (void)astore(ary, ++sp, str = str_static(&str_no));
1814 for (elem = grent->gr_mem; *elem; elem++) {
1815 str_cat(str, *elem);
1817 str_ncat(str," ",1);
1823 fatal("group routines not implemented");
1828 do_dirop(optype,stab,gimme,arglast)
1834 #if defined(DIRENT) && defined(READDIR)
1835 register ARRAY *ary = stack;
1836 register STR **st = ary->ary_array;
1837 register int sp = arglast[1];
1838 register STIO *stio;
1843 struct DIRENT *readdir();
1844 register struct DIRENT *dp;
1848 if (!(stio = stab_io(stab)))
1849 stio = stab_io(stab) = stio_new();
1850 if (!stio->dirp && optype != O_OPENDIR)
1856 closedir(stio->dirp);
1857 if (!(stio->dirp = opendir(str_get(st[sp+1]))))
1861 if (gimme == G_ARRAY) {
1863 while (dp = readdir(stio->dirp)) {
1865 (void)astore(ary,++sp,
1866 str_2static(str_make(dp->d_name,dp->d_namlen)));
1868 (void)astore(ary,++sp,
1869 str_2static(str_make(dp->d_name,0)));
1874 if (!(dp = readdir(stio->dirp)))
1876 st[sp] = str_static(&str_undef);
1878 str_nset(st[sp], dp->d_name, dp->d_namlen);
1880 str_set(st[sp], dp->d_name);
1885 st[sp] = str_static(&str_undef);
1886 str_numset(st[sp], (double)telldir(stio->dirp));
1889 st[sp] = str_static(&str_undef);
1890 along = (long)str_gnum(st[sp+1]);
1891 (void)seekdir(stio->dirp,along);
1894 st[sp] = str_static(&str_undef);
1895 (void)rewinddir(stio->dirp);
1898 st[sp] = str_static(&str_undef);
1899 (void)closedir(stio->dirp);
1906 st[sp] = &str_undef;
1910 fatal("Unimplemented directory operation");
1918 register STR **st = stack->ary_array;
1919 register int sp = arglast[1];
1920 register int items = arglast[2] - sp;
1923 register int tot = 0;
1927 for (st += ++sp; items--; st++)
1928 tainted |= (*st)->str_tainted;
1929 st = stack->ary_array;
1931 items = arglast[2] - sp;
1936 taintproper("Insecure dependency in chmod");
1940 val = (int)str_gnum(st[++sp]);
1942 if (chmod(str_get(st[++sp]),val))
1949 taintproper("Insecure dependency in chown");
1954 val = (int)str_gnum(st[++sp]);
1955 val2 = (int)str_gnum(st[++sp]);
1957 if (chown(str_get(st[++sp]),val,val2))
1964 taintproper("Insecure dependency in kill");
1968 s = str_get(st[++sp]);
1970 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1972 if (!(val = whichsig(s)))
1973 fatal("Unrecognized signal name \"%s\"",s);
1976 val = (int)str_gnum(st[sp]);
1980 int proc = (int)str_gnum(st[++sp]);
1982 if (killpg(proc,val)) /* BSD */
1984 if (kill(-proc,val)) /* SYSV */
1991 if (kill((int)(str_gnum(st[++sp])),val))
1999 taintproper("Insecure dependency in unlink");
2003 s = str_get(st[++sp]);
2004 if (euid || unsafe) {
2008 else { /* don't let root wipe out directories without -U */
2010 if (lstat(s,&statbuf) < 0 ||
2012 if (stat(s,&statbuf) < 0 ||
2014 (statbuf.st_mode & S_IFMT) == S_IFDIR )
2025 taintproper("Insecure dependency in utime");
2029 struct utimbuf utbuf;
2037 Zero(&utbuf, sizeof utbuf, char);
2038 utbuf.actime = (long)str_gnum(st[++sp]); /* time accessed */
2039 utbuf.modtime = (long)str_gnum(st[++sp]); /* time modified */
2044 if (utime(str_get(st[++sp]),&utbuf))
2056 /* Do the permissions allow some operation? Assumes statcache already set. */
2059 cando(bit, effective, statbufp)
2062 register struct stat *statbufp;
2064 if ((effective ? euid : uid) == 0) { /* root is special */
2065 if (bit == S_IEXEC) {
2066 if (statbufp->st_mode & 0111 ||
2067 (statbufp->st_mode & S_IFMT) == S_IFDIR )
2071 return TRUE; /* root reads and writes anything */
2074 if (statbufp->st_uid == (effective ? euid : uid) ) {
2075 if (statbufp->st_mode & bit)
2076 return TRUE; /* ok as "user" */
2078 else if (ingroup((int)statbufp->st_gid,effective)) {
2079 if (statbufp->st_mode & bit >> 3)
2080 return TRUE; /* ok as "group" */
2082 else if (statbufp->st_mode & bit >> 6)
2083 return TRUE; /* ok as "other" */
2088 ingroup(testgid,effective)
2092 if (testgid == (effective ? egid : gid))
2099 GIDTYPE gary[NGROUPS];
2102 anum = getgroups(NGROUPS,gary);
2104 if (gary[anum] == testgid)