home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / dev / src / amilock / source / deamon.c next >
Encoding:
C/C++ Source or Header  |  1993-08-03  |  3.7 KB  |  164 lines

  1. #include <exec/types.h>
  2. #include <exec/PORTS.h>
  3. #include <dos/dos.h>
  4. #include <clib/exec_protos.h>
  5. #include <clib/alib_protos.h>
  6. #include <stdio.h>
  7. #include <intuition/intuition.h>
  8. #include <proto/all.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11.  
  12. #include "headers/Deamon.h"
  13. #include "headers/Deamon_proto.h"
  14. #include "headers/talkto_proto.h"
  15. #include "headers/global.h"
  16. #include "headers/error_proto.h"
  17.  
  18. #define PASSWORD PASSWD
  19. BOOL LoginCheck(char *, char *);
  20.  
  21. struct CurrentUser     Current = {"Nobody","NDBY",NULL,NULL,NULL};
  22. ULONG            lock=NULL;
  23.  
  24. BOOL LoginCheck(user,pass)
  25. char *user,*pass;
  26. {
  27.     FILE    *fin;
  28.     char    User[100]="";
  29.     char    Name[100]="";
  30.     char    Pass[100]="";
  31.     char    buffer[500]="";
  32.     ULONG     GID=0,UID=0;
  33.     short    i=0,j=0;
  34.     if (lock) UnLock(lock);
  35.     retry1:
  36.     if (!(fin = fopen(PASSWORD,"r"))){
  37.         if (Error("Can't open password file\n")) goto retry1;
  38.         exit(10);
  39.     }
  40.     if (strlen(user) < 2) {
  41.         fclose(fin);
  42.         lock = Lock(PASSWORD,EXCLUSIVE_LOCK);
  43.         return FALSE;
  44.     }
  45.     while (!feof(fin)&&strcmp(user,User)) {
  46.         if (!fgets(buffer,500,fin)) {
  47.             break;
  48.         }
  49.  
  50.         i =0;
  51.         j=0;
  52.         while (buffer[i] != '|') 
  53.             User[j++] = buffer[i++];
  54.         User[j] = NULL;
  55.         i++;
  56.         j = 0;
  57.         while (buffer[i] != '|') 
  58.             Pass[j++] = buffer[i++];
  59.         Pass[j] = NULL;
  60.         i++;
  61.         j = 0;
  62.         while (buffer[i] != '|') 
  63.             Name[j++] = buffer[i++];
  64.         Name[j] = NULL;
  65.         
  66.         sscanf(&(buffer[i+1]),"%ld|%ld",&GID,&UID);
  67.     }
  68.  
  69.     fclose(fin);
  70.     if ((strlen(Pass)<=1)&&(!strcmp(user,User))||(!strcmp(user,User))&&(!strcmp(pass,Pass))) {
  71.         strcpy(Current.Login,User);
  72.         strcpy(Current.Name,Name);
  73.         Current.GID = GID;
  74.         Current.UID = UID;
  75.         Current.Locks = NULL;
  76.         if (!strcmp("Root",User)) {
  77.             lock = NULL;
  78.         }
  79.         else lock = Lock(PASSWORD,EXCLUSIVE_LOCK);
  80.         return TRUE;
  81.     }
  82.     lock = Lock(PASSWORD,EXCLUSIVE_LOCK);
  83.     return FALSE;
  84. }
  85.  
  86. static const char __Version[]=DEAMVERST;
  87.  
  88.  
  89. #if (DEBUG)
  90.     void main()
  91. #else
  92.     void __main(argv)
  93.     char     *argv;
  94. #endif
  95. {
  96.         struct MsgPort         *SecPort;
  97.         struct SecMessage     *SecMsg;
  98.         ULONG             portsig,usersig,signal;
  99.         BOOL             ABORT = FALSE;
  100.     struct CurrentUser    *User;
  101.     
  102.     if (FindPort(DEAMON)) {
  103.         if (Error("Port exists, kill deamon")) {
  104.             TalkTo(NULL,NULL,NULL,QUIT);
  105.         }
  106.         exit(0);
  107.     }
  108.  
  109.     lock = Lock(PASSWORD,EXCLUSIVE_LOCK/*SHARED_LOCK*/);
  110.     if (!lock) Error("Unable to lock data base");
  111.  
  112.         if (SecPort = CreatePort(DEAMON, 0))
  113.         {
  114.             portsig = 1 << SecPort->mp_SigBit;
  115.             usersig = SIGBREAKF_CTRL_C;
  116.             do {
  117.                     signal = Wait(portsig); 
  118.                     if (signal | portsig){ 
  119.                         while(SecMsg = (struct SecMessage *)GetMsg(SecPort)) {
  120.                     SecMsg->Access = FALSE;
  121.                                if (SecMsg->Control == QUIT) {
  122.                         if (Error("Quit???")) 
  123.                         ABORT = TRUE;
  124.                         SecMsg->Access = TRUE;
  125.                     }
  126.                     else if (SecMsg->Control == WHOAMI) {
  127.                         if (!(User = malloc(sizeof(struct CurrentUser))))
  128.                             Error("Cannot Malloc()");
  129.                         strcpy(User->Name,Current.Name);
  130.                         strcpy(User->Login,Current.Login);
  131.                         User->GID = Current.GID;
  132.                         User->UID = Current.UID;
  133.                         SecMsg->UserData = User;
  134.                         User = NULL;
  135.                         SecMsg->Access = TRUE;    
  136.                     }
  137.                     else if (SecMsg->Control == LOGOUT) {
  138.                         strcpy(Current.Name,"");
  139.                         strcpy(Current.Login,"");
  140.                         Current.GID = Current.UID = NULL;
  141.                         system("login");
  142.                     }
  143.                     else if (SecMsg->Control == LOGIN) {
  144. #if (DEBUG)
  145. #endif
  146.                         SecMsg->Access = LoginCheck(SecMsg->User,SecMsg->Password);
  147. #if (DEBUG)
  148. #endif
  149.                     }
  150.                                    ReplyMsg((struct Message *)SecMsg);
  151.                 }
  152.             }
  153.             else ABORT = FALSE;
  154.             while (SecMsg = (struct SecMessage *)GetMsg(SecPort)) {
  155.                            ReplyMsg((struct Message *)SecMsg);
  156.             }
  157.  
  158.             } while (!ABORT);
  159.         DeletePort(SecPort);
  160.         }
  161.     else Error("Couldn't create 'Security Port'");
  162.     UnLock(lock);
  163. }
  164.