MAC 運行shell 獲取權(quán)限

原文地址:http://www.michaelvobrien.com/blog/2009/07/authorizationexecutewithprivileges-a-simple-example/

Introduction

I didn’t find Apple’s documentation to be completely clear on how to grant an Mac OS X application authorization to run system-level commands. The best solution and only solution I could find was to use the function AuthorizationExecuteWithPrivileges. I wrote two simple Xcode projects,OSXSimpleAuth and OSXSlightlyBetterAuth, for OS X Leopard (10.5) to demonstrate its use, and I hope it will help others get something working quickly and gain a basic understanding, so they can concentrate on adding more robust functionality

Simple Example

A simple example of how to use AuthorizationExecuteWithPrivileges is as follows:

  • Create a Authorization Reference (AuthorizationCreate)
  • Run your tool with the authorization reference (AuthorizationExecuteWithPrivileges)

For this example, OSXSimpleAuth, I created a Foundation Tool and added the Security framework to it.

// Create authorization reference
AuthorizationRef authorizationRef;
OSStatus status;
status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
                             kAuthorizationFlagDefaults, &authorizationRef);

// Run the tool using the authorization reference
char *tool = "/sbin/dmesg";
char *args[] = {NULL};
FILE *pipe = NULL;
status = AuthorizationExecuteWithPrivileges(authorizationRef, tool,
                                            kAuthorizationFlagDefaults, args, &pipe);

Slightly Better Example

A slightly better example that uses more options to run AuthorizationExecuteWithPrivilegesand has links to some explanations from Apple’s documentation can be found in OSXSlightlyBetterAuth.

// Create authorization reference
OSStatus status;
AuthorizationRef authorizationRef;

// AuthorizationCreate and pass NULL as the initial
// AuthorizationRights set so that the AuthorizationRef gets created
// successfully, and then later call AuthorizationCopyRights to
// determine or extend the allowable rights.
// http://developer.apple.com/qa/qa2001/qa1172.html
status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
                             kAuthorizationFlagDefaults, &authorizationRef);
if (status != errAuthorizationSuccess)
    NSLog(@"Error Creating Initial Authorization: %d", status);

// kAuthorizationRightExecute == "system.privilege.admin"
AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights rights = {1, &right};
AuthorizationFlags flags = kAuthorizationFlagDefaults |
                           kAuthorizationFlagInteractionAllowed |
                           kAuthorizationFlagPreAuthorize |
                           kAuthorizationFlagExtendRights;

// Call AuthorizationCopyRights to determine or extend the allowable rights.
status = AuthorizationCopyRights(authorizationRef, &rights, NULL, flags, NULL);
if (status != errAuthorizationSuccess)
    NSLog(@"Copy Rights Unsuccessful: %d", status);

NSLog(@"\n\n** %@ **\n\n", @"This command should work.");
char *tool = "/sbin/dmesg";
char *args[] = {NULL};
FILE *pipe = NULL;

status = AuthorizationExecuteWithPrivileges(authorizationRef, tool,
                                            kAuthorizationFlagDefaults, args, &pipe);
if (status != errAuthorizationSuccess)
    NSLog(@"Error: %d", status);

// The only way to guarantee that a credential acquired when you
// request a right is not shared with other authorization instances is
// to destroy the credential.  To do so, call the AuthorizationFree
// function with the flag kAuthorizationFlagDestroyRights.
// http://developer.apple.com/documentation/Security/Conceptual/authorization_concepts/02authconcepts/chapter_2_section_7.html
status = AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights);

Notice the “Right” label in the authorization dialog box screenshot. The AuthorizationItem was set with “system.privilege.admin” via the kAuthorizationRightExecute constant.

Conclusion

Apple recommends only using AuthorizationExecuteWithPrivileges in two cases. One is to create an installer. The other is to repair your helper tool by setting the setuid bit. The helper tool is supposed to encapsulate the root privileged portion of the code. Be aware that I didn’t do this in the examples. Go to the OSXSimpleAuth project page and the OSXSlightlyBetterAuth project page to download the example Xcode projects.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市抵窒,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌颗品,老刑警劉巖苍碟,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件啥供,死亡現(xiàn)場離奇詭異稠集,居然都是意外死亡奶段,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進(jìn)店門剥纷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來痹籍,“玉大人,你說我怎么就攤上這事晦鞋《撞” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵悠垛,是天一觀的道長线定。 經(jīng)常有香客問我,道長鼎文,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任因俐,我火速辦了婚禮拇惋,結(jié)果婚禮上周偎,老公的妹妹穿的比我還像新娘。我一直安慰自己撑帖,他們只是感情好蓉坎,可當(dāng)我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著胡嘿,像睡著了一般蛉艾。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上衷敌,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天勿侯,我揣著相機(jī)與錄音,去河邊找鬼缴罗。 笑死助琐,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的面氓。 我是一名探鬼主播兵钮,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼舌界!你這毒婦竟也來了掘譬?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤呻拌,失蹤者是張志新(化名)和其女友劉穎葱轩,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體柏锄,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡酿箭,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了趾娃。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片汉额。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡愕秫,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情固惯,我是刑警寧澤,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布需频,位于F島的核電站漓帅,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏炕泳。R本人自食惡果不足惜纵诞,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望培遵。 院中可真熱鬧浙芙,春花似錦登刺、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至南窗,卻和暖如春揍很,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背万伤。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工窒悔, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人壕翩。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓蛉迹,卻偏偏與公主長得像,于是被迫代替她去往敵國和親放妈。 傳聞我的和親對象是個殘疾皇子北救,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容