home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / security / winnt / gina / options.c < prev    next >
C/C++ Source or Header  |  1997-10-09  |  9KB  |  332 lines

  1. //+---------------------------------------------------------------------------
  2. //
  3. //  Microsoft Windows
  4. //  Copyright 1992 - 1997 Microsoft Corporation.
  5. //
  6. //  File:       options.c
  7. //
  8. //  Contents:
  9. //
  10. //  Classes:
  11. //
  12. //  Functions:
  13. //
  14. //  History:    5-02-95   RichardW   Created
  15. //
  16. //----------------------------------------------------------------------------
  17.  
  18. #include "gina.h"
  19. #pragma hdrstop
  20.  
  21. typedef
  22. NET_API_STATUS (NET_API_FUNCTION * NUCP)(LPWSTR, LPWSTR, LPWSTR, LPWSTR);
  23.  
  24. NUCP    NetUserChangePasswordFn = NULL;
  25. HMODULE hNetApi32;
  26.  
  27. BOOL
  28. LoadNetapi(HWND hDlg)
  29. {
  30.     hNetApi32 = LoadLibrary(TEXT("netapi32.dll"));
  31.     if (hNetApi32)
  32.     {
  33.         NetUserChangePasswordFn = (NUCP) GetProcAddress(hNetApi32, "NetUserChangePassword");
  34.         if (NetUserChangePasswordFn)
  35.         {
  36.             return(TRUE);
  37.         }
  38.     }
  39.  
  40.     ErrorMessage(hDlg, TEXT("Change Password"), MB_ICONSTOP | MB_OK);
  41.  
  42.     return(FALSE);
  43.  
  44. }
  45.  
  46. PWSTR
  47. AllocAndCaptureText(
  48.     HWND    hDlg,
  49.     int     Id)
  50. {
  51.     WCHAR   szTemp[MAX_PATH];
  52.     PWSTR   New;
  53.     DWORD   cb;
  54.  
  55.     cb = GetDlgItemText(hDlg, Id, szTemp, MAX_PATH);
  56.     New = LocalAlloc(LMEM_FIXED, (cb + 1) * sizeof(WCHAR));
  57.     if (New)
  58.     {
  59.         wcscpy(New, szTemp);
  60.     }
  61.     return(New);
  62. }
  63.  
  64. PWSTR
  65. DupString(
  66.     PWSTR   pszText)
  67. {
  68.     PWSTR   New;
  69.     DWORD   cb;
  70.  
  71.     cb = (wcslen(pszText) + 1) * sizeof(WCHAR);
  72.     New = LocalAlloc(LMEM_FIXED, cb);
  73.  
  74.     if (New)
  75.     {
  76.         wcscpy(New, pszText);
  77.     }
  78.  
  79.     return(New);
  80. }
  81.  
  82. BOOL
  83. TryToChangePassword(
  84.     HWND        hDlg,
  85.     PGlobals    pGlobals)
  86. {
  87.     PWSTR   pszUsername;
  88.     PWSTR   pszDomain;
  89.     PWSTR   pszOld;
  90.     PWSTR   pszNew;
  91.     PWSTR   pszTemp;
  92.  
  93.     NET_API_STATUS  NetStatus;
  94.  
  95.     if (!NetUserChangePasswordFn)
  96.     {
  97.         if (!LoadNetapi(hDlg))
  98.         {
  99.             return(FALSE);
  100.         }
  101.     }
  102.  
  103.     pszUsername = AllocAndCaptureText(hDlg, IDD_USER_NAME);
  104.     pszDomain = AllocAndCaptureText(hDlg, IDD_DOMAIN);
  105.     pszOld = AllocAndCaptureText(hDlg, IDD_OLD_PASSWORD);
  106.     pszNew = AllocAndCaptureText(hDlg, IDD_NEW_PASSWORD);
  107.  
  108.     if (!pszUsername || !pszDomain || !pszOld || !pszNew)
  109.     {
  110.         goto clean_exit;
  111.     }
  112.  
  113.     pszTemp = AllocAndCaptureText(hDlg, IDD_CONFIRM_PASSWORD);
  114.     if (wcscmp(pszNew, pszTemp))
  115.     {
  116.         LocalFree(pszTemp);
  117.         MessageBox(hDlg, TEXT("Your passwords did not match."), TEXT("Change Password"),
  118.                         MB_ICONSTOP | MB_OK);
  119.         goto clean_exit;
  120.     }
  121.  
  122.     NetStatus = NetUserChangePasswordFn(pszDomain, pszUsername, pszOld, pszNew);
  123.     if (NetStatus != NERR_Success)
  124.     {
  125.         SetLastError(NetStatus);
  126.         ErrorMessage(hDlg, TEXT("Change Password"), MB_ICONSTOP | MB_OK);
  127.     }
  128.     else
  129.         MessageBox(hDlg, TEXT("Your password was changed successfully"),
  130.                     TEXT("Change Password"), MB_ICONINFORMATION | MB_OK);
  131.  
  132. clean_exit:
  133.     if (pszUsername)
  134.     {
  135.         LocalFree(pszUsername);
  136.     }
  137.     if (pszDomain)
  138.     {
  139.         LocalFree(pszDomain);
  140.     }
  141.     if (pszOld)
  142.     {
  143.         LocalFree(pszOld);
  144.     }
  145.     if (pszNew)
  146.     {
  147.         LocalFree(pszNew);
  148.     }
  149.  
  150.     return(NetStatus == NERR_Success);
  151. }
  152.  
  153. int
  154. CALLBACK
  155. ChangePasswordDlgProc(
  156.     HWND        hDlg,
  157.     UINT        Message,
  158.     WPARAM      wParam,
  159.     LPARAM      lParam)
  160. {
  161.     PGlobals    pGlobals;
  162.  
  163.     pGlobals = (PGlobals) GetWindowLong(hDlg, GWL_USERDATA);
  164.     switch (Message)
  165.     {
  166.         case WM_INITDIALOG:
  167.             CenterWindow(hDlg);
  168.             SetWindowLong(hDlg, GWL_USERDATA, lParam);
  169.             return(TRUE);
  170.  
  171.         case WM_COMMAND:
  172.             if (LOWORD(wParam) == IDOK)
  173.             {
  174.                 if (TryToChangePassword(hDlg, pGlobals))
  175.                 {
  176.                     SetWindowText(GetDlgItem(hDlg, IDCANCEL), TEXT("Done"));
  177.                 }
  178.                 SetDlgItemText(hDlg, IDD_OLD_PASSWORD, TEXT(""));
  179.                 SetDlgItemText(hDlg, IDD_NEW_PASSWORD, TEXT(""));
  180.                 SetDlgItemText(hDlg, IDD_CONFIRM_PASSWORD, TEXT(""));
  181.                 SetFocus(GetDlgItem(hDlg, IDD_OLD_PASSWORD));
  182.  
  183.                 return(TRUE);
  184.             }
  185.             if (LOWORD(wParam) == IDCANCEL)
  186.             {
  187.                 EndDialog(hDlg, 0);
  188.                 return(TRUE);
  189.             }
  190.  
  191.     }
  192.     return(FALSE);
  193. }
  194.  
  195.  
  196.  
  197. int
  198. CALLBACK
  199. ConfigDlgProc(
  200.     HWND        hDlg,
  201.     UINT        Message,
  202.     WPARAM      wParam,
  203.     LPARAM      lParam)
  204. {
  205.     PGlobals    pGlobals;
  206.  
  207.     pGlobals = (PGlobals) GetWindowLong(hDlg, GWL_USERDATA);
  208.     switch (Message)
  209.     {
  210.         case WM_INITDIALOG:
  211.             pGlobals = (PGlobals) lParam;
  212.  
  213.             CenterWindow(hDlg);
  214.             SetWindowLong(hDlg, GWL_USERDATA, lParam);
  215.  
  216.             CheckDlgButton(hDlg, IDD_NO_NEW_USERS, !pGlobals->fAllowNewUser);
  217.  
  218.             CheckDlgButton(hDlg, IDD_AUTO_LOGON,
  219.                     (pGlobals->pAccount->Flags & MINI_AUTO_LOGON) ? 1 : 0 );
  220.  
  221.             return(TRUE);
  222.  
  223.         case WM_COMMAND:
  224.             switch (LOWORD(wParam))
  225.             {
  226.                 case IDOK:
  227.                     pGlobals->fAllowNewUser = !IsDlgButtonChecked(hDlg, IDD_NO_NEW_USERS);
  228.                     if (IsDlgButtonChecked(hDlg, IDD_AUTO_LOGON))
  229.                     {
  230.                         pGlobals->pAccount->Flags |= MINI_AUTO_LOGON;
  231.                     }
  232.                     else
  233.                     {
  234.                         pGlobals->pAccount->Flags &= ~MINI_AUTO_LOGON;
  235.                     }
  236.                     EndDialog(hDlg, IDOK);
  237.                     return(TRUE);
  238.  
  239.                 case IDCANCEL:
  240.                     EndDialog(hDlg, IDCANCEL);
  241.                     return(TRUE);
  242.  
  243.             }
  244.     }
  245.  
  246.     return(FALSE);
  247.  
  248. }
  249.  
  250. int
  251. InitOptionsDialog(
  252.     HWND        hDlg,
  253.     LPARAM      lParam)
  254. {
  255.     CenterWindow(hDlg);
  256.     SetWindowLong(hDlg, GWL_USERDATA, lParam);
  257.  
  258.     return(1);
  259. }
  260.  
  261.  
  262.  
  263. int
  264. CALLBACK
  265. OptionsDlgProc(
  266.     HWND        hDlg,
  267.     UINT        Message,
  268.     WPARAM      wParam,
  269.     LPARAM      lParam)
  270. {
  271.     PGlobals    pGlobals;
  272.     int         result;
  273.  
  274.  
  275.     pGlobals = (PGlobals) GetWindowLong(hDlg, GWL_USERDATA);
  276.  
  277.     switch (Message)
  278.     {
  279.         case WM_INITDIALOG:
  280.             return InitOptionsDialog(hDlg, lParam);
  281.  
  282.         case WM_COMMAND:
  283.             switch (LOWORD(wParam))
  284.             {
  285.                 case IDCANCEL:
  286.                     EndDialog(hDlg, WLX_SAS_ACTION_NONE);
  287.                     return(TRUE);
  288.  
  289.                 case IDD_LOCK_BUTTON:
  290.                     EndDialog(hDlg, WLX_SAS_ACTION_LOCK_WKSTA);
  291.                     return(TRUE);
  292.  
  293.                 case IDD_TASK_BUTTON:
  294.                     EndDialog(hDlg, WLX_SAS_ACTION_TASKLIST);
  295.                     return(TRUE);
  296.  
  297.                 case IDD_OPTIONS_EXIT:
  298.                     result = pWlxFuncs->WlxDialogBoxParam(  hGlobalWlx,
  299.                                                         hDllInstance,
  300.                                                         (LPTSTR) MAKEINTRESOURCE(IDD_SHUTDOWN),
  301.                                                         hDlg,
  302.                                                         ShutdownDlgProc,
  303.                                                         (LONG) pGlobals);
  304.                     if (result != WLX_SAS_ACTION_NONE)
  305.                     {
  306.                         EndDialog(hDlg, result);
  307.                     }
  308.                     return(TRUE);
  309.  
  310.                 case IDD_PASSWORD_BUTTON:
  311.                     pWlxFuncs->WlxDialogBoxParam(   hGlobalWlx,
  312.                                                     hDllInstance,
  313.                                                     (LPTSTR) MAKEINTRESOURCE(IDD_CHANGE_PASSWORD),
  314.                                                     hDlg,
  315.                                                     ChangePasswordDlgProc,
  316.                                                     (LONG) pGlobals);
  317.                     return(TRUE);
  318.  
  319.                 case IDD_CONFIG_BUTTON:
  320.                     pWlxFuncs->WlxDialogBoxParam(   hGlobalWlx,
  321.                                                     hDllInstance,
  322.                                                     (LPTSTR) MAKEINTRESOURCE(IDD_LOGON_CONFIG),
  323.                                                     hDlg,
  324.                                                     ConfigDlgProc,
  325.                                                     (LONG) pGlobals);
  326.                     return(TRUE);
  327.  
  328.             }
  329.     }
  330.     return(FALSE);
  331. }
  332.