#include "uucp.h"
#include "sysfiles.h"
#include <pwd.h>
#include <utmpx.h>
#ifdef DATAKIT
#include <dk.h>
extern int dkminor();
#endif
#define ROOT 0
#define SYS 3
#define TTYGID (gid_t) 7
#define TTYMOD (mode_t) 0622
#define DEV "/dev/"
#define TELNOSIZE 32
#define LEGAL "0123456789-*#="
#define USAGE "[-h] [-v] [-w n] [-x n] [-s speed] telno ..."
#define LOG "/var/adm/ctlog"
#define TTYMON "/usr/lib/saf/ttymon"
#define TRUE 1
#define FALSE 0
int Dologin = 0;
static
int _Status;
static
pid_t _Pid = 0;
static
char
_Tty[sizeof DEV+12] = "",
*_Dev[D_MAX + 1],
_Devbuf[BUFSIZ];
static
char
*_Num,
*_Flds[7];
static
time_t _Log_on,
_Log_elpsd;
static
FILE *_Fdl;
extern int optind;
extern char *optarg, *fdig();
extern void cleanup();
extern struct passwd *getpwuid ();
extern int getto(), rddev();
static int gdev(), logproc(), exists();
static void startat(), stopat(), disconnect(), zero();
void
assert (s1, s2, i1, s3, i2)
char *s1, *s2, *s3;
int i1, i2;
{ }
void
logent (s1, s2)
char *s1, *s2;
{ }
jmp_buf Sjbuf;
int
main (argc, argv)
int argc;
char *argv[];
{
int c;
int found = 0,
errors = 0,
first = TRUE;
int count,
logprocflag,
hangup = 1,
minutes = 0;
int fdl;
struct termio termio;
typedef void (*save_sig)();
save_sig save_hup,
save_quit,
save_int;
extern void setservice(), devreset();
extern int sysaccess();
save_hup = signal (SIGHUP, cleanup);
save_quit = signal (SIGQUIT, cleanup);
save_int = signal (SIGINT, cleanup);
(void) signal (SIGTERM, cleanup);
(void) strcpy (Progname, "ct");
setservice("cu");
if ( sysaccess(EACCESS_DEVICES) != 0 ) {
(void) fprintf(stderr, "ct: can't access Devices file\n");
cleanup(101);
}
_Flds[F_NAME] = "dummy";
_Flds[F_TIME] = "Any";
_Flds[F_TYPE] = "ACU";
_Flds[F_CLASS] = "1200";
_Flds[F_PHONE] = "";
_Flds[F_LOGIN] = "";
_Flds[6] = NULL;
while ((c = getopt (argc, argv, "hvw:s:x:")) != EOF) {
switch (c) {
case 'h':
hangup = 0;
break;
case 'v':
Verbose = 1;
break;
case 'w':
minutes = atoi (optarg);
if (minutes < 1) {
(void) fprintf(stderr,
"\tusage: %s %s\n", Progname, USAGE);
(void) fprintf(stderr, "(-w %s) Wait time must be > 0\n",
optarg);
cleanup(101);
}
break;
case 's':
_Flds[F_CLASS] = optarg;
break;
case 'x':
Debug = atoi(optarg);
if (Debug < 0 || Debug > 9) {
(void) fprintf(stderr,
"\tusage: %s %s\n", Progname, USAGE);
(void) fprintf(stderr, "(-x %s) value must be 0-9\n",
optarg);
cleanup(101);
}
break;
case '?':
(void) fprintf(stderr, "\tusage: %s %s\n", Progname, USAGE);
cleanup(101);
}
}
if (optind == argc) {
(void) fprintf(stderr, "\tusage: %s %s\n", Progname, USAGE);
(void) fprintf(stderr, "No phone numbers specified!\n");
cleanup(101);
}
for (count = argc - 1; count >= optind; --count) {
_Num = argv[count];
if (strlen(_Num) >= (size_t)(TELNOSIZE - 1)) {
(void) fprintf(stderr, "ct: phone number too long -- %s\n", _Num);
++errors;
}
if ((int)strspn(_Num, LEGAL) < (int)strlen(_Num)) {
(void) fprintf(stderr, "ct: bad phone number -- %s\n", _Num);
++errors;
}
}
if (errors)
cleanup(101);
for (count = 0;; count++) {
if ( (found = gdev (_Flds)) > 0) {
(void) fprintf(stdout, "Allocated dialer at %s baud\n",
_Flds[F_CLASS]);
break;
}
else if (found == 0) {
(void) fprintf(stdout, "No %s dialers on this system\n",
fdig(_Flds[F_CLASS]) );
cleanup(101);
}
if (!first) {
VERBOSE("%s busy", (found == -1) ? "Dialer is" : "Dialers are");
VERBOSE(" (%d minute(s))\n", count);
if (count < minutes) {
sleep(60);
continue;
}
break;
}
first = FALSE;
(void) fprintf(stdout, "The (%d) %s dialer%s busy\n", -found,
_Flds[F_CLASS], (found == -1 ? " is" : "s are"));
if (minutes) {
(void) fprintf(stdout, "Waiting for %d minute%s\n", minutes,
(minutes > 1 ? "s" : "") );
sleep(60);
continue;
}
if (!isatty(0) ) {
cleanup(101);
}
(void) fputs("Do you want to wait for dialer? (y for yes): ", stdout);
if ((c = getchar ()) == EOF || tolower (c) != 'y')
cleanup(101);
while ( (c = getchar()) != EOF && c != '\n')
;
(void) fputs ("Time, in minutes? ", stdout);
(void) scanf ("%d", &minutes);
while ( (c = getchar()) != EOF && c != '\n')
;
if (minutes <= 0)
cleanup(101);
(void) fputs ("Waiting for dialer\n", stdout);
sleep(60);
continue;
}
if (found < 0) {
(void) fputs("*** TIMEOUT ***\n", stdout);
cleanup(101);
}
(void) signal(SIGHUP, SIG_IGN);
if (!isatty(0))
hangup = 0;
if (hangup) {
do {
(void) fputs ("Confirm hang-up? (y/n): ", stdout);
switch (c=tolower(getchar())) {
case EOF:
case 'n':
cleanup(101);
break;
case 'y':
break;
default:
while ( c != EOF && c != '\n' )
c=getchar();
break;
}
} while (c != 'y');
if ( isatty(2) ) {
Verbose = 0;
Debug = 0;
(void) close (2);
}
(void) ioctl (0, TCGETA, &termio);
termio.c_cflag = 0;
(void) ioctl (0, TCSETAW, &termio);
(void) sleep (5);
}
(void) close(0);
(void) close(1);
for (count = optind; count < argc; count++) {
_Flds[F_PHONE] = argv[count];
rmlock(CNULL);
devreset();
fdl = getto(_Flds);
if (fdl >= 0) {
if ( (logprocflag = logproc(Dc)) )
rmlock(CNULL);
_Fdl = fdopen(fdl, "r+");
(void) sprintf(_Tty, "%s%s", DEV, Dc);
break;
}
}
if (count == argc)
cleanup(101);
VERBOSE("Connected\n%s", "");
#ifdef DATAKIT
if (!strcmp(_Dev[D_CALLER], "DK")) {
strcpy(_Tty, dtnamer(dkminor(fdl)));
strcpy(Dc, (strrchr(_Tty, '/')+1));
if ((_Fdl = fopen(_Tty, "r+")) == NULL) {
(void) fprintf(stderr, "ct: Cannot open %s, errno %d\n",
_Tty, errno);
cleanup(101);
}
}
#endif
if (save_hup == SIG_IGN)
(void) signal (SIGHUP, SIG_IGN);
else
(void) signal (SIGHUP, disconnect);
if (save_quit == SIG_IGN)
(void) signal (SIGQUIT, SIG_IGN);
else
(void) signal (SIGQUIT, disconnect);
if (save_int == SIG_IGN)
(void) signal (SIGINT, SIG_IGN);
else
(void) signal (SIGINT, disconnect);
(void) signal (SIGTERM, disconnect);
(void) signal (SIGALRM, disconnect);
(void) sleep (2);
_Log_on = time ((time_t *) 0);
if (logprocflag) {
(void) fputs("Hit carriage return ", _Fdl);
(void) fclose(_Fdl);
CDEBUG(4, "there is a login process; exit\n%s", "");
exit(0);
}
CDEBUG(4, "start login process (%s ", TTYMON);
CDEBUG(4, "-g -h -t 60 -l %s)\n", fdig(_Flds[F_CLASS]));
for (;;) {
pid_t w_ret;
switch(_Pid = fork()) {
case -1:
if ((!hangup || Verbose))
(void) fputs ("ct: can't fork for login process\n", stderr);
cleanup(101);
case 0:
startat ();
(void) close(2);
if ( fdl != 0 ) {
(void) close(0);
dup(fdl);
}
(void) signal(SIGHUP, SIG_DFL);
(void) execl(TTYMON, "ttymon", "-g", "-h", "-t", "60",
"-l", fdig(_Flds[F_CLASS]), (char *) 0);
cleanup(101);
default:
break;
}
while ((w_ret = wait(&_Status)) != _Pid)
if (w_ret == -1 && errno != EINTR) {
VERBOSE("ct: wait failed errno=%d\n", errno);
cleanup(101);
}
if ((_Status & 0xff00) < 0) {
if (!hangup)
VERBOSE("ct: can't exec login process\n%s", "");
cleanup(101);
}
stopat(_Flds[F_PHONE]);
rewind (_Fdl);
(void) fputs ("\nReconnect? ", _Fdl);
rewind (_Fdl);
(void) alarm (20);
c = getc (_Fdl);
if (c == EOF || tolower (c) == 'n')
disconnect (0);
while ( (c = getc(_Fdl)) != EOF && c != '\n')
;
(void) alarm (0);
}
}
static void
disconnect(int code)
{
struct termio termio;
(void) alarm(0);
(void) signal (SIGALRM, SIG_IGN);
(void) signal (SIGINT, SIG_IGN);
(void) signal (SIGTERM, SIG_IGN);
_Log_elpsd = time ((time_t *) 0) - _Log_on;
(void) ioctl (fileno(_Fdl), TCGETA, &termio);
termio.c_cflag = 0;
(void) ioctl (fileno(_Fdl), TCSETAW, &termio);
(void) fclose (_Fdl);
DEBUG(5, "Disconnect(%d)\n", code);
VERBOSE("Disconnected\n%s", "");
if ((code != 0) && (code != SIGALRM))
stopat(_Flds[F_PHONE]);
cleanup(code);
}
void
cleanup (code)
int code;
{
CDEBUG(5, "cleanup(%d)\n", code);
rmlock (CNULL);
if (*_Tty != '\0') {
CDEBUG(5, "chmod/chown %s\n", _Tty);
if (chown(_Tty , UUCPUID, TTYGID) < 0 ) {
CDEBUG(5, "Can't chown to uid=%u, ", UUCPUID);
CDEBUG(5, "gid=%u\n", TTYGID);
}
if (chmod(_Tty , TTYMOD) < 0) {
CDEBUG(5, "Can't chmod to %lo\n", (unsigned long) TTYMOD);
}
}
if (_Pid) {
(void) signal(SIGHUP, SIG_IGN);
(void) signal(SIGQUIT, SIG_IGN);
(void) kill (_Pid, SIGKILL);
}
exit (code);
}
static int
gdev (flds)
char *flds[];
{
int count = 0;
extern void devreset();
devreset();
while (rddev ("ACU", _Dev, _Devbuf, D_MAX) != FAIL) {
if (!EQUALS (flds[F_TYPE] , _Dev[D_TYPE]))
continue;
if (!EQUALS (flds[F_CLASS] , _Dev[D_CLASS]))
continue;
count++;
if (fn_cklock(_Dev[D_LINE]) == FAIL)
continue;
return (count);
}
return (- count);
}
static int
logproc(line)
char *line;
{
struct utmpx *u;
while ((u = getutxent()) != NULL) {
if (u->ut_type == LOGIN_PROCESS
&& EQUALS(u->ut_line, line)
&& EQUALS(u->ut_user, "LOGIN") ) {
CDEBUG(7, "ut_line %s, ", u->ut_line);
CDEBUG(7, "ut_user %s, ", u->ut_user);
CDEBUG(7, "ut_id %.4s, ", u->ut_id);
CDEBUG(7, "ut_pid %d\n", u->ut_pid);
if (kill(u->ut_pid, 0) == 0 || errno == EPERM) {
CDEBUG(4, "process still active\n%s", "");
return(1);
}
}
}
return(0);
}
static void
startat ()
{
struct utmpx utmpxbuf, *u;
int fd;
u = &utmpxbuf;
zero (&u -> ut_user[0], sizeof (u -> ut_user));
zero (&u -> ut_line[0], sizeof (u -> ut_line));
u -> ut_id[0] = 'c';
u -> ut_id[1] = 't';
u -> ut_id[2] = _Tty[strlen(_Tty)-2];
u -> ut_id[3] = _Tty[strlen(_Tty)-1];
u -> ut_pid = getpid ();
u -> ut_exit.e_termination = 0;
u -> ut_exit.e_exit = 0;
u -> ut_type = INIT_PROCESS;
time (&u -> ut_xtime);
setutxent ();
strncpy (&u -> ut_user[0], "ttymon", sizeof (u -> ut_user));
strncpy (&u -> ut_line[0], Dc, sizeof (u -> ut_line));
pututxline (u);
if ((fd = open(WTMPX_FILE, O_WRONLY | O_APPEND)) != -1) {
(void) write(fd, u, sizeof(*u));
(void) close(fd);
}
endutxent ();
return;
}
static void
stopat (num)
char *num;
{
struct utmpx utmpxbuf, *u;
int fd;
FILE * fp;
setutxent();
u = &utmpxbuf;
zero (&u -> ut_user[0], sizeof (u -> ut_user));
zero (&u -> ut_line[0], sizeof (u -> ut_line));
u -> ut_id[0] = 'c';
u -> ut_id[1] = 't';
u -> ut_id[2] = _Tty[strlen(_Tty)-2];
u -> ut_id[3] = _Tty[strlen(_Tty)-1];
u -> ut_pid = (pid_t) _Pid;
u -> ut_type = USER_PROCESS;
if (u = getutxid (u)) {
utmpxbuf = *u;
u = &utmpxbuf;
}
u -> ut_exit.e_termination = _Status & 0xff;
u -> ut_exit.e_exit = (_Status >> 8) & 0xff;
u -> ut_type = DEAD_PROCESS;
time (&u -> ut_xtime);
pututxline (u);
if ((fd = open(WTMPX_FILE, O_WRONLY | O_APPEND)) != -1) {
(void) write(fd, u, sizeof(*u));
(void) close(fd);
}
endutxent ();
if (exists (LOG) && (fp = fopen (LOG, "a")) != NULL) {
char *aptr;
int hrs,
mins,
secs;
if ( (aptr = getenv ("TZ")) != NULL )
*aptr = '\0';
(aptr = ctime (&_Log_on))[16] = '\0';
hrs = _Log_elpsd / 3600;
mins = (_Log_elpsd %= 3600) / 60;
secs = _Log_elpsd % 60;
(void) fprintf(fp, "%-8s ", getpwuid (getuid ()) -> pw_name);
(void) fprintf(fp, "(%4s) %s ", fdig(_Flds[F_CLASS]), aptr);
if (hrs)
(void) fprintf(fp, "%2d:%.2d", hrs, mins);
else
(void) fprintf(fp, " %2d", mins);
(void) fprintf(fp, ":%.2d %s\n", secs, num);
(void) fclose (fp);
}
return;
}
static int
exists (file)
char *file;
{
struct stat statb;
if (stat (file, &statb) == -1 && errno == ENOENT)
return (0);
return (1);
}
static void
zero (adr, size)
char *adr;
int size;
{
while (size--)
*adr++ = '\0';
return;
}