10 req_envs = %w(ARVADOS_API_HOST ARVADOS_API_TOKEN ARVADOS_VIRTUAL_MACHINE_UUID)
13 abort "Fatal: These environment vars must be set: #{req_envs}"
17 exclusive_mode = ARGV.index("--exclusive")
18 exclusive_banner = "#######################################################################################
19 # THIS FILE IS MANAGED BY #{$0} -- CHANGES WILL BE OVERWRITTEN #
20 #######################################################################################\n\n"
21 start_banner = "### BEGIN Arvados-managed keys -- changes between markers will be overwritten\n"
22 end_banner = "### END Arvados-managed keys -- changes between markers will be overwritten\n"
29 uids = Hash[Etc.to_enum(:passwd).map { |ent| [ent.name, ent.uid] }]
30 gids = Hash[Etc.to_enum(:group).map { |ent| [ent.name, ent.gid] }]
31 arv = Arvados.new({ :suppress_ssl_warnings => false })
33 vm_uuid = ENV['ARVADOS_VIRTUAL_MACHINE_UUID']
35 logins = arv.virtual_machine.logins(:uuid => vm_uuid)[:items]
36 logins = [] if logins.nil?
37 logins = logins.reject { |l| l[:username].nil? or l[:hostname].nil? or l[:public_key].nil? or l[:virtual_machine_uuid] != vm_uuid }
41 open("/etc/login.defs", encoding: "utf-8") do |login_defs|
42 login_defs.each_line do |line|
43 next unless match = /^UID_MIN\s+(\S+)$/.match(line)
44 if match[1].start_with?("0x")
46 elsif match[1].start_with?("0")
51 new_uid_min = match[1].to_i(base)
52 uid_min = new_uid_min if (new_uid_min > 0)
55 logins.reject! { |l| (uids[l[:username]] || 65535) < uid_min }
61 keys[l[:username]] = Array.new() if not keys.has_key?(l[:username])
63 # Handle putty-style ssh public keys
64 key.sub!(/^(Comment: "r[^\n]*\n)(.*)$/m,'ssh-rsa \2 \1')
65 key.sub!(/^(Comment: "d[^\n]*\n)(.*)$/m,'ssh-dss \2 \1')
69 keys[l[:username]].push(key) if not keys[l[:username]].include?(key)
73 devnull = open("/dev/null", "w")
76 next if seen[l[:username]]
77 seen[l[:username]] = true if not seen.has_key?(l[:username])
79 unless uids[l[:username]]
80 STDERR.puts "Creating account #{l[:username]}"
81 groups = l[:groups] || []
82 # Adding users to the FUSE group has long been hardcoded behavior.
84 groups.select! { |name| gids[name] }
86 next unless system("useradd", "-m",
89 "-G", groups.join(","),
93 # Create .ssh directory if necessary
94 @homedir = Etc.getpwnam(l[:username]).dir
95 userdotssh = File.join(@homedir, ".ssh")
96 Dir.mkdir(userdotssh) if !File.exists?(userdotssh)
98 newkeys = "###\n###\n" + keys[l[:username]].join("\n") + "\n###\n###\n"
100 keysfile = File.join(userdotssh, "authorized_keys")
102 if File.exists?(keysfile)
103 oldkeys = IO::read(keysfile)
109 newkeys = exclusive_banner + newkeys
110 elsif oldkeys.start_with?(exclusive_banner)
111 newkeys = start_banner + newkeys + end_banner
112 elsif (m = /^(.*?\n|)#{start_banner}(.*?\n|)#{end_banner}(.*)/m.match(oldkeys))
113 newkeys = m[1] + start_banner + newkeys + end_banner + m[3]
115 newkeys = start_banner + newkeys + end_banner + oldkeys
118 if oldkeys != newkeys then
119 f = File.new(keysfile, 'w')
123 FileUtils.chown_R(l[:username], nil, userdotssh)
124 File.chmod(0700, userdotssh)
125 File.chmod(0750, @homedir)
126 File.chmod(0600, keysfile)
130 rescue Exception => bang
131 puts "Error: " + bang.to_s
132 puts bang.backtrace.join("\n")