Créer un compte Admin dans une appli

cs_zitou Messages postés 1 Date d'inscription dimanche 9 février 2003 Statut Membre Dernière intervention 9 février 2003 - 9 févr. 2003 à 20:35
ddegiorgis Messages postés 1 Date d'inscription jeudi 7 août 2003 Statut Membre Dernière intervention 23 février 2004 - 23 févr. 2004 à 16:36
Bonjour a tous,

dans le cadre de mon projet je cherche a créer un compte Administrateur a l'interieur d'une application.
Helas, je n'ai pas trouvé de fonction simple qui puisse le faire. J'ai bien tenté de créer le compte en User puis de le "déplacer" dans le groupe administrateur. Seulement, je suis obligé de désigner en dur le nom du groupe. Ce qui ne me conviens pas car le projet doit marcher pour Windows 2000 tous langages (En francais et anglais cela donne: Administrateurs et Administrators).
Ensuite, j'ai essayé de comprendre le principe des Access Token pour les appliquer mais cela ne marche pas lors de son ouverture alors que je suis les recommendations des MSDN sur le sujet. Le message d'erreur me dit que j'essaie d'ouvrir un Token qui n'existe pas. Bref, je pense que ma comprehension des Access token n'est pas bonne.

Quelqu'un aurait il la bontée de m'expliquer comment marche ces Access token ou plus simplement m'expliquer comment créer un compte administrateur?
Je travaille sur Windows 2000, Visual C++ avec MFC et Platform SDK.

Merci d'avance.

1 réponse

ddegiorgis Messages postés 1 Date d'inscription jeudi 7 août 2003 Statut Membre Dernière intervention 23 février 2004
23 févr. 2004 à 16:36
Denis D.

Bonjour, je vois que cette requête à déjà 1 an, mais comme je suis tombé sur le même problème et qu'il n'y a pas de réponse, en voici une si ça peut aider quelqu'un:

Extrait de MSDN (Platform SDK / Security / Access Control / Access Control / Using Access Control / Searching for a SID in an Access Token)

Searching for a SID in an Access Token
The following example uses the OpenProcessToken and GetTokenInformation functions to get the group memberships in an access token. Then it uses the AllocateAndInitializeSid function to create a SID that identifies the well-known SID of the administrator's group for the local computer. Next, it uses the EqualSid function to compare the well-known SID with the group SIDs from the access token. If the SID is present in the token, the function checks the SIDs attributes to see if it is enabled.

Windows 2000/XP: The CheckTokenMembership function should be used with Windows 2000 and later to determine whether a specified SID is present and enabled in an access token. This function eliminates potential misinterpretations of the active group membership if changes to access tokens are made in future releases.

#define MAX_NAME 256

BOOL SearchTokenGroupsForSID (VOID)
{DWORD i, dwSize 0, dwResult 0;
HANDLE hToken;
PTOKEN_GROUPS pGroupInfo;
SID_NAME_USE SidType;
char lpName[MAX_NAME];
char lpDomain[MAX_NAME];
BYTE sidBuffer[100];
PSID pSID = (PSID)&sidBuffer;
SID_IDENTIFIER_AUTHORITY SIDAuth = SECURITY_NT_AUTHORITY;

// Open a handle to the access token for the calling process.

if (!OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &hToken )) {
printf( "OpenProcessToken Error %u\n", GetLastError() );
return FALSE;
}

// Call GetTokenInformation to get the buffer size.

if(!GetTokenInformation(hToken, TokenGroups, NULL, dwSize, &dwSize)) {
dwResult = GetLastError();
if( dwResult != ERROR_INSUFFICIENT_BUFFER ) {
printf( "GetTokenInformation Error %u\n", dwResult );
return FALSE;
}
}

// Allocate the buffer.

pGroupInfo = (PTOKEN_GROUPS) GlobalAlloc( GPTR, dwSize );

// Call GetTokenInformation again to get the group information.

if(! GetTokenInformation(hToken, TokenGroups, pGroupInfo,
dwSize, &dwSize ) ) {
printf( "GetTokenInformation Error %u\n", GetLastError() );
return FALSE;
}

// Create a SID for the BUILTIN\Administrators group.

if(! AllocateAndInitializeSid( &SIDAuth, 2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&pSID) ) {
printf( "AllocateAndInitializeSid Error %u\n", GetLastError() );
return FALSE;
}

// Loop through the group SIDs looking for the administrator SID.

for(i=0; iGroupCount; i++) {
if ( EqualSid(pSID, pGroupInfo->Groups[i].Sid) ) {

// Lookup the account name and print it.

dwSize = MAX_NAME;
if( !LookupAccountSid( NULL, pGroupInfo->Groups[i].Sid,
lpName, &dwSize, lpDomain,
&dwSize, &SidType ) ) {
dwResult = GetLastError();
if( dwResult == ERROR_NONE_MAPPED )
strcpy( lpName, "NONE_MAPPED" );
else {
printf("LookupAccountSid Error %u\n", GetLastError());
return FALSE;
}
}
printf( "Current user is a member of the %s\\%s group\n",
lpDomain, lpName );

// Find out if the SID is enabled in the token.
if (pGroupInfo->Groups[i].Attributes & SE_GROUP_ENABLED)
printf("The group SID is enabled.\n");
else if (pGroupInfo->Groups[i].Attributes &
SE_GROUP_USE_FOR_DENY_ONLY)
printf("The group SID is a deny-only SID.\n");
else
printf("The group SID is not enabled.\n");
}
}

if (pSID)
FreeSid(pSID);
if ( pGroupInfo )
GlobalFree( pGroupInfo );
return TRUE;
}

Je l'ai essayé, et avec quelques adaptations on en fait ce que l'on veut !
0
Rejoignez-nous