new-dialogue #8
@@ -11,6 +11,7 @@
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
#define _GNU_SOURCE
 | 
			
		||||
#include "cc.h"
 | 
			
		||||
#include "perm_permissions_table.h"
 | 
			
		||||
#include "real_filename.h"
 | 
			
		||||
#include "temp_permissions_table.h"
 | 
			
		||||
@@ -23,7 +24,9 @@
 | 
			
		||||
#include <sys/un.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
#define ZENITY_TEMP_ALLOW_MESSAGE "Allow this time\n"
 | 
			
		||||
#define ZENITY_YES 0
 | 
			
		||||
#define ZENITY_NO 1
 | 
			
		||||
#define ZENITY_PERM 2
 | 
			
		||||
 | 
			
		||||
int init_ui_socket(const char *perm_permissions_db_filename) {
 | 
			
		||||
  FILE *fp = NULL;
 | 
			
		||||
@@ -66,12 +69,8 @@ void destroy_ui_socket(void) {
 | 
			
		||||
access_t ask_access(const char *filename, struct process_info proc_info) {
 | 
			
		||||
  FILE *fp = NULL;
 | 
			
		||||
  char *command = NULL;
 | 
			
		||||
  int ret =
 | 
			
		||||
      asprintf(&command,
 | 
			
		||||
               "zenity --question --extra-button \"Allow this time\" --title "
 | 
			
		||||
               "\"Allow Access?\" --text \"Allow process "
 | 
			
		||||
               "<tt>%s</tt> with PID <tt>%d</tt> to access <tt>%s</tt>\"",
 | 
			
		||||
               proc_info.name, proc_info.PID, filename, get_mountpoint());
 | 
			
		||||
  int ret = asprintf(&command, "zenity \"%d\" \"%s\" \"%s\" \"%s\"",
 | 
			
		||||
                     proc_info.PID, proc_info.name, filename, get_mountpoint());
 | 
			
		||||
 | 
			
		||||
  if (ret < 0) {
 | 
			
		||||
    // If asprintf fails, the contents of command are undefined (see man
 | 
			
		||||
@@ -92,24 +91,30 @@ access_t ask_access(const char *filename, struct process_info proc_info) {
 | 
			
		||||
    return DENY;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // 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
 | 
			
		||||
  // to manually check the output.
 | 
			
		||||
  char buffer[sizeof(ZENITY_TEMP_ALLOW_MESSAGE) + 1];
 | 
			
		||||
  while (fgets(buffer, sizeof(buffer), fp)) {
 | 
			
		||||
    printf("%s", buffer);
 | 
			
		||||
    if (strcmp(buffer, ZENITY_TEMP_ALLOW_MESSAGE) == 0) {
 | 
			
		||||
      pclose(fp);
 | 
			
		||||
      return ALLOW_TEMP;
 | 
			
		||||
    }
 | 
			
		||||
  str(char) zenity_output;
 | 
			
		||||
  init(&zenity_output);
 | 
			
		||||
 | 
			
		||||
  size_t total_read = 0;
 | 
			
		||||
  char line[1024]; // Buffer to read individual lines
 | 
			
		||||
 | 
			
		||||
  // Read the command output line by line
 | 
			
		||||
  while (fgets(line, sizeof(line), fp)) {
 | 
			
		||||
    size_t line_len = strlen(line);
 | 
			
		||||
    push_fmt(&zenity_output, line);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  int zenity_exit_code = WEXITSTATUS(pclose(fp));
 | 
			
		||||
  fprintf(stderr, "zenity wrote out %s\n", first(&zenity_output));
 | 
			
		||||
  fprintf(stderr, "zenity returned %d\n", zenity_exit_code);
 | 
			
		||||
  // zenity returns 1 on "No" >:(
 | 
			
		||||
  if (zenity_exit_code == 0) {
 | 
			
		||||
 | 
			
		||||
  cleanup(&zenity_output);
 | 
			
		||||
 | 
			
		||||
  if (zenity_exit_code == (ZENITY_YES | ZENITY_PERM)) {
 | 
			
		||||
    return ALLOW;
 | 
			
		||||
  }
 | 
			
		||||
  if (zenity_exit_code == ZENITY_YES) {
 | 
			
		||||
    return ALLOW_TEMP;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return DENY;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user