1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
|
/* Miscellaneous system dependent routines for splitsh */
#define _GNU_SOURCE /* for getpt and other gnu extensions to libc */
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <utime.h>
#include <string.h>
#ifdef HAVE_TERMIO_H
# include <termio.h>
#elif defined(HAVE_TERMIOS_H)
# include <termios.h>
# include <sys/ioctl.h>
# ifndef termio
# define termio termios
# endif /* !termio */
#else /* !HAVE_TERMIO_H && !HAVE_TERMIOS_H */
# include <sys/ioctl.h>
#endif
#ifdef HAVE_BSDTTY_H
#include <sys/bsdtty.h>
#ifndef TIOCNOTTY
#define TIOCNOTTY _IO('t', 113) /* HP-UX void tty definition */
#endif
#endif /* HAVE_BSDTTY_H */
#ifdef NEED_INET_H
/*#define STTY_HACK*/
#endif
#ifndef OLCUC
# define OLCUC 0 /* Missing in FreeBSD, GNU/kFreeBSD */
#endif /* !OLCUC */
#include "splitvt.h"
int tty_reset(int fd);
/*
* Initialize a pty, fork a command running under it, and then
* return the master file descriptor
*/
extern int WU_lines, WL_lines, W_columns; /* From vt100.c */
#define UPPER 0 /* Upper window definition */
#define LOWER 1 /* Lower window definition */
int pty_open(argv, childpid, win)
char *argv[];
int *childpid;
int win; /* 0 for upper, 1 for lower */
{
void dropctty();
int get_master_pty(), get_slave_pty();
char *get_ttyname();
#ifndef TIOCGWINSZ
char LINES[12], COLUMNS[12];
#endif
char SPLITVT[24];
int returnfd, slave_fd;
/* Get the master pty file descriptor */
if ( (returnfd=get_master_pty()) < 0 )
return(-1);
/* Fork and go! */
if ( ((*childpid)=fork()) < 0 )
return(-1);
else if ( (*childpid) == 0 )
{
dropctty(); /* Lose controlling tty */
if ( (slave_fd=get_slave_pty()) < 0 )
{
perror("Can't open slave tty");
exit(128);
}
close(0); close(1); close(2);
dup(slave_fd); dup(slave_fd); dup(slave_fd);
close(slave_fd); close(returnfd);
/* Reattatch the new tty as the controlling terminal */
/* Under old UNIX, just opening the new tty after
losing the controlling tty is good enough.
Under newer Unices, it requires an ioctl(). */
#ifdef TIOCSCTTY
(void) ioctl(0, TIOCSCTTY, 0);
#endif
/* Set the lines and columns on the new tty */
#ifdef TIOCGWINSZ /* We don't want to set the environment if possible */
pty_setwin(0, win);
#else
if ( win == UPPER )
sprintf(LINES, "LINES=%d", WU_lines);
else
sprintf(LINES, "LINES=%d", WL_lines);
myputenv(LINES);
sprintf(COLUMNS, "COLUMNS=%d", W_columns);
myputenv(COLUMNS);
#endif /* TIOCGWINSZ */
/* Set the SPLITVT environment variable for shell scripts */
if ( win == UPPER )
sprintf(SPLITVT, "SPLITVT=upper");
else
sprintf(SPLITVT, "SPLITVT=lower");
myputenv(SPLITVT);
myputenv("TERM=vt100"); /* Put the new TERM in the env. */
#ifdef SIGTSTP
signal(SIGTSTP, SIG_IGN);
#endif
#ifdef STTY_HACK
system("stty sane echo echoe intr '^C' erase '^H'");
#else
(void) tty_reset(0);
#endif
/* "touch" the tty so 'w' reports proper idle times */
(void) utime(get_ttyname(), NULL);
/* Set our gid to our real gid if necessary */
if (setgid(getgid()) != 0) {
perror("setgid");
exit(1);
}
/* Set our uid to our real uid if necessary */
if (setuid(getuid()) != 0) {
perror("setuid");
exit(1);
}
/* Run the requested program, with possible leading dash. */
execvp(((*argv[0] == '-') ? argv[0]+1 : argv[0]), argv);
perror(argv[0]);
exit(255);
}
return(returnfd);
}
/*
* Pseudo-terminal routines for Unix System V Release 3.2 and BSD4.2-3
*/
extern int errno;
int master_fd;
char tty_name[64]={'\0'};
char pty_name[64]={'\0'};
char *get_ttyname()
{
if ( tty_name[0] )
return(tty_name);
return(NULL);
}
#ifdef IRIX /* IRIX System V for SGI machines */
extern char *_getpty();
int get_master_pty()
{
char *ttyptr;
ttyptr=_getpty(&master_fd, O_RDWR, 0600, 0);
if ( ttyptr == NULL || strlen(ttyptr)+1 > sizeof(tty_name) )
return(-1);
else
strcpy(tty_name, ttyptr);
return(master_fd);
}
/*
* Open the slave half of a pseudo-terminal.
*/
int get_slave_pty()
{
int slave_fd;
char *slavename;
slavename=tty_name;
if (slavename == NULL) {
close(master_fd);
return(-1);
}
if ( (slave_fd=open(slavename, O_RDWR)) < 0 ) /* open the slave */
{
close(master_fd);
return(-1);
}
return(slave_fd);
}
#else /* ! IRIX */
#if defined(SOLARIS) || defined(__GLIBC__) /* System V.4 pty routines from W. Richard Stevens */
#ifdef SOLARIS
#include <stropts.h>
#define DEV_CLONE "/dev/ptmx"
#endif
int get_master_pty()
{
char *ttyptr;
#ifdef SOLARIS
if ( (master_fd=open(DEV_CLONE, O_RDWR)) < 0 )
return(-1);
#else /* GLIBC */
if ( (master_fd=getpt()) == -1 )
return (-1);
#endif
if ( grantpt(master_fd) < 0 ) /* grant access to slave */
{
close(master_fd);
#ifdef DEBUG
perror("grantpt()");
#endif
return(-1);
}
if ( unlockpt(master_fd) < 0 ) /* clear slave's lock flag */
{
close(master_fd);
return(-1);
}
ttyptr=ptsname(master_fd);
if ( ttyptr == NULL || strlen(ttyptr)+1 > sizeof(tty_name) )
{
close(master_fd);
return(-1);
}
else
strcpy(tty_name, ttyptr);
return(master_fd);
}
/*
* Open the slave half of a pseudo-terminal.
*/
int get_slave_pty()
{
int slave_fd;
char *slavename;
slavename=tty_name;
if ( (slave_fd=open(slavename, O_RDWR)) < 0 ) /* open the slave */
{
close(master_fd);
return(-1);
}
#ifdef SOLARIS
if ( ioctl(slave_fd, I_PUSH, "ptem") < 0 )
{
close(master_fd);
close(slave_fd);
return(-1);
}
if ( ioctl(slave_fd, I_PUSH, "ldterm") < 0 )
{
close(master_fd);
close(slave_fd);
return(-1);
}
if ( ioctl(slave_fd, I_PUSH, "ttcompat") < 0 )
{
close(master_fd);
close(slave_fd);
return(-1);
}
#endif
return(slave_fd);
}
#else /* BSD, Sun/OS, AIX, ULTRIX, HP-UX, AT&T SYSV */
#include <setjmp.h>
#ifndef X_OK
#define R_OK 4 /* Test for Read permission */
#define W_OK 2 /* Test for Write permission */
#define X_OK 1 /* Test for eXecute permission */
#endif /* X_OK */
jmp_buf next;
void trynext()
{
longjmp(next, 2);
}
int get_master_pty()
{
int i, master_fd;
char *ptr;
struct stat statbuff;
#ifdef PTYCHAR
static char ptychar[]=PTYCHAR; /* X */
static char hexdigit[]=HEXDIGIT; /* Y */
#else
static char ptychar[]="pqrstuvwxyzPQRST"; /* X */
static char hexdigit[]="0123456789abcdef"; /* Y */
#endif
for (ptr=ptychar; *ptr != 0; ptr++)
{
strcpy(pty_name, "/dev/ptyXY");
pty_name[8]=(*ptr); /* X */
pty_name[9]='0'; /* Y */
if ( stat(pty_name, &statbuff) < 0 )
break;
#ifdef OLDDEBUG
fprintf(stderr, "statted.\n");
#endif
i=(-1); /* Initialize i */
/* Set a time limit for the open */
if ( setjmp(next) == -1 )
return(-1);
signal(SIGALRM, trynext);
for ( ++i; hexdigit[i]; ++i)
{
pty_name[5]='p';
pty_name[9]=hexdigit[i];
alarm(2); /* Set an open timeout */
if ( (master_fd=open(pty_name, O_RDWR)) >= 0 )
{
alarm(0); /* Reset the alarm */
pty_name[5]='t';
sprintf(tty_name, "%s", pty_name);
#ifdef OLDDEBUG
fprintf(stderr, "tty: %s\n", tty_name);
#endif
if ( access(tty_name, R_OK|W_OK) == 0 ) {
signal(SIGALRM, SIG_DFL);
return (master_fd);
} else {
pty_name[5]='p';
(void) close(master_fd);
}
}
/* reset the alarm */
alarm(0);
}
}
return(-1);
}
/* Open the slave half of a pseudo-terminal. */
int get_slave_pty()
{
int slave_fd;
if ( (slave_fd=open(tty_name, O_RDWR)) < 0 )
{
close(master_fd);
return(-1);
}
return(slave_fd);
}
#endif /* if linux or SOLARIS */
#endif /* if IRIX */
/* These are the binary data functions that I am using instead of
bcopy() and bzero(), written by Richard A. O'Keefe.
Thanks!
*/
void d_copy(src, dst, len)
register char *src, *dst;
register int len;
{
while (--len >= 0) *dst++ = *src++;
}
void d_zero(dst, len)
register char *dst;
register int len;
{
while (--len >= 0) *dst++ = 0;
}
/* Here are the Terminal manipulation routines... */
/* Code to disassociate from my tty. Yay! :) */
void dropctty()
{
#ifndef CIBAUD
int fd;
#endif
#if defined(_POSIX_SOURCE) || defined(SOLARIS) || \
defined(__386BSD__) || defined(__FreeBSD__)
setsid(); /* The POSIX solution is simple. :) */
#else
#ifdef TIOCNOTTY /* We want to get HP-UX, BSD, and Sun/OS here */
setpgrp(0, 0);
#ifndef CIBAUD /* Sun/OS doesn't need to do TIOCNOTTY. */
if ( (fd=open("/dev/tty", O_RDWR)) > (-1) )
{
if (ioctl(fd, TIOCNOTTY, 0) < 0)
{
perror("ioctl TIOCNOTTY error");
fprintf(stderr, "\r");
}
close(fd);
}
#endif /* CIBAUD */
#else /* SYSV */
setpgrp();
#endif /* TIOCNOTTY */
#endif /* _POSIX_SOURCE */
}
#if defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)
/* Get the modes of the controlling tty and save them. Saves
ttymodes in tty_mode and returns -1 if ioctl fails. */
struct termio tty_mode; /* Save tty mode here */
static int tty_init=0;
int tty_getmode(fd)
int fd;
{
d_zero((char *)&tty_mode, sizeof(struct termio));
tty_init=1; /* Flag: we have initialized the tty_mode struct */
if ( ! isatty(fd) )
return(0);
#ifdef OLDDEBUG
fprintf(stderr, "Getting tty modes for tty_mode.\r\n");
#endif
#ifdef HAVE_TERMIOS_H
if (tcgetattr(fd, &tty_mode) < 0)
#else /* HAVE_TERMIO_H */
if (ioctl(fd, TCGETA, (char *) &tty_mode) < 0)
#endif
{
#ifdef DEBUG
perror("tty_getmode(): ioctl error");
#endif
return(-1);
}
return(0);
}
/* Set a tty to a sane mode */
int tty_sane(fd)
int fd;
{
struct termio temp_mode;
if ( ! isatty(fd) )
return(0);
if ( ! tty_init )
{
#ifdef HAVE_TERMIOS_H
if (tcgetattr(fd, &tty_mode) < 0)
#else /* HAVE_TERMIO_H */
if (ioctl(fd, TCGETA, (char *) &tty_mode) < 0)
#endif
return(-1);
}
#ifdef SEVEN_BIT
temp_mode.c_iflag=(tty_mode.c_iflag|(BRKINT|IGNPAR|ISTRIP|ICRNL|IXON));
temp_mode.c_oflag=(tty_mode.c_oflag|(OPOST|ONLCR));
#else
temp_mode.c_iflag=(tty_mode.c_iflag|(BRKINT|IGNPAR|ICRNL|IXON));
temp_mode.c_cflag=(tty_mode.c_cflag|(CS8|CREAD));
#endif
temp_mode.c_lflag=(tty_mode.c_lflag|(ISIG|ICANON|ECHO|ECHOE|ECHOK));
temp_mode.c_cflag=(tty_mode.c_cflag|(CS7|PARENB|CREAD));
temp_mode.c_cc[VERASE]=('H'^64);
temp_mode.c_cc[VKILL]=('U'^64);
temp_mode.c_cc[VQUIT]=('\\'^64);
temp_mode.c_cc[VINTR]=('C'^64);
temp_mode.c_cc[VEOF]=('D'^64);
/* TCSADRAIN/TCSETAW is important for letting tty input drain. */
#ifdef HAVE_TERMIOS_H
if ( tcsetattr(fd, TCSADRAIN, &temp_mode) < 0 )
#else /* HAVE_TERMIO_H */
if ( ioctl(fd, TCSETAW, (char *)&temp_mode) < 0 )
#endif
{
#ifdef DEBUG
perror("Can't set tty modes");
#endif
return(-1);
}
return(0);
}
/* Set a terminal in raw mode */
int tty_raw(fd)
int fd; /* of tty device */
{
struct termio temp_mode;
if ( ! tty_init )
return(-1);
if ( ! isatty(fd) )
return(0);
#ifdef HAVE_TERMIOS_H
if ( tcgetattr(fd, &temp_mode) < 0 )
#else /* HAVE_TERMIO_H */
if ( ioctl(fd, TCGETA, (char *)&temp_mode) < 0 )
#endif
return(-1);
#ifdef SEVEN_BIT
temp_mode.c_iflag=(IGNBRK | ISTRIP); /* turn off all input control */
#else
temp_mode.c_iflag=(IGNBRK); /* turn off all input control */
#endif
temp_mode.c_oflag &= ~(OLCUC | ONLCR | OCRNL | ONLRET);
/* disable output post-processing */
temp_mode.c_lflag = 0;
temp_mode.c_cc[VMIN]=1; /* 1 or more chars satisfy read */
temp_mode.c_cc[VTIME]=0; /* 10'ths of seconds between chars */
/* TCSADRAIN/TCSETAW is important for letting tty input drain. */
#ifdef HAVE_TERMIOS_H
if (tcsetattr(fd, TCSADRAIN, &temp_mode) < 0)
#else /* HAVE_TERMIO_H */
if (ioctl(fd, TCSETAW, (char *) &temp_mode) < 0)
#endif
return(-1);
return(0);
}
/* Restore terminal's mode to whatever it was on the most
recent call to the tty_getmode() function. */
int tty_reset(fd)
int fd;
{
if ( ! tty_init )
return(-1);
if ( ! isatty(fd) )
return(0);
/* TCSADRAIN/TCSETAW is important for letting tty input drain. */
#ifdef HAVE_TERMIOS_H
if (tcsetattr(fd, TCSADRAIN, &tty_mode) < 0)
#else /* HAVE_TERMIO_H */
if (ioctl(fd, TCSETAW, (char *) &tty_mode) < 0)
#endif
return(-1);
return(0);
}
#else /* no /usr/include/termio{,s}.h */
#ifdef NEED_COMPAT_H /* FreeBSD needs this */
#include <sys/ioctl_compat.h>
#endif /* NEED_COMPAT_H */
/* Set a tty to a sane mode */
int tty_sane(fd)
int fd;
{
struct sgttyb temp_mode;
if ( ! isatty(fd) )
return(0);
if (ioctl(fd, TIOCGETP, (char *) &temp_mode) < 0)
return(-1);
temp_mode.sg_flags|=ECHO;
if (ioctl(fd, TIOCSETP, (char *) &temp_mode) < 0)
return(-1);
return(0);
}
/* Get the modes of the controlling tty and save them. Saves
ttymodes in tty_mode and returns 1 if ioctl fails. */
static struct sgttyb tty_mode; /* save tty mode here */
int tty_getmode(fd)
int fd;
{
if ( ! isatty(fd) )
return(0);
if (ioctl(fd, TIOCGETP, (char *) &tty_mode) < 0)
return(-1);
return(0);
}
/*
* Put a terminal device into RAW mode with ECHO off.
* Before doing so we first save the terminal's current mode,
* assuming the caller will call the tty_reset() function
* (also in this file) when it's done with raw mode.
*/
int tty_raw(fd)
int fd; /* of terminal device */
{
struct sgttyb temp_mode;
if ( ! isatty(fd) )
return(0);
temp_mode = tty_mode;
temp_mode.sg_flags |= RAW; /* turn RAW mode on */
temp_mode.sg_flags &= ~ECHO; /* turn ECHO off */
if (ioctl(fd, TIOCSETP, (char *) &temp_mode) < 0)
return(-1);
return(0);
}
/*
* Restore a terminal's mode to whatever it was on the most
* recent call to the tty_getmode() function above.
*/
int tty_reset(fd)
int fd; /* of terminal device */
{
if ( ! isatty(fd) )
return(0);
if (ioctl(fd, TIOCSETP, (char *) &tty_mode) < 0)
return(-1);
return(0);
}
#endif /* HAVE_TERMIO_H */
/* Set the pty window size to the size of the virtual window */
#ifdef TIOCSWINSZ
static struct /* winsize */ {
unsigned short ws_row; /* rows, in characters */
unsigned short ws_col; /* columns, in characters */
unsigned short ws_xpixel; /* horizontal size - not used */
unsigned short ws_ypixel; /* vertical size - not used */
} mywinz;
void pty_setwin(fd, win)
int fd; /* The pty file descriptor */
int win; /* 0 for upper, 1 for lower window */
{
if ( win == UPPER )
mywinz.ws_row=WU_lines;
else
mywinz.ws_row=WL_lines;
mywinz.ws_col=W_columns;
mywinz.ws_xpixel=0;
mywinz.ws_ypixel=0;
(void) ioctl(fd, TIOCSWINSZ, &mywinz);
}
#else
void pty_setwin(fd, win)
int fd;
int win;
{
/* Bogus routine */
}
#endif /* TIOCSWINSZ */
/*
* Write "n" bytes to a descriptor.
* Use in place of write() when fd is a stream socket.
*/
int writen(fd, ptr, nbytes)
register int fd;
register char *ptr;
register int nbytes;
{
int nleft, nwritten;
nleft = nbytes;
while (nleft > 0) {
nwritten = write(fd, ptr, nleft);
if (nwritten <= 0)
return(nwritten); /* error */
nleft -= nwritten;
ptr += nwritten;
}
return(nbytes - nleft);
}
/*
A function to put strings in the environment using malloc().
Returns a pointer to the environment string or NULL if malloc() fails.
*/
char *myputenv(string)
char *string;
{
extern char **environ; /* The process environment strings */
char *newptr, **envptr;
int distance, n=0;
for ( distance=0; ((*(string+distance)) &&
((*(string+distance)) != '=')); ++distance );
if ( ! (*(string+distance)) )
return(NULL);
else
++distance;
if ( (newptr=(char *)malloc(strlen(string)+1)) == NULL )
return(NULL);
else
strcpy(newptr, string);
for ( envptr=environ; *envptr; ++envptr, ++n ) {
if ( strncmp(*envptr, string, distance) == 0 ) {
*envptr=newptr;
return(newptr);
}
}
*envptr=newptr;
++envptr;
*envptr=NULL;
return(newptr);
}
/* * * * * * * Routines to parse a line into an array of tokens * * * * * * */
static int istoken(c, tokens)
char c;
char *tokens;
{
while ( *tokens ) {
if ( c == *(tokens++) )
return(1);
}
return(0);
}
/* This version of tokenize is destructive to the line it parses. */
void tokenize(array, size, line, tokens)
char *array[];
int size;
char *line;
char *tokens;
{
char *head;
int i=0;
for ( head=line; *line && i < size-2; ) {
if ( istoken(*line, tokens) ) {
*(line++)='\0';
array[i++]=head;
while ( istoken(*line, tokens) )
++line;
head=line;
} else
++line;
}
array[i++]=head;
array[i]=NULL;
}
/* Return the pathname of the command, or NULL if it's not in our PATH */
/* Warning: We use a static buffer that is overwritten at each invocation. */
char *pathsearch(command, secure)
char *command;
int secure;
{
#ifndef S_IFREG
#define S_IFREG 0100000
#endif
char *path, *newpath, *paths[256];
static char buffer[1024];
int i;
struct stat sb;
if ( (path=(char *)getenv("PATH")) == NULL )
return(NULL);
if ( (newpath=(char *)malloc(strlen(path)+1)) == NULL )
return(NULL);
strcpy(newpath, path);
tokenize(paths, 256, newpath, ":");
for ( i=0; paths[i]; ++i ) {
if ( secure && paths[i][0] != '/' ) {
/* Only allow full pathnames */
continue;
}
/* Make sure the file exists */
sprintf(buffer, "%s/%s", paths[i], command);
if ( stat(buffer, &sb) != 0 )
continue;
/* Make sure it's a regular file */
if ( (sb.st_mode & S_IFREG) != S_IFREG )
continue;
/* Now make sure we can execute it */
if ( sb.st_uid == getuid() ) {
/* User execute permission? */
if ( sb.st_mode & 0100 )
break;
} else if ( sb.st_gid == getgid() ) {
/* Group execute permission? */
if ( sb.st_mode & 0010 )
break;
} else {
/* Other execute permission? */
if ( sb.st_mode & 0001 )
break;
}
}
(void) free(newpath);
if ( paths[i] == NULL )
return(NULL);
else
return(buffer);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Safe version of popen() and pclose(), they reset the uid and gid. */
FILE *safe_popen(command, type)
char *command;
char *type;
{
char *argv[4];
int pipe_fds[2];
int rw;
if ( strcmp(type, "r") == 0 )
rw=0; /* READ access for parent */
else if ( strcmp(type, "w") == 0 )
rw=1; /* WRITE access for parent */
else
return(NULL); /* Unsupported type */
if ( pipe(pipe_fds) < 0 ) {
return(NULL);
}
switch(fork()) {
case 0: /* Child runs here */
/* Reassign file descriptors */
if ( rw == 0 ) {
close(1); dup(pipe_fds[1]); close(0);
} else {
close(0); dup(pipe_fds[0]); close(1);
}
close(pipe_fds[0]); close(pipe_fds[1]);
/* Set our uid to our real uid if necessary */
if (setgid(getgid()) != 0) {
perror("setgid");
exit(1);
}
if (setuid(getuid()) != 0) {
perror("setuid");
exit(1);
}
/* Run the requested program */
argv[0]="/bin/sh";
argv[1]="-c";
argv[2]=command;
argv[3]=NULL;
execvp(argv[0], argv);
fprintf(stderr, "Can't execute %s: ", argv[0]);
perror("");
exit(255);
case -1: /* fork() error */
close(pipe_fds[0]);
close(pipe_fds[1]);
return(NULL);
break;
default: /* Parent runs */
break;
}
if ( rw == 0 )
close(pipe_fds[1]);
else
close(pipe_fds[0]);
return(fdopen(pipe_fds[rw], type));
}
int safe_pclose(pipefp)
FILE *pipefp;
{
int status;
/* Close the pipe */
fclose(pipefp);
/* Wait for the child to terminate */
wait(&status);
return(status);
}
|