Mercurial > emacs
comparison src/w32.c @ 87990:78715eba45dd
(g_b_init_get_sid_sub_authority, g_b_init_get_sid_sub_authority_count): New
static variables.
(GetSidSubAuthority_Proc, GetSidSubAuthorityCount_Proc): New typedefs.
(get_sid_sub_authority, get_sid_sub_authority_count): New functions.
(init_user_info): Use the above two new functions to retrieve uid and gid.
Use 500/513, the Windows defaults, as Administrator's uid/gid.
author | Eli Zaretskii <eliz@gnu.org> |
---|---|
date | Sat, 26 Jan 2008 13:03:39 +0000 |
parents | f7a641e1f03d |
children | e576feac5b90 |
comparison
equal
deleted
inserted
replaced
87989:a0f010afb29f | 87990:78715eba45dd |
---|---|
118 static BOOL g_b_init_is_windows_9x; | 118 static BOOL g_b_init_is_windows_9x; |
119 static BOOL g_b_init_open_process_token; | 119 static BOOL g_b_init_open_process_token; |
120 static BOOL g_b_init_get_token_information; | 120 static BOOL g_b_init_get_token_information; |
121 static BOOL g_b_init_lookup_account_sid; | 121 static BOOL g_b_init_lookup_account_sid; |
122 static BOOL g_b_init_get_sid_identifier_authority; | 122 static BOOL g_b_init_get_sid_identifier_authority; |
123 static BOOL g_b_init_get_sid_sub_authority; | |
124 static BOOL g_b_init_get_sid_sub_authority_count; | |
123 | 125 |
124 /* | 126 /* |
125 BEGIN: Wrapper functions around OpenProcessToken | 127 BEGIN: Wrapper functions around OpenProcessToken |
126 and other functions in advapi32.dll that are only | 128 and other functions in advapi32.dll that are only |
127 supported in Windows NT / 2k / XP | 129 supported in Windows NT / 2k / XP |
159 LPTSTR DomainName, | 161 LPTSTR DomainName, |
160 LPDWORD cbDomainName, | 162 LPDWORD cbDomainName, |
161 PSID_NAME_USE peUse); | 163 PSID_NAME_USE peUse); |
162 typedef PSID_IDENTIFIER_AUTHORITY (WINAPI * GetSidIdentifierAuthority_Proc) ( | 164 typedef PSID_IDENTIFIER_AUTHORITY (WINAPI * GetSidIdentifierAuthority_Proc) ( |
163 PSID pSid); | 165 PSID pSid); |
166 typedef PDWORD (WINAPI * GetSidSubAuthority_Proc) ( | |
167 PSID pSid, | |
168 DWORD n); | |
169 typedef PUCHAR (WINAPI * GetSidSubAuthorityCount_Proc) ( | |
170 PSID pSid); | |
171 | |
164 | 172 |
165 /* ** A utility function ** */ | 173 /* ** A utility function ** */ |
166 static BOOL | 174 static BOOL |
167 is_windows_9x () | 175 is_windows_9x () |
168 { | 176 { |
347 return NULL; | 355 return NULL; |
348 } | 356 } |
349 return (s_pfn_Get_Sid_Identifier_Authority (pSid)); | 357 return (s_pfn_Get_Sid_Identifier_Authority (pSid)); |
350 } | 358 } |
351 | 359 |
360 PDWORD WINAPI get_sid_sub_authority ( | |
361 PSID pSid, | |
362 DWORD n) | |
363 { | |
364 static GetSidSubAuthority_Proc s_pfn_Get_Sid_Sub_Authority = NULL; | |
365 HMODULE hm_advapi32 = NULL; | |
366 if (is_windows_9x () == TRUE) | |
367 { | |
368 return NULL; | |
369 } | |
370 if (g_b_init_get_sid_sub_authority == 0) | |
371 { | |
372 g_b_init_get_sid_sub_authority = 1; | |
373 hm_advapi32 = LoadLibrary ("Advapi32.dll"); | |
374 s_pfn_Get_Sid_Sub_Authority = | |
375 (GetSidSubAuthority_Proc) GetProcAddress ( | |
376 hm_advapi32, "GetSidSubAuthority"); | |
377 } | |
378 if (s_pfn_Get_Sid_Sub_Authority == NULL) | |
379 { | |
380 return NULL; | |
381 } | |
382 return (s_pfn_Get_Sid_Sub_Authority (pSid, n)); | |
383 } | |
384 | |
385 PUCHAR WINAPI get_sid_sub_authority_count ( | |
386 PSID pSid) | |
387 { | |
388 static GetSidSubAuthorityCount_Proc s_pfn_Get_Sid_Sub_Authority_Count = NULL; | |
389 HMODULE hm_advapi32 = NULL; | |
390 if (is_windows_9x () == TRUE) | |
391 { | |
392 return NULL; | |
393 } | |
394 if (g_b_init_get_sid_sub_authority_count == 0) | |
395 { | |
396 g_b_init_get_sid_sub_authority_count = 1; | |
397 hm_advapi32 = LoadLibrary ("Advapi32.dll"); | |
398 s_pfn_Get_Sid_Sub_Authority_Count = | |
399 (GetSidSubAuthorityCount_Proc) GetProcAddress ( | |
400 hm_advapi32, "GetSidSubAuthorityCount"); | |
401 } | |
402 if (s_pfn_Get_Sid_Sub_Authority_Count == NULL) | |
403 { | |
404 return NULL; | |
405 } | |
406 return (s_pfn_Get_Sid_Sub_Authority_Count (pSid)); | |
407 } | |
408 | |
352 /* | 409 /* |
353 END: Wrapper functions around OpenProcessToken | 410 END: Wrapper functions around OpenProcessToken |
354 and other functions in advapi32.dll that are only | 411 and other functions in advapi32.dll that are only |
355 supported in Windows NT / 2k / XP | 412 supported in Windows NT / 2k / XP |
356 */ | 413 */ |
545 (PVOID) user_sid, sizeof (user_sid), &trash) | 602 (PVOID) user_sid, sizeof (user_sid), &trash) |
546 && lookup_account_sid (NULL, *((PSID *) user_sid), name, &length, | 603 && lookup_account_sid (NULL, *((PSID *) user_sid), name, &length, |
547 domain, &dlength, &user_type)) | 604 domain, &dlength, &user_type)) |
548 { | 605 { |
549 strcpy (the_passwd.pw_name, name); | 606 strcpy (the_passwd.pw_name, name); |
550 /* Determine a reasonable uid value. */ | 607 /* Determine a reasonable uid value. */ |
551 if (stricmp ("administrator", name) == 0) | 608 if (stricmp ("administrator", name) == 0) |
552 { | 609 { |
553 the_passwd.pw_uid = 0; | 610 the_passwd.pw_uid = 500; /* well-known Administrator uid */ |
554 the_passwd.pw_gid = 0; | 611 the_passwd.pw_gid = 513; /* well-known None gid */ |
555 } | 612 } |
556 else | 613 else |
557 { | 614 { |
558 SID_IDENTIFIER_AUTHORITY * pSIA; | 615 /* Use RID, the relative portion of the SID, that is the last |
559 | 616 sub-authority value of the SID. */ |
560 pSIA = get_sid_identifier_authority (*((PSID *) user_sid)); | 617 DWORD n_subauthorities = |
561 /* I believe the relative portion is the last 4 bytes (of 6) | 618 *get_sid_sub_authority_count (*((PSID *) user_sid)); |
562 with msb first. */ | 619 |
563 the_passwd.pw_uid = ((pSIA->Value[2] << 24) + | 620 if (n_subauthorities < 1) |
564 (pSIA->Value[3] << 16) + | 621 the_passwd.pw_uid = 0; /* the "World" RID */ |
565 (pSIA->Value[4] << 8) + | 622 else |
566 (pSIA->Value[5] << 0)); | 623 { |
567 /* restrict to conventional uid range for normal users */ | 624 the_passwd.pw_uid = |
568 the_passwd.pw_uid = the_passwd.pw_uid % 60001; | 625 *get_sid_sub_authority (*((PSID *) user_sid), |
626 n_subauthorities - 1); | |
627 /* Restrict to conventional uid range for normal users. */ | |
628 the_passwd.pw_uid %= 60001; | |
629 } | |
569 | 630 |
570 /* Get group id */ | 631 /* Get group id */ |
571 if (get_token_information (token, TokenPrimaryGroup, | 632 if (get_token_information (token, TokenPrimaryGroup, |
572 (PVOID) user_sid, sizeof (user_sid), &trash)) | 633 (PVOID) user_sid, sizeof (user_sid), &trash)) |
573 { | 634 { |
574 SID_IDENTIFIER_AUTHORITY * pSIA; | 635 n_subauthorities = |
575 | 636 *get_sid_sub_authority_count (*((PSID *) user_sid)); |
576 pSIA = get_sid_identifier_authority (*((PSID *) user_sid)); | 637 |
577 the_passwd.pw_gid = ((pSIA->Value[2] << 24) + | 638 if (n_subauthorities < 1) |
578 (pSIA->Value[3] << 16) + | 639 the_passwd.pw_gid = 0; /* the "World" RID */ |
579 (pSIA->Value[4] << 8) + | 640 else |
580 (pSIA->Value[5] << 0)); | 641 { |
581 /* I don't know if this is necessary, but for safety... */ | 642 the_passwd.pw_gid = |
582 the_passwd.pw_gid = the_passwd.pw_gid % 60001; | 643 *get_sid_sub_authority (*((PSID *) user_sid), |
644 n_subauthorities - 1); | |
645 /* I don't know if this is necessary, but for safety... */ | |
646 the_passwd.pw_gid %= 60001; | |
647 } | |
583 } | 648 } |
584 else | 649 else |
585 the_passwd.pw_gid = the_passwd.pw_uid; | 650 the_passwd.pw_gid = the_passwd.pw_uid; |
586 } | 651 } |
587 } | 652 } |