Compare commits

..

No commits in common. "13fd0db8a8c041327640e4f3f72c0f7740020464" and "402a5d109ff6e6946955608a027c4247003c64dd" have entirely different histories.

2 changed files with 20 additions and 24 deletions

2
.gitignore vendored
View File

@ -4,5 +4,3 @@ build/*
test/protected/* test/protected/*
test/.pt.db test/.pt.db
compile_commands.json compile_commands.json
test/perf*
test/callgraph*

View File

@ -13,6 +13,7 @@
#include "perm_permissions_table.h" #include "perm_permissions_table.h"
#include "temp_permissions_table.h" #include "temp_permissions_table.h"
#include "ui-socket.h" #include "ui-socket.h"
#include <errno.h>
#include <pthread.h> #include <pthread.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -21,10 +22,9 @@
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#define ZENITY_TEMP_ALLOW_MESSAGE "Allow this time\n"
int init_ui_socket(const char *perm_permissions_db_filename) { int init_ui_socket(const char *perm_permissions_db_filename) {
FILE *fp = NULL; char line[256];
FILE *fp;
if (init_temp_permissions_table()) { if (init_temp_permissions_table()) {
fprintf(stderr, "Could not initialize temporary permissions table.\n"); fprintf(stderr, "Could not initialize temporary permissions table.\n");
@ -43,11 +43,13 @@ int init_ui_socket(const char *perm_permissions_db_filename) {
return 1; return 1;
} }
while (fgets(line, sizeof(line), fp))
printf("%s", line);
pclose(fp); pclose(fp);
return 0; return 0;
} }
void destroy_ui_socket(void) { void destroy_ui_socket() {
destroy_temp_permissions_table(); destroy_temp_permissions_table();
destroy_perm_permissions_table(); destroy_perm_permissions_table();
} }
@ -61,16 +63,16 @@ void destroy_ui_socket(void) {
* @return: 0 if access is denied, 1 if access is allowed, 2 if access is * @return: 0 if access is denied, 1 if access is allowed, 2 if access is
* allowed for the runtime of the process * allowed for the runtime of the process
*/ */
int ask_access(const char *filename, struct process_info proc_info) { int ask_access(const char *filename, struct process_info pi) {
FILE *fp = NULL; FILE *fp;
size_t command_len = size_t command_len =
139 + sizeof(pid_t) * 8 + strlen(proc_info.name) + strlen(filename); 139 + sizeof(pid_t) * 8 + strlen(pi.name) + strlen(filename);
char *command = (char *)malloc(command_len); char *command = (char *)malloc(command_len);
snprintf(command, command_len, snprintf(command, command_len,
"zenity --question --extra-button \"Allow this time\" --title " "zenity --question --extra-button \"Allow this time\" --title "
"\"Allow Access?\" --text \"Allow process " "\"Allow Access?\" --text \"Allow process "
"<tt>%s</tt> with PID <tt>%d</tt> to access <tt>%s</tt>\"", "<tt>%s</tt> with PID <tt>%d</tt> to access <tt>%s</tt>\"",
proc_info.name, proc_info.PID, filename); pi.name, pi.PID, filename);
// Zenity Question Message Popup // Zenity Question Message Popup
fp = popen(command, "r"); fp = popen(command, "r");
@ -84,10 +86,10 @@ int ask_access(const char *filename, struct process_info proc_info) {
// if the user clicks the "Allow this time" button, `zenity` will only // if the user clicks the "Allow this time" button, `zenity` will only
// write it to `stdout`, but the exit code will still be `1`. So, we need // write it to `stdout`, but the exit code will still be `1`. So, we need
// to manually check the output. // to manually check the output.
char buffer[sizeof(ZENITY_TEMP_ALLOW_MESSAGE) + 1]; char buffer[1024];
while (fgets(buffer, sizeof(buffer), fp)) { while (fgets(buffer, sizeof(buffer), fp)) {
printf("%s", buffer); printf("%s", buffer);
if (strcmp(buffer, ZENITY_TEMP_ALLOW_MESSAGE) == 0) { if (strcmp(buffer, "Allow this time\n") == 0) {
pclose(fp); pclose(fp);
return 2; return 2;
} }
@ -114,11 +116,9 @@ int ask_access(const char *filename, struct process_info proc_info) {
* @param opts: options (GRANT_TEMP, GRANT_PERM) * @param opts: options (GRANT_TEMP, GRANT_PERM)
* @return: 0 if access is denied, 1 if access is allowed * @return: 0 if access is denied, 1 if access is allowed
*/ */
int interactive_access(const char *filename, struct process_info proc_info, int interactive_access(const char *filename, struct process_info pi, int opts) {
int opts) {
if (check_temp_access(filename, proc_info) || if (check_temp_access(filename, pi) || check_perm_access(filename, pi)) {
check_perm_access(filename, proc_info)) {
// access was already granted before // access was already granted before
return 1; return 1;
} }
@ -127,24 +127,22 @@ int interactive_access(const char *filename, struct process_info proc_info,
// permissions are granted // permissions are granted
if (opts & GRANT_PERM) { if (opts & GRANT_PERM) {
give_perm_access(filename, proc_info); give_perm_access(filename, pi);
return 1; return 1;
} }
if (opts & GRANT_TEMP) { if (opts & GRANT_TEMP) {
give_temp_access(filename, proc_info); give_temp_access(filename, pi);
return 1; return 1;
} }
int user_response = ask_access(filename, proc_info); int user_response = ask_access(filename, pi);
if (user_response == 1) { if (user_response == 1) {
// user said "yes" // user said "yes"
give_perm_access(filename, proc_info); give_perm_access(filename, pi);
return 1; return 1;
} } else if (user_response == 2) {
if (user_response == 2) {
// user said "yes, but only this time" // user said "yes, but only this time"
give_temp_access(filename, proc_info); give_temp_access(filename, pi);
return 1; return 1;
} }