Lines Matching full:local

7 local unistd = require("posix.unistd")
8 local sys_stat = require("posix.sys.stat")
9 local lfs = require("lfs")
11 local function getlocalbase()
12 local f = io.popen("sysctl -in user.localbase 2> /dev/null")
13 local localbase = f:read("*l")
17 localbase = "/usr/local"
22 local function decode_base64(input)
26 local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
29 local result = {}
30 local bits = ''
34 local x = input:sub(i, i)
38 local f = b:find(x) - 1
45 local byte = bits:sub(i, i + 7)
47 local c = 0
58 local function encode_base64(input)
62 local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
63 local result = {}
64 local pos = 1
65 local padding = ""
67 local a = string.byte(input, pos)
68 local bb = pos + 1 <= #input and string.byte(input, pos + 1) or 0
69 local c = pos + 2 <= #input and string.byte(input, pos + 2) or 0
87 local function shell_escape(s)
91 local function warnmsg(str, prepend)
95 local tag = ""
102 local function errmsg(str, prepend)
107 local function chmod(path, mode)
109 local _, err, msg = sys_stat.chmod(path, mode)
115 local function chown(path, owner, group)
116 local _, err, msg = unistd.chown(path, owner, group)
122 local function dirname(oldpath)
126 local path = oldpath:gsub("[^/]+/*$", "")
136 local function mkdir_p(path)
140 local r, err = mkdir_p(dirname(path))
147 local function sethostname(hostname)
178 local root = os.getenv("NUAGE_FAKE_ROOTDIR")
182 local hostnamepath = root .. "/etc/rc.conf.d/hostname"
185 local f, err = io.open(hostnamepath, "w")
194 local function update_etc_hosts(root, hostname)
198 local hosts_path = root .. "/etc/hosts"
199 local lines = {}
200 local already_present = false
202 local f = io.open(hosts_path, "r")
205 local nf = io.open(hosts_path, "w")
229 local new_lines = {}
230 local found_localhost = false
255 local function splitlist(list)
256 local ret = {}
269 local function splitlines(s)
270 local ret = {}
279 local function getgroups()
280 local root = os.getenv("NUAGE_FAKE_ROOTDIR")
281 local cmd = "pw "
286 local f = io.popen(cmd .. "groupshow -a 2> /dev/null | cut -d: -f1")
287 local groups = f:read("*a")
293 local function purge_group(groups)
294 local existing = getgroups()
295 local ret = {}
298 local found = false
315 local function adduser(pwd)
320 local root = os.getenv("NUAGE_FAKE_ROOTDIR")
321 local cmd = "pw "
325 local f = io.popen(cmd .. " usershow " .. shell_escape(pwd.name) .. " -7 2> /dev/null")
326 local pwdstr = f:read("*a")
337 local extraargs = ""
339 local list = splitlist(pwd.groups)
346 local escaped_list = {}
364 local postcmd = ""
365 local input = nil
385 local r = f:close()
402 local function addgroup(grp)
407 local root = os.getenv("NUAGE_FAKE_ROOTDIR")
408 local cmd = "pw "
412 local f = io.popen(cmd .. " groupshow " .. shell_escape(grp.name) .. " 2> /dev/null")
413 local grpstr = f:read("*a")
418 local extraargs = ""
420 local list = splitlist(grp.members)
421 local escaped_list = {}
432 local r = os.execute(cmd)
441 local function addsshkey(homedir, key)
442 local root = os.getenv("NUAGE_FAKE_ROOTDIR")
446 local ak_path = homedir .. "/.ssh/authorized_keys"
447 local dotssh_path = homedir .. "/.ssh"
450 local ak_exists = lfs.attributes(ak_path) ~= nil
451 local dotssh_exists = lfs.attributes(dotssh_path) ~= nil
455 local r, err = mkdir_p(dotssh_path)
463 local dirattrs = lfs.attributes(homedir)
469 local f = io.open(ak_path, "a")
488 local function adddoas(pwd)
489 local root = os.getenv("NUAGE_FAKE_ROOTDIR")
490 local localbase = getlocalbase()
491 local etcdir = localbase .. "/etc"
495 local doasconf = etcdir .. "/doas.conf"
497 local doasconf_exists = lfs.attributes(doasconf) ~= nil
498 local etcdir_exists = lfs.attributes(etcdir) ~= nil
502 local r, err = mkdir_p(etcdir)
509 local f = io.open(doasconf, "a")
515 local rule = pwd.doas
520 local rule = str
536 local function addsudo(pwd)
537 local root = os.getenv("NUAGE_FAKE_ROOTDIR")
538 local localbase = getlocalbase()
539 local sudoers_dir = localbase .. "/etc/sudoers.d"
543 local sudoers = sudoers_dir .. "/90-nuageinit-users"
545 local sudoers_exists = lfs.attributes(sudoers) ~= nil
546 local sudoers_dir_exists = lfs.attributes(sudoers_dir) ~= nil
550 local r, err = mkdir_p(sudoers_dir)
557 local f = io.open(sudoers, "a")
580 local function update_sshd_config(key, value)
581 local sshd_config = "/etc/ssh/sshd_config"
582 local root = os.getenv("NUAGE_FAKE_ROOTDIR")
586 local f = io.open(sshd_config, "r")
599 local lines = {}
600 local found = false
601 local pattern = "^%s*"..key:lower().."%s+(%w+)%s*#?.*$"
603 local _, _, val = line:lower():find(pattern)
632 local function delete_ssh_host_keys(root)
633 local ssh_dir = root .. "/etc/ssh"
634 local attrs = lfs.attributes(ssh_dir)
645 local function exec_change_password(user, password, type, expire)
646 local root = os.getenv("NUAGE_FAKE_ROOTDIR")
647 local cmd = "pw "
651 local postcmd = " -H 0"
652 local input = password
667 local f = io.popen(cmd .. " >/dev/null", "w")
672 local r = f:close()
679 local function change_password_from_line(line, expire)
680 local user, password = line:match("%s*(%w+):(%S+)%s*")
681 local type = nil
695 local function chpasswd(obj)
700 local expire = false
739 local function settimezone(timezone)
743 local root = os.getenv("NUAGE_FAKE_ROOTDIR")
748 local f, _, rc = os.execute("tzsetup -s -C " .. shell_escape(root) .. " " .. shell_escape(timezone))
756 local function pkg_bootstrap()
767 local function install_package(package)
771 local install_cmd = "pkg install -y " .. shell_escape(package)
772 local test_cmd = "pkg info -q " .. shell_escape(package)
784 local function run_pkg_cmd(subcmd)
785 local cmd = "env ASSUME_ALWAYS_YES=yes pkg " .. subcmd
792 local function update_packages()
796 local function upgrade_packages()
800 local function addfile(file, defer)
813 local content = nil
825 local mode = "w"
830 local root = os.getenv("NUAGE_FAKE_ROOTDIR")
834 local filepath = root .. file.path
836 local f = assert(io.open(filepath, mode))
845 local owner, group = string.match(file.owner, "([^:]+):([^:]+)")
854 local function add_fstab_entry(root, device, mount_point, fstype, options, dump_freq, passno)
855 local fstab_path = root .. "/etc/fstab"
856 local f = io.open(fstab_path, "a")
870 local function write_resolv_conf(root, config)
871 local path = root .. "/etc/resolv.conf"
872 local f = io.open(path, "w")
887 local opts = {}
901 local function remove_fstab_entry(root, mount_point)
902 local fstab_path = root .. "/etc/fstab"
903 local f = io.open(fstab_path, "r")
907 local lines = {}
909 local fields = {}
918 local nf = io.open(fstab_path, "w")
929 local function parse_mime_multipart(data)
930 local boundary = data:match("boundary=\"([^\"]+)\"")
937 local parts = {}
938 local pos = data:find("\n") or 1
939 local first = data:find("--" .. boundary, pos, true)
947 local nextb = data:find("--" .. boundary, pos, true)
949 local part = data:sub(pos, nextb - 1)
951 local header_end = part:find("\r?\n\r?\n")
952 local headers_str, body
959 local ct = "text/plain"
961 local m = headers_str:match("[Cc]ontent%-[Tt]ype:%s*([^%s;]+)")
965 local after = data:sub(nextb + 2 + #boundary, nextb + 3 + #boundary)
973 local n = {