1 /* $Header: doio.c,v 3.0.1.10 90/08/13 22:14:29 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.10 90/08/13 22:14:29 lwall
10 * patch28: close-on-exec problems on dup'ed file descriptors
11 * patch28: F_FREESP wasn't implemented the way I thought
13 * Revision 3.0.1.9 90/08/09 02:56:19 lwall
14 * patch19: various MSDOS and OS/2 patches folded in
15 * patch19: prints now check error status better
16 * patch19: printing a list with null elements only printed front of list
17 * patch19: on machines with vfork child would allocate memory in parent
18 * patch19: getsockname and getpeername gave bogus warning on error
19 * patch19: MACH doesn't have seekdir or telldir
21 * Revision 3.0.1.8 90/03/27 15:44:02 lwall
22 * patch16: MSDOS support
23 * patch16: support for machines that can't cast negative floats to unsigned ints
24 * patch16: system() can lose arguments passed to shell scripts on SysV machines
26 * Revision 3.0.1.7 90/03/14 12:26:24 lwall
27 * patch15: commands involving execs could cause malloc arena corruption
29 * Revision 3.0.1.6 90/03/12 16:30:07 lwall
30 * patch13: system 'FOO=bar command' didn't invoke sh as it should
32 * Revision 3.0.1.5 90/02/28 17:01:36 lwall
33 * patch9: open(FOO,"$filename\0") will now protect trailing spaces in filename
34 * patch9: removed obsolete checks to avoid opening block devices
35 * patch9: removed references to acusec and modusec that some utime.h's have
36 * patch9: added pipe function
38 * Revision 3.0.1.4 89/12/21 19:55:10 lwall
39 * patch7: select now works on big-endian machines
40 * patch7: errno may now be a macro with an lvalue
41 * patch7: ANSI strerror() is now supported
42 * patch7: Configure now detects DG/UX thingies like [sg]etpgrp2 and utime.h
44 * Revision 3.0.1.3 89/11/17 15:13:06 lwall
45 * patch5: some systems have symlink() but not lstat()
46 * patch5: some systems have dirent.h but not readdir()
48 * Revision 3.0.1.2 89/11/11 04:25:51 lwall
49 * patch2: orthogonalized the file modes some so we can have <& +<& etc.
50 * patch2: do_open() now detects sockets passed to process from parent
51 * patch2: fd's above 2 are now closed on exec
52 * patch2: csh code can now use csh from other than /bin
53 * patch2: getsockopt, get{sock,peer}name didn't define result properly
54 * patch2: warn("shutdown") was replicated
55 * patch2: gethostbyname was misdeclared
56 * patch2: telldir() is sometimes a macro
58 * Revision 3.0.1.1 89/10/26 23:10:05 lwall
59 * patch1: Configure now checks for BSD shadow passwords
61 * Revision 3.0 89/10/18 15:10:54 lwall
70 #include <sys/socket.h>
74 #if defined(SELECT) && (defined(M_UNIX) || defined(M_XENIX))
75 #include <sys/select.h>
92 do_open(stab,name,len)
98 register STIO *stio = stab_io(stab);
99 char *myname = savestr(name);
103 char mode[3]; /* stdio file mode ("r\0" or "r+\0") */
106 forkprocess = 1; /* assume true if no fork */
107 while (len && isspace(name[len-1]))
110 stio = stab_io(stab) = stio_new();
111 else if (stio->ifp) {
112 fd = fileno(stio->ifp);
113 if (stio->type == '|')
114 result = mypclose(stio->ifp);
115 else if (stio->ifp != stio->ofp) {
118 result = fclose(stio->ifp);
120 else if (stio->type != '-')
121 result = fclose(stio->ifp);
124 if (result == EOF && fd > 2)
125 fprintf(stderr,"Warning: unable to close filehandle %s properly.\n",
127 stio->ofp = stio->ifp = Nullfp;
129 if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */
140 for (name++; isspace(*name); name++) ;
143 taintproper("Insecure dependency in piped open");
145 fp = mypopen(name,"w");
148 else if (*name == '>') {
150 taintproper("Insecure dependency in open");
154 mode[0] = stio->type = 'a';
163 while (isspace(*name))
168 stab = stabent(name,FALSE);
169 if (!stab || !stab_io(stab))
171 if (stab_io(stab) && stab_io(stab)->ifp) {
172 fd = fileno(stab_io(stab)->ifp);
173 if (stab_io(stab)->type == 's')
179 fp = fdopen(dup(fd),mode);
182 while (isspace(*name))
184 if (strEQ(name,"-")) {
189 fp = fopen(name,mode);
197 while (isspace(*name))
201 if (strEQ(name,"-")) {
206 fp = fopen(name,mode);
208 else if (name[len-1] == '|') {
211 taintproper("Insecure dependency in piped open");
214 while (len && isspace(name[len-1]))
216 for (; isspace(*name); name++) ;
217 fp = mypopen(name,"r");
222 for (; isspace(*name); name++) ;
223 if (strEQ(name,"-")) {
228 fp = fopen(name,"r");
235 stio->type != '|' && stio->type != '-') {
236 if (fstat(fileno(fp),&statbuf) < 0) {
240 result = (statbuf.st_mode & S_IFMT);
242 if (result == S_IFSOCK || result == 0)
243 stio->type = 's'; /* in case a socket was passed in to us */
246 #if defined(FCNTL) && defined(F_SETFD)
248 fcntl(fd,F_SETFD,fd >= 3);
252 if (stio->type != 's')
255 stio->ofp = fdopen(fileno(fp),"w");
266 int filemode,fileuid,filegid;
268 while (alen(stab_xarray(stab)) >= 0) {
269 str = ashift(stab_xarray(stab));
270 str_sset(stab_val(stab),str);
271 STABSET(stab_val(stab));
272 oldname = str_get(stab_val(stab));
273 if (do_open(stab,oldname,stab_val(stab)->str_cur)) {
276 taintproper("Insecure dependency in inplace open");
278 filemode = statbuf.st_mode;
279 fileuid = statbuf.st_uid;
280 filegid = statbuf.st_gid;
283 add_suffix(str,inplace);
285 str_cat(str,inplace);
289 (void)rename(oldname,str->str_ptr);
291 do_close(stab,FALSE);
292 (void)unlink(str->str_ptr);
293 (void)rename(oldname,str->str_ptr);
294 do_open(stab,str->str_ptr,stab_val(stab)->str_cur);
297 (void)UNLINK(str->str_ptr);
298 (void)link(oldname,str->str_ptr);
299 (void)UNLINK(oldname);
304 (void)UNLINK(oldname);
306 fatal("Can't do inplace edit without backup");
311 str_cat(str,oldname);
312 errno = 0; /* in case sprintf set errno */
313 if (!do_open(argvoutstab,str->str_ptr,str->str_cur))
314 fatal("Can't do inplace edit");
315 defoutstab = argvoutstab;
317 (void)fchmod(fileno(stab_io(argvoutstab)->ifp),filemode);
319 (void)chmod(oldname,filemode);
322 (void)fchown(fileno(stab_io(argvoutstab)->ifp),fileuid,filegid);
325 (void)chown(oldname,fileuid,filegid);
330 return stab_io(stab)->ifp;
333 fprintf(stderr,"Can't open %s\n",str_get(str));
337 (void)do_close(argvoutstab,FALSE);
338 defoutstab = stabent("STDOUT",TRUE);
345 do_pipe(str, rstab, wstab)
350 register STIO *rstio;
351 register STIO *wstio;
359 rstio = stab_io(rstab);
360 wstio = stab_io(wstab);
363 rstio = stab_io(rstab) = stio_new();
365 do_close(rstab,FALSE);
367 wstio = stab_io(wstab) = stio_new();
369 do_close(wstab,FALSE);
373 rstio->ifp = fdopen(fd[0], "r");
374 wstio->ofp = fdopen(fd[1], "w");
375 wstio->ifp = wstio->ofp;
379 str_sset(str,&str_yes);
383 str_sset(str,&str_undef);
389 do_close(stab,explicit)
394 register STIO *stio = stab_io(stab);
397 if (!stio) { /* never opened */
398 if (dowarn && explicit)
399 warn("Close on unopened file <%s>",stab_name(stab));
403 if (stio->type == '|') {
404 status = mypclose(stio->ifp);
405 retval = (status >= 0);
406 statusvalue = (unsigned short)status & 0xffff;
408 else if (stio->type == '-')
411 if (stio->ofp && stio->ofp != stio->ifp) /* a socket */
413 retval = (fclose(stio->ifp) != EOF);
415 stio->ofp = stio->ifp = Nullfp;
430 if (!stab) { /* eof() */
432 stio = stab_io(argvstab);
437 stio = stab_io(stab);
444 #ifdef STDSTDIO /* (the code works without this) */
445 if (stio->ifp->_cnt > 0) /* cheat a little, since */
446 return FALSE; /* this is the most usual case */
449 ch = getc(stio->ifp);
451 (void)ungetc(ch, stio->ifp);
454 if (!stab) { /* not necessarily a real EOF yet? */
455 if (!nextargv(argvstab)) /* get another fp handy */
459 return TRUE; /* normal fp, definitely end of file */
473 stio = stab_io(stab);
474 if (!stio || !stio->ifp)
478 (void)fseek (stio->ifp, 0L, 2); /* ultrix 1.2 workaround */
480 return ftell(stio->ifp);
484 warn("tell() on unopened file");
489 do_seek(stab, pos, whence)
499 stio = stab_io(stab);
500 if (!stio || !stio->ifp)
504 (void)fseek (stio->ifp, 0L, 2); /* ultrix 1.2 workaround */
506 return fseek(stio->ifp, pos, whence) >= 0;
510 warn("seek() on unopened file");
515 do_ctl(optype,stab,func,argstr)
525 if (!stab || !argstr)
527 stio = stab_io(stab);
531 if (argstr->str_pok || !argstr->str_nok) {
532 if (!argstr->str_pok)
537 #define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
541 retval = IOCPARM_LEN(func); /* on BSDish systes we're safe */
543 retval = 256; /* otherwise guess at what's safe */
545 if (argstr->str_cur < retval) {
546 Str_Grow(argstr,retval+1);
547 argstr->str_cur = retval;
551 s[argstr->str_cur] = 17; /* a little sanity check here */
554 retval = (int)str_gnum(argstr);
555 s = (char*)retval; /* ouch */
559 if (optype == O_IOCTL)
560 retval = ioctl(fileno(stio->ifp), func, s);
563 retval = fcntl(fileno(stio->ifp), func, s);
565 fatal("fcntl is not implemented");
571 if (argstr->str_pok) {
572 if (s[argstr->str_cur] != 17)
573 fatal("Return value overflowed string");
574 s[argstr->str_cur] = 0; /* put our null back */
580 do_stat(str,arg,gimme,arglast)
586 register ARRAY *ary = stack;
587 register int sp = arglast[0] + 1;
591 if ((arg[1].arg_type & A_MASK) == A_WORD) {
592 tmpstab = arg[1].arg_ptr.arg_stab;
593 if (tmpstab != defstab) {
595 str_set(statname,"");
596 if (!stab_io(tmpstab) ||
597 fstat(fileno(stab_io(tmpstab)->ifp),&statcache) < 0) {
603 str_sset(statname,ary->ary_array[sp]);
606 if (arg->arg_type == O_LSTAT)
607 i = lstat(str_get(statname),&statcache);
610 i = stat(str_get(statname),&statcache);
615 if (gimme != G_ARRAY) {
617 str_sset(str,&str_yes);
619 str_sset(str,&str_undef);
621 ary->ary_array[sp] = str;
627 (void)astore(ary,++sp,
628 str_2static(str_nmake((double)statcache.st_dev)));
629 (void)astore(ary,++sp,
630 str_2static(str_nmake((double)statcache.st_ino)));
631 (void)astore(ary,++sp,
632 str_2static(str_nmake((double)statcache.st_mode)));
633 (void)astore(ary,++sp,
634 str_2static(str_nmake((double)statcache.st_nlink)));
635 (void)astore(ary,++sp,
636 str_2static(str_nmake((double)statcache.st_uid)));
637 (void)astore(ary,++sp,
638 str_2static(str_nmake((double)statcache.st_gid)));
639 (void)astore(ary,++sp,
640 str_2static(str_nmake((double)statcache.st_rdev)));
641 (void)astore(ary,++sp,
642 str_2static(str_nmake((double)statcache.st_size)));
643 (void)astore(ary,++sp,
644 str_2static(str_nmake((double)statcache.st_atime)));
645 (void)astore(ary,++sp,
646 str_2static(str_nmake((double)statcache.st_mtime)));
647 (void)astore(ary,++sp,
648 str_2static(str_nmake((double)statcache.st_ctime)));
650 (void)astore(ary,++sp,
651 str_2static(str_nmake((double)statcache.st_blksize)));
652 (void)astore(ary,++sp,
653 str_2static(str_nmake((double)statcache.st_blocks)));
655 (void)astore(ary,++sp,
656 str_2static(str_make("",0)));
657 (void)astore(ary,++sp,
658 str_2static(str_make("",0)));
661 (void)astore(ary,++sp,str_nmake(0.0));
667 #if !defined(TRUNCATE) && !defined(CHSIZE) && defined(F_FREESP)
668 /* code courtesy of Pim Zandbergen */
671 int chsize(fd, length)
672 int fd; /* file descriptor */
673 off_t length; /* length to set file to */
679 if (fstat(fd, &filebuf) < 0)
682 if (filebuf.st_size < length) {
684 /* extend file length */
686 if ((lseek(fd, (length - 1), 0)) < 0)
689 /* write a "0" byte */
691 if ((write(fd, "", 1)) != 1)
695 /* truncate length */
700 fl.l_type = F_WRLCK; /* write lock on file space */
703 * This relies on the UNDOCUMENTED F_FREESP argument to
704 * fcntl(2), which truncates the file so that it ends at the
705 * position indicated by fl.l_start.
707 * Will minor miracles never cease?
710 if (fcntl(fd, F_FREESP, &fl) < 0)
717 #endif /* F_FREESP */
720 do_truncate(str,arg,gimme,arglast)
726 register ARRAY *ary = stack;
727 register int sp = arglast[0] + 1;
728 off_t len = (off_t)str_gnum(ary->ary_array[sp+1]);
732 #if defined(TRUNCATE) || defined(CHSIZE)
734 if ((arg[1].arg_type & A_MASK) == A_WORD) {
735 tmpstab = arg[1].arg_ptr.arg_stab;
736 if (!stab_io(tmpstab) ||
737 ftruncate(fileno(stab_io(tmpstab)->ifp), len) < 0)
740 else if (truncate(str_get(ary->ary_array[sp]), len) < 0)
743 if ((arg[1].arg_type & A_MASK) == A_WORD) {
744 tmpstab = arg[1].arg_ptr.arg_stab;
745 if (!stab_io(tmpstab) ||
746 chsize(fileno(stab_io(tmpstab)->ifp), len) < 0)
752 if ((tmpfd = open(str_get(ary->ary_array[sp]), 0)) < 0)
755 if (chsize(tmpfd, len) < 0)
763 str_sset(str,&str_yes);
765 str_sset(str,&str_undef);
767 ary->ary_array[sp] = str;
770 fatal("truncate not implemented");
775 looks_like_number(str)
784 send = s + str->str_cur;
789 if (*s == '+' || *s == '-')
797 else if (s == str->str_ptr)
803 if (*s == 'e' || *s == 'E') {
805 if (*s == '+' || *s == '-')
826 warn("print to unopened file");
832 ((str->str_nok && str->str_u.str_nval != 0.0)
833 || (looks_like_number(str) && str_gnum(str) != 0.0) ) ) {
834 fprintf(fp, ofmt, str->str_u.str_nval);
839 if (*tmps == 'S' && tmps[1] == 't' && tmps[2] == 'a' && tmps[3] == 'b'
840 && str->str_cur == sizeof(STBP) && strlen(tmps) < str->str_cur) {
841 tmps = stab_name(((STAB*)str)); /* a stab value, be nice */
842 str = ((STAB*)str)->str_magic;
845 if (str->str_cur && (fwrite(tmps,1,str->str_cur,fp) == 0 || ferror(fp)))
852 do_aprint(arg,fp,arglast)
857 register STR **st = stack->ary_array;
858 register int sp = arglast[1];
860 register int items = arglast[2] - sp;
864 warn("print to unopened file");
868 if (arg->arg_type == O_PRTF) {
869 do_sprintf(arg->arg_ptr.arg_str,items,st);
870 retval = do_print(arg->arg_ptr.arg_str,fp);
873 retval = (items <= 0);
874 for (; items > 0; items--,st++) {
875 if (retval && ofslen) {
876 if (fwrite(ofs, 1, ofslen, fp) == 0 || ferror(fp)) {
881 if (!(retval = do_print(*st, fp)))
884 if (retval && orslen)
885 if (fwrite(ors, 1, orslen, fp) == 0 || ferror(fp))
898 if (arg[1].arg_type & A_DONT) {
899 stio = stab_io(arg[1].arg_ptr.arg_stab);
900 if (stio && stio->ifp) {
901 statstab = arg[1].arg_ptr.arg_stab;
902 str_set(statname,"");
903 return fstat(fileno(stio->ifp), &statcache);
906 if (arg[1].arg_ptr.arg_stab == defstab)
909 warn("Stat on unopened file <%s>",
910 stab_name(arg[1].arg_ptr.arg_stab));
912 str_set(statname,"");
918 str_sset(statname,str);
919 return stat(str_get(str),&statcache);
935 if (arg[1].arg_type & A_DONT) {
936 if (arg[1].arg_ptr.arg_stab == defstab) {
938 stio = stab_io(statstab);
941 goto really_filename;
945 statstab = arg[1].arg_ptr.arg_stab;
946 str_set(statname,"");
947 stio = stab_io(statstab);
949 if (stio && stio->ifp) {
951 fstat(fileno(stio->ifp),&statcache);
952 if (stio->ifp->_cnt <= 0) {
955 (void)ungetc(i,stio->ifp);
957 if (stio->ifp->_cnt <= 0) /* null file is anything */
959 len = stio->ifp->_cnt + (stio->ifp->_ptr - stio->ifp->_base);
960 s = stio->ifp->_base;
962 fatal("-T and -B not implemented on filehandles\n");
967 warn("Test on unopened file <%s>",
968 stab_name(arg[1].arg_ptr.arg_stab));
974 str_sset(statname,str);
976 i = open(str_get(str),0);
980 len = read(i,tbuf,512);
981 if (len <= 0) /* null file is anything */
987 /* now scan s to look for textiness */
989 for (i = 0; i < len; i++,s++) {
990 if (!*s) { /* null never allowed in text */
997 *s != '\n' && *s != '\r' && *s != '\b' &&
998 *s != '\t' && *s != '\f' && *s != 27)
1002 if ((odd * 10 > len) == (arg->arg_type == O_FTTEXT)) /* allow 10% odd */
1009 do_aexec(really,arglast)
1013 register STR **st = stack->ary_array;
1014 register int sp = arglast[1];
1015 register int items = arglast[2] - sp;
1021 New(401,argv, items+1, char*);
1023 for (st += ++sp; items > 0; items--,st++) {
1025 *a++ = str_get(*st);
1031 if (*argv[0] != '/') /* will execvp use PATH? */
1032 taintenv(); /* testing IFS here is overkill, probably */
1034 if (really && *(tmps = str_get(really)))
1037 execvp(argv[0],argv);
1043 static char **Argv = Null(char **);
1044 static char *Cmd = Nullch;
1051 Argv = Null(char **);
1069 taintproper("Insecure dependency in exec");
1072 /* save an extra exec if possible */
1075 if (strnEQ(cmd,cshname,cshlen) && strnEQ(cmd+cshlen," -c",3)) {
1091 if (s[-1] == '\'') {
1093 execl(cshname,"csh", flags,ncmd,(char*)0);
1101 /* see if there are shell metacharacters in it */
1103 for (s = cmd; *s && isalpha(*s); s++) ; /* catch VAR=val gizmo */
1106 for (s = cmd; *s; s++) {
1107 if (*s != ' ' && !isalpha(*s) && index("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1108 if (*s == '\n' && !s[1]) {
1113 execl("/bin/sh","sh","-c",cmd,(char*)0);
1117 New(402,Argv, (s - cmd) / 2 + 2, char*);
1118 Cmd = nsavestr(cmd, s-cmd);
1120 for (s = Cmd; *s;) {
1121 while (*s && isspace(*s)) s++;
1124 while (*s && !isspace(*s)) s++;
1130 execvp(Argv[0],Argv);
1131 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1142 do_socket(stab, arglast)
1146 register STR **st = stack->ary_array;
1147 register int sp = arglast[1];
1148 register STIO *stio;
1149 int domain, type, protocol, fd;
1154 stio = stab_io(stab);
1156 stio = stab_io(stab) = stio_new();
1158 do_close(stab,FALSE);
1160 domain = (int)str_gnum(st[++sp]);
1161 type = (int)str_gnum(st[++sp]);
1162 protocol = (int)str_gnum(st[++sp]);
1164 taintproper("Insecure dependency in socket");
1166 fd = socket(domain,type,protocol);
1169 stio->ifp = fdopen(fd, "r"); /* stdio gets confused about sockets */
1170 stio->ofp = fdopen(fd, "w");
1177 do_bind(stab, arglast)
1181 register STR **st = stack->ary_array;
1182 register int sp = arglast[1];
1183 register STIO *stio;
1189 stio = stab_io(stab);
1190 if (!stio || !stio->ifp)
1193 addr = str_get(st[++sp]);
1195 taintproper("Insecure dependency in bind");
1197 return bind(fileno(stio->ifp), addr, st[sp]->str_cur) >= 0;
1201 warn("bind() on closed fd");
1207 do_connect(stab, arglast)
1211 register STR **st = stack->ary_array;
1212 register int sp = arglast[1];
1213 register STIO *stio;
1219 stio = stab_io(stab);
1220 if (!stio || !stio->ifp)
1223 addr = str_get(st[++sp]);
1225 taintproper("Insecure dependency in connect");
1227 return connect(fileno(stio->ifp), addr, st[sp]->str_cur) >= 0;
1231 warn("connect() on closed fd");
1237 do_listen(stab, arglast)
1241 register STR **st = stack->ary_array;
1242 register int sp = arglast[1];
1243 register STIO *stio;
1249 stio = stab_io(stab);
1250 if (!stio || !stio->ifp)
1253 backlog = (int)str_gnum(st[++sp]);
1254 return listen(fileno(stio->ifp), backlog) >= 0;
1258 warn("listen() on closed fd");
1263 do_accept(str, nstab, gstab)
1268 register STIO *nstio;
1269 register STIO *gstio;
1270 int len = sizeof buf;
1278 gstio = stab_io(gstab);
1279 nstio = stab_io(nstab);
1281 if (!gstio || !gstio->ifp)
1284 nstio = stab_io(nstab) = stio_new();
1285 else if (nstio->ifp)
1286 do_close(nstab,FALSE);
1288 fd = accept(fileno(gstio->ifp),buf,&len);
1291 nstio->ifp = fdopen(fd, "r");
1292 nstio->ofp = fdopen(fd, "w");
1295 str_nset(str, buf, len);
1300 warn("accept() on closed fd");
1302 str_sset(str,&str_undef);
1307 do_shutdown(stab, arglast)
1311 register STR **st = stack->ary_array;
1312 register int sp = arglast[1];
1313 register STIO *stio;
1319 stio = stab_io(stab);
1320 if (!stio || !stio->ifp)
1323 how = (int)str_gnum(st[++sp]);
1324 return shutdown(fileno(stio->ifp), how) >= 0;
1328 warn("shutdown() on closed fd");
1334 do_sopt(optype, stab, arglast)
1339 register STR **st = stack->ary_array;
1340 register int sp = arglast[1];
1341 register STIO *stio;
1349 stio = stab_io(stab);
1350 if (!stio || !stio->ifp)
1353 fd = fileno(stio->ifp);
1354 lvl = (int)str_gnum(st[sp+1]);
1355 optname = (int)str_gnum(st[sp+2]);
1358 st[sp] = str_2static(str_new(257));
1359 st[sp]->str_cur = 256;
1360 st[sp]->str_pok = 1;
1361 if (getsockopt(fd, lvl, optname, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
1366 if (setsockopt(fd, lvl, optname, st[sp]->str_ptr, st[sp]->str_cur) < 0)
1376 warn("[gs]etsockopt() on closed fd");
1377 st[sp] = &str_undef;
1383 do_getsockname(optype, stab, arglast)
1388 register STR **st = stack->ary_array;
1389 register int sp = arglast[1];
1390 register STIO *stio;
1396 stio = stab_io(stab);
1397 if (!stio || !stio->ifp)
1400 st[sp] = str_2static(str_new(257));
1401 st[sp]->str_cur = 256;
1402 st[sp]->str_pok = 1;
1403 fd = fileno(stio->ifp);
1406 if (getsockname(fd, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
1410 if (getpeername(fd, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
1419 warn("get{sock,peer}name() on closed fd");
1421 st[sp] = &str_undef;
1427 do_ghent(which,gimme,arglast)
1432 register ARRAY *ary = stack;
1433 register int sp = arglast[0];
1434 register char **elem;
1436 struct hostent *gethostbyname();
1437 struct hostent *gethostbyaddr();
1439 struct hostent *gethostent();
1441 struct hostent *hent;
1444 if (gimme != G_ARRAY) {
1445 astore(ary, ++sp, str_static(&str_undef));
1449 if (which == O_GHBYNAME) {
1450 char *name = str_get(ary->ary_array[sp+1]);
1452 hent = gethostbyname(name);
1454 else if (which == O_GHBYADDR) {
1455 STR *addrstr = ary->ary_array[sp+1];
1456 int addrtype = (int)str_gnum(ary->ary_array[sp+2]);
1457 char *addr = str_get(addrstr);
1459 hent = gethostbyaddr(addr,addrstr->str_cur,addrtype);
1463 hent = gethostent();
1465 fatal("gethostent not implemented");
1469 (void)astore(ary, ++sp, str = str_static(&str_no));
1470 str_set(str, hent->h_name);
1471 (void)astore(ary, ++sp, str = str_static(&str_no));
1472 for (elem = hent->h_aliases; *elem; elem++) {
1473 str_cat(str, *elem);
1475 str_ncat(str," ",1);
1477 (void)astore(ary, ++sp, str = str_static(&str_no));
1478 str_numset(str, (double)hent->h_addrtype);
1479 (void)astore(ary, ++sp, str = str_static(&str_no));
1480 len = hent->h_length;
1481 str_numset(str, (double)len);
1483 for (elem = hent->h_addr_list; *elem; elem++) {
1484 (void)astore(ary, ++sp, str = str_static(&str_no));
1485 str_nset(str, *elem, len);
1488 (void)astore(ary, ++sp, str = str_static(&str_no));
1489 str_nset(str, hent->h_addr, len);
1494 (void)astore(ary, ++sp, str_static(&str_no));
1502 do_gnent(which,gimme,arglast)
1507 register ARRAY *ary = stack;
1508 register int sp = arglast[0];
1509 register char **elem;
1511 struct netent *getnetbyname();
1512 struct netent *getnetbyaddr();
1513 struct netent *getnetent();
1514 struct netent *nent;
1516 if (gimme != G_ARRAY) {
1517 astore(ary, ++sp, str_static(&str_undef));
1521 if (which == O_GNBYNAME) {
1522 char *name = str_get(ary->ary_array[sp+1]);
1524 nent = getnetbyname(name);
1526 else if (which == O_GNBYADDR) {
1527 STR *addrstr = ary->ary_array[sp+1];
1528 int addrtype = (int)str_gnum(ary->ary_array[sp+2]);
1529 char *addr = str_get(addrstr);
1531 nent = getnetbyaddr(addr,addrtype);
1538 (void)astore(ary, ++sp, str = str_static(&str_no));
1539 str_set(str, nent->n_name);
1540 (void)astore(ary, ++sp, str = str_static(&str_no));
1541 for (elem = nent->n_aliases; *elem; elem++) {
1542 str_cat(str, *elem);
1544 str_ncat(str," ",1);
1546 (void)astore(ary, ++sp, str = str_static(&str_no));
1547 str_numset(str, (double)nent->n_addrtype);
1548 (void)astore(ary, ++sp, str = str_static(&str_no));
1549 str_numset(str, (double)nent->n_net);
1553 (void)astore(ary, ++sp, str_static(&str_no));
1561 do_gpent(which,gimme,arglast)
1566 register ARRAY *ary = stack;
1567 register int sp = arglast[0];
1568 register char **elem;
1570 struct protoent *getprotobyname();
1571 struct protoent *getprotobynumber();
1572 struct protoent *getprotoent();
1573 struct protoent *pent;
1575 if (gimme != G_ARRAY) {
1576 astore(ary, ++sp, str_static(&str_undef));
1580 if (which == O_GPBYNAME) {
1581 char *name = str_get(ary->ary_array[sp+1]);
1583 pent = getprotobyname(name);
1585 else if (which == O_GPBYNUMBER) {
1586 int proto = (int)str_gnum(ary->ary_array[sp+1]);
1588 pent = getprotobynumber(proto);
1591 pent = getprotoent();
1595 (void)astore(ary, ++sp, str = str_static(&str_no));
1596 str_set(str, pent->p_name);
1597 (void)astore(ary, ++sp, str = str_static(&str_no));
1598 for (elem = pent->p_aliases; *elem; elem++) {
1599 str_cat(str, *elem);
1601 str_ncat(str," ",1);
1603 (void)astore(ary, ++sp, str = str_static(&str_no));
1604 str_numset(str, (double)pent->p_proto);
1608 (void)astore(ary, ++sp, str_static(&str_no));
1616 do_gsent(which,gimme,arglast)
1621 register ARRAY *ary = stack;
1622 register int sp = arglast[0];
1623 register char **elem;
1625 struct servent *getservbyname();
1626 struct servent *getservbynumber();
1627 struct servent *getservent();
1628 struct servent *sent;
1630 if (gimme != G_ARRAY) {
1631 astore(ary, ++sp, str_static(&str_undef));
1635 if (which == O_GSBYNAME) {
1636 char *name = str_get(ary->ary_array[sp+1]);
1637 char *proto = str_get(ary->ary_array[sp+2]);
1639 if (proto && !*proto)
1642 sent = getservbyname(name,proto);
1644 else if (which == O_GSBYPORT) {
1645 int port = (int)str_gnum(ary->ary_array[sp+1]);
1646 char *proto = str_get(ary->ary_array[sp+2]);
1648 sent = getservbyport(port,proto);
1651 sent = getservent();
1654 (void)astore(ary, ++sp, str = str_static(&str_no));
1655 str_set(str, sent->s_name);
1656 (void)astore(ary, ++sp, str = str_static(&str_no));
1657 for (elem = sent->s_aliases; *elem; elem++) {
1658 str_cat(str, *elem);
1660 str_ncat(str," ",1);
1662 (void)astore(ary, ++sp, str = str_static(&str_no));
1664 str_numset(str, (double)ntohs(sent->s_port));
1666 str_numset(str, (double)(sent->s_port));
1668 (void)astore(ary, ++sp, str = str_static(&str_no));
1669 str_set(str, sent->s_proto);
1673 (void)astore(ary, ++sp, str_static(&str_no));
1684 do_select(gimme,arglast)
1688 register STR **st = stack->ary_array;
1689 register int sp = arglast[0];
1697 struct timeval timebuf;
1698 struct timeval *tbuf = &timebuf;
1700 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1706 #if BYTEORDER & 0xf0000
1707 #define ORDERBYTE (0x88888888 - BYTEORDER)
1709 #define ORDERBYTE (0x4444 - BYTEORDER)
1714 for (i = 1; i <= 3; i++) {
1715 j = st[sp+i]->str_cur;
1720 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
1721 growsize = maxlen; /* little endians can use vecs directly */
1729 masksize = NFDBITS / NBBY;
1731 masksize = sizeof(long); /* documented int, everyone seems to use long */
1733 growsize = maxlen + (masksize - (maxlen % masksize));
1734 Zero(&fd_sets[0], 4, char*);
1737 for (i = 1; i <= 3; i++) {
1742 Str_Grow(str,growsize);
1743 s = str_get(str) + j;
1744 while (++j <= growsize) {
1748 else if (str->str_ptr) {
1749 Safefree(str->str_ptr);
1750 str->str_ptr = Nullch;
1753 #if BYTEORDER != 0x1234 && BYTEORDER != 0x12345678
1756 New(403, fd_sets[i], growsize, char);
1757 for (offset = 0; offset < growsize; offset += masksize) {
1758 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1759 fd_sets[i][j+offset] = s[(k % masksize) + offset];
1765 if (str->str_nok || str->str_pok) {
1766 value = str_gnum(str);
1769 timebuf.tv_sec = (long)value;
1770 value -= (double)timebuf.tv_sec;
1771 timebuf.tv_usec = (long)(value * 1000000.0);
1774 tbuf = Null(struct timeval*);
1776 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
1790 for (i = 1; i <= 3; i++) {
1794 for (offset = 0; offset < growsize; offset += masksize) {
1795 for (j = 0, k=ORDERBYTE; j < masksize; j++, (k >>= 4))
1796 s[(k % masksize) + offset] = fd_sets[i][j+offset];
1802 st[++sp] = str_static(&str_no);
1803 str_numset(st[sp], (double)nfound);
1804 if (gimme == G_ARRAY && tbuf) {
1805 value = (double)(timebuf.tv_sec) +
1806 (double)(timebuf.tv_usec) / 1000000.0;
1807 st[++sp] = str_static(&str_no);
1808 str_numset(st[sp], value);
1816 do_spair(stab1, stab2, arglast)
1821 register STR **st = stack->ary_array;
1822 register int sp = arglast[2];
1823 register STIO *stio1;
1824 register STIO *stio2;
1825 int domain, type, protocol, fd[2];
1827 if (!stab1 || !stab2)
1830 stio1 = stab_io(stab1);
1831 stio2 = stab_io(stab2);
1833 stio1 = stab_io(stab1) = stio_new();
1834 else if (stio1->ifp)
1835 do_close(stab1,FALSE);
1837 stio2 = stab_io(stab2) = stio_new();
1838 else if (stio2->ifp)
1839 do_close(stab2,FALSE);
1841 domain = (int)str_gnum(st[++sp]);
1842 type = (int)str_gnum(st[++sp]);
1843 protocol = (int)str_gnum(st[++sp]);
1845 taintproper("Insecure dependency in socketpair");
1848 if (socketpair(domain,type,protocol,fd) < 0)
1851 fatal("Socketpair unimplemented");
1853 stio1->ifp = fdopen(fd[0], "r");
1854 stio1->ofp = fdopen(fd[0], "w");
1856 stio2->ifp = fdopen(fd[1], "r");
1857 stio2->ofp = fdopen(fd[1], "w");
1866 do_gpwent(which,gimme,arglast)
1872 register ARRAY *ary = stack;
1873 register int sp = arglast[0];
1875 struct passwd *getpwnam();
1876 struct passwd *getpwuid();
1877 struct passwd *getpwent();
1878 struct passwd *pwent;
1880 if (gimme != G_ARRAY) {
1881 astore(ary, ++sp, str_static(&str_undef));
1885 if (which == O_GPWNAM) {
1886 char *name = str_get(ary->ary_array[sp+1]);
1888 pwent = getpwnam(name);
1890 else if (which == O_GPWUID) {
1891 int uid = (int)str_gnum(ary->ary_array[sp+1]);
1893 pwent = getpwuid(uid);
1899 (void)astore(ary, ++sp, str = str_static(&str_no));
1900 str_set(str, pwent->pw_name);
1901 (void)astore(ary, ++sp, str = str_static(&str_no));
1902 str_set(str, pwent->pw_passwd);
1903 (void)astore(ary, ++sp, str = str_static(&str_no));
1904 str_numset(str, (double)pwent->pw_uid);
1905 (void)astore(ary, ++sp, str = str_static(&str_no));
1906 str_numset(str, (double)pwent->pw_gid);
1907 (void)astore(ary, ++sp, str = str_static(&str_no));
1909 str_numset(str, (double)pwent->pw_change);
1912 str_numset(str, (double)pwent->pw_quota);
1915 str_set(str, pwent->pw_age);
1919 (void)astore(ary, ++sp, str = str_static(&str_no));
1921 str_set(str,pwent->pw_class);
1923 str_set(str, pwent->pw_comment);
1925 (void)astore(ary, ++sp, str = str_static(&str_no));
1926 str_set(str, pwent->pw_gecos);
1927 (void)astore(ary, ++sp, str = str_static(&str_no));
1928 str_set(str, pwent->pw_dir);
1929 (void)astore(ary, ++sp, str = str_static(&str_no));
1930 str_set(str, pwent->pw_shell);
1932 (void)astore(ary, ++sp, str = str_static(&str_no));
1933 str_numset(str, (double)pwent->pw_expire);
1939 fatal("password routines not implemented");
1944 do_ggrent(which,gimme,arglast)
1950 register ARRAY *ary = stack;
1951 register int sp = arglast[0];
1952 register char **elem;
1954 struct group *getgrnam();
1955 struct group *getgrgid();
1956 struct group *getgrent();
1957 struct group *grent;
1959 if (gimme != G_ARRAY) {
1960 astore(ary, ++sp, str_static(&str_undef));
1964 if (which == O_GGRNAM) {
1965 char *name = str_get(ary->ary_array[sp+1]);
1967 grent = getgrnam(name);
1969 else if (which == O_GGRGID) {
1970 int gid = (int)str_gnum(ary->ary_array[sp+1]);
1972 grent = getgrgid(gid);
1978 (void)astore(ary, ++sp, str = str_static(&str_no));
1979 str_set(str, grent->gr_name);
1980 (void)astore(ary, ++sp, str = str_static(&str_no));
1981 str_set(str, grent->gr_passwd);
1982 (void)astore(ary, ++sp, str = str_static(&str_no));
1983 str_numset(str, (double)grent->gr_gid);
1984 (void)astore(ary, ++sp, str = str_static(&str_no));
1985 for (elem = grent->gr_mem; *elem; elem++) {
1986 str_cat(str, *elem);
1988 str_ncat(str," ",1);
1994 fatal("group routines not implemented");
1999 do_dirop(optype,stab,gimme,arglast)
2005 #if defined(DIRENT) && defined(READDIR)
2006 register ARRAY *ary = stack;
2007 register STR **st = ary->ary_array;
2008 register int sp = arglast[1];
2009 register STIO *stio;
2014 struct DIRENT *readdir();
2015 register struct DIRENT *dp;
2019 if (!(stio = stab_io(stab)))
2020 stio = stab_io(stab) = stio_new();
2021 if (!stio->dirp && optype != O_OPENDIR)
2027 closedir(stio->dirp);
2028 if (!(stio->dirp = opendir(str_get(st[sp+1]))))
2032 if (gimme == G_ARRAY) {
2034 while (dp = readdir(stio->dirp)) {
2036 (void)astore(ary,++sp,
2037 str_2static(str_make(dp->d_name,dp->d_namlen)));
2039 (void)astore(ary,++sp,
2040 str_2static(str_make(dp->d_name,0)));
2045 if (!(dp = readdir(stio->dirp)))
2047 st[sp] = str_static(&str_undef);
2049 str_nset(st[sp], dp->d_name, dp->d_namlen);
2051 str_set(st[sp], dp->d_name);
2061 st[sp] = str_static(&str_undef);
2062 str_numset(st[sp], (double)telldir(stio->dirp));
2065 st[sp] = str_static(&str_undef);
2066 along = (long)str_gnum(st[sp+1]);
2067 (void)seekdir(stio->dirp,along);
2071 st[sp] = str_static(&str_undef);
2072 (void)rewinddir(stio->dirp);
2075 st[sp] = str_static(&str_undef);
2076 (void)closedir(stio->dirp);
2083 st[sp] = &str_undef;
2087 fatal("Unimplemented directory operation");
2095 register STR **st = stack->ary_array;
2096 register int sp = arglast[1];
2097 register int items = arglast[2] - sp;
2100 register int tot = 0;
2104 for (st += ++sp; items--; st++)
2105 tainted |= (*st)->str_tainted;
2106 st = stack->ary_array;
2108 items = arglast[2] - sp;
2113 taintproper("Insecure dependency in chmod");
2117 val = (int)str_gnum(st[++sp]);
2119 if (chmod(str_get(st[++sp]),val))
2127 taintproper("Insecure dependency in chown");
2132 val = (int)str_gnum(st[++sp]);
2133 val2 = (int)str_gnum(st[++sp]);
2135 if (chown(str_get(st[++sp]),val,val2))
2144 taintproper("Insecure dependency in kill");
2148 s = str_get(st[++sp]);
2150 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
2152 if (!(val = whichsig(s)))
2153 fatal("Unrecognized signal name \"%s\"",s);
2156 val = (int)str_gnum(st[sp]);
2160 int proc = (int)str_gnum(st[++sp]);
2162 if (killpg(proc,val)) /* BSD */
2164 if (kill(-proc,val)) /* SYSV */
2171 if (kill((int)(str_gnum(st[++sp])),val))
2180 taintproper("Insecure dependency in unlink");
2184 s = str_get(st[++sp]);
2185 if (euid || unsafe) {
2189 else { /* don't let root wipe out directories without -U */
2191 if (lstat(s,&statbuf) < 0 ||
2193 if (stat(s,&statbuf) < 0 ||
2195 (statbuf.st_mode & S_IFMT) == S_IFDIR )
2206 taintproper("Insecure dependency in utime");
2210 struct utimbuf utbuf;
2218 Zero(&utbuf, sizeof utbuf, char);
2219 utbuf.actime = (long)str_gnum(st[++sp]); /* time accessed */
2220 utbuf.modtime = (long)str_gnum(st[++sp]); /* time modified */
2225 if (utime(str_get(st[++sp]),&utbuf))
2237 /* Do the permissions allow some operation? Assumes statcache already set. */
2240 cando(bit, effective, statbufp)
2243 register struct stat *statbufp;
2245 if ((effective ? euid : uid) == 0) { /* root is special */
2246 if (bit == S_IEXEC) {
2247 if (statbufp->st_mode & 0111 ||
2248 (statbufp->st_mode & S_IFMT) == S_IFDIR )
2252 return TRUE; /* root reads and writes anything */
2255 if (statbufp->st_uid == (effective ? euid : uid) ) {
2256 if (statbufp->st_mode & bit)
2257 return TRUE; /* ok as "user" */
2259 else if (ingroup((int)statbufp->st_gid,effective)) {
2260 if (statbufp->st_mode & bit >> 3)
2261 return TRUE; /* ok as "group" */
2263 else if (statbufp->st_mode & bit >> 6)
2264 return TRUE; /* ok as "other" */
2269 ingroup(testgid,effective)
2273 if (testgid == (effective ? egid : gid))
2280 GIDTYPE gary[NGROUPS];
2283 anum = getgroups(NGROUPS,gary);
2285 if (gary[anum] == testgid)