Polkit functions

pull/129/head
Ivan Yartsev 6 months ago
parent dd306c4017
commit 396f8cd529

@ -13,7 +13,7 @@ include_directories(${VTE291_INCLUDE_DIRS})
link_directories(${VTE291_LIBRARY_DIRS})
add_definitions(${VTE291_CFLAGS_OTHER})
pkg_check_modules(POLKIT REQUIRED polkit-agent-1)
pkg_check_modules(POLKIT REQUIRED polkit-gobject-1)
include_directories(${POLKIT_INCLUDE_DIRS})
link_directories(${POLKIT_LIBRARY_DIRS})
add_definitions(${POLKIT_CFLAGS_OTHER})

@ -2519,12 +2519,6 @@ void on_main_window_tree_selection_changed(GtkWidget *,main_window *widgets){
void on_main_window_services_tree_selection_changed(GtkWidget *,main_window *widgets){
GtkTreeModel *model = GTK_TREE_MODEL(widgets->ServicesList);
GtkTreeIter iter;
if (getuid()!=0){
gtk_widget_set_sensitive(widgets->ServicesEditButton,0);
gtk_widget_set_sensitive(widgets->ServicesEnableDisableButton,0);
gtk_widget_set_sensitive(widgets->ServicesStartStopButton,0);
return;
}
if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->ServicesTree)),&model,&iter)){
gboolean is_active;
@ -2576,12 +2570,12 @@ void on_start_stop_clicked(GtkButton *self, main_window *widgets){
gboolean is_started;
gtk_tree_model_get(model,&iter,0,&is_started,4,&start_command,5,&stop_command,-1);
if (is_started){
yon_launch(stop_command);
g_spawn_command_line_sync(yon_debug_output("%s\n",stop_command),NULL,NULL,NULL,NULL);
gtk_list_store_set(widgets->ServicesList,&iter,0,0,-1);
gtk_image_set_from_icon_name(GTK_IMAGE(gtk_button_get_image(self)),start_icon_name,GTK_ICON_SIZE_BUTTON);
gtk_widget_set_tooltip_text(widgets->ServicesStartStopButton,START_SERVICE_LABEL);
} else {
yon_launch(start_command);
g_spawn_command_line_sync(yon_debug_output("%s\n",start_command),NULL,NULL,NULL,NULL);
gtk_list_store_set(widgets->ServicesList,&iter,0,1,-1);
gtk_image_set_from_icon_name(GTK_IMAGE(gtk_button_get_image(self)),stop_icon_name,GTK_ICON_SIZE_BUTTON);
gtk_widget_set_tooltip_text(widgets->ServicesStartStopButton,STOP_SERVICE_LABEL);
@ -2589,6 +2583,14 @@ void on_start_stop_clicked(GtkButton *self, main_window *widgets){
}
}
queue_func *yon_queue_add_function(GSourceFunc func,void *data);
queue_func *yon_queue_add_function(GSourceFunc func,void *data){
queue_func *queue = malloc(sizeof(queue_func));
queue->func=func;
queue->data=data;
return queue;
}
void on_enable_disable_clicked(GtkButton *self, main_window *widgets){
GtkTreeModel *model = GTK_TREE_MODEL(widgets->ServicesList);
GtkTreeIter iter;
@ -2603,6 +2605,12 @@ void on_enable_disable_clicked(GtkButton *self, main_window *widgets){
gtk_image_set_from_icon_name(GTK_IMAGE(gtk_button_get_image(self)),enable_icon_name,GTK_ICON_SIZE_BUTTON);
gtk_widget_set_tooltip_text(widgets->ServicesEnableDisableButton,AUTOSTART_ON_LABEL);
} else {
/*
g_mutex_lock(&main_config.authorized_mutex);
main_config.queue_size += 1;
main_config.queue = yon_remalloc(main_config.queue,sizeof(queue_func**)*(main_config.queue_size));
main_config.queue[main_config.queue_size-1] = yon_queue_add_function(G_SOURCE_FUNC(system),start_command);
g_mutex_unlock(&main_config.authorized_mutex);*/
yon_launch(start_command);
gtk_list_store_set(widgets->ServicesList,&iter,1,1,-1);
gtk_image_set_from_icon_name(GTK_IMAGE(gtk_button_get_image(self)),disable_icon_name,GTK_ICON_SIZE_BUTTON);
@ -2612,56 +2620,72 @@ void on_enable_disable_clicked(GtkButton *self, main_window *widgets){
}
// standard functions
gboolean on_unelevate(main_window* widgets);
gboolean on_unelevate(main_window* widgets){
yon_ubl_status_box_render(UNAUTHTORIZED_LABEL,BACKGROUND_IMAGE_INFO_TYPE);
gtk_widget_set_sensitive(widgets->UpgradePermissionsMenuItem,1);
g_object_unref(main_config.authorization_result);
main_config.authorization_result=NULL;
return G_SOURCE_REMOVE;
}
gboolean yon_timeoutcheck_authtorization(gpointer widgets);
gboolean yon_timeoutcheck_authtorization(gpointer widgets){
void on_permissions_changed(){
if (g_permission_get_allowed(main_config.permissions)){
PolkitAuthority *auth = polkit_authority_get_sync(NULL,NULL);
PolkitSubject *subject;
pid_t parent_pid;
parent_pid = getppid ();
subject = polkit_unix_process_new_for_owner (parent_pid,0,getuid());
PolkitDetails *dets = polkit_details_new();
if (polkit_authority_check_authorization_sync(auth,subject,"com.ublinux.ubl-settings-logging",dets,POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,NULL,NULL)){
printf("Allowed\n");
char *command[] = {"/bin/pkexec","/bin/echo","1", NULL};
gchar *standard_output = NULL;
gchar *standard_error = NULL;
gint exit_status = -1;
GError *error = NULL;
gboolean success = g_spawn_sync(
NULL,
command,
NULL,
G_SPAWN_DEFAULT,
NULL,
NULL,
&standard_output,
&standard_error,
NULL,
&error
);
if (!polkit_authorization_result_get_is_authorized(main_config.authorization_result)){
gdk_threads_add_idle((GSourceFunc)on_unelevate,widgets);
return G_SOURCE_REMOVE;
}
return G_SOURCE_CONTINUE;
}
if (success) {
if (standard_output != NULL) {
printf("Output: %s\n", standard_output);
g_free(standard_output);
void *yon_authorized_loop(void*);
void *yon_authorized_loop(void*){
while (polkit_authorization_result_get_is_authorized(main_config.authorization_result)){
g_mutex_lock(&main_config.authorized_mutex);
if (main_config.queue){
for (int i=0;i<main_config.queue_size;i++){
G_SOURCE_FUNC(main_config.queue[i]->func)(main_config.queue[i]->data);
}
if (standard_error != NULL) {
printf("Error output: %s\n", standard_error);
g_free(standard_error);
free(main_config.queue);
main_config.queue=NULL;
main_config.queue_size=0;
}
printf("Exit status: %d\n", exit_status);
} else {
g_printerr("Error running command: %s\n", error->message);
g_error_free(error);
g_mutex_unlock(&main_config.authorized_mutex);
}
} else {
if (g_permission_get_can_acquire(main_config.permissions)){
g_permission_acquire(main_config.permissions,NULL,NULL);
return NULL;
}
void authorize_callback(GObject *source, GAsyncResult *res, main_window *widgets);
void authorize_callback(GObject *source, GAsyncResult *res, main_window *widgets){
GError *err = NULL;
main_config.authorization_result = polkit_authority_check_authorization_finish(POLKIT_AUTHORITY(source),res,&err);
if (err){
}
g_thread_new("auth_loop",(GThreadFunc)yon_authorized_loop,NULL);
yon_ubl_status_box_render(AUTHTORIZED_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
g_timeout_add(10000, (GSourceFunc)yon_timeoutcheck_authtorization,widgets);
}
void on_permissions_changed(GtkMenuItem *,main_window *widgets){
PolkitAuthority *authority;
GError *error = NULL;
authority = polkit_authority_get_sync(NULL,&error);
if (!authority){
return;
}
polkit_authority_check_authorization(
authority,
main_config.process,
"org.freedesktop.policykit.exec",
NULL,
POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
NULL,
(GAsyncReadyCallback)authorize_callback,
widgets
);
}
void config_init(){
@ -2677,18 +2701,15 @@ void config_init(){
main_config.remove_thread=-1;
main_config.load_mode=1;
main_config.save_config=1;
PolkitSubject *subject = NULL;
main_config.queue=NULL;
main_config.queue_size=0;
main_config.process = polkit_unix_process_new_for_owner(getpid(),0,getuid());
pid_t parent_pid;
parent_pid = getppid ();
if (parent_pid == 1)
{
g_printerr ("Parent process was reaped by init(1)\n");
}
subject = polkit_unix_process_new_for_owner (parent_pid,0,getuid());
main_config.permissions = polkit_permission_new_sync("com.ublinux.ubl-settings-logging",subject,NULL,NULL);
g_signal_connect(G_OBJECT(main_config.permissions),"notify::allowed",G_CALLBACK(on_permissions_changed),NULL);
}
void yon_main_window_complete(template_main_window **window){
@ -2712,15 +2733,13 @@ void yon_main_window_complete(template_main_window **window){
widgets->ServicesList = GTK_LIST_STORE(gtk_builder_get_object(builder,"ServicesList"));
widgets->UpgradePermissionsMenuItem = yon_gtk_builder_get_widget(builder,"UpgradePermissionsMenuItem");
gtk_menu_shell_prepend(GTK_MENU_SHELL(gtk_widget_get_parent(widgets->AboutMenuItem)),widgets->UpgradePermissionsMenuItem);
widgets->MainTree = yon_gtk_builder_get_widget(builder,"MainTree");
gtk_window_set_title(GTK_WINDOW(widgets->Window),TITLE_LABEL);
{
// GtkWidget *menu = yon_gtk_builder_get_widget(builder,"menu2");
// gtk_menu_shell_append(GTK_MENU_SHELL(menu),widgets->DocumentationMenuItem);
// gtk_menu_shell_append(GTK_MENU_SHELL(menu),widgets->AboutMenuItem);
if (main_config.lock_load_global == 1){
gtk_widget_set_sensitive(widgets->LoadGlobalMenuItem,0);
@ -2868,7 +2887,7 @@ void yon_main_window_complete(template_main_window **window){
g_signal_connect(G_OBJECT(widgets->LoadLocalMenuItem),"activate",G_CALLBACK(on_load_local),NULL);
g_signal_connect(G_OBJECT(widgets->LoadCustomMenuItem),"activate",G_CALLBACK(on_config_custom_load),NULL);
g_signal_connect(G_OBJECT(widgets->UpgradePermissionsMenuItem),"activate",G_CALLBACK(on_permissions_changed),NULL);
g_signal_connect(G_OBJECT(widgets->UpgradePermissionsMenuItem),"activate",G_CALLBACK(on_permissions_changed),widgets);
g_signal_connect(G_OBJECT(widgets->MainTree),"cursor-changed",G_CALLBACK(on_main_window_tree_selection_changed),widgets);
g_signal_connect(G_OBJECT(widgets->ServicesTree),"cursor-changed",G_CALLBACK(on_main_window_services_tree_selection_changed),widgets);
@ -2894,6 +2913,7 @@ void yon_main_window_complete(template_main_window **window){
}
yon_load_proceed(YON_CONFIG_LOCAL);
update_loaded_logrotate();
gtk_widget_set_sensitive(widgets->SaveGlobalMenuItem,1);
}
int main(int argc, char *argv[]){

@ -82,6 +82,11 @@ string version_application;
char *local;
typedef struct {
GSourceFunc func;
void *data;
} queue_func;
typedef struct {
template_config_fields
config_str launch_arguments;
@ -102,6 +107,13 @@ typedef struct {
pthread_t inspector_thread;
int remove_thread;
PolkitAuthorizationResult *authorization_result;
PolkitSubject *process;
queue_func** queue;
int queue_size;
GMutex authorized_mutex;
} config;
typedef struct {

@ -210,3 +210,6 @@
#define FILECHOOSER_TITLE_LABEL _("Logs and events - choose a path")
#define SAVE_TITLE_LABEL _("Logs and events - saving")
#define AUTHTORIZED_LABEL _("Authorization has been elevated")
#define UNAUTHTORIZED_LABEL _("Authorization has been unelevated")

@ -53,6 +53,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
<column type="gchararray"/>
</columns>
</object>
<object class="GtkCheckMenuItem" id="UpgradePermissionsMenuItem">
<property name="can-focus">False</property>
<property name="label" translatable="yes">Upgrade permissions to root</property>
</object>
<object class="GtkImage" id="image1">
<property name="visible">True</property>
<property name="can-focus">False</property>
@ -231,6 +235,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
<property name="can-focus">True</property>
<property name="model">liststore1</property>
<property name="tooltip-column">2</property>
<child internal-child="selection">
<object class="GtkTreeSelection"/>
</child>
<child>
<object class="GtkTreeViewColumn">
<property name="resizable">True</property>
@ -360,7 +367,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
<child>
<object class="GtkButton" id="ServicesEditButton">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="tooltip-text" translatable="yes">Configure service</property>
@ -392,6 +398,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="model">ServicesList</property>
<child internal-child="selection">
<object class="GtkTreeSelection"/>
</child>
<child>
<object class="GtkTreeViewColumn">
<property name="sizing">fixed</property>
@ -481,76 +490,4 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
</packing>
</child>
</object>
<object class="GtkMenu" id="menu1">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="reserve-toggle-size">False</property>
<child>
<object class="GtkMenuItem" id="LoadGlobalConfigurationMenuItem">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Load global configuration</property>
<style>
<class name="menuitemtop"/>
</style>
</object>
</child>
<child>
<object class="GtkMenuItem" id="LoadLocalConfigurationMenuItem">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Load local configuration</property>
<style>
<class name="menuitembottom"/>
</style>
</object>
</child>
</object>
<object class="GtkMenu" id="menu2">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="take-focus">False</property>
<property name="reserve-toggle-size">False</property>
<child>
<object class="GtkCheckMenuItem" id="UpgradePermissionsMenuItem">
<property name="can-focus">False</property>
<property name="label" translatable="yes">Upgrade permissions to root</property>
</object>
</child>
</object>
<object class="GtkMenu" id="menu3">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="reserve-toggle-size">False</property>
<child>
<object class="GtkMenuItem" id="SaveGlobalLocalConfigurationMenuItem">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Save to global and local configuration</property>
<style>
<class name="menuitemtop"/>
</style>
</object>
</child>
<child>
<object class="GtkMenuItem" id="SaveGlobalConfigurationMenuItem">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Save to global configuration</property>
<style>
<class name="menuitemmiddle"/>
</style>
</object>
</child>
<child>
<object class="GtkMenuItem" id="SaveLocalConfigurationMenuItem">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Save to local configuration</property>
<style>
<class name="menuitembottom"/>
</style>
</object>
</child>
</object>
</interface>

Loading…
Cancel
Save