2 # Copyright (C) The Arvados Authors. All rights reserved.
4 # SPDX-License-Identifier: AGPL-3.0
13 req_envs = %w(ARVADOS_API_HOST ARVADOS_API_TOKEN ARVADOS_VIRTUAL_MACHINE_UUID)
16 abort "Fatal: These environment vars must be set: #{req_envs}"
20 exclusive_mode = ARGV.index("--exclusive")
21 exclusive_banner = "#######################################################################################
22 # THIS FILE IS MANAGED BY #{$0} -- CHANGES WILL BE OVERWRITTEN #
23 #######################################################################################\n\n"
24 start_banner = "### BEGIN Arvados-managed keys -- changes between markers will be overwritten\n"
25 end_banner = "### END Arvados-managed keys -- changes between markers will be overwritten\n"
27 # Don't try to create any local accounts
28 skip_missing_users = ARGV.index("--skip-missing-users")
33 arv = Arvados.new({ :suppress_ssl_warnings => false })
35 vm_uuid = ENV['ARVADOS_VIRTUAL_MACHINE_UUID']
37 logins = arv.virtual_machine.logins(:uuid => vm_uuid)[:items]
38 logins = [] if logins.nil?
39 logins = logins.reject { |l| l[:username].nil? or l[:hostname].nil? or l[:virtual_machine_uuid] != vm_uuid }
43 open("/etc/login.defs", encoding: "utf-8") do |login_defs|
44 login_defs.each_line do |line|
45 next unless match = /^UID_MIN\s+(\S+)$/.match(line)
46 if match[1].start_with?("0x")
48 elsif match[1].start_with?("0")
53 new_uid_min = match[1].to_i(base)
54 uid_min = new_uid_min if (new_uid_min > 0)
60 if not pwnam[l[:username]]
62 pwnam[l[:username]] = Etc.getpwnam(l[:username])
65 STDERR.puts "Account #{l[:username]} not found. Skipping"
69 if pwnam[l[:username]].uid < uid_min
70 STDERR.puts "Account #{l[:username]} uid #{pwnam[l[:username]].uid} < uid_min #{uid_min}. Skipping"
80 keys[l[:username]] = Array.new() if not keys.has_key?(l[:username])
83 # Handle putty-style ssh public keys
84 key.sub!(/^(Comment: "r[^\n]*\n)(.*)$/m,'ssh-rsa \2 \1')
85 key.sub!(/^(Comment: "d[^\n]*\n)(.*)$/m,'ssh-dss \2 \1')
89 keys[l[:username]].push(key) if not keys[l[:username]].include?(key)
95 current_user_groups = Hash.new
96 while (ent = Etc.getgrent()) do
97 ent.mem.each do |member|
98 current_user_groups[member] ||= Array.new
99 current_user_groups[member].push ent.name
105 next if seen[l[:username]]
106 seen[l[:username]] = true
108 username = l[:username]
110 unless pwnam[l[:username]]
111 STDERR.puts "Creating account #{l[:username]}"
113 unless system("useradd", "-m",
117 STDERR.puts "Account creation failed for #{l[:username]}: #{$?}"
121 pwnam[username] = Etc.getpwnam(username)
123 STDERR.puts "Created account but then getpwnam() failed for #{l[:username]}: #{e}"
128 existing_groups = current_user_groups[username] || []
129 groups = l[:groups] || []
130 # Adding users to the FUSE group has long been hardcoded behavior.
133 groups.select! { |g| Etc.getgrnam(g) rescue false }
135 groups.each do |addgroup|
136 if existing_groups.index(addgroup).nil?
137 # User should be in group, but isn't, so add them.
138 STDERR.puts "Add user #{username} to #{addgroup} group"
139 system("adduser", username, addgroup)
143 existing_groups.each do |removegroup|
144 if groups.index(removegroup).nil?
145 # User is in a group, but shouldn't be, so remove them.
146 STDERR.puts "Remove user #{username} from #{removegroup} group"
147 system("deluser", username, removegroup)
151 homedir = pwnam[l[:username]].dir
152 userdotssh = File.join(homedir, ".ssh")
153 Dir.mkdir(userdotssh) if !File.exist?(userdotssh)
155 newkeys = "###\n###\n" + keys[l[:username]].join("\n") + "\n###\n###\n"
157 keysfile = File.join(userdotssh, "authorized_keys")
159 if File.exist?(keysfile)
160 oldkeys = IO::read(keysfile)
166 newkeys = exclusive_banner + newkeys
167 elsif oldkeys.start_with?(exclusive_banner)
168 newkeys = start_banner + newkeys + end_banner
169 elsif (m = /^(.*?\n|)#{start_banner}(.*?\n|)#{end_banner}(.*)/m.match(oldkeys))
170 newkeys = m[1] + start_banner + newkeys + end_banner + m[3]
172 newkeys = start_banner + newkeys + end_banner + oldkeys
175 if oldkeys != newkeys then
176 f = File.new(keysfile, 'w')
181 userdotconfig = File.join(homedir, ".config")
182 if !File.exist?(userdotconfig)
183 Dir.mkdir(userdotconfig)
186 configarvados = File.join(userdotconfig, "arvados")
187 Dir.mkdir(configarvados) if !File.exist?(configarvados)
189 tokenfile = File.join(configarvados, "settings.conf")
192 if !File.exist?(tokenfile)
193 user_token = arv.api_client_authorization.create(api_client_authorization: {owner_uuid: l[:user_uuid], api_client_id: 0})
194 f = File.new(tokenfile, 'w')
195 f.write("ARVADOS_API_HOST=#{ENV['ARVADOS_API_HOST']}\n")
196 f.write("ARVADOS_API_TOKEN=v2/#{user_token[:uuid]}/#{user_token[:api_token]}\n")
200 STDERR.puts "Error setting token for #{l[:username]}: #{e}"
203 FileUtils.chown_R(l[:username], nil, userdotssh)
204 FileUtils.chown_R(l[:username], nil, userdotconfig)
205 File.chmod(0700, userdotssh)
206 File.chmod(0700, userdotconfig)
207 File.chmod(0700, configarvados)
208 File.chmod(0750, homedir)
209 File.chmod(0600, keysfile)
210 if File.exist?(tokenfile)
211 File.chmod(0600, tokenfile)
215 rescue Exception => bang
216 puts "Error: " + bang.to_s
217 puts bang.backtrace.join("\n")