about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/lcurses/window.c2
-rw-r--r--src/teliva.c136
-rw-r--r--src/teliva.h35
3 files changed, 154 insertions, 19 deletions
diff --git a/src/lcurses/window.c b/src/lcurses/window.c
index ded837f..c722a32 100644
--- a/src/lcurses/window.c
+++ b/src/lcurses/window.c
@@ -1321,6 +1321,8 @@ Wgetch(lua_State *L)
 	}
 	if (c == CTRL_E)
 		developer_mode(L);
+	if (c == CTRL_P)
+		permissions_mode(L);
 	/* handle other standard menu hotkeys here */
 
 	return pushintresult(c);
diff --git a/src/teliva.c b/src/teliva.c
index 00c3ca8..f2814c2 100644
--- a/src/teliva.c
+++ b/src/teliva.c
@@ -66,24 +66,31 @@ static void draw_menu(lua_State* L) {
 static void render_permissions(lua_State* L) {
   attrset(A_NORMAL);
   mvaddstr(LINES-1, COLS-12, "");
-  attron(COLOR_PAIR(COLOR_PAIR_RISK));
+  int file_colors = file_operations_allowed ? COLOR_PAIR_WARN : COLOR_PAIR_SAFE;
+  int net_colors = net_operations_allowed ? COLOR_PAIR_WARN : COLOR_PAIR_SAFE;
+  if (file_operations_allowed && net_operations_allowed) {
+    file_colors = net_colors = COLOR_PAIR_RISK;
+  }
+
+  attron(COLOR_PAIR(file_colors));
   addstr("file ");
   attron(A_REVERSE);
   addstr(" ");
-  attroff(COLOR_PAIR(COLOR_PAIR_RISK));
-  attron(COLOR_PAIR(COLOR_PAIR_RISK));
+  attroff(COLOR_PAIR(file_colors));
+
+  attron(COLOR_PAIR(net_colors));
   addstr(" ");
   attroff(A_REVERSE);
   addstr(" net");
-  attroff(COLOR_PAIR(COLOR_PAIR_RISK));
+  attroff(COLOR_PAIR(net_colors));
 }
 
 void render_trusted_teliva_data(lua_State* L) {
   init_pair(COLOR_PAIR_ERROR, COLOR_ERROR_FOREGROUND, COLOR_ERROR_BACKGROUND);
   init_pair(COLOR_PAIR_MENU, COLOR_FOREGROUND, COLOR_BACKGROUND);
-  init_pair(COLOR_PAIR_SAFE, COLOR_SAFE, COLOR_FOREGROUND);
-  init_pair(COLOR_PAIR_WARN, COLOR_WARN, COLOR_FOREGROUND);
-  init_pair(COLOR_PAIR_RISK, COLOR_RISK, COLOR_FOREGROUND);
+  init_pair(COLOR_PAIR_SAFE, COLOR_SAFE_REVERSE, COLOR_FOREGROUND);
+  init_pair(COLOR_PAIR_WARN, COLOR_WARN_REVERSE, COLOR_FOREGROUND);
+  init_pair(COLOR_PAIR_RISK, COLOR_RISK_REVERSE, COLOR_FOREGROUND);
   draw_menu(L);
 }
 
@@ -1005,7 +1012,6 @@ static void clear_call_graph(lua_State* L) {
   assert(lua_gettop(L) == oldtop);
 }
 
-
 char* Image_name = NULL;
 extern void set_args (lua_State *L, char **argv, int n);
 extern void load_tlv(lua_State* L, char* filename);
@@ -1029,3 +1035,117 @@ int handle_image(lua_State* L, char** argv, int n) {
   if (status != 0) return report_in_developer_mode(L, status);
   return 0;
 }
+
+int file_operations_allowed = false;
+int net_operations_allowed = false;
+
+static void permissions_menu() {
+  attrset(A_REVERSE);
+  for (int x = 0; x < COLS; ++x)
+    mvaddch(LINES-1, x, ' ');
+  attrset(A_NORMAL);
+  menu_column = 2;
+  draw_menu_item("^x", "go back");
+  draw_menu_item("^f", "toggle file permissions");
+  draw_menu_item("^n", "toggle network permissions");
+  attrset(A_NORMAL);
+}
+
+static void render_permissions_screen(lua_State* L) {
+  clear();
+  attrset(A_BOLD);
+  mvaddstr(1, 0, "Permissions");
+  attrset(A_NORMAL);
+  int file_colors = file_operations_allowed ? COLOR_PAIR_WARN : COLOR_PAIR_SAFE;
+  int net_colors = net_operations_allowed ? COLOR_PAIR_WARN : COLOR_PAIR_SAFE;
+  if (file_operations_allowed && net_operations_allowed) {
+    file_colors = net_colors = COLOR_PAIR_RISK;
+  }
+
+  attron(COLOR_PAIR(file_colors));
+  mvaddstr(3, 5, "File operations");
+  attron(A_REVERSE);
+  switch (file_colors) {
+    case COLOR_PAIR_SAFE:
+      mvaddstr(3, 30, " forbidden (safe)              ");
+      break;
+    case COLOR_PAIR_WARN:
+      mvaddstr(3, 30, " allowed (more risky)          ");
+      break;
+    case COLOR_PAIR_RISK:
+      mvaddstr(3, 30, "                               ");
+      break;
+    default:
+      abort();
+  }
+  attroff(A_REVERSE);
+  attroff(COLOR_PAIR(file_colors));
+
+  attron(COLOR_PAIR(net_colors));
+  mvaddstr(5, 5, "Network operations");
+  attron(A_REVERSE);
+  switch (net_colors) {
+    case COLOR_PAIR_SAFE:
+      mvaddstr(5, 30, " forbidden (safe)              ");
+      break;
+    case COLOR_PAIR_WARN:
+      mvaddstr(5, 30, " allowed (more risky)          ");
+      break;
+    case COLOR_PAIR_RISK:
+      mvaddstr(5, 30, "                               ");
+      break;
+    default:
+      abort();
+  }
+  attroff(A_REVERSE);
+  attroff(COLOR_PAIR(net_colors));
+
+  if (file_operations_allowed && net_operations_allowed) {
+    attron(COLOR_PAIR(COLOR_PAIR_RISK));
+    mvaddstr(8, 5, "When both file and network operations are permitted, Teliva can't tell if this app does something sketchy.");
+    mvaddstr(9, 5, "You're relying either on your understanding of its code, or your trust of its author(s).");
+    attroff(COLOR_PAIR(COLOR_PAIR_RISK));
+  }
+  permissions_menu();
+  refresh();
+}
+
+static void permissions_view(lua_State* L) {
+  while (true) {
+    render_permissions_screen(L);
+    int c = getch();
+    switch (c) {
+      case CTRL_X:
+        return;
+      case CTRL_F:
+        file_operations_allowed = !file_operations_allowed;
+        break;
+      case CTRL_N:
+        net_operations_allowed = !net_operations_allowed;
+        break;
+    }
+  }
+}
+
+void permissions_mode(lua_State* L) {
+  assume_default_colors(COLOR_FOREGROUND, COLOR_BACKGROUND);
+  init_pair(COLOR_PAIR_NORMAL, COLOR_FOREGROUND, COLOR_BACKGROUND);
+  init_pair(COLOR_PAIR_SELECTABLE, COLOR_SELECTABLE_FOREGROUND, COLOR_SELECTABLE_BACKGROUND);
+  init_pair(COLOR_PAIR_FADE, COLOR_FADE, COLOR_BACKGROUND);
+  init_pair(COLOR_PAIR_MENU_ALTERNATE, COLOR_MENU_ALTERNATE, COLOR_BACKGROUND);
+  init_pair(COLOR_PAIR_LUA_COMMENT, COLOR_LUA_COMMENT, COLOR_BACKGROUND);
+  init_pair(COLOR_PAIR_LUA_KEYWORD, COLOR_LUA_KEYWORD, COLOR_BACKGROUND);
+  init_pair(COLOR_PAIR_LUA_CONSTANT, COLOR_LUA_CONSTANT, COLOR_BACKGROUND);
+  init_pair(COLOR_PAIR_MATCH, COLOR_MATCH_FOREGROUND, COLOR_MATCH_BACKGROUND);
+  init_pair(COLOR_PAIR_ERROR, COLOR_ERROR_FOREGROUND, COLOR_ERROR_BACKGROUND);
+  /* permissions colors slightly different than in the menu */
+  init_pair(COLOR_PAIR_SAFE, COLOR_SAFE_NORMAL, COLOR_BACKGROUND);
+  init_pair(COLOR_PAIR_WARN, COLOR_WARN_NORMAL, COLOR_BACKGROUND);
+  init_pair(COLOR_PAIR_RISK, COLOR_RISK_NORMAL, COLOR_BACKGROUND);
+  nodelay(stdscr, 0);  /* always make getch() block in developer mode */
+  curs_set(1);  /* always display cursor in developer mode */
+  permissions_view(L);
+  cleanup_curses();
+  execv(Argv[0], Argv);
+  /* never returns */
+}
diff --git a/src/teliva.h b/src/teliva.h
index 3a3a443..bf01c11 100644
--- a/src/teliva.h
+++ b/src/teliva.h
@@ -20,6 +20,8 @@ enum KEY_ACTION {
   ENTER = 10,
   CTRL_K = 11,
   CTRL_L = 12,
+  CTRL_N = 14,
+  CTRL_P = 16,
   CTRL_Q = 17,
   CTRL_R = 18,
   CTRL_S = 19,
@@ -52,7 +54,7 @@ enum KEY_ACTION {
  * for a map of available colors. */
 
 /* Toggle between a few color schemes */
-#define COLOR_SCHEME 0
+#define COLOR_SCHEME 2
 #if COLOR_SCHEME == 0
 /* Light color scheme. */
 enum color {
@@ -64,9 +66,12 @@ enum color {
   COLOR_SELECTABLE_BACKGROUND = 250,
   COLOR_ERROR_FOREGROUND = COLOR_BACKGROUND,
   COLOR_ERROR_BACKGROUND = 124,               /* deep red */
-  COLOR_WARN = 172,                           /* orange */
-  COLOR_SAFE = 46,                            /* green */
-  COLOR_RISK = 196,                           /* red */
+  COLOR_SAFE_NORMAL = 28,                     /* green */
+  COLOR_SAFE_REVERSE = 46,                    /* green */
+  COLOR_WARN_NORMAL = 130,                    /* orange */
+  COLOR_WARN_REVERSE = 172,                   /* orange */
+  COLOR_RISK_NORMAL = 196,                    /* red */
+  COLOR_RISK_REVERSE = 196,                   /* red */
   COLOR_LUA_COMMENT = 27,                     /* blue */
   COLOR_LUA_KEYWORD = 172,                    /* orange */
   COLOR_LUA_CONSTANT = 31,                    /* cyan */
@@ -84,9 +89,12 @@ enum color {
   COLOR_SELECTABLE_BACKGROUND = 250,
   COLOR_ERROR_FOREGROUND = COLOR_FOREGROUND,
   COLOR_ERROR_BACKGROUND = 124,               /* deep red */
-  COLOR_WARN = 172,                           /* orange */
-  COLOR_SAFE = 28,                            /* green */
-  COLOR_RISK = 196,                           /* red */
+  COLOR_SAFE_NORMAL = 46,                     /* green */
+  COLOR_SAFE_REVERSE = 28,                    /* green */
+  COLOR_WARN_NORMAL = 172,                    /* orange */
+  COLOR_WARN_REVERSE = 130,                   /* orange */
+  COLOR_RISK_NORMAL = 196,                    /* red */
+  COLOR_RISK_REVERSE = 196,                   /* red */
   COLOR_LUA_COMMENT = 39,                     /* blue */
   COLOR_LUA_KEYWORD = 172,                    /* orange */
   COLOR_LUA_CONSTANT = 37,                    /* cyan */
@@ -104,9 +112,12 @@ enum color {
   COLOR_SELECTABLE_BACKGROUND = 31,
   COLOR_ERROR_FOREGROUND = 250,
   COLOR_ERROR_BACKGROUND = 124,               /* deep red */
-  COLOR_WARN = 130,                           /* orange */
-  COLOR_SAFE = 28,                            /* green */
-  COLOR_RISK = 196,                           /* red */
+  COLOR_SAFE_NORMAL = 46,                     /* green */
+  COLOR_SAFE_REVERSE = 28,                    /* green */
+  COLOR_WARN_NORMAL = 172,                    /* orange */
+  COLOR_WARN_REVERSE = 130,                   /* orange */
+  COLOR_RISK_NORMAL = 201,                    /* red */
+  COLOR_RISK_REVERSE = 196,                   /* red */
   COLOR_LUA_COMMENT = 45,                     /* light blue */
   COLOR_LUA_KEYWORD = 172,                    /* orange */
   COLOR_LUA_CONSTANT = 37,                    /* cyan */
@@ -141,8 +152,10 @@ enum color_pair {
 /* Integrate with Lua VM */
 extern char** Argv;
 extern int handle_image(lua_State* L, char** argv, int n);
-
 extern void developer_mode(lua_State* L);
+extern void permissions_mode(lua_State* L);
+extern int file_operations_allowed;
+extern int net_operations_allowed;
 
 extern int load_editor_buffer_to_current_definition_in_image(lua_State* L);
 extern void save_to_current_definition_and_editor_buffer(lua_State* L, const char* definition);