home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / util / misc / multiuser / src / support / passwd.c < prev    next >
C/C++ Source or Header  |  1994-03-03  |  5KB  |  193 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. #include "Locale.h"
  26.  
  27. char __VersTag__[] = VERSTAG;
  28.  
  29.  
  30. static void __regargs myputs(char *str, struct DosLibrary *DOSBase);
  31. static void __regargs mygets(char *str, struct DosLibrary *DOSBase);
  32.  
  33.  
  34. int __saveds Start(char *arg)
  35. {
  36.     struct ExecBase *SysBase;
  37.     struct DosLibrary *DOSBase;
  38.     struct muBase *muBase = NULL;
  39.     struct ReqToolsBase *ReqToolsBase = NULL;
  40.     char oldpwd[muPASSWORDSIZE];
  41.     char newpwd1[muPASSWORDSIZE];
  42.     char newpwd2[muPASSWORDSIZE];
  43.     struct RDArgs *rdargs;
  44.     LONG args[] = {
  45. #define argGUI    0
  46.         NULL
  47.     };
  48.     static struct TagItem allwaystags[] = {
  49.         RT_ReqPos, REQPOS_CENTERSCR,
  50.         RT_LockWindow, TRUE,
  51.         TAG_DONE
  52.     };
  53.     struct TagItem infotags[] = {
  54.         RTEZ_Flags, EZREQF_CENTERTEXT,
  55.         TAG_MORE, (LONG)allwaystags
  56.     };
  57.     struct TagItem pwdtags[] = {
  58.         RTGS_TextFmt, NULL,
  59.         RTGS_Invisible, TRUE,
  60.         RTGS_AllowEmpty, TRUE,
  61.         RTGS_Flags, GSREQF_CENTERTEXT,
  62.         RTGS_GadFmt, (LONG)"OK",
  63.         TAG_MORE, (LONG)allwaystags
  64.     };
  65.     BOOL gui;
  66.     int rc = RETURN_ERROR;
  67.     struct LocaleInfo li;
  68.  
  69.     SysBase = *(struct ExecBase **)4;
  70.     
  71.     if ((!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37))) ||
  72.          (!(muBase = (struct muBase *)OpenLibrary("multiuser.library", 39))) ||
  73.          (!(ReqToolsBase = (struct ReqToolsBase *)OpenLibrary("reqtools.library", 38)))) {
  74.         rc = ERROR_INVALID_RESIDENT_LIBRARY;
  75.         goto Exit;
  76.     }
  77.  
  78.     OpenLoc(&li);
  79.  
  80.     rdargs = ReadArgs("GUI/S", args, NULL);
  81.     if (!rdargs)
  82.         PrintFault(IoErr(), NULL);
  83.     else {
  84.         gui = args[argGUI] &&
  85.                 (((struct Process *)SysBase->ThisTask)->pr_WindowPtr != (APTR)-1);
  86.         if (gui) {
  87.             memset(oldpwd, '\0', sizeof(oldpwd));
  88.             memset(newpwd1, '\0', sizeof(newpwd1));
  89.             memset(newpwd2, '\0', sizeof(newpwd2));
  90.             pwdtags[0].ti_Data = (LONG)GetLocS(&li,MSG_ENTEROLDPWD_GUI);
  91.             rtGetStringA(oldpwd, muPASSWORDSIZE-1, GetLocS(&li,MSG_PASSWDREQ_GUI),
  92.                              NULL, pwdtags);
  93.             pwdtags[0].ti_Data = (LONG)GetLocS(&li,MSG_ENTERNEWPWD_GUI);
  94.             rtGetStringA(newpwd1, muPASSWORDSIZE-1, GetLocS(&li,MSG_PASSWDREQ_GUI),
  95.                              NULL, pwdtags);
  96.             pwdtags[0].ti_Data = (LONG)GetLocS(&li,MSG_RETYPEPWD_GUI);
  97.             rtGetStringA(newpwd2, muPASSWORDSIZE-1, GetLocS(&li,MSG_PASSWDREQ_GUI),
  98.                              NULL, pwdtags);
  99.         } else {
  100.             myputs(GetLocS(&li,MSG_ENTEROLDPWD_CON), DOSBase);
  101.             myputs(" ", DOSBase);
  102.             mygets(oldpwd, DOSBase);
  103.             myputs("\n", DOSBase);
  104.             myputs(GetLocS(&li,MSG_ENTERNEWPWD_CON), DOSBase);
  105.             myputs(" ", DOSBase);
  106.             mygets(newpwd1, DOSBase);
  107.             myputs("\n", DOSBase);
  108.             myputs(GetLocS(&li,MSG_RETYPEPWD_CON), DOSBase);
  109.             myputs(" ", DOSBase);
  110.             mygets(newpwd2, DOSBase);
  111.             myputs("\n\n", DOSBase);
  112.         };
  113.  
  114.         if (strcmp(newpwd1, newpwd2))
  115.             if (gui)
  116.                 rtEZRequestA(GetLocS(&li,MSG_RETYPEERROR_GUI), GetLocS(&li,MSG_OK), NULL, NULL, infotags);
  117.             else
  118.                 myputs(GetLocS(&li,MSG_RETYPEERROR_CON), DOSBase);
  119.         else
  120.             if (muPasswd(oldpwd, newpwd1)) {
  121.                 if (gui)
  122.                     rtEZRequestA(GetLocS(&li,MSG_PWDCHANGEOK_GUI), GetLocS(&li,MSG_OK), NULL, NULL, infotags);
  123.                 else
  124.                     myputs(GetLocS(&li,MSG_PWDCHANGEOK_CON), DOSBase);
  125.                 rc = RETURN_OK;
  126.             } else
  127.                 if (gui)
  128.                     rtEZRequestA(GetLocS(&li,MSG_PWDCHANGEFAIL_GUI), GetLocS(&li,MSG_OK), NULL, NULL, infotags);
  129.                 else
  130.                     myputs(GetLocS(&li,MSG_PWDCHANGEFAIL_CON), DOSBase);
  131.     }
  132.     FreeArgs(rdargs);
  133.  
  134.     CloseLoc(&li);
  135.  
  136. Exit:
  137.     CloseLibrary((struct Library *)ReqToolsBase);
  138.     CloseLibrary((struct Library *)muBase);
  139.     CloseLibrary((struct Library *)DOSBase);
  140.  
  141.     return(rc);
  142. }    
  143.  
  144.  
  145. static void __regargs myputs(char *str, struct DosLibrary *DOSBase)
  146. {
  147.     Write(Output(), str, strlen(str));
  148. }
  149.  
  150.  
  151. static void __regargs mygets(char *str, struct DosLibrary *DOSBase)
  152. {
  153.     LONG len = 0;
  154.     char buf;
  155.     BOOL done = FALSE;
  156.     BPTR in, out;
  157.  
  158.     in = Input();
  159.     out = Output();
  160.     SetMode(in, 1);
  161.     do {
  162.         Read(in, &buf, 1);
  163.         switch(buf) {
  164.             case    '\b':
  165.                 if (len) {
  166.                     FPutC(out, '\b');
  167.                     Flush(out);
  168.                     len--;
  169.                 }
  170.                 break;
  171.     
  172.             case    '\n':
  173.             case    '\r':
  174.                 done = TRUE;
  175.                 break;
  176.     
  177.             default:
  178.                 if ((len < muPASSWORDSIZE-1) && ((buf & 0x7f) > 31)
  179.                      && (buf != 127)) {
  180.                     FPutC(out, ' ');
  181.                     str[len++] = buf;
  182.                 } else
  183.                     FPutC(out, 7);
  184.                 Flush(out);
  185.                 break;
  186.         }
  187.     } while (!done);
  188.     str[len] = 0;
  189.     SetMode(in, 0);
  190.     FPutC(out, '\r');
  191.     Flush(out);
  192. }
  193.