include std/pipeio.e as pipe public sequence userLogin = "" public function execCommand(sequence cmd) sequence s = "" object z = pipe:create() object p = pipe:exec(cmd, z) if atom(p) then printf(1, "Failed to exec() with error %x", pipe:error_no()) pipe:kill(p) return -1 end if object c = pipe:read(p[pipe:STDOUT], 256) while sequence(c) and length(c) do s &= c if atom(c) then printf(1, "Failed on read with error %x", pipe:error_no()) pipe:kill(p) return -1 end if c = pipe:read(p[pipe:STDOUT], 256) end while --Close pipes and make sure process is terminated pipe:kill(p) return s end function public function checkUser(sequence user) -- check user account sequence cmd = "grep "&user&" /etc/passwd | wc -l" puts(1, "cmd = '"&cmd&"'\n") object un = execCommand(cmd) if atom(un) then puts(1, "Failed to verify user account\n") return 0 end if puts(1, "un = '"&un&"'\n") if equal(un, "1\n") then userLogin = user return 1 else return 0 end if end function public function checkPassword(sequence password) -- python is needed to ckeck user password -- check password if length(userLogin)=0 then return 0 end if sequence cmd = "grep "&userLogin&" /etc/shadow | cut -d':' -f2" puts(1, "cmd = '"&cmd&"'\n") object pw = execCommand(cmd) if atom(pw) then puts(1, "Failed to verify user password\n") return 0 end if puts(1, "pw = '"&pw&"'\n") cmd = sprintf("python -c 'import crypt; print crypt.crypt(\"%s\",\"%s\")'",{password,pw[1..$-1]}) object shadow = execCommand(cmd) if atom(shadow) then puts(1, "Failed to verify user password\n") return 0 end if puts(1, "shadow = '"&shadow&"'\n") -- Shadow password is correct when crypt("unencrypted password","crypted password") equals "crypted password" if equal(shadow, pw) then return 1 else return 0 end if end function /* Usage include std/console.e include checkUserAndPassword.e if checkUser("your_login") then puts(1, "User known, enter password\n") if checkPassword("your_password") then puts(1, "Password correct, you are now logged in\n") else puts(1, "Wrong password, user rejected\n") end if else puts(1, "User unknown, login rejected\n") end if Needs to be run as root!!! */