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"
24 # some LDAP systems have already the user there
26 dont_create_user = ARGV.index("--dont-create-user")
33 uids = Hash[Etc.to_enum(:passwd).map { |ent| [ent.name, ent.uid] }]
34 gids = Hash[Etc.to_enum(:group).map { |ent| [ent.name, ent.gid] }]
35 arv = Arvados.new({ :suppress_ssl_warnings => false })
37 vm_uuid = ENV['ARVADOS_VIRTUAL_MACHINE_UUID']
39 logins = arv.virtual_machine.logins(:uuid => vm_uuid)[:items]
40 logins = [] if logins.nil?
41 logins = logins.reject { |l| l[:username].nil? or l[:hostname].nil? or l[:public_key].nil? or l[:virtual_machine_uuid] != vm_uuid }
45 open("/etc/login.defs", encoding: "utf-8") do |login_defs|
46 login_defs.each_line do |line|
47 next unless match = /^UID_MIN\s+(\S+)$/.match(line)
48 if match[1].start_with?("0x")
50 elsif match[1].start_with?("0")
55 new_uid_min = match[1].to_i(base)
56 uid_min = new_uid_min if (new_uid_min > 0)
59 logins.reject! { |l| (uids[l[:username]] || 65535) < uid_min }
65 keys[l[:username]] = Array.new() if not keys.has_key?(l[:username])
67 # Handle putty-style ssh public keys
68 key.sub!(/^(Comment: "r[^\n]*\n)(.*)$/m,'ssh-rsa \2 \1')
69 key.sub!(/^(Comment: "d[^\n]*\n)(.*)$/m,'ssh-dss \2 \1')
73 keys[l[:username]].push(key) if not keys[l[:username]].include?(key)
77 devnull = open("/dev/null", "w")
80 next if seen[l[:username]]
81 seen[l[:username]] = true if not seen.has_key?(l[:username])
83 unless uids[l[:username]] and not dont_create_user
84 STDERR.puts "Creating account #{l[:username]}"
85 groups = l[:groups] || []
86 # Adding users to the FUSE group has long been hardcoded behavior.
88 groups.select! { |name| gids[name] }
90 next unless system("useradd", "-m",
93 "-G", groups.join(","),
98 # If after all this effort isn't listed using Etc.getpwnam()
99 # this means that wont be available in the system
100 # some LDAP configurations will need this
102 # Create .ssh directory if necessary
103 Etc.getpwnam(l[:username])
105 STDERR.puts "Account #{l[:username]} not found. Skipping"
109 @homedir = Etc.getpwnam(l[:username]).dir
110 userdotssh = File.join(@homedir, ".ssh")
111 Dir.mkdir(userdotssh) if !File.exists?(userdotssh)
113 newkeys = "###\n###\n" + keys[l[:username]].join("\n") + "\n###\n###\n"
115 keysfile = File.join(userdotssh, "authorized_keys")
117 if File.exists?(keysfile)
118 oldkeys = IO::read(keysfile)
124 newkeys = exclusive_banner + newkeys
125 elsif oldkeys.start_with?(exclusive_banner)
126 newkeys = start_banner + newkeys + end_banner
127 elsif (m = /^(.*?\n|)#{start_banner}(.*?\n|)#{end_banner}(.*)/m.match(oldkeys))
128 newkeys = m[1] + start_banner + newkeys + end_banner + m[3]
130 newkeys = start_banner + newkeys + end_banner + oldkeys
133 if oldkeys != newkeys then
134 f = File.new(keysfile, 'w')
138 FileUtils.chown_R(l[:username], nil, userdotssh)
139 File.chmod(0700, userdotssh)
140 File.chmod(0750, @homedir)
141 File.chmod(0600, keysfile)
145 rescue Exception => bang
146 puts "Error: " + bang.to_s
147 puts bang.backtrace.join("\n")