Class: Specinfra::Command::Base::User

Inherits:
Specinfra::Command::Base show all
Defined in:
lib/specinfra/command/base/user.rb

Class Method Summary collapse

Methods inherited from Specinfra::Command::Base

create, escape

Class Method Details

.add(user, options) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/specinfra/command/base/user.rb', line 81

def add(user, options)
  command = ['useradd']
  command << '-g' << escape(options[:gid])            if options[:gid]
  command << '-d' << escape(options[:home_directory]) if options[:home_directory]
  command << '-p' << escape(options[:password])       if options[:password]
  command << '-s' << escape(options[:shell])          if options[:shell]
  command << '-m' if options[:create_home]
  command << '-r' if options[:system_user]
  command << '-u' << escape(options[:uid])            if options[:uid]
  command << escape(user)
  command.join(' ')
end

.check_belongs_to_group(user, group) ⇒ Object



7
8
9
# File 'lib/specinfra/command/base/user.rb', line 7

def check_belongs_to_group(user, group)
  "id #{escape(user)} | sed 's/ context=.*//g' | cut -f 4 -d '=' | grep -- #{escape(group)}"
end

.check_belongs_to_primary_group(user, group) ⇒ Object



11
12
13
# File 'lib/specinfra/command/base/user.rb', line 11

def check_belongs_to_primary_group(user, group)
  "id -gn #{escape(user)}| grep ^#{escape(group)}$"
end

.check_exists(user) ⇒ Object



3
4
5
# File 'lib/specinfra/command/base/user.rb', line 3

def check_exists(user)
  "id #{escape(user)}"
end

.check_has_authorized_key(user, key) ⇒ Object



36
37
38
39
# File 'lib/specinfra/command/base/user.rb', line 36

def check_has_authorized_key(user, key)
  key.sub!(/\s+\S*$/, '') if key.match(/^\S+\s+\S+\s+\S*$/)
  "grep -w -- #{escape(key)} ~#{escape(user)}/.ssh/authorized_keys"
end

.check_has_home_directory(user, path_to_home) ⇒ Object



28
29
30
# File 'lib/specinfra/command/base/user.rb', line 28

def check_has_home_directory(user, path_to_home)
  "getent passwd #{escape(user)} | cut -f 6 -d ':' | grep -w -- #{escape(path_to_home)}"
end

.check_has_login_shell(user, path_to_shell) ⇒ Object



32
33
34
# File 'lib/specinfra/command/base/user.rb', line 32

def (user, path_to_shell)
  "getent passwd #{escape(user)} | cut -f 7 -d ':' | grep -w -- #{escape(path_to_shell)}"
end

.check_has_uid(user, uid) ⇒ Object



23
24
25
26
# File 'lib/specinfra/command/base/user.rb', line 23

def check_has_uid(user, uid)
  regexp = "^uid=#{uid}("
  "id #{escape(user)} | grep -- #{escape(regexp)}"
end

.check_is_system_user(user) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/specinfra/command/base/user.rb', line 15

def check_is_system_user(user)
  exists = "getent passwd #{escape(user)} > /dev/null 2>&1"
  uid = "getent passwd #{escape(user)} | cut -f 3 -d ':'"
  sys_uid_min = "awk 'BEGIN{sys_uid_min=101} {if($1~/^SYS_UID_MIN/){sys_uid_min=$2}} END{print sys_uid_min}' /etc/login.defs"
  sys_uid_max = "awk 'BEGIN{sys_uid_max=0;uid_min=1000} {if($1~/^SYS_UID_MAX/){sys_uid_max=$2}if($1~/^UID_MIN/){uid_min=$2}} END{if(sys_uid_max!=0){print sys_uid_max}else{print uid_min-1}}' /etc/login.defs"
  %Q|#{exists} && test "$(#{uid})" -ge "$(#{sys_uid_min})" && test "$(#{uid})" -le "$(#{sys_uid_max})"|
end

.get_encrypted_password(user) ⇒ Object



98
99
100
# File 'lib/specinfra/command/base/user.rb', line 98

def get_encrypted_password(user)
  "getent shadow #{escape(user)} | cut -f 2 -d ':'"
end

.get_gid(user) ⇒ Object



53
54
55
# File 'lib/specinfra/command/base/user.rb', line 53

def get_gid(user)
  "id -g #{escape(user)}"
end

.get_home_directory(user) ⇒ Object



57
58
59
# File 'lib/specinfra/command/base/user.rb', line 57

def get_home_directory(user)
  "getent passwd #{escape(user)} | cut -f 6 -d ':'"
end

.get_login_shell(user) ⇒ Object



61
62
63
# File 'lib/specinfra/command/base/user.rb', line 61

def (user)
  "getent passwd #{escape(user)} | cut -f 7 -d ':'"
end

.get_maximum_days_between_password_change(user) ⇒ Object



45
46
47
# File 'lib/specinfra/command/base/user.rb', line 45

def get_maximum_days_between_password_change(user)
  "chage -l #{escape(user)} | sed -n 's/^Maximum.*: //p'"
end

.get_minimum_days_between_password_change(user) ⇒ Object



41
42
43
# File 'lib/specinfra/command/base/user.rb', line 41

def get_minimum_days_between_password_change(user)
  "chage -l #{escape(user)} | sed -n 's/^Minimum.*: //p'"
end

.get_uid(user) ⇒ Object



49
50
51
# File 'lib/specinfra/command/base/user.rb', line 49

def get_uid(user)
  "id -u #{escape(user)}"
end

.update_encrypted_password(user, encrypted_password) ⇒ Object



94
95
96
# File 'lib/specinfra/command/base/user.rb', line 94

def update_encrypted_password(user, encrypted_password)
  %Q!echo #{escape("#{user}:#{encrypted_password}")} | chpasswd -e!
end

.update_gid(user, gid) ⇒ Object



77
78
79
# File 'lib/specinfra/command/base/user.rb', line 77

def update_gid(user, gid)
  "usermod -g #{escape(gid)} #{escape(user)}"
end

.update_home_directory(user, directory) ⇒ Object



65
66
67
# File 'lib/specinfra/command/base/user.rb', line 65

def update_home_directory(user, directory)
  "usermod -d #{escape(directory)} #{escape(user)}"
end

.update_login_shell(user, shell) ⇒ Object



69
70
71
# File 'lib/specinfra/command/base/user.rb', line 69

def (user, shell)
  "usermod -s #{escape(shell)} #{escape(user)}"
end

.update_uid(user, uid) ⇒ Object



73
74
75
# File 'lib/specinfra/command/base/user.rb', line 73

def update_uid(user, uid)
  "usermod -u #{escape(uid)} #{escape(user)}"
end