לוגו אתר Fresh          
 
 
  אפשרות תפריט  ראשי     אפשרות תפריט  צ'אט     אפשרות תפריט  מבזקים     אפשרות תפריט  צור קשר     חץ שמאלה ‎print ‎"Hello World!"; if‎ ‎not rules.‎know ‎then rules.‎read(); חץ ימינה  

לך אחורה   לובי הפורומים > מחשבים > תכנות ובניית אתרים
שמור לעצמך קישור לדף זה באתרי שמירת קישורים חברתיים
תגובה
 
כלי אשכול חפש באשכול זה



  #1  
ישן 11-02-2008, 21:40
  לפנים לפנים אינו מחובר  
 
חבר מתאריך: 08.11.07
הודעות: 675
What's the difference between primary access tokens and impersonation access tokens?

מה ההבדל בין primary token ו-impersonation token? האם תמיד אפשר להחליף אחד בשני בעזרת DuplicateTokenEx?

השאלה הזו הטרידה אותי כבר זמן מה, והייתה לי בעיה למצוא תשובה. אם מחפשים בגוגל עזרה בנושא, קצת קשה למצוא משהו מועיל. הנה דוגמה לדף על access tokens, שאומר:
But, What's an access token? A token is:
A group of security attributes permanently attached to a process when a user logs on to the operating system. An access token contains privileges and security identifiers for a user, global group, or local group. The privileges regulate the use of some system services and the security identifiers regulate access to objects that are protected by access-control lists (ACLs). There are two kinds of access token: primary and impersonation.
אבל לא מסביר מה ההבדל בין שני הסוגים. זה לא כל-כך מועיל.

באתר MSDN יש דף שכותרות Access Tokens, שאומר:
An access token is an object that describes the security context of a process or thread. The information in a token includes the identity and privileges of the user account associated with the process or thread. When a user logs on, the system verifies the user's password by comparing it with information stored in a security database. If the password is authenticated, the system produces an access token. Every process executed on behalf of this user has a copy of this access token.

...

Every process has a primary token that describes the security context of the user account associated with the process. By default, the system uses the primary token when a thread of the process interacts with a securable object. Moreover, a thread can impersonate a client account. Impersonation allows the thread to interact with securable objects using the client's security context. A thread that is impersonating a client has both a primary token and an impersonation token.

Use the OpenProcessToken function to retrieve a handle to the primary token of a process. Use the OpenThreadToken function to retrieve a handle to the impersonation token of a thread. For more information, see Impersonation.

אפשר להעלות ניחושים על סמך הכתוב, אבל הם מתבססים על מידע חלקי, ולא ודאיים.

בסופו של דבר הגעתי לספר Programming Window Security מאת Keith Brown, ושם מצאתי את צמד הפסקות הנפלא הבא::
The Type parameter is a historical artifact. If you look at the definition
of the TOKEN_TYPE enumeration, you'll find that token have been
taxonomized into two categories: impersonation versus primary tokens.
Don't get hung up on this nomenclature; the meaning is actually much
simpler than it sounds. Impersonation tokens can only be attached to
threads, and primary tokens can only be attached to processes. That's
all it means.
The process token obtained earlier via OpenProcessToken
was therefore a primary token.

In the very early version of Windows NT (3.x), there were much more
severe restrictions on what you could do with a token, depending on
where you originally got it from, and hence the token type was
introduced to track the intended usage of the token. Because this text
assumes you're using Windows NT 4.0 or greater, just think of an
impersonation token as a "thread token", and a primary token as a
"process token", and use DuplicateTokenEx to convert between the
two whenever necessary.
Windows NT 4.0 tore down the boundaries
between the two by introducing DuplicateTokenEx; the Windows NT
3.x version of this function, DuplicateToken, was hardcoded to only
produce impersonation tokens.
הדגשתי את המשפטים החשובים. ההבדל היחיד הוא ש-primary tokens משייכים לתהליכים ו-impersonation tokens ל-thread-ים. אם רוצים, תמיד, אפשר להמיר אחד לשני.

