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
|
/*
** Actually does the killing of terminal jobs
*/
#include <sys/types.h>
#ifdef __hpux
# include <sys/ptyio.h>
# include <termios.h> /* for ioctl kill VQUIT and termios struct */
# define seteuid(x) setresuid(-1,x,-1) /* No seteuid in HP-UX */
#endif /* __hpux */
#include <sys/file.h>
#include <pwd.h> /* For struct passwd and getpwnam */
#include <errno.h> /* For errno */
#include <sys/time.h> /* For time() */
#include <sys/resource.h> /* For rlimit stuff */
#include <fcntl.h> /* For open() */
#ifndef UTMPPID
# include <termios.h> /* for ioctl kill VQUIT and termios struct */
#endif
#include "idled.h"
#ifdef SYSV
# include <termios.h>
#endif /* SYSV */
#include <signal.h> /* For "kill" and SIGKILL */
static char message[] = "\n\n\007Logged out by the system.\n";
extern char *ctime();
time_t time();
void close_descriptors();
void pidkill();
#ifndef UTMPPID
# ifdef PS_HACK
void ps_hack_kill();
# else /* !PS_HACK */
int thangup();
# endif
#endif
#ifndef DEBUG
# define DEBUG 0
#endif
#ifndef DEBUG
# define DEBUG 0
#endif
#if (DEBUG > 0)
# define debug(level,string) if (DEBUG > level) logfile string
#else
# define debug(level,string) /* Erase debug code, since DEBUG is off */
#endif
/**************************************************************************
* Disconnect the person logged on to tty "dev". *
* On SYSV systems, just slaughter the shell. *
* On BSD systems, force the line to hangup. *
* On some other systems, such as Ultrix, find the shell pid from *
* the tty parent group and slaughter the shell that way. *
**************************************************************************/
void zap(him, type, do_msg)
struct user *him;
char *type;
int do_msg;
{
int td;
time_t tempus;
struct passwd *pw;
/* close all the child's descriptors (from the fork() in warn.c) */
close_descriptors();
#ifdef MAILMESSAGE
if (!do_msg)
{
char cmd[256];
sprintf(cmd,"%s -s 'Logged out by the system.' %s < %s",MAILPATH,
him->uid, MAILMESSAGEFILE);
system(cmd);
}
#endif /* MAILMESSAGE */
/* now tell him it's over, and disconnect */
if (do_msg)
{
td = open (him->line, O_RDWR, 0600);
(void) tcflow(td, TCOON);
}
tempus = time ((time_t *) NULL);
/* Become the user for the killing and warning */
pw = getpwnam(him->uid);
if (pw == NULL)
logfile("Error getting user information for %s in zap. Will attempt kill\n\twithout uid changing.",
him->uid);
else
seteuid(pw->pw_uid);
if (td >= 0 && do_msg)
{
#if (DEBUG < 1)
(void) write (td, message, sizeof (message));
(void) fsync (td);
#ifndef __linux__ /* discard, as libbsd does this */
(void) ioctl (td, TCIOFLUSH, (char *) 0);
#endif /* __linux__ */
(void) close (td);
#endif /* DEBUG < 1 */
}
else if (do_msg)
{
seteuid(0);
logfile
(
"%19.19s : couldn't open %s for %s.",
ctime (&tempus),
him->line,
him->uid
);
}
#if (DEBUG < 1)
#if defined(UTMPPID)
pidkill(him->pid);
#elif defined(PS_HACK) /* !UTMPPID */
ps_hack_kill(him);
#else /* !UTMPPID && !PS_HACK */
td = open (him->line, O_RDWR, 0600);
if (td < 0)
{
seteuid(0);
logfile
(
"%19.19s : Open failed on tty %s. Errno = %d",
ctime (&tempus),
him->line,
errno
);
/* Neither kill method from here can work, so quit */
return;
}
#ifndef BAD_PGRP
if ((him->pid = tcgetpgrp(td)) != -1)
{
/* Make sure to kill all foreground processes, ending with the
* shell process.
*/
while ((him->pid = tcgetpgrp(td)) != -1)
pidkill(him->pid);
}
else
#endif /* !BAD_PGRP */
{
if (thangup(td,him,tempus) < 0)
return; /* Failed */
}
(void) close(td);
#endif /* !UTMPPID && !PS_HACK */
seteuid(0);
logfile
(
"%19.19s : %s on %s because %s",
ctime (&tempus),
him->uid,
him->line,
type
);
#else /* DEBUG < 1*/
debug(1,("Just pretended to kill user %s on %s because of %s with pid %d.",
him->uid, him->line, type, him->pid));
#endif /* DEBUG < 1*/
}
/**************************************************************************
* Close all of the descriptors for this process, since we recently *
* forked it in warn.c. *
**************************************************************************/
void close_descriptors()
{
int nfds, fd;
struct rlimit rl;
#ifdef RLIMIT_NOFILE
if (getrlimit (RLIMIT_NOFILE, &rl) < 0)
logfile ("Error calling system function: getrlimit");
nfds = rl.rlim_max;
#else /* RLIMIT_NOFILE */
nfds = getdtablesize ();
#endif /* RLIMIT_NOFILE */
/* Close all fds. */
#ifdef BSD_OS2
for (fd = 2; fd < nfds; ++fd) /* Don't close fd 0 or 1 on BSDI BSD/OS 2 */
/* This works around some weird bug */
#else
for (fd = 0; fd < nfds; ++fd)
#endif
{
(void) close (fd);
}
}
/**************************************************************************
* Kill a particular process. This is used for UTMPPID systems, *
* and some other systems that have good tcgetgprp() functions that *
* work properly to find the controlling shell of the terminal. *
* Currently, this procedure simply runs through several kill signals, *
* starting with a kinder SIGHUP and ending in a SIGKILL. Sometimes this *
* should call the harsher ones only if necessary. *
* *
* A problem here is that I have seen some shells, such as tcsh, which *
* hangup but do not kill their forground processes. If the user is in *
* opening, then, their login shell disappears while they remain safe in *
* openwin (on Solaris 2.x). *
**************************************************************************/
void pidkill(pid)
int pid;
{
#ifdef RUDEKILL
/* Just slaughter */
(void) kill ((pid_t) pid, SIGKILL);
#else
/* Kill nicely */
(void) kill ((pid_t) pid, SIGHUP);
(void) kill ((pid_t) pid, SIGTERM);
(void) kill ((pid_t) pid, SIGKILL);
#endif
}
/**************************************************************************
* ps_hack_kill() uses a call to 'ps' to find the pid of the process on *
* the specified tty and kills those processes. *
* Original ps hack by Scott Gifford <swgsh@genesee.freenet.org> *
**************************************************************************/
#ifdef PS_HACK
void ps_hack_kill(him)
struct user *him;
{
FILE *f;
char pscmd[50], psLine[100], *s;
int killMe;
/* We don't have the pid that we need to kill, but let's fake it. */
/* Instead of doing it the right way, fork off a ps.
* Pull off the last two chars of the "/dev/tty??" string
* and pass them along to ps as the terminal.
*/
sprintf (pscmd, "/bin/ps -t%s", &him->line[8]);
f = popen (pscmd, "r");
/* Pitch the first line (the headers line) */
fgets (psLine, 100, f);
/* Just to be safe, kill off everything on that terminal. */
while (fgets (psLine, 100, f))
{
sscanf (psLine, "%d", &killMe);
pidkill (killMe);
}
pclose (f);
}
#endif /* PS_HACK */
/**************************************************************************
* thangup() is used on systems on which we can not find the pid of the *
* of the master shell. Since we can't just call kill() on it, we will *
* try to get the current attributes for the terminal, and then change *
* them to set the baud rate to 0. This *should* tell the shell to *
* hangup, and hence the user will be kicked off. Some operating systems *
* prefer not to pay attention to it. *
**************************************************************************/
#if !defined(UTMPPID) && !defined(PS_HACK)
int thangup(td, him, tempus)
int td;
struct user *him;
time_t tempus;
{
struct termios zap_ioctl;
if (tcgetattr(td,&zap_ioctl) < 0)
{
logfile
(
"%19.19s : zap failed on tty %s for %s, due to failure of tcgetattr().",
ctime (&tempus),
him->line,
him->uid
);
(void) close(td);
return -1;
}
/* Set the speed of the terminal to B0 to tell it to hangup */
cfsetospeed(&zap_ioctl,B0);
if (tcsetattr(td,TCSANOW,&zap_ioctl) < 0)
{
logfile
(
"%19.19s : zap failed on tty %s for %s.",
ctime (&tempus),
him->line,
him->uid
);
return -1;
}
return 0;
}
#endif /* !UTMPPID && !PS_HACK*/
|