Class: AutomateIt::AccountManager::Linux

Inherits:
Portable show all
Defined in:
lib/automateit/account_manager/linux.rb

Overview

AccountManager::Linux

A Linux-specific driver for the AccountManager.

Constant Summary

Constants inherited from Plugin::Driver

Plugin::Driver::BASE_DRIVER_NAME

Constants included from Constants

Constants::PERROR, Constants::PEXEC, Constants::PNOTE, Constants::WARNING_BOILERPLATE

Instance Attribute Summary

Attributes inherited from Plugin::Driver

#manager

Attributes inherited from Common

#interpreter

Instance Method Summary collapse

Methods inherited from Portable

#groups, #groups_for_user, #has_etc?, has_etc?, #has_group?, #has_user?, #users, #users_for_group, #users_to_groups

Methods inherited from Plugin::Driver

abstract_driver, #available?, base_driver, base_driver?, depends_on, inherited, manager_token

Methods inherited from Plugin::Base

#token, token

Methods inherited from Common

#initialize, #log, #nitpick, #noop, #noop=, #noop?, #preview, #preview=, #preview?, #preview_for, #superuser?, #writing, #writing=, #writing?

Constructor Details

This class inherits a constructor from AutomateIt::Common

Instance Method Details

#add_group(groupname, opts = {}) ⇒ Object

See AccountManager#add_group



104
105
106
107
108
109
110
111
112
113
# File 'lib/automateit/account_manager/linux.rb', line 104

def add_group(groupname, opts={})
  return false if has_group?(groupname)
  cmd = "groupadd"
  cmd << " -g #{opts[:gid]}" if opts[:gid]
  cmd << " #{groupname}"
  interpreter.sh(cmd)
  interpreter.sh("nscd --invalidate group") if nscd?
  add_users_to_group(opts[:members], groupname) if opts[:members]
  return groups[groupname]
end

#add_groups_to_user(groups, username) ⇒ Object

See AccountManager#add_groups_to_user



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/automateit/account_manager/linux.rb', line 76

def add_groups_to_user(groups, username)
  groups = [groups].flatten
  present = groups_for_user(username)
  missing = groups - present
  return false if missing.empty?

  cmd = "usermod -a -G #{missing.join(',')} #{username}"
  interpreter.sh(cmd)
  interpreter.sh("nscd --invalidate group") if nscd?
  return missing
end

#add_user(username, opts = {}) ⇒ Object

See AccountManager#add_user



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
# File 'lib/automateit/account_manager/linux.rb', line 28

def add_user(username, opts={})
  return false if has_user?(username)
  cmd = "useradd"
  cmd << " --comment #{opts[:description] || username}"
  cmd << " --home #{opts[:home]}" if opts[:home]
  cmd << " --create-home" unless opts[:create_home] == false
  cmd << " --groups #{opts[:groups].join(' ')}" if opts[:groups]
  cmd << " --shell #{opts[:shell] || "/bin/bash"}"
  cmd << " --uid #{opts[:uid]}" if opts[:uid]
  cmd << " --gid #{opts[:gid]}" if opts[:gid]
  cmd << " #{username} < /dev/null"
  cmd << " > /dev/null" if opts[:quiet]
  interpreter.sh(cmd)
  interpreter.sh("nscd --invalidate passwd") if nscd?

  unless opts[:group] == false
    groupname = opts[:group] || username
    unless has_group?(groupname)
      opts = {:members => [username]}
      # In preview mode, user doesn't exist and has no UID
      opts[:gid] = users[username].uid if writing?
      add_group(groupname, opts)
    end
  end

  interpreter..passwd(username, opts[:passwd]) if opts[:passwd]

  return users[username]
end

#add_users_to_group(users, groupname) ⇒ Object

See AccountManager#add_users_to_group



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/automateit/account_manager/linux.rb', line 128

def add_users_to_group(users, groupname)
  users = [users].flatten
  # XXX Include pwent.gid?
  grent = groups[groupname]
  missing = \
    if writing? and not grent
      raise ArgumentError.new("no such group: #{groupname}")
    elsif writing? or grent
      users - grent.mem
    else
      users
    end
  return false if missing.empty?

  for username in missing
    cmd = "usermod -a -G #{groupname} #{username}"
    interpreter.sh(cmd)
  end
  interpreter.sh("nscd --invalidate group") if nscd?
  return missing
end

#nscd?Boolean

Is “nscd” available on this platform?

Returns:

  • (Boolean)


21
22
23
# File 'lib/automateit/account_manager/linux.rb', line 21

def nscd?
  @nscd ||= interpreter.which("nscd")
end

#remove_group(groupname, opts = {}) ⇒ Object

See AccountManager#remove_group



119
120
121
122
123
124
125
# File 'lib/automateit/account_manager/linux.rb', line 119

def remove_group(groupname, opts={})
  return false unless has_group?(groupname)
  cmd = "groupdel #{groupname}"
  interpreter.sh(cmd)
  interpreter.sh("nscd --invalidate group") if nscd?
  return true
end

#remove_groups_from_user(groups, username) ⇒ Object

See AccountManager#remove_groups_from_user



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/automateit/account_manager/linux.rb', line 89

def remove_groups_from_user(groups, username)
  groups = [groups].flatten
  present = groups_for_user(username)
  removeable = groups & present
  return false if removeable.empty?

  cmd = "usermod -G #{(present-groups).join(',')} #{username}"
  interpreter.sh(cmd)
  interpreter.sh("nscd --invalidate group") if nscd?
  return removeable
end

#remove_user(username, opts = {}) ⇒ Object

See AccountManager#remove_user



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/automateit/account_manager/linux.rb', line 62

def remove_user(username, opts={})
  return false unless has_user?(username)
  # Options: -r -- remove the home directory and mail spool
  cmd = "userdel"
  cmd << " -r" unless opts[:remove_home] == false
  cmd << " #{username}"
  cmd << " > /dev/null" if opts[:quiet]
  interpreter.sh(cmd)
  interpreter.sh("nscd --invalidate passwd") if nscd?
  remove_group(username) if has_group?(username)
  return true
end

#remove_users_from_group(users, groupname) ⇒ Object

See AccountManager#remove_users_from_group



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/automateit/account_manager/linux.rb', line 151

def remove_users_from_group(users, groupname)
  users = [users].flatten
  grent = groups[groupname]
  present = \
    if writing? and not grent
      raise ArgumentError.new("no such group: #{groupname}")
    elsif writing? or grent
      grent.mem & users
    else
      users
    end
  return false if present.empty?

  u2g = users_to_groups
  for username in present
    user_groups = u2g[username]
    cmd = "usermod -G #{(user_groups.to_a-[groupname]).join(',')} #{username}"
    interpreter.sh(cmd)
  end
  interpreter.sh("nscd --invalidate group") if nscd?
  return present
end

#setup(*args) ⇒ Object

:nodoc:



16
17
18
# File 'lib/automateit/account_manager/linux.rb', line 16

def setup(*args) # :nodoc:
  super(*args)
end

#suitability(method, *args) ⇒ Object

:nodoc:



11
12
13
14
# File 'lib/automateit/account_manager/linux.rb', line 11

def suitability(method, *args) # :nodoc:
  # Level must be higher than Portable
  return available? ? 2 : 0
end