74 lines
1.3 KiB
Ruby
74 lines
1.3 KiB
Ruby
require 'fileutils'
|
|
|
|
POSIX_NAME_PREFIX = "emacs"
|
|
|
|
|
|
$user_count = 0
|
|
$users = []
|
|
$id_map = {}
|
|
|
|
|
|
## User manipulation
|
|
|
|
class User
|
|
attr_accessor :id, :path, :posixname
|
|
|
|
def to_s
|
|
"#{@id}, #{@path}"
|
|
end
|
|
end
|
|
|
|
def get_user(id)
|
|
$id_map[id]
|
|
end
|
|
|
|
def add_user(id)
|
|
if not system("useradd -m #{POSIX_NAME_PREFIX}#{id}") then
|
|
raise "User creation failed"
|
|
end
|
|
|
|
user = User.new
|
|
user.id = id
|
|
user.path = "/home/#{POSIX_NAME_PREFIX}#{id}"
|
|
user.posixname = "#{POSIX_NAME_PREFIX}#{id}"
|
|
$user_count += 1
|
|
$users << user
|
|
$id_map[id] = user
|
|
|
|
FileUtils.mkdir "#{user.path}/.ssh"
|
|
FileUtils.touch "#{user.path}/.ssh/authorized_keys"
|
|
FileUtils.chown_R user.id, user.id, "#{user.path}/.ssh"
|
|
|
|
user
|
|
end
|
|
|
|
def remove_user(id)
|
|
system("userdel -r #{POSIX_NAME_PREFIX}#{id}")
|
|
$user_count -= 1
|
|
end
|
|
|
|
## Key manipulation
|
|
|
|
class SSHKey
|
|
attr_accessor :cipher, :pubkey, :comment
|
|
|
|
def initialize(cipher,pubkey,comment="")
|
|
@cipher = cipher
|
|
@pubkey = pubkey
|
|
@comment = comment
|
|
end
|
|
end
|
|
|
|
def get_ssh_keys(id)
|
|
user = get_user(id)
|
|
extract_ssh_keys("#{user.path}/.ssh/authorized_keys").each do |k|
|
|
puts k.comment
|
|
end
|
|
end
|
|
|
|
def extract_ssh_keys(file)
|
|
File.open(file,"r") do |f|
|
|
f.read.split("\n").select{ |line| line[0..2] == "ssh" }.map{ |ks| k = ks.split(" "); SSHKey.new(k[0][4..6], k[1], k[2]) }
|
|
end
|
|
end
|