home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / util / misc / MUser17src.lha / MultiUser / src / Support / Passwd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-07  |  4.5 KB  |  183 lines

  1. /************************************************************
  2. * MultiUser - MultiUser Task/File Support System                *
  3. * ---------------------------------------------------------    *
  4. * Change Password                                                            *
  5. * ---------------------------------------------------------    *
  6. * © Copyright 1993-1994 Geert Uytterhoeven                        *
  7. * All Rights Reserved.                                                    *
  8. ************************************************************/
  9.  
  10.  
  11. #include <exec/types.h>
  12. #include <exec/execbase.h>
  13. #include <dos/dos.h>
  14. #include <proto/exec.h>
  15. #include <proto/dos.h>
  16. #include <utility/tagitem.h>
  17. #include <proto/reqtools.h>
  18. #include <libraries/reqtools.h>
  19. #include <string.h>
  20. #include <libraries/multiuser.h>
  21. #include <proto/multiuser.h>
  22.  
  23. #include "Passwd_rev.h"
  24.  
  25.  
  26. char __VersTag__[] = VERSTAG;
  27.  
  28.  
  29. static void __regargs myputs(char *str, struct DosLibrary *DOSBase);
  30. static void __regargs mygets(char *str, struct DosLibrary *DOSBase);
  31.  
  32.  
  33. int __saveds Start(char *arg)
  34. {
  35.     struct ExecBase *SysBase;
  36.     struct DosLibrary *DOSBase;
  37.     struct muBase *muBase = NULL;
  38.     struct ReqToolsBase *ReqToolsBase = NULL;
  39.     char oldpwd[muPASSWORDSIZE];
  40.     char newpwd1[muPASSWORDSIZE];
  41.     char newpwd2[muPASSWORDSIZE];
  42.     struct RDArgs *rdargs;
  43.     LONG args[] = {
  44. #define argGUI    0
  45.         NULL
  46.     };
  47.     static struct TagItem allwaystags[] = {
  48.         RT_ReqPos, REQPOS_CENTERSCR,
  49.         RT_LockWindow, TRUE,
  50.         TAG_DONE
  51.     };
  52.     struct TagItem infotags[] = {
  53.         RTEZ_Flags, EZREQF_CENTERTEXT,
  54.         TAG_MORE, (LONG)allwaystags
  55.     };
  56.     struct TagItem pwdtags[] = {
  57.         RTGS_TextFmt, NULL,
  58.         RTGS_Invisible, TRUE,
  59.         RTGS_AllowEmpty, TRUE,
  60.         RTGS_Flags, GSREQF_CENTERTEXT,
  61.         RTGS_GadFmt, (LONG)"OK",
  62.         TAG_MORE, (LONG)allwaystags
  63.     };
  64.     BOOL gui;
  65.     int rc = RETURN_ERROR;
  66.  
  67.  
  68.     SysBase = *(struct ExecBase **)4;
  69.     
  70.     if ((!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37))) ||
  71.          (!(muBase = (struct muBase *)OpenLibrary("multiuser.library", 39))) ||
  72.          (!(ReqToolsBase = (struct ReqToolsBase *)OpenLibrary("reqtools.library", 38)))) {
  73.         rc = ERROR_INVALID_RESIDENT_LIBRARY;
  74.         goto Exit;
  75.     }
  76.  
  77.     rdargs = ReadArgs("GUI/S", args, NULL);
  78.     if (!rdargs)
  79.         PrintFault(IoErr(), NULL);
  80.     else {
  81.         gui = args[argGUI] &&
  82.                 (((struct Process *)SysBase->ThisTask)->pr_WindowPtr != (APTR)-1);
  83.         if (gui) {
  84.             memset(oldpwd, '\0', sizeof(oldpwd));
  85.             memset(newpwd1, '\0', sizeof(newpwd1));
  86.             memset(newpwd2, '\0', sizeof(newpwd2));
  87.             pwdtags[0].ti_Data = (LONG)"Enter your old password";
  88.             rtGetStringA(oldpwd, muPASSWORDSIZE-1, "MultiUser Passwd Request",
  89.                              NULL, pwdtags);
  90.             pwdtags[0].ti_Data = (LONG)"Enter your new password";
  91.             rtGetStringA(newpwd1, muPASSWORDSIZE-1, "MultiUser Passwd Request",
  92.                              NULL, pwdtags);
  93.             pwdtags[0].ti_Data = (LONG)"Retype your new password";
  94.             rtGetStringA(newpwd2, muPASSWORDSIZE-1, "MultiUser Passwd Request",
  95.                              NULL, pwdtags);
  96.         } else {
  97.             myputs("\nOld password        : ", DOSBase);
  98.             mygets(oldpwd, DOSBase);
  99.             myputs("\n\nNew password        : ", DOSBase);
  100.             mygets(newpwd1, DOSBase);
  101.             myputs("\n\nRetype new password : ", DOSBase);
  102.             mygets(newpwd2, DOSBase);
  103.             myputs("\n\n", DOSBase);
  104.         };
  105.  
  106.         if (strcmp(newpwd1, newpwd2))
  107.             if (gui)
  108.                 rtEZRequestA("Error retyping new password", "OK", NULL, NULL, infotags);
  109.             else
  110.                 myputs("Error retyping new password\n", DOSBase);
  111.         else
  112.             if (muPasswd(oldpwd, newpwd1)) {
  113.                 if (gui)
  114.                     rtEZRequestA("Password successfully changed", "OK", NULL, NULL, infotags);
  115.                 else
  116.                     myputs("Password successfully changed\n", DOSBase);
  117.                 rc = RETURN_OK;
  118.             } else
  119.                 if (gui)
  120.                     rtEZRequestA("Changing password failed", "OK", NULL, NULL, infotags);
  121.                 else
  122.                     myputs("Changing password failed\n", DOSBase);
  123.     }
  124.     FreeArgs(rdargs);
  125.  
  126. Exit:
  127.     CloseLibrary((struct Library *)ReqToolsBase);
  128.     CloseLibrary((struct Library *)muBase);
  129.     CloseLibrary((struct Library *)DOSBase);
  130.  
  131.     return(rc);
  132. }    
  133.  
  134.  
  135. static void __regargs myputs(char *str, struct DosLibrary *DOSBase)
  136. {
  137.     Write(Output(), str, strlen(str));
  138. }
  139.  
  140.  
  141. static void __regargs mygets(char *str, struct DosLibrary *DOSBase)
  142. {
  143.     LONG len = 0;
  144.     char buf;
  145.     BOOL done = FALSE;
  146.     BPTR in, out;
  147.  
  148.     in = Input();
  149.     out = Output();
  150.     SetMode(in, 1);
  151.     do {
  152.         Read(in, &buf, 1);
  153.         switch(buf) {
  154.             case    '\b':
  155.                 if (len) {
  156.                     FPutC(out, '\b');
  157.                     Flush(out);
  158.                     len--;
  159.                 }
  160.                 break;
  161.     
  162.             case    '\n':
  163.             case    '\r':
  164.                 done = TRUE;
  165.                 break;
  166.     
  167.             default:
  168.                 if ((len < muPASSWORDSIZE-1) && ((buf & 0x7f) > 31)
  169.                      && (buf != 127)) {
  170.                     FPutC(out, ' ');
  171.                     str[len++] = buf;
  172.                 } else
  173.                     FPutC(out, 7);
  174.                 Flush(out);
  175.                 break;
  176.         }
  177.     } while (!done);
  178.     str[len] = 0;
  179.     SetMode(in, 0);
  180.     FPutC(out, '\r');
  181.     Flush(out);
  182. }
  183.