זה איפשר לי לכתוב כמה מחלקות נורמליות שיטפלו בכל העסק, הן נראות בערך כך:
קוד PHP:
 class AccessToken {
public:
    
enum LogonTypes {...}; // LogonUser parameter
    
enum LogonProviders {...}; // LogonUser parameter
    
    
enum SpecialTokens {CURRENT_THREAD_TOKENCURRENT_PROCESS_TOKEN};
    
    
AccessToken(String usernameString passwordString domainLogonTypes logonTypeLogonProviders logonProvider) {
        
LogonUser(...);
    }
    
    
AccessToken(SpecialTokens specialTokenType) {
        if (
specialTokenType == CURRENT_THREAD_TOKEN) {
            
OpenThreadToken(...);
        } else if (
specialTokenType == CURRENT_PROCESS_TOKEN) {
            
OpenProcessToken(...);
        }
    }
    
    
AccessToken(const AccessTokenotherToken) {
        
DuplicateToken(...)
    }
    
    
HANDLE getTokenHandle() const {
        return 
m_tokenHandle;
    }
};

class 
PrimaryToken : public AccessToken {
public:
    
PrimaryToken(const AccessTokenotherToken) {
        
DuplicateTokenEx(...,TokenPrimary,...);
    }
};

class 
PrimaryToken : public AccessToken {
public:
    
PrimaryToken(const AccessTokenotherToken) {
        
DuplicateTokenEx(...,TokenImpersonation,...);
    }
}; 

המחלקות PrimaryToken ו-ImpersonationToken יכולות להפוך כל token שהוא (אין להן מושג איזה סוג הוא ולא אכפת להן) לסוג "הנכון" (שהן מייצגות). היופי בזה שהוא שעכשיו אני יכול לממש את המחלקה Process (שמריצה תוכנית) בצורה פשוטה:
קוד PHP:
 class Process {
public:
    
Process(StartupInformationstartupInfoPrimaryToken tokenString pathString argsString env, ...) {
        
CreateProcessAsUser(token.getTokenHandle(), ...);
    }
}; 

לתהליכים אפשר להצמיד רק primary token, והדרך היחידה בערך לעשות
את זה היא בזמן ההרצה, על ידי CreateProcessAsUser (או כמה אפשרויות
חדשות שהוסיפו בגרסות חדשות יחסית של ווינדוס). לפחות דרכים מתועדות
ופומביות. לכן המחלקה Process מקבלת ב-constructor רק PrimaryToken-ים,
אבל המשתמש לא זוכר אילו מהקונפיגורציות של פרמטרים ל-LogonUser
ייצרו primary token-ים ואילו ייצרו impersonation token-ים. או שאולי הוא
יוצר אובייקט AccessToken מה-token של ה-thread הנוכחי (ואז הוא יהיה
impersonation token כי זה מה שיש ל-thread-ים)?
אז זה הקטע, הוא לא צריך לזכור, ולא אכפת לו. הוא יוצר AccessToken
בדרך כלשהי, ומעביר אותו ל-constructor של Process. אז מתבצעת
implicit construction של אובייקט PrimaryToken מה-AccessToken או
אפילו מ-ImpersonationToken אם זה מה שניתן (כי ניתן להתייחס
ל-ImpersonationToken&‎ כ-AccessToken&‎), ומשתמש בו. זה בזכות
העובדה שאני יודע שניתן לבצע את ההמרה תמיד, ואין חשש שהיא תיכשל.
אם היה לי חשש שהיא תיכשל, כנראה שהייתי דורש מהמשתמש במחלקות
לבצע את הבנייה מחדש לבד (בעזרת explicit constructors, לדוגמה)
ולבדוק אם עפים exception-ים.

משו לפנים, אה?
_____________________________________
"Saying java is nice because it works on all platforms is like saying anal sex is nice because it works on all genders".

תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
תגובה

כלי אשכול חפש באשכול זה
חפש באשכול זה:

חיפוש מתקדם
מצבי תצוגה דרג אשכול זה
דרג אשכול זה:

מזער את תיבת המידע אפשרויות משלוח הודעות
אתה לא יכול לפתוח אשכולות חדשים
אתה לא יכול להגיב לאשכולות
אתה לא יכול לצרף קבצים
אתה לא יכול לערוך את ההודעות שלך

קוד vB פעיל
קוד [IMG] פעיל
קוד HTML כבוי
מעבר לפורום



כל הזמנים המוצגים בדף זה הם לפי איזור זמן GMT +2. השעה כעת היא 15:23

הדף נוצר ב 0.06 שניות עם 11 שאילתות

הפורום מבוסס על vBulletin, גירסא 3.0.6
כל הזכויות לתוכנת הפורומים שמורות © 2026 - 2000 לחברת Jelsoft Enterprises.
כל הזכויות שמורות ל Fresh.co.il ©

צור קשר | תקנון האתר