home *** CD-ROM | disk | FTP | other *** search
- /*
- * PASSWD.C
- *
- * (C) Copyright 1989-1990 by Matthew Dillon, All Rights Reserved.
- *
- */
-
- #include "defs.h"
-
- Prototype void RunPasswdEntry (void);
- Prototype void RunPasswdEntryCmd (char *);
- Prototype void RunPasswdEntryShell (char *);
- Prototype void InteractiveTransfer (FifoHan, FifoHan, int);
- Prototype void SendBreak (int);
- Prototype int CheckLoginAndPassword (void);
-
- #define SHELL_TIMEOUT (60 * 10) /* 10 minutes of idle */
-
- static struct passwd
- *Pas;
- static char
- CmdBuf [512],
- FifoName [256],
- ScriptFile [256];
-
- #define OwnerBuf FifoName /* so we don't allocate another 256 bytes */
-
- struct Library
- *FifoBase = NULL;
- short
- RMsgIP, /* read message in-process */
- WMsgIP; /* write message in-process */
-
- int
- CheckLoginAndPassword (void)
- {
- Pas = getpwnam (LoginBuf);
-
- if (Pas == NULL)
- return 0;
-
- if (strcmp (Pas->pw_passwd, "*") == 0)
- return 1;
-
- if (strcmp (PasswdBuf, Pas->pw_passwd) == 0)
- return 1;
-
- return 0;
- }
-
- void
- RunPasswdEntry (void)
- {
- if (Pas->pw_shell_arg0 [0] == '*')
- RunPasswdEntryShell (Pas->pw_shell_arg0 + 1);
- else
- RunPasswdEntryCmd (Pas->pw_shell_arg0);
-
- return;
- }
-
- void
- RunPasswdEntryCmd (char *arg0)
- {
- struct Process
- *proc = (struct Process *) FindTask (NULL);
- APTR
- oldConsoleTask;
- BPTR
- oldDir,
- DirLock;
- char
- smallBuf [8];
-
- sprintf (CmdBuf, "%s >null: <null: %s -Getty -DEVICE %s -UNIT %ld",
- arg0,
- Pas->pw_shell_argn,
- DeviceName,
- DeviceUnit
- );
-
- if (BaudReport) {
- strcat (CmdBuf, " -BAUD ");
- sprintf (smallBuf, "%ld", BaudRate);
- strcat (CmdBuf, smallBuf);
- }
-
- if (UserReport) {
- strcat (CmdBuf, " -USER ");
- strcat (CmdBuf, LoginBuf);
- }
-
- if (HardLink)
- strcat (CmdBuf, " -HLINK");
-
- DirLock = Lock (Pas->pw_dir, SHARED_LOCK);
- if (DirLock)
- oldDir = CurrentDir (DirLock);
- oldConsoleTask = proc->pr_ConsoleTask;
-
- ulog (1, "Execute %s\n", CmdBuf);
-
- sprintf (OwnerBuf, "Getty started %s", arg0);
- NameDevUnit (DeviceName, DeviceUnit, OwnerBuf);
- Execute (CmdBuf, NullFH, NullFH);
- NameDevUnit (DeviceName, DeviceUnit, "Getty");
-
- proc->pr_ConsoleTask = oldConsoleTask;
- if (DirLock)
- UnLock (CurrentDir (oldDir));
-
- return;
- }
-
- void
- RunPasswdEntryShell (char *arg0)
- {
- long
- r;
- FifoHan
- mast_r,
- mast_w;
- int
- propBreak = 0;
- FILE
- *fi;
-
- sprintf (ScriptFile, "T:%s.%ld.script", DeviceName, DeviceUnit);
- sprintf (FifoName, "%s.%ld", DeviceName, DeviceUnit);
-
- FifoBase = OpenLibrary (FIFONAME, 0);
- if (!FifoBase) {
- ulog (-1, "Unable to open fifo.library");
- return;
- }
-
- if (fi = fopen (ScriptFile, "w")) {
- if (DOSBase->dl_lib.lib_Version >= 36) {
- fprintf (fi, "SET GettyLogin TRUE\n");
- if (UserReport) {
- fprintf (fi, "SET User %s\n", LoginBuf);
- fprintf (fi, "SET RealName %s\n", Pas->pw_real_name);
- }
- }
- fprintf (fi, "EXECUTE S:Shell-Startup\n");
- fprintf (fi, "FAILAT 99\n"); /* DOESN'T WORK! */
- fprintf (fi, "CD %s\n", Pas->pw_dir);
- fprintf (fi, "NOREQ\n");
- if (*arg0) {
- fprintf (fi, "%s %s\n", arg0, Pas->pw_shell_argn);
- fprintf (fi, "ENDCLI >nil:\n");
- }
- else {
- propBreak = 1;
- }
- fclose (fi);
- }
- else {
- CloseLibrary (FifoBase);
- ulog (-1, "can't create %s", ScriptFile);
- return;
- }
-
- sprintf (CmdBuf, "NewShell FIFO:%s/rwecs From %s", FifoName, ScriptFile);
- NameDevUnit (DeviceName, DeviceUnit, "Getty started Shell Process");
- Execute (CmdBuf, 0, 0);
- r = IoErr ();
- if (r != 0) {
- CloseLibrary (FifoBase);
- ulog (-1, "Unable to start shell for %s %s", arg0, Pas->pw_shell_argn);
- remove (ScriptFile);
- NameDevUnit (DeviceName, DeviceUnit, "Getty");
- return;
- }
-
- /*
- * master side for shell, transfer serial device data to fifo and
- * back again until EOF.
- */
-
- sprintf (CmdBuf, "%s_m", FifoName);
- mast_w = OpenFifo (CmdBuf, 256, FIFOF_WRITE | FIFOF_NORMAL | FIFOF_NBIO);
-
- sprintf (CmdBuf, "%s_s", FifoName);
- mast_r = OpenFifo (CmdBuf, 256, FIFOF_READ | FIFOF_NORMAL | FIFOF_NBIO);
-
- if (mast_w && mast_r)
- InteractiveTransfer (mast_r, mast_w, propBreak);
-
- if (IoswIP) {
- NiceAbortIO (&Iosw);
- WaitIO ((struct IORequest *) &Iosw);
- IoswIP = 0;
- }
-
- if (mast_w)
- CloseFifo (mast_w, FIFOF_EOF);
- if (mast_r)
- CloseFifo (mast_r, FIFOF_EOF);
- CloseLibrary (FifoBase);
- FifoBase = NULL;
-
- remove (ScriptFile);
- NameDevUnit (DeviceName, DeviceUnit, "Getty");
-
- return;
- }
-
- void
- InteractiveTransfer (FifoHan mast_r, FifoHan mast_w, int propBreak)
- {
- struct Message
- RMsg,
- WMsg;
- short
- notdone = 1;
- long
- serWriteLen = 0,
- idleTimeout = SHELL_TIMEOUT,
- n;
- char
- *ptr;
-
- RMsg.mn_ReplyPort = IoSink;
- WMsg.mn_ReplyPort = IoSink;
-
- RequestFifo (mast_r, &RMsg, FREQ_RPEND);
- RMsgIP = 1;
- RxStart ();
- Timeout (0);
-
- while (notdone) {
- long
- mask = Wait(SIGBREAKF_CTRL_C | (1 << IoSink->mp_SigBit));
- struct Message
- *msg;
-
- while (msg = GetMsg (IoSink)) {
- if (msg == (struct Message *) &Iosr) { /* serial data */
- IosrIP = 0;
- idleTimeout = SHELL_TIMEOUT;
- ser_rretry:
- if (Iosr.IOSer.io_Actual > 0) {
- if (propBreak) {
- unsigned char
- c = *(char *)Iosr.IOSer.io_Data;
-
- if (c >= 3 && c <= 6) {
- SendBreak (c);
- Iosr.IOSer.io_Data = (APTR) ((char *) Iosr.IOSer.io_Data + 1);
- --Iosr.IOSer.io_Actual;
- break;
- }
- }
- n = WriteFifo (mast_w, Iosr.IOSer.io_Data, Iosr.IOSer.io_Actual);
- if (n != Iosr.IOSer.io_Actual) {
- if (WMsgIP == 0) {
- RequestFifo (mast_w, &WMsg, FREQ_WAVAIL);
- WMsgIP = 1;
- }
- continue;
- }
- }
- RxStart ();
- continue;
- }
-
- if (msg == (struct Message *) &Iosw) {
- IoswIP = 0;
- n = ReadFifo (mast_r, &ptr, serWriteLen);
- ser_wretry:
- n = ReadFifo (mast_r, &ptr, 0);
-
- if (n > 0) {
- idleTimeout = SHELL_TIMEOUT;
- Iosw.IOSer.io_Data = (APTR) ptr;
- Iosw.IOSer.io_Length = n;
- serWriteLen = n;
- SendIO ((struct IORequest *) &Iosw);
- IoswIP = 1;
- }
- if (n < 0) {
- notdone = 0;
- }
- else
- if (RMsgIP == 0) {
- RequestFifo (mast_r, &RMsg, FREQ_RPEND);
- RMsgIP = 1;
- }
- continue;
- }
-
- if (msg == (struct Message *) &Iot1) { /* carrier check */
- Iot1IP = 0;
- if (GetStatus () & CIAF_COMCD) /* lost carrier */
- notdone = 0;
- Iot1.tr_time.tv_secs = MainTimeout;
- Iot1.tr_time.tv_micro= 0;
- SendIO ((struct IORequest *) &Iot1);
- Iot1IP = 1;
- idleTimeout -= MainTimeout;
- if (idleTimeout < 0)
- notdone = 0;
- continue;
- }
-
- if (msg == &RMsg) { /* <- fifo */
- RMsgIP = 0;
- if (IoswIP == 0)
- goto ser_wretry;
- continue;
- }
-
- if (msg == &WMsg) { /* -> fifo */
- WMsgIP = 0;
- if (IosrIP == 0)
- goto ser_rretry;
- continue;
- }
- }
- }
-
- if (RMsgIP) {
- RequestFifo (mast_r, &RMsg, FREQ_ABORT);
- WaitMsg (&RMsg);
- RMsgIP = 0;
- }
-
- if (WMsgIP) {
- RequestFifo (mast_w, &WMsg, FREQ_ABORT);
- WaitMsg (&WMsg);
- WMsgIP = 0;
- }
-
- return;
- }
-
- void
- SendBreak (int c)
- {
- char
- buf [256];
- BPTR
- fh;
-
- sprintf (buf, "FIFO:%s/%c", FifoName, c | 'A');
- if (fh = Open (buf, MODE_OLDFILE))
- Close (fh);
-
- return;
- }
-