diff options
| author | terminaldweller <thabogre@gmail.com> | 2023-01-14 04:30:53 +0000 | 
|---|---|---|
| committer | terminaldweller <thabogre@gmail.com> | 2023-01-14 04:30:53 +0000 | 
| commit | c81052b70888eb18dca82e33444ebbd9910f5ebc (patch) | |
| tree | 6b6eb892438a15f7b4849d7508d61d9491b809c3 /bin | |
| parent | update (diff) | |
| download | scripts-c81052b70888eb18dca82e33444ebbd9910f5ebc.tar.gz scripts-c81052b70888eb18dca82e33444ebbd9910f5ebc.zip | |
update
Diffstat (limited to '')
| -rwxr-xr-x | bin/clipd | 83 | ||||
| -rwxr-xr-x | bin/scotch | 18 | 
2 files changed, 85 insertions, 16 deletions
| @@ -4,6 +4,15 @@  -- luarocks-5.3 install --local luaposix  -- luarocks-5.3 install --local argparse  -- cat .clip_history | dmenu -l 10 | xsel -ib +local string = require("string") +local signal = require("posix.signal") +local argparse = require("argparse") +local sys_stat = require("posix.sys.stat") +local unistd_getuid = require("posix.unistd.getuid") +local unistd_getgid = require("posix.unistd.getgid") +local unistd_getpid = require("posix.unistd.getpid") +local posix_syslog = require("posix.syslog") +  local function default_luarocks_modules()      local luarocks_handle = io.popen("luarocks-5.3 path --bin")      local path_b = false @@ -24,12 +33,6 @@ local function default_luarocks_modules()  end  default_luarocks_modules() -local string = require("string") -local signal = require("posix.signal") -local argparse = require("argparse") - -signal.signal(signal.SIGINT, function(signum) os.exit(128 + signum) end) -  local function sleep(n) os.execute("sleep " .. tonumber(n)) end  local function trim(s) return s:gsub("^%s+", ""):gsub("%s+$", "") end @@ -38,6 +41,60 @@ parser:option("-s --hist_size", "history file size", 200)  parser:option("-f --hist_file", "history file location",                "/home/devi/.clip_history") +local function log_to_syslog(log_str, log_priority) +    posix_syslog.openlog("clipd", +                         posix_syslog.LOG_NDELAY | posix_syslog.LOG_PID, +                         posix_syslog.LOG_LOCAL0) +    posix_syslog.syslog(log_priority, log_str) +    posix_syslog.closelog() +end + +local function check_clip_hist_perms(clip_hist) +    local uid = unistd_getuid() +    local gid = unistd_getgid() +    for k, v in pairs(sys_stat.stat(clip_hist)) do +        if k == "st_uid" then +            if v ~= uid then +                log_to_syslog( +                    "clipboard history file owned by uid other than the clipd uid", +                    posix_syslog.LOG_CRIT) +                os.exit(1) +            end +        end +        if k == "st_gid" then +            if v ~= gid then +                log_to_syslog( +                    "clipboard history file owned by gid other than the clipd gid", +                    posix_syslog.LOG_CRIT) +                os.exit(1) +            end +        end +        if k == "st_mode" then +            if v & sys_stat.S_IRWXU ~= 0 then +                log_to_syslog( +                    "file permissions are too open. they need to be 0600.", +                    posix_syslog.LOG_CRIT) +                os.exit(1) +            end +        end +    end +end + +local function check_pid_file() +    local f = sys_stat("/var/run/clipd.pid") +    if f ~= nil then +        log_to_syslog("clipd is already running", posix_syslog.LOG_CRIT) +        os.exit(1) +    end +end + +local function write_pid_file() +    local f = io.open("/var/run/clipd.pid") +    f.write(unistd_getpid()) +end + +local function remove_pid_file() end +  local function loop(clip_hist, clip_hist_size)      local clips_table = {}      local hist_current_count = 0 @@ -83,5 +140,15 @@ local function loop(clip_hist, clip_hist_size)      end  end -local args = parser:parse() -loop(args["hist_file"], args["hist_size"]) +local function main() +    signal.signal(signal.SIGINT, function(signum) os.exit(128 + signum) end) +    local args = parser:parse() +    check_clip_hist_perms(args["hist_file"]) +    check_pid_file() +    write_pid_file() +    local status, err = pcall(loop(args["hist_file"], args["hist_size"])) +    if ~status then log_to_syslog(err, posix_syslog.LOG_CRIT) end +    remove_pid_file() +end + +main() @@ -535,16 +535,18 @@ def main():          lines = lines[:-2]          for line in lines:              # this is here to support the -i option -            if line[0] == "[": +            if "-t" in sys.argv or "-tt" in sys.argv or "-ttt" in sys.argv: +                timestamp_end_index = line.find(" ") +                print(Color.six + line[: timestamp_end_index - 1], end=" ") +                line = line[timestamp_end_index + 1 :] +            if "-n" in sys.argv:                  idx = line.find("]") -                if idx - 1 < 16: -                    print(Color.twelve + line[0 : idx + 1], end=" ") -                    line = line[idx + 2 :] -            if line[0] == "[": +                print(Color.twelve + line[0 : idx + 1], end=" ") +                line = line[idx + 2 :] +            if "-i" in sys.argv:                  idx = line.find("]") -                if idx - 1 == 16: -                    print(Color.thirteen + line[0 : idx + 1], end=" ") -                    line = line[idx + 2 :] +                print(Color.thirteen + line[0 : idx + 1], end=" ") +                line = line[idx + 2 :]              if line[0 : line.find("(")] in syscall_set:                  syscall = line[0 : line.find("(")]                  sysargs = line[line.find("(") + 1 : line.find(")")].split() | 
