Class: DanarchyDeploy::Users

Inherits:
Object
  • Object
show all
Defined in:
lib/danarchy_deploy/users.rb

Class Method Summary collapse

Class Method Details

.add_to_group(user, options) ⇒ Object



102
103
104
105
106
# File 'lib/danarchy_deploy/users.rb', line 102

def self.add_to_group(user, options)
  groups = user[:groups].join(',')
  groupadd_cmd = "usermod #{user[:username]} --groups #{groups} --append"
  DanarchyDeploy::Helpers.run_command(groupadd_cmd, options)
end

.authorized_keys(user) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/danarchy_deploy/users.rb', line 114

def self.authorized_keys(user)
  ssh_path = user[:home] + '/.ssh'
  authkeys = ssh_path + '/authorized_keys'

  Dir.exist?(ssh_path) || Dir.mkdir(ssh_path, 0700)
  File.chown(user[:uid], user[:gid], ssh_path)
  File.open(authkeys, 'a+') do |f|
    contents = f.read
    user[:authorized_keys].each do |authkey|
      if contents.include?(authkey)
        puts "   - Key already in place: #{authkey}"
      else
        puts "   + Adding authorized_key: #{authkey}"
        f.puts authkey
      end
    end

    f.chown(user[:uid], user[:gid])
    f.close
  end
end

.checkgroups(usercheck_result, user, options) ⇒ Object



89
90
91
92
93
94
# File 'lib/danarchy_deploy/users.rb', line 89

def self.checkgroups(usercheck_result, user, options)
  return nil if !usercheck_result[:stdout]
  livegroups = usercheck_result[:stdout].split(/\s+/).last.split('=').last.gsub(/\(([^)]*)\)/, '').split(',').map(&:to_i)
  livegroups.delete(user[:gid])
  livegroups.sort == user[:groups].sort
end

.new(deployment, options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/danarchy_deploy/users.rb', line 4

def self.new(deployment, options)
  puts "\n" + self.name
  (useradd_result, userdel_result, archives_result) = nil

  deployment[:users].each do |username, user|
    user[:username] = username.to_s
    puts "\n > Checking if user '#{user[:username]}' already exists."
    usercheck_result = usercheck(user, options)

    if usercheck_result[:stdout]
      puts "   - User: #{user[:username]} already exists!"
    else
      group = { groupname: user[:username] }
      group[:gid] = user[:gid] ? user[:gid] : nil
      group[:system] = user[:system] ? user[:system] : nil

      groupcheck_result = DanarchyDeploy::Groups.groupcheck(group, options)
      if !groupcheck_result[:stdout] && group[:gid]
        puts "   |+ Adding group: #{group[:groupname]}"
        DanarchyDeploy::Groups.groupadd(group, options)
      end

      puts "   |+ Adding user: #{user[:username]}"
      useradd_result = useradd(user, options)
      File.chmod(0750, user[:home]) if Dir.exist?(user[:home])
    end

    if !options[:pretend]
      puts "\n > Checking groups for user: #{user[:username]}"
      if user[:groups] && checkgroups(usercheck_result, user, options) == false
        updategroups(user, options)
        puts "   |+ Updated groups: #{user[:groups].join(',')}"
      else
        puts "   - No change to groups needed."
      end

      if user[:authorized_keys]
        puts "\n > Checking on #{user[:authorized_keys].count} authorized_keys for user: #{user[:username]}"
        authorized_keys(user)
      end
      
      if user[:sudoer]
        puts "\n > Checking sudo rules for user: #{user[:username]}"
        sudoer(user)
      end
    end

    if user[:applications]
      puts "\n > Checking #{user[:username]}'s applications."
      user = DanarchyDeploy::Applicator.new(deployment[:os], user, options)
    end

    user.delete(:username)
  end

  # [useradd_result, userdel_result]
  deployment
end

.remove_from_group(user, group, options) ⇒ Object



108
109
110
111
112
# File 'lib/danarchy_deploy/users.rb', line 108

def self.remove_from_group(user, group, options)
  groups = user[:groups].join(',')
  removegroup_cmd = "gpasswd --remove #{user[:username]} #{group}"
  DanarchyDeploy::Helpers.run_command(removegroup_cmd, options)
end

.sudoer(user) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/danarchy_deploy/users.rb', line 136

def self.sudoer(user)
  sudoer_file = '/etc/sudoers.d/danarchy_deploy-' + user[:username]
  File.open(sudoer_file, 'a+') do |f|
    if !f.read.include?(user[:sudoer])
      puts "   |+ Added: '#{user[:sudoer]}'"
      f.puts user[:sudoer]
    else
      puts '   - No change needed'
    end

    f.close
  end
end

.updategroups(user, options) ⇒ Object



96
97
98
99
100
# File 'lib/danarchy_deploy/users.rb', line 96

def self.updategroups(user, options)
  groups = user[:groups].join(',')
  groupupdate_cmd = "usermod #{user[:username]} --groups #{groups}"
  DanarchyDeploy::Helpers.run_command(groupupdate_cmd, options)
end

.useradd(user, options) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/danarchy_deploy/users.rb', line 64

def self.useradd(user, options)
  useradd_cmd  = "useradd #{user[:username]} "
  useradd_cmd += "--home-dir #{user[:home]} " if user[:home]
  useradd_cmd += "--create-home " if !Dir.exist?(user[:home])
  useradd_cmd += "--uid #{user[:uid]} " if user[:uid]
  useradd_cmd += "--gid #{user[:gid]} " if user[:gid]
  useradd_cmd += "--groups #{user[:groups].join(',')} " if user[:groups]
  useradd_cmd += "--shell /sbin/nologin " if user[:nologin]
  useradd_cmd += "--system " if user[:system]
  DanarchyDeploy::Helpers.run_command(useradd_cmd, options)
end

.usercheck(user, options) ⇒ Object



85
86
87
# File 'lib/danarchy_deploy/users.rb', line 85

def self.usercheck(user, options)
  DanarchyDeploy::Helpers.run_command("id #{user[:username]}", options)
end

.userdel(user, options) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/danarchy_deploy/users.rb', line 76

def self.userdel(user, options)
  userdel_cmd  = "userdel --remove #{user[:username]}"
  if options[:pretend]
    puts "\tFake run: #{userdel_cmd}"
  else
    DanarchyDeploy::Helpers.run_command(userdel_cmd, options)
  end
end