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[:public_key].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])
82 # Handle putty-style ssh public keys
83 key.sub!(/^(Comment: "r[^\n]*\n)(.*)$/m,'ssh-rsa \2 \1')
84 key.sub!(/^(Comment: "d[^\n]*\n)(.*)$/m,'ssh-dss \2 \1')
88 keys[l[:username]].push(key) if not keys[l[:username]].include?(key)
92 devnull = open("/dev/null", "w")
95 next if seen[l[:username]]
96 seen[l[:username]] = true
98 unless pwnam[l[:username]]
99 STDERR.puts "Creating account #{l[:username]}"
100 groups = l[:groups] || []
101 # Adding users to the FUSE group has long been hardcoded behavior.
103 groups.select! { |g| Etc.getgrnam(g) rescue false }
105 unless system("useradd", "-m",
108 "-G", groups.join(","),
111 STDERR.puts "Account creation failed for #{l[:username]}: $?"
115 pwnam[l[:username]] = Etc.getpwnam(l[:username])
117 STDERR.puts "Created account but then getpwnam() failed for #{l[:username]}: #{e}"
122 @homedir = pwnam[l[:username]].dir
123 userdotssh = File.join(@homedir, ".ssh")
124 Dir.mkdir(userdotssh) if !File.exists?(userdotssh)
126 newkeys = "###\n###\n" + keys[l[:username]].join("\n") + "\n###\n###\n"
128 keysfile = File.join(userdotssh, "authorized_keys")
130 if File.exists?(keysfile)
131 oldkeys = IO::read(keysfile)
137 newkeys = exclusive_banner + newkeys
138 elsif oldkeys.start_with?(exclusive_banner)
139 newkeys = start_banner + newkeys + end_banner
140 elsif (m = /^(.*?\n|)#{start_banner}(.*?\n|)#{end_banner}(.*)/m.match(oldkeys))
141 newkeys = m[1] + start_banner + newkeys + end_banner + m[3]
143 newkeys = start_banner + newkeys + end_banner + oldkeys
146 if oldkeys != newkeys then
147 f = File.new(keysfile, 'w')
151 FileUtils.chown_R(l[:username], nil, userdotssh)
152 File.chmod(0700, userdotssh)
153 File.chmod(0750, @homedir)
154 File.chmod(0600, keysfile)
158 rescue Exception => bang
159 puts "Error: " + bang.to_s
160 puts bang.backtrace.join("\n")