home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 239.lha / amiga / src / server / spasswd.c < prev    next >
C/C++ Source or Header  |  1989-05-02  |  3KB  |  170 lines

  1.  
  2. /*
  3.  *  SPASSWD.C
  4.  *
  5.  *  DNET (c)Copyright 1988, Matthew Dillon, All Rights Reserved.
  6.  *
  7.  *  PASSWORD SERVER
  8.  *
  9.  *  user-password entry.  USER entries in s:dnet.config are scanned for
  10.  *  a match and the various dnet enviroment variables are set accordingly.
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <fcntl.h>
  15. #include <local/typedefs.h>
  16. #include "servers.h"
  17. #include "/dnet/channel.h"
  18.  
  19. int Enable_Abort;
  20.  
  21. extern struct MsgPort *DListen();
  22.  
  23. extern void printfile();
  24.  
  25. _main()
  26. {
  27.     struct MsgPort *port;
  28.     PROC    *myproc = FindTask(NULL);
  29.     long    chan;
  30.     long    mask, rmask;
  31.     short   res;
  32.  
  33.     Enable_Abort = 0;
  34.  
  35.     if (myproc->pr_CLI) {
  36.     Version("SPasswd", VERSION, SPASSWD_VERSION);
  37.     _exit(0);
  38.     }
  39.  
  40.     port = DListen(PORT_PASSWD);
  41.     WaitPort(&myproc->pr_MsgPort);
  42.     ReplyMsg(GetMsg(&myproc->pr_MsgPort));
  43.  
  44.     mask = SIGBREAKF_CTRL_C|(1 << port->mp_SigBit);
  45.     for (;;) {
  46.     rmask = Wait(mask);
  47.     if (rmask & SIGBREAKF_CTRL_C)
  48.         break;
  49.     while (chan = DAccept(port)) {
  50.         res = do_password(chan);
  51.         Delay(10L);     /*  prevent name scan   */
  52.         if (res)
  53.         DPuts(chan, "user-password accepted\n");
  54.         else
  55.         DPuts(chan, "user-password failed\n");
  56.         Delay(50L);     /*  prevent name scan   */
  57.         DClose(chan);
  58.     }
  59.     }
  60.     DUnListen(port);
  61. }
  62.  
  63. do_password(chan)
  64. {
  65.     static char user[64];
  66.     static char pass[64];
  67.     static char buf[128];
  68.     static char suser[64];
  69.     static char spass[64];
  70.     short res;
  71.     long fh;
  72.     short i, n;
  73.     int s_level, s_read, s_write;
  74.  
  75.     DPuts(chan, "User Name: ");
  76.     res = DGetLine(chan, user, 64);
  77.     if (res < 0)
  78.     return(0);
  79.     DIoctl(chan, CIO_MODE, 0, 0);
  80.     DPuts(chan, "Password : ");
  81.     res = DGetLine(chan, pass, 64);
  82.     DIoctl(chan, CIO_MODE, 7, 0);
  83.     if (res < 0)
  84.     return(0);
  85.  
  86.     /*
  87.      *    search for USER entry in s:dnet.config
  88.      */
  89.  
  90.     fh = Open("s:dnet.config", 1005);
  91.     if (fh == NULL) {
  92.     DPuts(chan, "s:dnet.config does not exist\n");
  93.     return(0);
  94.     }
  95.     while ((n = Read(fh, buf, 256)) > 0) {
  96.     for (i = 0; i < n - 1; ++i) {
  97.         if (buf[i] == '\n')
  98.         break;
  99.     }
  100.     buf[i] = 0;
  101.     Seek(fh, (i + 1) - n, 0);
  102.     if (strncmp(buf, "USER", 4) == 0) {
  103.         suser[0] = spass[0] = 0;
  104.         s_level = s_read = s_write = 0;
  105.         sscanf(buf + 4, "%s %s %d %d %d", suser, spass, &s_level, &s_read, &s_write);
  106.         if (strcmp(user, suser) == 0 && strcmp(pass, spass) == 0) {
  107.         Close(fh);
  108.         sprintf(buf, "%d", s_level);
  109.         SetDEnv(DNET_LEVEL, buf);
  110.         sprintf(buf, "%d", s_read);
  111.         SetDEnv(DNET_READ, buf);
  112.         sprintf(buf, "%d", s_write);
  113.         SetDEnv(DNET_WRITE, buf);
  114.         return(1);
  115.         }
  116.     }
  117.     }
  118.     Close(fh);
  119.     return(0);
  120. }
  121.  
  122. DPuts(chan, str)
  123. long chan;
  124. char *str;
  125. {
  126.     DWrite(chan, str, strlen(str));
  127. }
  128.  
  129. DGetLine(chan, buf, max)
  130. long chan;
  131. char *buf;
  132. short max;
  133. {
  134.     register short i = 0;
  135.     register short n;
  136.     short notdone = 1;
  137.     char c;
  138.  
  139.     while (notdone) {
  140.     n = DRead(chan, &c, 1);
  141.     if (n == 0) {   /*  ioctl, ignore   */
  142.         short pv;
  143.         short pa;
  144.         DGetIoctl(chan, &pv, &pa);
  145.         continue;
  146.     }
  147.     if (n < 0) {
  148.         buf[i] = 0;
  149.         return(-1);
  150.     }
  151.     switch(c) {
  152.     case 8:     /*    BS  */
  153.         if (i)
  154.         --i;
  155.         break;
  156.     case 10:    /*    eol */
  157.     case 13:
  158.         notdone = 0;
  159.         break;
  160.     default:
  161.         if (i < max - 1)
  162.         buf[i++] = c;
  163.         break;
  164.     }
  165.     }
  166.     buf[i] = 0;
  167.     return(1);
  168. }
  169.  
  170.