Improved code readability
This commit is contained in:
parent
402a5d109f
commit
55fb5c54c6
@ -13,7 +13,6 @@
|
|||||||
#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>
|
||||||
@ -22,9 +21,10 @@
|
|||||||
#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) {
|
||||||
char line[256];
|
FILE *fp = NULL;
|
||||||
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,13 +43,11 @@ 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 destroy_ui_socket(void) {
|
||||||
destroy_temp_permissions_table();
|
destroy_temp_permissions_table();
|
||||||
destroy_perm_permissions_table();
|
destroy_perm_permissions_table();
|
||||||
}
|
}
|
||||||
@ -63,16 +61,16 @@ void destroy_ui_socket() {
|
|||||||
* @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 pi) {
|
int ask_access(const char *filename, struct process_info proc_info) {
|
||||||
FILE *fp;
|
FILE *fp = NULL;
|
||||||
size_t command_len =
|
size_t command_len =
|
||||||
139 + sizeof(pid_t) * 8 + strlen(pi.name) + strlen(filename);
|
139 + sizeof(pid_t) * 8 + strlen(proc_info.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>\"",
|
||||||
pi.name, pi.PID, filename);
|
proc_info.name, proc_info.PID, filename);
|
||||||
|
|
||||||
// Zenity Question Message Popup
|
// Zenity Question Message Popup
|
||||||
fp = popen(command, "r");
|
fp = popen(command, "r");
|
||||||
@ -86,10 +84,10 @@ int ask_access(const char *filename, struct process_info pi) {
|
|||||||
// 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[1024];
|
char buffer[sizeof(ZENITY_TEMP_ALLOW_MESSAGE) + 1];
|
||||||
while (fgets(buffer, sizeof(buffer), fp)) {
|
while (fgets(buffer, sizeof(buffer), fp)) {
|
||||||
printf("%s", buffer);
|
printf("%s", buffer);
|
||||||
if (strcmp(buffer, "Allow this time\n") == 0) {
|
if (strcmp(buffer, ZENITY_TEMP_ALLOW_MESSAGE) == 0) {
|
||||||
pclose(fp);
|
pclose(fp);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
@ -116,9 +114,11 @@ int ask_access(const char *filename, struct process_info pi) {
|
|||||||
* @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 pi, int opts) {
|
int interactive_access(const char *filename, struct process_info proc_info,
|
||||||
|
int opts) {
|
||||||
|
|
||||||
if (check_temp_access(filename, pi) || check_perm_access(filename, pi)) {
|
if (check_temp_access(filename, proc_info) ||
|
||||||
|
check_perm_access(filename, proc_info)) {
|
||||||
// access was already granted before
|
// access was already granted before
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -127,22 +127,24 @@ int interactive_access(const char *filename, struct process_info pi, int opts) {
|
|||||||
// permissions are granted
|
// permissions are granted
|
||||||
|
|
||||||
if (opts & GRANT_PERM) {
|
if (opts & GRANT_PERM) {
|
||||||
give_perm_access(filename, pi);
|
give_perm_access(filename, proc_info);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (opts & GRANT_TEMP) {
|
if (opts & GRANT_TEMP) {
|
||||||
give_temp_access(filename, pi);
|
give_temp_access(filename, proc_info);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int user_response = ask_access(filename, pi);
|
int user_response = ask_access(filename, proc_info);
|
||||||
if (user_response == 1) {
|
if (user_response == 1) {
|
||||||
// user said "yes"
|
// user said "yes"
|
||||||
give_perm_access(filename, pi);
|
give_perm_access(filename, proc_info);
|
||||||
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, pi);
|
give_temp_access(filename, proc_info);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user