commit 687d174eb525fb1f87e1f9b262704f48babf89a3 Author: Christoffer Müller Madsen Date: Fri May 26 23:10:41 2017 +0200 humble beginnings diff --git a/collab.rb b/collab.rb new file mode 100644 index 0000000..fe151e2 --- /dev/null +++ b/collab.rb @@ -0,0 +1,73 @@ +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