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
|
/* pty_unicos.c - routines to allocate ptys - for CRAY UNICOS 5.1 and 6.0 */
/*
Original by: Don Libes, NIST, 2/6/90
Hacked for Unicos 5.1 by: Frank Terhaar-Yonkers, US EPA, 1/10/91
Hacked for Unicos 6.0 by: Pete TerMaat, pete@willow.cray.com, 3/27/91
Design and implementation of this program was paid for by U.S. tax
dollars. Therefore it is public domain. However, the author and NIST
would appreciate credit if this program or parts of it are used.
*/
#include "expect_cf.h"
#include <stdio.h>
#include <signal.h>
#if defined(SIGCLD) && !defined(SIGCHLD)
#define SIGCHLD SIGCLD
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#else
extern int fork(), execl(), wait();
#endif
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#ifdef HAVE_SYS_FCNTL_H
# include <sys/fcntl.h>
#else
# include <fcntl.h>
#endif
/*#if CRAY>=60*/
#if defined(HAVE_TERMIOS)
# include <sys/termios.h>
#else
# include <sys/termio.h>
/*#endif /* 60 */*/
#endif /* defined(HAVE_TERMIOS) */
#if CRAY>=70 && defined(_CRAY2)
#include <sys/session.h>
#endif /* 70 */
#include <sys/pty.h>
#include <pwd.h>
#include <utmp.h>
#include <signal.h>
#include "exp_tty_in.h"
#include "exp_rename.h"
#ifdef HAVE_SYSCONF_H
#include <sys/sysconfig.h>
#endif
void debuglog();
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif /* MAXHOSTNAMELEN */
static char linep[] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
static char linet[] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
static int lowpty;
static int highpty;
static int realuid;
static int realgid;
static int *ptys;
static char myname[32];
static char hostname[MAXHOSTNAMELEN];
char *exp_pty_slave_name;
char *exp_pty_error;
static void
pty_stty(s,name)
char *s; /* args to stty */
char *name; /* name of pty */
{
#define MAX_ARGLIST 10240
char buf[MAX_ARGLIST]; /* overkill is easier */
RETSIGTYPE (*old)(); /* save old sigalarm handler */
#ifdef STTY_READS_STDOUT
sprintf(buf,"/bin/stty %s > %s",s,name);
#else
sprintf(buf,"/bin/stty %s < %s",s,name);
#endif
old = signal(SIGCHLD, SIG_DFL);
system(buf);
signal(SIGCHLD, old); /* restore signal handler */
}
int exp_dev_tty; /* file descriptor to /dev/tty or -1 if none */
static int knew_dev_tty;/* true if we had our hands on /dev/tty at any time */
#ifdef TIOCGWINSZ
static struct winsize winsize = {0, 0};
#endif
#if defined(TIOCGSIZE) && !defined(TIOCGWINSZ)
static struct ttysize winsize = {0, 0};
#endif
/*struct termio exp_tty_original;*/
exp_tty exp_tty_original;
#define GET_TTYTYPE 0
#define SET_TTYTYPE 1
static void
ttytype(request,fd,ttycopy,ttyinit,s)
int request;
int fd;
/* following are used only if request == SET_TTYTYPE */
int ttycopy; /* true/false, copy from /dev/tty */
int ttyinit; /* if true, initialize to sane state */
char *s; /* stty args, used only if request == SET_TTYTYPE */
{
if (request == GET_TTYTYPE) {
if (-1 == ioctl(fd, TCGETA, (char *)&exp_tty_original)) {
knew_dev_tty = FALSE;
exp_dev_tty = -1;
}
#ifdef TIOCGWINSZ
ioctl(fd,TIOCGWINSZ,&winsize);
#endif
#if defined(TIOCGSIZE) && !defined(TIOCGWINSZ)
ioctl(fd,TIOCGSIZE,&winsize);
#endif
} else { /* type == SET_TTYTYPE */
if (ttycopy && knew_dev_tty) {
(void) ioctl(fd, TCSETA, (char *)&exp_tty_current);
#ifdef TIOCSWINSZ
ioctl(fd,TIOCSWINSZ,&winsize);
#endif
#if defined(TIOCSSIZE) && !defined(TIOCSWINSZ)
ioctl(fd,TIOCGSIZE,&winsize);
#endif
}
if (ttyinit) {
/* overlay parms originally supplied by Makefile */
pty_stty(DFLT_STTY,linet);
}
/* lastly, give user chance to override any terminal parms */
if (s) {
pty_stty(s,linet);
}
}
}
void
exp_init_pty()
{
int npty;
char *myline;
lowpty=0;
#ifdef _SC_CRAY_NPTY
highpty=sysconf(_SC_CRAY_NPTY);
#else
highpty=128;
#endif /* _SC_CRAY_NPTY */
ptys = (int *) malloc(sizeof(int)*(highpty+1));
if (ptys == NULL) {
fprintf(stderr,"exp_init_pty: couldn't allocate pty array\n");
exit(1);
}
for (npty = lowpty;npty <= highpty;npty++)
ptys[npty] = 0;
realuid=getuid(); /* get REAL uid */
realgid=getgid(); /* get REAL uid */
exp_dev_tty = open("/dev/tty",O_RDWR);
knew_dev_tty = (exp_dev_tty != -1);
if (knew_dev_tty) ttytype(GET_TTYTYPE,exp_dev_tty,0,0,(char *)0);
/*
* Acquire (as root) current user name and host.
*/
(void) cuserid(myname);
(void) gethostname(hostname,sizeof(hostname));
/*
* Set the real and effective userids to root using 'setuid'. Then
* set the real and effective userids to the actual user using
* 'setreuid'. This allows using 'seteuid' to go back and forth from
* root and the actual userid. Don't ask me why it works.
*/
setuid(0);
setreuid(realuid,realuid);
}
/* returns fd of master end of pseudotty */
int
getptymaster()
{
struct stat sb;
int master;
int npty;
exp_pty_error = 0;
debuglog("getptymaster: lowpty=%d highpty=%d\n",lowpty,highpty);
for (npty = lowpty; npty <= highpty; npty++) {
if (seteuid(0) == -1) { /* we need to be root! */
debuglog("getptymaster: seteuid root errno=%d\n",
errno);
}
(void) sprintf(linep, "/dev/pty/%03d", npty);
master = open(linep, O_RDWR);
if (master < 0) {
debuglog("getptymaster: open linep=%s errno=%d\n",
linep,errno);
continue;
}
(void) sprintf(linet, "/dev/ttyp%03d", npty);
if(stat(linet, &sb) < 0) {
debuglog("getptymaster: stat linet=%s errno=%d\n",
linet,errno);
(void) close(master);
continue;
}
if (sb.st_uid || sb.st_gid || sb.st_mode != 0600) {
if (chown(linet, realuid, realgid) == -1) {
debuglog("getptymaster: chown linet=%s errno=%d\n",
linet,errno);
}
if (chmod(linet, 0600) == -1) {
debuglog("getptymaster: chmod linet=%s errno=%d\n",
linet,errno);
}
(void)close(master);
master = open(linep, 2);
if (master < 0) {
debuglog("getptymaster: reopen linep=%s errno=%d\n",
linep,errno);
continue;
}
}
if (seteuid(realuid) == -1) { /* back to who we are! */
debuglog("getptymaster: seteuid user errno=%d\n",
errno);
}
if (access(linet, R_OK|W_OK) != 0) {
debuglog("getptymaster: access linet=%s errno=%d\n",
linet,errno);
(void) close(master);
continue;
}
debuglog("getptymaster: allocated %s\n",linet);
ptys[npty] = -1;
exp_pty_slave_name = linet;
return(master);
}
if (seteuid(realuid) == -1) { /* back to who we are! */
debuglog("getptymaster: seteuid user errno=%d\n",errno);
}
return(-1);
}
/* see comment in pty_termios.c */
/*ARGSUSED*/
void
exp_slave_control(master,control)
int master;
int control;
{
}
int
getptyslave(ttycopy,ttyinit,stty_args)
int ttycopy;
int ttyinit;
char *stty_args;
{
int slave;
if (0 > (slave = open(linet, O_RDWR))) {
debuglog("getptyslave: open linet=%s errno=%d\n",linet,errno);
return(-1);
}
/* sanity check - if slave not 0, skip rest of this and return */
/* to what will later be detected as an error in caller */
if (0 != slave) {
debuglog("getptyslave: slave fd not 0\n");
return(slave);
}
if (0 == slave) {
/* if opened in a new process, slave will be 0 (and */
/* ultimately, 1 and 2 as well) */
/* duplicate 0 onto 1 and 2 to prepare for stty */
fcntl(0,F_DUPFD,1);
fcntl(0,F_DUPFD,2);
}
ttytype(SET_TTYTYPE,slave,ttycopy,ttyinit,stty_args);
return(slave);
}
setptyutmp()
{
struct utmp utmp;
if (seteuid(0) == -1) { /* Need to be root */
debuglog("setptyutmp: setuid root errno=%d\n",errno);
return(-1);
}
(void) time(&utmp.ut_time);
utmp.ut_type = USER_PROCESS;
utmp.ut_pid = getpid();
strncpy(utmp.ut_user,myname,sizeof(utmp.ut_user));
strncpy(utmp.ut_host,hostname,sizeof(utmp.ut_host));
strncpy(utmp.ut_line,linet+5,sizeof(utmp.ut_line));
strncpy(utmp.ut_id,linet+8,sizeof(utmp.ut_id));
if (pututline(&utmp) == NULL) {
debuglog("setptyutmp: pututline failed\n");
}
endutent();
if (seteuid(realuid) == -1)
debuglog("setptyutmp: seteuid user errno=%d\n",errno);
return(0);
}
setptypid(pid)
int pid;
{
int npty;
for (npty = lowpty; npty <= highpty; npty++) {
if (ptys[npty] < 0) {
debuglog("setptypid: ttyp%03d pid=%d\n",npty,pid);
ptys[npty] = pid;
break;
}
}
}
ttyp_reset()
{
int npty;
if (seteuid(0) == -1) { /* we need to be root! */
debuglog("ttyp_reset: seteuid root errno=%d\n",errno);
}
for (npty = lowpty; npty <= highpty; npty++) {
if (ptys[npty] <= 0)
continue;
(void) sprintf(linet, "/dev/ttyp%03d", npty);
debuglog("ttyp_reset: resetting %s, killing %d\n",
linet,ptys[npty]);
if (chown(linet,0,0) == -1) {
debuglog("ttyp_reset: chown %s errno=%d\n",linet,errno);
}
if (chmod(linet, 0666) == -1) {
debuglog("ttyp_reset: chmod %s errno=%d\n",linet,errno);
}
resetptyutmp();
if (kill(ptys[npty],SIGKILL) == -1) {
debuglog("ttyp_reset: kill pid=%d errno=%d\n",
ptys[npty],errno);
}
}
if (seteuid(realuid) == -1) { /* Back to who we really are */
debuglog("ttyp_reset: seteuid user errno=%d\n",errno);
}
}
resetptyutmp()
{
struct utmp utmp;
(void) setutent ();
/* set up entry to search for */
(void) strncpy(utmp.ut_id, linet + strlen(linet) - 4,
sizeof (utmp.ut_id));
utmp.ut_type = USER_PROCESS;
/* position to entry in utmp file */
if(getutid(&utmp) == NULL) {
debuglog("resetptyutmp: no utmp entry for %s\n",linet);
return(-1); /* no utmp entry for this line ??? */
}
/* set up the new entry */
strncpy(utmp.ut_name,"",sizeof(utmp.ut_name));
strncpy(utmp.ut_host,"",sizeof(utmp.ut_host));
time(&utmp.ut_time);
utmp.ut_type = DEAD_PROCESS;
utmp.ut_exit.e_exit = 0;
/* write out the entry */
pututline(&utmp);
/* close the file */
(void) endutent();
return(0);
}
|