Results 1 to 16 of 16

Thread: Two questions about the windows admin account

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218

    Two questions about the windows admin account

    1. How can retrieve in code the administrator login details. I need this because people are going to use my program from within many user accounts and it will have to run using the administrator account using the LogonUser function.

    2. How can I tell in code when I'm not running the windows administrator account? I saw this function for determining if I can write to the registry, but it does not always return false, and I want something more general.

    VB Code:
    1. Private Function CheckTokenPrivileges()
    2.     Dim dateStr As String
    3.     If winVersion = "WNT3" Or winVersion = "WNT4" Or winVersion = "W2000" Or winVersion = "WXP" Then
    4.         If EnablePrivilege(SE_BACKUP_NAME) = False And GetString(HKEY_LOCAL_MACHINE, "SOFTWARE\Xenon\Data", "AUN") <> "" Then
    5.             AdminPasswDialog.Show vbModal, Me   'ask for admin login data
    6.         Else
    7.             Call SetNTPrivileges(True, GetString(HKEY_LOCAL_MACHINE, "SOFTWARE\Xenon\Data", "AUN"), _
    8.                 GetString(HKEY_LOCAL_MACHINE, "SOFTWARE\Xenon\Data", "APW"), _
    9.                 GetString(HKEY_LOCAL_MACHINE, "SOFTWARE\Xenon\Data", "ADM"))
    10.         End If
    11.     End If
    12. End Function
    13. Private Function EnablePrivilege(seName As String) As Boolean   'used to see what token privileges user has. It can't had any new ones though
    14.  
    15.     Dim p_lngRtn As Long
    16.     Dim p_lngToken As Long
    17.     Dim p_lngBufferLen As Long
    18.     Dim p_typLUID As LUID
    19.     Dim p_typTokenPriv As TOKEN_PRIVILEGES
    20.     Dim p_typPrevTokenPriv As TOKEN_PRIVILEGES
    21.     p_lngRtn = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, p_lngToken)
    22.     If p_lngRtn = 0 Then
    23.         Exit Function ' Failed
    24.     ElseIf Err.LastDllError <> 0 Then
    25.         Exit Function ' Failed
    26.     End If
    27.     p_lngRtn = LookupPrivilegeValue(0&, seName, p_typLUID)  'Used to look up privileges LUID.
    28.     If p_lngRtn = 0 Then
    29.         Exit Function ' Failed
    30.     End If
    31.     ' Set it up to adjust the program's security privilege.
    32.     p_typTokenPriv.PrivilegeCount = 1
    33.     p_typTokenPriv.Privileges.Attributes = SE_PRIVILEGE_ENABLED
    34.     p_typTokenPriv.Privileges.pLuid = p_typLUID
    35.     EnablePrivilege = (AdjustTokenPrivileges(p_lngToken, False, p_typTokenPriv, Len(p_typPrevTokenPriv), p_typPrevTokenPriv, p_lngBufferLen) <> 0)
    36. End Function

    Many thanks to all.

  2. #2
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Does this help?
    Does the current user have administration rights?
    VB Code:
    1. 'Called with:
    2. MsgBox IsUserAdmin("YourUserName")
    3.  
    4.  
    5. Private Declare Function NetUserGetInfo Lib "netapi32" (ByVal servername As String, ByVal username As String, ByVal level As Long, bufptr As Long) As Long
    6. Private Const NERR_Success = 0
    7. Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
    8. Private Declare Function NetApiBufferFree Lib "netapi32" (ByVal Buffer As Long) As Long
    9.  
    10. Private Const USER_PRIV_GUEST = 0
    11. Private Const USER_PRIV_USER = 1
    12. Private Const USER_PRIV_ADMINISTRATOR = 2
    13.  
    14. Private Type USER_INFO_3
    15. usri3_name As Long
    16. usri3_password As Long
    17. usri3_password_age As Long
    18. usri3_priv As Long
    19. usri3_home_dir As Long
    20. usri3_comment As Long
    21. usri3_flags As Long
    22. usri3_script_path As Long
    23. usri3_auth_flags As Long
    24. usri3_full_name As Long
    25. usri3_usr_comment As Long
    26. usri3_parms As Long
    27. usri3_workstations As Long
    28. usri3_last_logon As Long
    29. usri3_last_logoff As Long
    30. usri3_acct_expires As Long
    31. usri3_max_storage As Long
    32. usri3_units_per_week As Long
    33. usri3_logon_hours As Long
    34. usri3_bad_pw_count As Long
    35. usri3_num_logons As Long
    36. usri3_logon_server As Long
    37. usri3_country_code As Long
    38. usri3_code_page As Long
    39. usri3_user_id As Long
    40. usri3_primary_group_id As Long
    41. usri3_profile As Long
    42. usri3_home_dir_drive As Long
    43. usri3_password_expired As Long
    44. End Type
    45.  
    46. Public Function IsUserAdmin(p_strUserName As String) As Boolean
    47. Dim udtUSER_INFO As USER_INFO_3
    48. Dim lngBufferPointer As Long
    49.  
    50. IsUserAdmin = False
    51.  
    52. If (NetUserGetInfo("", StrConv(p_strUserName, vbUnicode), 3, lngBufferPointer) = NERR_Success) Then
    53. Call MoveMemory(udtUSER_INFO, ByVal lngBufferPointer, Len(udtUSER_INFO))
    54. Call NetApiBufferFree(ByVal lngBufferPointer)
    55.  
    56. If udtUSER_INFO.usri3_priv = USER_PRIV_ADMINISTRATOR Then
    57. IsUserAdmin = True
    58. End If
    59. End If
    60. End Function

  3. #3

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218

    Thanks

    Thanks Jordan, that was spot on!

  4. #4

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218

    LogonUser

    I can't seem to get the return values expected here using the LogonUser. I get 0 whether the user exists or not. Any ideas anyone?

    Aslo, anyone has a reply to my 1st question. Thank!

    VB Code:
    1. Dim lngReturnCode As Long
    2.     Dim lngTokenHandle As Long
    3.     Dim strServerName As String
    4.    
    5.     On Error GoTo ErrTrap
    6.    
    7.     lngReturnCode = RevertToSelf()  'The blnSwitch value passed in will decipher whether to turn the privelages on or off depending on the value.
    8.                                     'If this is true, I attempt to log onto the domain with the hard coded user name & password. I then take the
    9.                                     'currently logged on users logon thread and promote them to have inherited these admin user rights.
    10.     Select Case blnSwitch
    11.         Case True
    12.             If Len(domain) = 0 Then domain = vbNullString
    13.             lngReturnCode = LogonUser(uname, domain, pword, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, lngTokenHandle)
    14.             If (lngReturnCode = 0) Then
    15.                 lngReturnCode = ImpersonateLoggedOnUser(lngTokenHandle)
    16.                 If (lngReturnCode = 0) Then
    17.                     blnWorkedOk = False
    18.                     Msg = GetLangText(lngArr(), "[ClFormDialog-String20]")
    19.                     Style = vbOKOnly + vbExclamation
    20.                     Title = GetLangText(lngArr(), "[ClFormDialog-String18]")
    21.                     If MsgBox(Msg, Style, Title) = vbOK Then
    22.                         Unload Me
    23.                         End
    24.                     End If
    25.                 Else
    26.                     GoTo LoggedOK
    27.                 End If
    28.             Else
    29.                 GoTo LoggedOK
    30.             End If  'return the users normal privileges and log off the above admin user.
    31.         Case False
    32.             lngReturnCode = RevertToSelf()
    33.     End Select
    34.    
    35. LoggedOK:
    36.     blnWorkedOk = True
    37.     Debug.Print (GetLastError)
    38.     Call SaveString(HKEY_LOCAL_MACHINE, "SOFTWARE\Xenon\Data\", "AUN", uname)
    39.     Call SaveString(HKEY_LOCAL_MACHINE, "SOFTWARE\Xenon\Data\", "APW", pword)
    40.     Call SaveString(HKEY_LOCAL_MACHINE, "SOFTWARE\Xenon\Data\", "ADM", domain)
    41.     CloseHandle lngTokenHandle
    42.     Exit Function

  5. #5

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218

    More such questions

    What I'm trying to do is to install an app so the installation (Windows Installer) and the running of the app works for all users. Anyone know how to peform this?

    The approach I have now is for the application to ask the windows administrator logon details to the user and run using this throughout. Problem is if the password changes, the app has to throw up this box again to ask for the new logon details. I want to avoid having to ask for the details again and wish to make all accounts install & use the app.

    As the program writes to the registry and the windows directory, an alternative could be to find an area that does not require rights.

    Anyone?

    Thanks.

  6. #6

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218

    Please

    *bump* !!

    Anyone ?

  7. #7

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    Plz!!!

  8. #8
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    How do you install your app.

    Did you make it with PDW ?

    If so there is an option you can set so all users can use the program .
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  9. #9

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    Ok. Thanks. I use Windows installer. I saw that using the compressed bootstrap otion I can choose what account to make the installtion run under. So I suppose this answers my 1st question. The other question is still out there. Thanks.

  10. #10

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    Anyone?

  11. #11

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    Please! Please!

  12. #12

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    My program has to be able to run no matter what the current user's rights are. The way i see i can do it is if i use logonuser and logon using the admin stuff. Problem is i need to prompt the user with a dialog box so he enters the username and password. I want to avoid this and have this application look for the details and run using them. I can't ask anyone individual to set up a admin account, even temporary, as the program will be sold to the public and we can't expect people to know how to do such a thing.

    So, when the user enters some username and password in this box i have, the application then uses logonuser to try and log on. Problem is logonuser always returns 0 whether the logon details were actually correct or not. 0, According to msdn indicates an error.

    How can i then check the validity of the admin details the user gives or logon automatically without user intervention?

    Best option would be to look up the credentials of some admin that has sufficient rights to at least make the app write to the registry and disk.

    Any help anyone?

    Thanks.

  13. #13
    Hyperactive Member Steve Stunning's Avatar
    Join Date
    Jul 1999
    Location
    Fairfax, Virginia
    Posts
    314
    Greetings and Salutations,


    Did you ever solve your problem?
    Steve Stunning

  14. #14

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    Hi Steve,

    Thanks. No I could not solve it, but the method I finally settled for is ok. I ask with a form for the admin credentials.

    Would still be better if this could be looked up in code instead.

    Would you have some alternative solution?

    Thanks.

  15. #15
    Hyperactive Member Steve Stunning's Avatar
    Join Date
    Jul 1999
    Location
    Fairfax, Virginia
    Posts
    314
    Try this out. I believe it may help.
    Attached Files Attached Files
    Steve Stunning

  16. #16

    Thread Starter
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    Thanks but I wanted code that would retrieve the admin credentials not just that said if a user is an admin or not.

    thks anyway,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width