2010-07-10 129 views
3

我的應用需要做一些特權工作。我一直在尋找,但我找不到任何有用的東西。我知道我想使用Policykit1和dbus,因爲我發現的所有其他選項都不再使用。如何爲我的應用程序獲得root權限?

這是我到目前爲止的代碼:

import dbus 
import os 

bus = dbus.SystemBus() 
proxy = bus.get_object('org.freedesktop.PolicyKit1', '/org/freedesktop/PolicyKit1/Authority') 
authority = dbus.Interface(proxy, dbus_interface='org.freedesktop.PolicyKit1.Authority') 

system_bus_name = bus.get_unique_name() 

subject = ('system-bus-name', {'name' : system_bus_name}) 
action_id = 'org.freedesktop.policykit.exec' 
details = {} 
flags = 1   # AllowUserInteraction flag 
cancellation_id = '' # No cancellation i 


result = authority.CheckAuthorization(subject, action_id, details, flags, cancellation_id) 

os.makedirs('/usr/local/share/somefolder') 

我不能讓目錄,我究竟做錯了什麼?

回答

1

文件系統安全性正在阻止您,因爲您的用戶沒有對/usr/local/share/somefolder的寫入權限。您可以使用sudo臨時升級該目錄創建權限。但是,如果您需要以超級用戶身份執行更多操作,它並不會停留在那裏。

如果你需要寫入一些不在用戶空間中的東西,整個程序可能會更好地以root身份運行(當然是sudo),比如sudo ./myscript.py

+1

整個過程是通過使用dbus和policykit作爲獲取權限的手段來避免使用諸如「sudo ./myscript.py」之類的東西。到目前爲止,這行代碼:result = authority.CheckAuthorization(subject,action_id,details,flags,cancellation_id)能夠請求身份驗證,但即使我完全使用root密碼,應用程序的權限也不會升高。 – zimio 2010-07-10 11:07:54

+0

如果你想通過調用PolicyKit D-Bus方法來提高權限(這是不可能的),這與在'sudo'下運行沒有區別,因爲用戶必須信任你的整個應用程序。 PolicyKit只允許調用特權系統服務的預配置方法。 – rkhayrov 2010-07-11 21:32:10

+0

您是否考慮過使用'gksudo',並用一個簡單的bash腳本打包您的應用程序來執行'gksudo myscript.py'?這當然是一個簡單的解決方法。 – jathanism 2010-07-12 16:31:22

相關問題