1 /* $Header: doio.c,v 3.0.1.3 89/11/17 15:13:06 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.3 89/11/17 15:13:06 lwall
10 * patch5: some systems have symlink() but not lstat()
11 * patch5: some systems have dirent.h but not readdir()
13 * Revision 3.0.1.2 89/11/11 04:25:51 lwall
14 * patch2: orthogonalized the file modes some so we can have <& +<& etc.
15 * patch2: do_open() now detects sockets passed to process from parent
16 * patch2: fd's above 2 are now closed on exec
17 * patch2: csh code can now use csh from other than /bin
18 * patch2: getsockopt, get{sock,peer}name didn't define result properly
19 * patch2: warn("shutdown") was replicated
20 * patch2: gethostbyname was misdeclared
21 * patch2: telldir() is sometimes a macro
23 * Revision 3.0.1.1 89/10/26 23:10:05 lwall
24 * patch1: Configure now checks for BSD shadow passwords
26 * Revision 3.0 89/10/18 15:10:54 lwall
35 #include <sys/socket.h>
55 int len = strlen(name);
56 register STIO *stio = stab_io(stab);
57 char *myname = savestr(name);
61 char mode[3]; /* stdio file mode ("r\0" or "r+\0") */
64 forkprocess = 1; /* assume true if no fork */
65 while (len && isspace(name[len-1]))
68 stio = stab_io(stab) = stio_new();
70 fd = fileno(stio->ifp);
71 if (stio->type == '|')
72 result = mypclose(stio->ifp);
73 else if (stio->ifp != stio->ofp) {
76 result = fclose(stio->ifp);
78 else if (stio->type != '-')
79 result = fclose(stio->ifp);
82 if (result == EOF && fd > 2)
83 fprintf(stderr,"Warning: unable to close filehandle %s properly.\n",
85 stio->ofp = stio->ifp = Nullfp;
87 if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */
98 for (name++; isspace(*name); name++) ;
101 taintproper("Insecure dependency in piped open");
103 fp = mypopen(name,"w");
106 else if (*name == '>') {
108 taintproper("Insecure dependency in open");
112 mode[0] = stio->type = 'a';
121 while (isspace(*name))
126 stab = stabent(name,FALSE);
127 if (!stab || !stab_io(stab))
129 if (stab_io(stab) && stab_io(stab)->ifp) {
130 fd = fileno(stab_io(stab)->ifp);
131 if (stab_io(stab)->type == 's')
137 fp = fdopen(dup(fd),mode);
140 while (isspace(*name))
142 if (strEQ(name,"-")) {
147 fp = fopen(name,mode);
155 while (isspace(*name))
159 if (strEQ(name,"-")) {
164 fp = fopen(name,mode);
166 else if (name[len-1] == '|') {
169 taintproper("Insecure dependency in piped open");
172 while (len && isspace(name[len-1]))
174 for (; isspace(*name); name++) ;
175 fp = mypopen(name,"r");
180 for (; isspace(*name); name++) ;
181 if (strEQ(name,"-")) {
186 fp = fopen(name,"r");
193 stio->type != '|' && stio->type != '-') {
194 if (fstat(fileno(fp),&statbuf) < 0) {
198 result = (statbuf.st_mode & S_IFMT);
199 if (result != S_IFREG &&
201 result != S_IFSOCK &&
204 result != S_IFFIFO &&
209 result != 0 && /* socket? */
215 if (result == S_IFSOCK || result == 0)
216 stio->type = 's'; /* in case a socket was passed in to us */
219 #if defined(FCNTL) && defined(F_SETFD)
226 if (stio->type != 's')
229 stio->ofp = fdopen(fileno(fp),"w");
240 int filemode,fileuid,filegid;
242 while (alen(stab_xarray(stab)) >= 0) {
243 str = ashift(stab_xarray(stab));
244 str_sset(stab_val(stab),str);
245 STABSET(stab_val(stab));
246 oldname = str_get(stab_val(stab));
247 if (do_open(stab,oldname)) {
250 taintproper("Insecure dependency in inplace open");
252 filemode = statbuf.st_mode;
253 fileuid = statbuf.st_uid;
254 filegid = statbuf.st_gid;
256 str_cat(str,inplace);
258 (void)rename(oldname,str->str_ptr);
260 (void)UNLINK(str->str_ptr);
261 (void)link(oldname,str->str_ptr);
262 (void)UNLINK(oldname);
266 (void)UNLINK(oldname);
270 str_cat(str,oldname);
271 errno = 0; /* in case sprintf set errno */
272 if (!do_open(argvoutstab,str->str_ptr))
273 fatal("Can't do inplace edit");
274 defoutstab = argvoutstab;
276 (void)fchmod(fileno(stab_io(argvoutstab)->ifp),filemode);
278 (void)chmod(oldname,filemode);
281 (void)fchown(fileno(stab_io(argvoutstab)->ifp),fileuid,filegid);
283 (void)chown(oldname,fileuid,filegid);
287 return stab_io(stab)->ifp;
290 fprintf(stderr,"Can't open %s\n",str_get(str));
294 (void)do_close(argvoutstab,FALSE);
295 defoutstab = stabent("STDOUT",TRUE);
301 do_close(stab,explicit)
306 register STIO *stio = stab_io(stab);
309 if (!stio) { /* never opened */
310 if (dowarn && explicit)
311 warn("Close on unopened file <%s>",stab_name(stab));
315 if (stio->type == '|') {
316 status = mypclose(stio->ifp);
317 retval = (status >= 0);
318 statusvalue = (unsigned)status & 0xffff;
320 else if (stio->type == '-')
323 if (stio->ofp && stio->ofp != stio->ifp) /* a socket */
325 retval = (fclose(stio->ifp) != EOF);
327 stio->ofp = stio->ifp = Nullfp;
342 if (!stab) { /* eof() */
344 stio = stab_io(argvstab);
349 stio = stab_io(stab);
356 #ifdef STDSTDIO /* (the code works without this) */
357 if (stio->ifp->_cnt > 0) /* cheat a little, since */
358 return FALSE; /* this is the most usual case */
361 ch = getc(stio->ifp);
363 (void)ungetc(ch, stio->ifp);
366 if (!stab) { /* not necessarily a real EOF yet? */
367 if (!nextargv(argvstab)) /* get another fp handy */
371 return TRUE; /* normal fp, definitely end of file */
385 stio = stab_io(stab);
386 if (!stio || !stio->ifp)
390 (void)fseek (stio->ifp, 0L, 2); /* ultrix 1.2 workaround */
392 return ftell(stio->ifp);
396 warn("tell() on unopened file");
401 do_seek(stab, pos, whence)
411 stio = stab_io(stab);
412 if (!stio || !stio->ifp)
416 (void)fseek (stio->ifp, 0L, 2); /* ultrix 1.2 workaround */
418 return fseek(stio->ifp, pos, whence) >= 0;
422 warn("seek() on unopened file");
427 do_ctl(optype,stab,func,argstr)
437 if (!stab || !argstr)
439 stio = stab_io(stab);
443 if (argstr->str_pok || !argstr->str_nok) {
444 if (!argstr->str_pok)
449 #define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
453 retval = IOCPARM_LEN(func); /* on BSDish systes we're safe */
455 retval = 256; /* otherwise guess at what's safe */
457 if (argstr->str_cur < retval) {
458 str_grow(argstr,retval+1);
459 argstr->str_cur = retval;
463 s[argstr->str_cur] = 17; /* a little sanity check here */
466 retval = (int)str_gnum(argstr);
467 s = (char*)retval; /* ouch */
471 if (optype == O_IOCTL)
472 retval = ioctl(fileno(stio->ifp), func, s);
475 retval = fcntl(fileno(stio->ifp), func, s);
477 fatal("fcntl is not implemented");
483 if (argstr->str_pok) {
484 if (s[argstr->str_cur] != 17)
485 fatal("Return value overflowed string");
486 s[argstr->str_cur] = 0; /* put our null back */
492 do_stat(str,arg,gimme,arglast)
498 register ARRAY *ary = stack;
499 register int sp = arglast[0] + 1;
503 if ((arg[1].arg_type & A_MASK) == A_WORD) {
504 tmpstab = arg[1].arg_ptr.arg_stab;
505 if (tmpstab != defstab) {
507 str_set(statname,"");
508 if (!stab_io(tmpstab) ||
509 fstat(fileno(stab_io(tmpstab)->ifp),&statcache) < 0) {
515 str_sset(statname,ary->ary_array[sp]);
518 if (arg->arg_type == O_LSTAT)
519 i = lstat(str_get(statname),&statcache);
522 i = stat(str_get(statname),&statcache);
527 if (gimme != G_ARRAY) {
529 str_sset(str,&str_yes);
531 str_sset(str,&str_undef);
533 ary->ary_array[sp] = str;
539 (void)astore(ary,++sp,
540 str_2static(str_nmake((double)statcache.st_dev)));
541 (void)astore(ary,++sp,
542 str_2static(str_nmake((double)statcache.st_ino)));
543 (void)astore(ary,++sp,
544 str_2static(str_nmake((double)statcache.st_mode)));
545 (void)astore(ary,++sp,
546 str_2static(str_nmake((double)statcache.st_nlink)));
547 (void)astore(ary,++sp,
548 str_2static(str_nmake((double)statcache.st_uid)));
549 (void)astore(ary,++sp,
550 str_2static(str_nmake((double)statcache.st_gid)));
551 (void)astore(ary,++sp,
552 str_2static(str_nmake((double)statcache.st_rdev)));
553 (void)astore(ary,++sp,
554 str_2static(str_nmake((double)statcache.st_size)));
555 (void)astore(ary,++sp,
556 str_2static(str_nmake((double)statcache.st_atime)));
557 (void)astore(ary,++sp,
558 str_2static(str_nmake((double)statcache.st_mtime)));
559 (void)astore(ary,++sp,
560 str_2static(str_nmake((double)statcache.st_ctime)));
562 (void)astore(ary,++sp,
563 str_2static(str_nmake((double)statcache.st_blksize)));
564 (void)astore(ary,++sp,
565 str_2static(str_nmake((double)statcache.st_blocks)));
567 (void)astore(ary,++sp,
568 str_2static(str_make("",0)));
569 (void)astore(ary,++sp,
570 str_2static(str_make("",0)));
573 (void)astore(ary,++sp,str_nmake(0.0));
580 looks_like_number(str)
589 send = s + str->str_cur;
594 if (*s == '+' || *s == '-')
602 else if (s == str->str_ptr)
608 if (*s == 'e' || *s == 'E') {
610 if (*s == '+' || *s == '-')
631 warn("print to unopened file");
637 ((str->str_nok && str->str_u.str_nval != 0.0)
638 || (looks_like_number(str) && str_gnum(str) != 0.0) ) )
639 fprintf(fp, ofmt, str->str_u.str_nval);
642 if (*tmps == 'S' && tmps[1] == 't' && tmps[2] == 'a' && tmps[3] == 'b'
643 && str->str_cur == sizeof(STBP) && strlen(tmps) < str->str_cur) {
644 tmps = stab_name(((STAB*)str)); /* a stab value, be nice */
645 str = ((STAB*)str)->str_magic;
648 if (str->str_cur && fwrite(tmps,1,str->str_cur,fp) == 0)
655 do_aprint(arg,fp,arglast)
660 register STR **st = stack->ary_array;
661 register int sp = arglast[1];
663 register int items = arglast[2] - sp;
667 warn("print to unopened file");
671 if (arg->arg_type == O_PRTF) {
672 do_sprintf(arg->arg_ptr.arg_str,items,st);
673 retval = do_print(arg->arg_ptr.arg_str,fp);
676 retval = (items <= 0);
677 for (; items > 0; items--,st++) {
678 if (retval && ofslen) {
679 if (fwrite(ofs, 1, ofslen, fp) == 0) {
684 if (!(retval = do_print(*st, fp)))
687 if (retval && orslen)
688 if (fwrite(ors, 1, orslen, fp) == 0)
701 if (arg[1].arg_type & A_DONT) {
702 stio = stab_io(arg[1].arg_ptr.arg_stab);
703 if (stio && stio->ifp) {
704 statstab = arg[1].arg_ptr.arg_stab;
705 str_set(statname,"");
706 return fstat(fileno(stio->ifp), &statcache);
709 if (arg[1].arg_ptr.arg_stab == defstab)
712 warn("Stat on unopened file <%s>",
713 stab_name(arg[1].arg_ptr.arg_stab));
715 str_set(statname,"");
721 str_sset(statname,str);
722 return stat(str_get(str),&statcache);
738 if (arg[1].arg_type & A_DONT) {
739 if (arg[1].arg_ptr.arg_stab == defstab) {
741 stio = stab_io(statstab);
744 goto really_filename;
748 statstab = arg[1].arg_ptr.arg_stab;
749 str_set(statname,"");
750 stio = stab_io(statstab);
752 if (stio && stio->ifp) {
754 fstat(fileno(stio->ifp),&statcache);
755 if (stio->ifp->_cnt <= 0) {
758 (void)ungetc(i,stio->ifp);
760 if (stio->ifp->_cnt <= 0) /* null file is anything */
762 len = stio->ifp->_cnt + (stio->ifp->_ptr - stio->ifp->_base);
763 s = stio->ifp->_base;
765 fatal("-T and -B not implemented on filehandles\n");
770 warn("Test on unopened file <%s>",
771 stab_name(arg[1].arg_ptr.arg_stab));
777 str_sset(statname,str);
779 i = open(str_get(str),0);
783 len = read(i,tbuf,512);
784 if (len <= 0) /* null file is anything */
790 /* now scan s to look for textiness */
792 for (i = 0; i < len; i++,s++) {
793 if (!*s) { /* null never allowed in text */
800 *s != '\n' && *s != '\r' && *s != '\b' &&
801 *s != '\t' && *s != '\f' && *s != 27)
805 if ((odd * 10 > len) == (arg->arg_type == O_FTTEXT)) /* allow 10% odd */
812 do_aexec(really,arglast)
816 register STR **st = stack->ary_array;
817 register int sp = arglast[1];
818 register int items = arglast[2] - sp;
824 New(401,argv, items+1, char*);
826 for (st += ++sp; items > 0; items--,st++) {
834 if (*argv[0] != '/') /* will execvp use PATH? */
835 taintenv(); /* testing IFS here is overkill, probably */
837 if (really && *(tmps = str_get(really)))
840 execvp(argv[0],argv);
857 taintproper("Insecure dependency in exec");
860 /* save an extra exec if possible */
863 if (strnEQ(cmd,cshname,cshlen) && strnEQ(cmd+cshlen," -c",3)) {
881 execl(cshname,"csh", flags,ncmd,(char*)0);
889 /* see if there are shell metacharacters in it */
891 for (s = cmd; *s; s++) {
892 if (*s != ' ' && !isalpha(*s) && index("$&*(){}[]'\";\\|?<>~`\n",*s)) {
893 if (*s == '\n' && !s[1]) {
898 execl("/bin/sh","sh","-c",cmd,(char*)0);
902 New(402,argv, (s - cmd) / 2 + 2, char*);
906 while (*s && isspace(*s)) s++;
909 while (*s && !isspace(*s)) s++;
915 execvp(argv[0],argv);
916 if (errno == ENOEXEC) /* for system V NIH syndrome */
925 do_socket(stab, arglast)
929 register STR **st = stack->ary_array;
930 register int sp = arglast[1];
932 int domain, type, protocol, fd;
937 stio = stab_io(stab);
939 stio = stab_io(stab) = stio_new();
941 do_close(stab,FALSE);
943 domain = (int)str_gnum(st[++sp]);
944 type = (int)str_gnum(st[++sp]);
945 protocol = (int)str_gnum(st[++sp]);
947 taintproper("Insecure dependency in socket");
949 fd = socket(domain,type,protocol);
952 stio->ifp = fdopen(fd, "r"); /* stdio gets confused about sockets */
953 stio->ofp = fdopen(fd, "w");
960 do_bind(stab, arglast)
964 register STR **st = stack->ary_array;
965 register int sp = arglast[1];
972 stio = stab_io(stab);
973 if (!stio || !stio->ifp)
976 addr = str_get(st[++sp]);
978 taintproper("Insecure dependency in bind");
980 return bind(fileno(stio->ifp), addr, st[sp]->str_cur) >= 0;
984 warn("bind() on closed fd");
990 do_connect(stab, arglast)
994 register STR **st = stack->ary_array;
995 register int sp = arglast[1];
1002 stio = stab_io(stab);
1003 if (!stio || !stio->ifp)
1006 addr = str_get(st[++sp]);
1008 taintproper("Insecure dependency in connect");
1010 return connect(fileno(stio->ifp), addr, st[sp]->str_cur) >= 0;
1014 warn("connect() on closed fd");
1020 do_listen(stab, arglast)
1024 register STR **st = stack->ary_array;
1025 register int sp = arglast[1];
1026 register STIO *stio;
1032 stio = stab_io(stab);
1033 if (!stio || !stio->ifp)
1036 backlog = (int)str_gnum(st[++sp]);
1037 return listen(fileno(stio->ifp), backlog) >= 0;
1041 warn("listen() on closed fd");
1046 do_accept(str, nstab, gstab)
1051 register STIO *nstio;
1052 register STIO *gstio;
1053 int len = sizeof buf;
1061 gstio = stab_io(gstab);
1062 nstio = stab_io(nstab);
1064 if (!gstio || !gstio->ifp)
1067 nstio = stab_io(nstab) = stio_new();
1068 else if (nstio->ifp)
1069 do_close(nstab,FALSE);
1071 fd = accept(fileno(gstio->ifp),buf,&len);
1074 nstio->ifp = fdopen(fd, "r");
1075 nstio->ofp = fdopen(fd, "w");
1078 str_nset(str, buf, len);
1083 warn("accept() on closed fd");
1085 str_sset(str,&str_undef);
1090 do_shutdown(stab, arglast)
1094 register STR **st = stack->ary_array;
1095 register int sp = arglast[1];
1096 register STIO *stio;
1102 stio = stab_io(stab);
1103 if (!stio || !stio->ifp)
1106 how = (int)str_gnum(st[++sp]);
1107 return shutdown(fileno(stio->ifp), how) >= 0;
1111 warn("shutdown() on closed fd");
1117 do_sopt(optype, stab, arglast)
1122 register STR **st = stack->ary_array;
1123 register int sp = arglast[1];
1124 register STIO *stio;
1132 stio = stab_io(stab);
1133 if (!stio || !stio->ifp)
1136 fd = fileno(stio->ifp);
1137 lvl = (int)str_gnum(st[sp+1]);
1138 optname = (int)str_gnum(st[sp+2]);
1141 st[sp] = str_2static(str_new(257));
1142 st[sp]->str_cur = 256;
1143 st[sp]->str_pok = 1;
1144 if (getsockopt(fd, lvl, optname, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
1149 if (setsockopt(fd, lvl, optname, st[sp]->str_ptr, st[sp]->str_cur) < 0)
1159 warn("[gs]etsockopt() on closed fd");
1160 st[sp] = &str_undef;
1166 do_getsockname(optype, stab, arglast)
1171 register STR **st = stack->ary_array;
1172 register int sp = arglast[1];
1173 register STIO *stio;
1179 stio = stab_io(stab);
1180 if (!stio || !stio->ifp)
1183 st[sp] = str_2static(str_new(257));
1184 st[sp]->str_cur = 256;
1185 st[sp]->str_pok = 1;
1186 fd = fileno(stio->ifp);
1189 if (getsockname(fd, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
1193 if (getpeername(fd, st[sp]->str_ptr, &st[sp]->str_cur) < 0)
1202 warn("get{sock,peer}name() on closed fd");
1203 st[sp] = &str_undef;
1209 do_ghent(which,gimme,arglast)
1214 register ARRAY *ary = stack;
1215 register int sp = arglast[0];
1216 register char **elem;
1218 struct hostent *gethostbyname();
1219 struct hostent *gethostbyaddr();
1221 struct hostent *gethostent();
1223 struct hostent *hent;
1226 if (gimme != G_ARRAY) {
1227 astore(ary, ++sp, str_static(&str_undef));
1231 if (which == O_GHBYNAME) {
1232 char *name = str_get(ary->ary_array[sp+1]);
1234 hent = gethostbyname(name);
1236 else if (which == O_GHBYADDR) {
1237 STR *addrstr = ary->ary_array[sp+1];
1238 int addrtype = (int)str_gnum(ary->ary_array[sp+2]);
1239 char *addr = str_get(addrstr);
1241 hent = gethostbyaddr(addr,addrstr->str_cur,addrtype);
1245 hent = gethostent();
1247 fatal("gethostent not implemented");
1251 (void)astore(ary, ++sp, str = str_static(&str_no));
1252 str_set(str, hent->h_name);
1253 (void)astore(ary, ++sp, str = str_static(&str_no));
1254 for (elem = hent->h_aliases; *elem; elem++) {
1255 str_cat(str, *elem);
1257 str_ncat(str," ",1);
1259 (void)astore(ary, ++sp, str = str_static(&str_no));
1260 str_numset(str, (double)hent->h_addrtype);
1261 (void)astore(ary, ++sp, str = str_static(&str_no));
1262 len = hent->h_length;
1263 str_numset(str, (double)len);
1265 for (elem = hent->h_addr_list; *elem; elem++) {
1266 (void)astore(ary, ++sp, str = str_static(&str_no));
1267 str_nset(str, *elem, len);
1270 (void)astore(ary, ++sp, str = str_static(&str_no));
1271 str_nset(str, hent->h_addr, len);
1276 (void)astore(ary, ++sp, str_static(&str_no));
1284 do_gnent(which,gimme,arglast)
1289 register ARRAY *ary = stack;
1290 register int sp = arglast[0];
1291 register char **elem;
1293 struct netent *getnetbyname();
1294 struct netent *getnetbyaddr();
1295 struct netent *getnetent();
1296 struct netent *nent;
1298 if (gimme != G_ARRAY) {
1299 astore(ary, ++sp, str_static(&str_undef));
1303 if (which == O_GNBYNAME) {
1304 char *name = str_get(ary->ary_array[sp+1]);
1306 nent = getnetbyname(name);
1308 else if (which == O_GNBYADDR) {
1309 STR *addrstr = ary->ary_array[sp+1];
1310 int addrtype = (int)str_gnum(ary->ary_array[sp+2]);
1311 char *addr = str_get(addrstr);
1313 nent = getnetbyaddr(addr,addrtype);
1320 (void)astore(ary, ++sp, str = str_static(&str_no));
1321 str_set(str, nent->n_name);
1322 (void)astore(ary, ++sp, str = str_static(&str_no));
1323 for (elem = nent->n_aliases; *elem; elem++) {
1324 str_cat(str, *elem);
1326 str_ncat(str," ",1);
1328 (void)astore(ary, ++sp, str = str_static(&str_no));
1329 str_numset(str, (double)nent->n_addrtype);
1330 (void)astore(ary, ++sp, str = str_static(&str_no));
1331 str_numset(str, (double)nent->n_net);
1335 (void)astore(ary, ++sp, str_static(&str_no));
1343 do_gpent(which,gimme,arglast)
1348 register ARRAY *ary = stack;
1349 register int sp = arglast[0];
1350 register char **elem;
1352 struct protoent *getprotobyname();
1353 struct protoent *getprotobynumber();
1354 struct protoent *getprotoent();
1355 struct protoent *pent;
1357 if (gimme != G_ARRAY) {
1358 astore(ary, ++sp, str_static(&str_undef));
1362 if (which == O_GPBYNAME) {
1363 char *name = str_get(ary->ary_array[sp+1]);
1365 pent = getprotobyname(name);
1367 else if (which == O_GPBYNUMBER) {
1368 int proto = (int)str_gnum(ary->ary_array[sp+1]);
1370 pent = getprotobynumber(proto);
1373 pent = getprotoent();
1377 (void)astore(ary, ++sp, str = str_static(&str_no));
1378 str_set(str, pent->p_name);
1379 (void)astore(ary, ++sp, str = str_static(&str_no));
1380 for (elem = pent->p_aliases; *elem; elem++) {
1381 str_cat(str, *elem);
1383 str_ncat(str," ",1);
1385 (void)astore(ary, ++sp, str = str_static(&str_no));
1386 str_numset(str, (double)pent->p_proto);
1390 (void)astore(ary, ++sp, str_static(&str_no));
1398 do_gsent(which,gimme,arglast)
1403 register ARRAY *ary = stack;
1404 register int sp = arglast[0];
1405 register char **elem;
1407 struct servent *getservbyname();
1408 struct servent *getservbynumber();
1409 struct servent *getservent();
1410 struct servent *sent;
1412 if (gimme != G_ARRAY) {
1413 astore(ary, ++sp, str_static(&str_undef));
1417 if (which == O_GSBYNAME) {
1418 char *name = str_get(ary->ary_array[sp+1]);
1419 char *proto = str_get(ary->ary_array[sp+2]);
1421 if (proto && !*proto)
1424 sent = getservbyname(name,proto);
1426 else if (which == O_GSBYPORT) {
1427 int port = (int)str_gnum(ary->ary_array[sp+1]);
1428 char *proto = str_get(ary->ary_array[sp+2]);
1430 sent = getservbyport(port,proto);
1433 sent = getservent();
1436 (void)astore(ary, ++sp, str = str_static(&str_no));
1437 str_set(str, sent->s_name);
1438 (void)astore(ary, ++sp, str = str_static(&str_no));
1439 for (elem = sent->s_aliases; *elem; elem++) {
1440 str_cat(str, *elem);
1442 str_ncat(str," ",1);
1444 (void)astore(ary, ++sp, str = str_static(&str_no));
1446 str_numset(str, (double)ntohs(sent->s_port));
1448 str_numset(str, (double)(sent->s_port));
1450 (void)astore(ary, ++sp, str = str_static(&str_no));
1451 str_set(str, sent->s_proto);
1455 (void)astore(ary, ++sp, str_static(&str_no));
1463 do_select(gimme,arglast)
1467 register STR **st = stack->ary_array;
1468 register int sp = arglast[0];
1476 struct timeval timebuf;
1477 struct timeval *tbuf = &timebuf;
1479 for (i = 1; i <= 3; i++) {
1480 j = st[sp+i]->str_len;
1484 for (i = 1; i <= 3; i++) {
1489 str_grow(str,maxlen);
1490 s = str_get(str) + j;
1491 while (++j <= maxlen) {
1495 else if (str->str_ptr) {
1496 Safefree(str->str_ptr);
1497 str->str_ptr = Nullch;
1502 if (str->str_nok || str->str_pok) {
1503 value = str_gnum(str);
1506 timebuf.tv_sec = (long)value;
1507 value -= (double)timebuf.tv_sec;
1508 timebuf.tv_usec = (long)(value * 1000000.0);
1511 tbuf = Null(struct timeval*);
1520 st[++sp] = str_static(&str_no);
1521 str_numset(st[sp], (double)nfound);
1522 if (gimme == G_ARRAY && tbuf) {
1523 value = (double)(timebuf.tv_sec) +
1524 (double)(timebuf.tv_usec) / 1000000.0;
1525 st[++sp] = str_static(&str_no);
1526 str_numset(st[sp], value);
1532 do_spair(stab1, stab2, arglast)
1537 register STR **st = stack->ary_array;
1538 register int sp = arglast[2];
1539 register STIO *stio1;
1540 register STIO *stio2;
1541 int domain, type, protocol, fd[2];
1543 if (!stab1 || !stab2)
1546 stio1 = stab_io(stab1);
1547 stio2 = stab_io(stab2);
1549 stio1 = stab_io(stab1) = stio_new();
1550 else if (stio1->ifp)
1551 do_close(stab1,FALSE);
1553 stio2 = stab_io(stab2) = stio_new();
1554 else if (stio2->ifp)
1555 do_close(stab2,FALSE);
1557 domain = (int)str_gnum(st[++sp]);
1558 type = (int)str_gnum(st[++sp]);
1559 protocol = (int)str_gnum(st[++sp]);
1561 taintproper("Insecure dependency in socketpair");
1564 if (socketpair(domain,type,protocol,fd) < 0)
1567 fatal("Socketpair unimplemented");
1569 stio1->ifp = fdopen(fd[0], "r");
1570 stio1->ofp = fdopen(fd[0], "w");
1572 stio2->ifp = fdopen(fd[1], "r");
1573 stio2->ofp = fdopen(fd[1], "w");
1582 do_gpwent(which,gimme,arglast)
1588 register ARRAY *ary = stack;
1589 register int sp = arglast[0];
1590 register char **elem;
1592 struct passwd *getpwnam();
1593 struct passwd *getpwuid();
1594 struct passwd *getpwent();
1595 struct passwd *pwent;
1598 if (gimme != G_ARRAY) {
1599 astore(ary, ++sp, str_static(&str_undef));
1603 if (which == O_GPWNAM) {
1604 char *name = str_get(ary->ary_array[sp+1]);
1606 pwent = getpwnam(name);
1608 else if (which == O_GPWUID) {
1609 int uid = (int)str_gnum(ary->ary_array[sp+1]);
1611 pwent = getpwuid(uid);
1617 (void)astore(ary, ++sp, str = str_static(&str_no));
1618 str_set(str, pwent->pw_name);
1619 (void)astore(ary, ++sp, str = str_static(&str_no));
1620 str_set(str, pwent->pw_passwd);
1621 (void)astore(ary, ++sp, str = str_static(&str_no));
1622 str_numset(str, (double)pwent->pw_uid);
1623 (void)astore(ary, ++sp, str = str_static(&str_no));
1624 str_numset(str, (double)pwent->pw_gid);
1625 (void)astore(ary, ++sp, str = str_static(&str_no));
1627 str_numset(str, (double)pwent->pw_change);
1630 str_numset(str, (double)pwent->pw_quota);
1633 str_set(str, pwent->pw_age);
1637 (void)astore(ary, ++sp, str = str_static(&str_no));
1639 str_set(str,pwent->pw_class);
1641 str_set(str, pwent->pw_comment);
1643 (void)astore(ary, ++sp, str = str_static(&str_no));
1644 str_set(str, pwent->pw_gecos);
1645 (void)astore(ary, ++sp, str = str_static(&str_no));
1646 str_set(str, pwent->pw_dir);
1647 (void)astore(ary, ++sp, str = str_static(&str_no));
1648 str_set(str, pwent->pw_shell);
1650 (void)astore(ary, ++sp, str = str_static(&str_no));
1651 str_numset(str, (double)pwent->pw_expire);
1657 fatal("password routines not implemented");
1662 do_ggrent(which,gimme,arglast)
1668 register ARRAY *ary = stack;
1669 register int sp = arglast[0];
1670 register char **elem;
1672 struct group *getgrnam();
1673 struct group *getgrgid();
1674 struct group *getgrent();
1675 struct group *grent;
1678 if (gimme != G_ARRAY) {
1679 astore(ary, ++sp, str_static(&str_undef));
1683 if (which == O_GGRNAM) {
1684 char *name = str_get(ary->ary_array[sp+1]);
1686 grent = getgrnam(name);
1688 else if (which == O_GGRGID) {
1689 int gid = (int)str_gnum(ary->ary_array[sp+1]);
1691 grent = getgrgid(gid);
1697 (void)astore(ary, ++sp, str = str_static(&str_no));
1698 str_set(str, grent->gr_name);
1699 (void)astore(ary, ++sp, str = str_static(&str_no));
1700 str_set(str, grent->gr_passwd);
1701 (void)astore(ary, ++sp, str = str_static(&str_no));
1702 str_numset(str, (double)grent->gr_gid);
1703 (void)astore(ary, ++sp, str = str_static(&str_no));
1704 for (elem = grent->gr_mem; *elem; elem++) {
1705 str_cat(str, *elem);
1707 str_ncat(str," ",1);
1713 fatal("group routines not implemented");
1718 do_dirop(optype,stab,gimme,arglast)
1724 #if defined(DIRENT) && defined(READDIR)
1725 register ARRAY *ary = stack;
1726 register STR **st = ary->ary_array;
1727 register int sp = arglast[1];
1728 register STIO *stio;
1733 struct DIRENT *readdir();
1734 register struct DIRENT *dp;
1738 if (!(stio = stab_io(stab)))
1739 stio = stab_io(stab) = stio_new();
1740 if (!stio->dirp && optype != O_OPENDIR)
1746 closedir(stio->dirp);
1747 if (!(stio->dirp = opendir(str_get(st[sp+1]))))
1751 if (gimme == G_ARRAY) {
1753 while (dp = readdir(stio->dirp)) {
1755 (void)astore(ary,++sp,
1756 str_2static(str_make(dp->d_name,dp->d_namlen)));
1758 (void)astore(ary,++sp,
1759 str_2static(str_make(dp->d_name,0)));
1764 if (!(dp = readdir(stio->dirp)))
1766 st[sp] = str_static(&str_undef);
1768 str_nset(st[sp], dp->d_name, dp->d_namlen);
1770 str_set(st[sp], dp->d_name);
1775 st[sp] = str_static(&str_undef);
1776 str_numset(st[sp], (double)telldir(stio->dirp));
1779 st[sp] = str_static(&str_undef);
1780 along = (long)str_gnum(st[sp+1]);
1781 (void)seekdir(stio->dirp,along);
1784 st[sp] = str_static(&str_undef);
1785 (void)rewinddir(stio->dirp);
1788 st[sp] = str_static(&str_undef);
1789 (void)closedir(stio->dirp);
1796 st[sp] = &str_undef;
1800 fatal("Unimplemented directory operation");
1808 register STR **st = stack->ary_array;
1809 register int sp = arglast[1];
1810 register int items = arglast[2] - sp;
1813 register int tot = 0;
1817 for (st += ++sp; items--; st++)
1818 tainted |= (*st)->str_tainted;
1819 st = stack->ary_array;
1821 items = arglast[2] - sp;
1826 taintproper("Insecure dependency in chmod");
1830 val = (int)str_gnum(st[++sp]);
1832 if (chmod(str_get(st[++sp]),val))
1839 taintproper("Insecure dependency in chown");
1844 val = (int)str_gnum(st[++sp]);
1845 val2 = (int)str_gnum(st[++sp]);
1847 if (chown(str_get(st[++sp]),val,val2))
1854 taintproper("Insecure dependency in kill");
1858 s = str_get(st[++sp]);
1860 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1862 if (!(val = whichsig(s)))
1863 fatal("Unrecognized signal name \"%s\"",s);
1866 val = (int)str_gnum(st[sp]);
1870 int proc = (int)str_gnum(st[++sp]);
1872 if (killpg(proc,val)) /* BSD */
1874 if (kill(-proc,val)) /* SYSV */
1881 if (kill((int)(str_gnum(st[++sp])),val))
1889 taintproper("Insecure dependency in unlink");
1893 s = str_get(st[++sp]);
1894 if (euid || unsafe) {
1898 else { /* don't let root wipe out directories without -U */
1900 if (lstat(s,&statbuf) < 0 ||
1902 if (stat(s,&statbuf) < 0 ||
1904 (statbuf.st_mode & S_IFMT) == S_IFDIR )
1915 taintproper("Insecure dependency in utime");
1923 utbuf.atime = (long)str_gnum(st[++sp]); /* time accessed */
1924 utbuf.mtime = (long)str_gnum(st[++sp]); /* time modified */
1929 if (utime(str_get(st[++sp]),&utbuf))
1941 /* Do the permissions allow some operation? Assumes statcache already set. */
1944 cando(bit, effective, statbufp)
1947 register struct stat *statbufp;
1949 if ((effective ? euid : uid) == 0) { /* root is special */
1950 if (bit == S_IEXEC) {
1951 if (statbufp->st_mode & 0111 ||
1952 (statbufp->st_mode & S_IFMT) == S_IFDIR )
1956 return TRUE; /* root reads and writes anything */
1959 if (statbufp->st_uid == (effective ? euid : uid) ) {
1960 if (statbufp->st_mode & bit)
1961 return TRUE; /* ok as "user" */
1963 else if (ingroup((int)statbufp->st_gid,effective)) {
1964 if (statbufp->st_mode & bit >> 3)
1965 return TRUE; /* ok as "group" */
1967 else if (statbufp->st_mode & bit >> 6)
1968 return TRUE; /* ok as "other" */
1973 ingroup(testgid,effective)
1977 if (testgid == (effective ? egid : gid))
1984 GIDTYPE gary[NGROUPS];
1987 anum = getgroups(NGROUPS,gary);
1989 if (gary[anum] == testgid)