home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / duucp-1.17 / AU-117b4-src.lha / src / getty / passwd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-24  |  6.8 KB  |  352 lines

  1. /*
  2.  *  PASSWD.C
  3.  *
  4.  *  (C) Copyright 1989-1990 by Matthew Dillon,    All Rights Reserved.
  5.  *
  6.  */
  7.  
  8. #include "defs.h"
  9.  
  10. Prototype void RunPasswdEntry (void);
  11. Prototype void RunPasswdEntryCmd (char *);
  12. Prototype void RunPasswdEntryShell (char *);
  13. Prototype void InteractiveTransfer (FifoHan, FifoHan, int);
  14. Prototype void SendBreak (int);
  15. Prototype int  CheckLoginAndPassword (void);
  16.  
  17. #define SHELL_TIMEOUT    (60 * 10)   /*    10 minutes of idle  */
  18.  
  19. static struct passwd
  20.     *Pas;
  21. static char
  22.     CmdBuf [512],
  23.     FifoName [256],
  24.     ScriptFile [256];
  25.  
  26. #define OwnerBuf FifoName    /* so we don't allocate another 256 bytes */
  27.  
  28. struct Library
  29.     *FifoBase = NULL;
  30. short
  31.     RMsgIP,     /* read message in-process */
  32.     WMsgIP;     /* write message in-process */
  33.  
  34. int
  35. CheckLoginAndPassword (void)
  36. {
  37.     Pas = getpwnam (LoginBuf);
  38.  
  39.     if (Pas == NULL)
  40.         return 0;
  41.  
  42.     if (strcmp (Pas->pw_passwd, "*") == 0)
  43.         return 1;
  44.  
  45.     if (strcmp (PasswdBuf, Pas->pw_passwd) == 0)
  46.         return 1;
  47.  
  48.     return 0;
  49. }
  50.  
  51. void
  52. RunPasswdEntry (void)
  53. {
  54.     if (Pas->pw_shell_arg0 [0] == '*')
  55.         RunPasswdEntryShell (Pas->pw_shell_arg0 + 1);
  56.     else
  57.         RunPasswdEntryCmd (Pas->pw_shell_arg0);
  58.  
  59.     return;
  60. }
  61.  
  62. void
  63. RunPasswdEntryCmd (char *arg0)
  64. {
  65.     struct Process
  66.         *proc = (struct Process *) FindTask (NULL);
  67.     APTR
  68.         oldConsoleTask;
  69.     BPTR
  70.         oldDir,
  71.         DirLock;
  72.     char
  73.         smallBuf [8];
  74.  
  75.     sprintf (CmdBuf, "%s >null: <null: %s -Getty -DEVICE %s -UNIT %ld",
  76.         arg0,
  77.         Pas->pw_shell_argn,
  78.         DeviceName,
  79.         DeviceUnit
  80.     );
  81.  
  82.     if (BaudReport) {
  83.         strcat (CmdBuf, " -BAUD ");
  84.         sprintf (smallBuf, "%ld", BaudRate);
  85.         strcat (CmdBuf, smallBuf);
  86.     }
  87.  
  88.     if (UserReport) {
  89.         strcat (CmdBuf, " -USER ");
  90.         strcat (CmdBuf, LoginBuf);
  91.     }
  92.  
  93.     if (HardLink)
  94.         strcat (CmdBuf, " -HLINK");
  95.  
  96.     DirLock = Lock (Pas->pw_dir, SHARED_LOCK);
  97.     if (DirLock)
  98.         oldDir = CurrentDir (DirLock);
  99.     oldConsoleTask = proc->pr_ConsoleTask;
  100.  
  101.     ulog (1, "Execute %s\n", CmdBuf);
  102.  
  103.     sprintf (OwnerBuf, "Getty started %s", arg0);
  104.     NameDevUnit (DeviceName, DeviceUnit, OwnerBuf);
  105.     Execute (CmdBuf, NullFH, NullFH);
  106.     NameDevUnit (DeviceName, DeviceUnit, "Getty");
  107.  
  108.     proc->pr_ConsoleTask = oldConsoleTask;
  109.     if (DirLock)
  110.         UnLock (CurrentDir (oldDir));
  111.  
  112.     return;
  113. }
  114.  
  115. void
  116. RunPasswdEntryShell (char *arg0)
  117. {
  118.     long
  119.         r;
  120.     FifoHan
  121.         mast_r,
  122.         mast_w;
  123.     int
  124.         propBreak = 0;
  125.     FILE
  126.         *fi;
  127.  
  128.     sprintf (ScriptFile, "T:%s.%ld.script", DeviceName, DeviceUnit);
  129.     sprintf (FifoName, "%s.%ld", DeviceName, DeviceUnit);
  130.  
  131.     FifoBase = OpenLibrary (FIFONAME, 0);
  132.     if (!FifoBase) {
  133.         ulog (-1, "Unable to open fifo.library");
  134.         return;
  135.     }
  136.  
  137.     if (fi = fopen (ScriptFile, "w")) {
  138.         if (DOSBase->dl_lib.lib_Version >= 36) {
  139.             fprintf (fi, "SET GettyLogin TRUE\n");
  140.             if (UserReport) {
  141.                 fprintf (fi, "SET User %s\n", LoginBuf);
  142.                 fprintf (fi, "SET RealName %s\n", Pas->pw_real_name);
  143.             }
  144.         }
  145.         fprintf (fi, "EXECUTE S:Shell-Startup\n");
  146.         fprintf (fi, "FAILAT 99\n");     /*  DOESN'T WORK! */
  147.         fprintf (fi, "CD %s\n", Pas->pw_dir);
  148.         fprintf (fi, "NOREQ\n");
  149.         if (*arg0) {
  150.             fprintf (fi, "%s %s\n", arg0, Pas->pw_shell_argn);
  151.             fprintf (fi, "ENDCLI >nil:\n");
  152.         }
  153.         else {
  154.             propBreak = 1;
  155.         }
  156.         fclose (fi);
  157.     }
  158.     else {
  159.         CloseLibrary (FifoBase);
  160.         ulog (-1, "can't create %s", ScriptFile);
  161.         return;
  162.     }
  163.  
  164.     sprintf (CmdBuf, "NewShell FIFO:%s/rwecs From %s", FifoName, ScriptFile);
  165.     NameDevUnit (DeviceName, DeviceUnit, "Getty started Shell Process");
  166.     Execute (CmdBuf, 0, 0);
  167.     r = IoErr ();
  168.     if (r != 0) {
  169.         CloseLibrary (FifoBase);
  170.         ulog (-1, "Unable to start shell for %s %s", arg0, Pas->pw_shell_argn);
  171.         remove (ScriptFile);
  172.         NameDevUnit (DeviceName, DeviceUnit, "Getty");
  173.         return;
  174.     }
  175.  
  176.     /*
  177.      *  master side for shell, transfer serial device data to fifo and
  178.      *  back again until EOF.
  179.      */
  180.  
  181.     sprintf (CmdBuf, "%s_m", FifoName);
  182.     mast_w = OpenFifo (CmdBuf, 256, FIFOF_WRITE | FIFOF_NORMAL | FIFOF_NBIO);
  183.  
  184.     sprintf (CmdBuf, "%s_s", FifoName);
  185.     mast_r = OpenFifo (CmdBuf, 256, FIFOF_READ  | FIFOF_NORMAL | FIFOF_NBIO);
  186.  
  187.     if (mast_w && mast_r)
  188.         InteractiveTransfer (mast_r, mast_w, propBreak);
  189.  
  190.     if (IoswIP) {
  191.         NiceAbortIO (&Iosw);
  192.         WaitIO ((struct IORequest *) &Iosw);
  193.         IoswIP = 0;
  194.     }
  195.  
  196.     if (mast_w)
  197.         CloseFifo (mast_w, FIFOF_EOF);
  198.     if (mast_r)
  199.         CloseFifo (mast_r, FIFOF_EOF);
  200.     CloseLibrary (FifoBase);
  201.     FifoBase = NULL;
  202.  
  203.     remove (ScriptFile);
  204.     NameDevUnit (DeviceName, DeviceUnit, "Getty");
  205.  
  206.     return;
  207. }
  208.  
  209. void
  210. InteractiveTransfer (FifoHan mast_r, FifoHan mast_w, int propBreak)
  211. {
  212.     struct Message
  213.         RMsg,
  214.         WMsg;
  215.     short
  216.         notdone = 1;
  217.     long
  218.         serWriteLen = 0,
  219.         idleTimeout = SHELL_TIMEOUT,
  220.         n;
  221.     char
  222.         *ptr;
  223.  
  224.     RMsg.mn_ReplyPort = IoSink;
  225.     WMsg.mn_ReplyPort = IoSink;
  226.  
  227.     RequestFifo (mast_r, &RMsg, FREQ_RPEND);
  228.     RMsgIP = 1;
  229.     RxStart ();
  230.     Timeout (0);
  231.  
  232.     while (notdone) {
  233.         long
  234.             mask = Wait(SIGBREAKF_CTRL_C | (1 << IoSink->mp_SigBit));
  235.         struct Message
  236.             *msg;
  237.  
  238.         while (msg = GetMsg (IoSink)) {
  239.             if (msg == (struct Message *) &Iosr) {    /* serial data */
  240.                 IosrIP = 0;
  241.                 idleTimeout = SHELL_TIMEOUT;
  242. ser_rretry:
  243.                 if (Iosr.IOSer.io_Actual > 0) {
  244.                     if (propBreak) {
  245.                         unsigned char
  246.                             c = *(char *)Iosr.IOSer.io_Data;
  247.  
  248.                         if (c >= 3 && c <= 6) {
  249.                             SendBreak (c);
  250.                             Iosr.IOSer.io_Data = (APTR) ((char *) Iosr.IOSer.io_Data + 1);
  251.                             --Iosr.IOSer.io_Actual;
  252.                             break;
  253.                         }
  254.                     }
  255.                     n = WriteFifo (mast_w, Iosr.IOSer.io_Data, Iosr.IOSer.io_Actual);
  256.                     if (n != Iosr.IOSer.io_Actual) {
  257.                         if (WMsgIP == 0) {
  258.                             RequestFifo (mast_w, &WMsg, FREQ_WAVAIL);
  259.                             WMsgIP = 1;
  260.                         }
  261.                         continue;
  262.                     }
  263.                 }
  264.                 RxStart ();
  265.                 continue;
  266.             }
  267.  
  268.             if (msg == (struct Message *) &Iosw) {
  269.                 IoswIP = 0;
  270.                 n = ReadFifo (mast_r, &ptr, serWriteLen);
  271. ser_wretry:
  272.                 n = ReadFifo (mast_r, &ptr, 0);
  273.  
  274.                 if (n > 0) {
  275.                     idleTimeout = SHELL_TIMEOUT;
  276.                     Iosw.IOSer.io_Data = (APTR) ptr;
  277.                     Iosw.IOSer.io_Length = n;
  278.                     serWriteLen = n;
  279.                     SendIO ((struct IORequest *) &Iosw);
  280.                     IoswIP = 1;
  281.                 }
  282.                 if (n < 0) {
  283.                     notdone = 0;
  284.                 }
  285.                 else
  286.                     if (RMsgIP == 0) {
  287.                         RequestFifo (mast_r, &RMsg, FREQ_RPEND);
  288.                         RMsgIP = 1;
  289.                     }
  290.                 continue;
  291.             }
  292.  
  293.             if (msg == (struct Message *) &Iot1) {     /*  carrier check */
  294.                 Iot1IP = 0;
  295.                 if (GetStatus () & CIAF_COMCD)         /*  lost carrier  */
  296.                     notdone = 0;
  297.                 Iot1.tr_time.tv_secs = MainTimeout;
  298.                 Iot1.tr_time.tv_micro= 0;
  299.                 SendIO ((struct IORequest *) &Iot1);
  300.                 Iot1IP = 1;
  301.                 idleTimeout -= MainTimeout;
  302.                 if (idleTimeout < 0)
  303.                     notdone = 0;
  304.                 continue;
  305.             }
  306.  
  307.             if (msg == &RMsg) {         /*  <- fifo     */
  308.                 RMsgIP = 0;
  309.                 if (IoswIP == 0)
  310.                     goto ser_wretry;
  311.                 continue;
  312.             }
  313.  
  314.             if (msg == &WMsg) {         /*  -> fifo     */
  315.                 WMsgIP = 0;
  316.                 if (IosrIP == 0)
  317.                     goto ser_rretry;
  318.                 continue;
  319.             }
  320.         }
  321.     }
  322.  
  323.     if (RMsgIP) {
  324.         RequestFifo (mast_r, &RMsg, FREQ_ABORT);
  325.         WaitMsg (&RMsg);
  326.         RMsgIP = 0;
  327.     }
  328.  
  329.     if (WMsgIP) {
  330.         RequestFifo (mast_w, &WMsg, FREQ_ABORT);
  331.         WaitMsg (&WMsg);
  332.         WMsgIP = 0;
  333.     }
  334.  
  335.     return;
  336. }
  337.  
  338. void
  339. SendBreak (int c)
  340. {
  341.     char
  342.         buf [256];
  343.     BPTR
  344.         fh;
  345.  
  346.     sprintf (buf, "FIFO:%s/%c", FifoName, c | 'A');
  347.     if (fh = Open (buf, MODE_OLDFILE))
  348.         Close (fh);
  349.  
  350.     return;
  351. }
  352.