Class: Cfruby::Users::FreeBSDUserManager

Inherits:
UserManager show all
Defined in:
lib/libcfruby/osmodules/freebsd.rb

Overview

Implementation of the UserManager class for generic FreeBSD systems

Instance Method Summary collapse

Methods inherited from UserManager

#delete_user_from_group, #get_gid, #get_group, #get_name, #get_uid, #remove_user_from_group, #set_groups

Instance Method Details

#add_group(group, gid = nil) ⇒ Object

adds a group to the system with an optional fixed uid



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/libcfruby/osmodules/freebsd.rb', line 178

def add_group(group, gid=nil)
  Cfruby.controller.attempt("Adding group \"#{group}\"", 'destructive') {
    # Only add the group if it's not already there
    if !group?(group)
      if(gid == nil)
        `/usr/sbin/pw groupadd '#{shellescape(group)}'`
      else
        `/usr/sbin/pw groupadd '#{shellescape(group)}' -g #{gid.to_i()}`
      end
    end
  }
end

#add_user(user, password = nil, uid = nil) ⇒ Object

adds a user to the system with an optional fixed uid



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/libcfruby/osmodules/freebsd.rb', line 129

def add_user(user, password=nil, uid=nil)
  Cfruby.controller.attempt("Adding user \"#{user.to_s}\"", 'destructive') {
    newuser = nil
    if(!user.respond_to?(:username))
      newuser = UserInfo.new()
      newuser.username = user.to_s
      if(uid != nil)
        newuser.uid = uid.to_i()
      end
      # FIXME: Handling the addition of new users needs to be better than this
      # FIXME: Assuming that /home/<username> is the dir is silly, we should use -m somehow
      # FIXME: but still make it overridable.
      newuser.homedir = "/home/#{newuser.username}"
    else
      newuser = user
    end

       if(users[newuser.username])
         Cfruby.controller.attempt_abort("user \"#{user.to_s}\" already exists")
       end

    if(uid == nil)
      `/usr/sbin/pw useradd #{shellescape(newuser.username)}`
    else
      `/usr/sbin/pw useradd #{shellescape(newuser.username)} -u #{uid.to_i()}`
    end

    if(newuser.gid != nil)
      `/usr/sbin/pw usermod #{shellescape(newuser.username)} -g #{newuser.gid}`
    end
    if(newuser.fullname != nil)
      `/usr/sbin/pw usermod #{shellescape(newuser.username)} -n '#{shellescape(newuser.fullname)}'`
    end
    if(newuser.shell != nil)
      `/usr/sbin/pw usermod #{shellescape(newuser.username)} -s #{newuser.shell}`
    end
    if(newuser.homedir != nil)
      `/usr/sbin/pw usermod #{shellescape(newuser.username)} -d '#{shellescape(newuser.homedir)}' -m`
    end

    # set the password
    if(password != nil)
      set_password(newuser.username, password)
    end
  }
end

#add_user_to_group(username, groupname) ⇒ Object

Add a user to a group



193
194
195
196
197
198
199
# File 'lib/libcfruby/osmodules/freebsd.rb', line 193

def add_user_to_group(username, groupname)
  # Check for validity first
  super(username, groupname)


  `/usr/sbin/pw groupmod #{shellescape(groupname)} -m #{shellescape(username)}`
end

#delete_group(group) ⇒ Object

deletes a group from the system



301
302
303
304
305
306
307
308
309
310
# File 'lib/libcfruby/osmodules/freebsd.rb', line 301

def delete_group(group)
  groupname = nil
  if(group.respond_to(:groupname))
    groupname = group.groupname
  else
    groupname = group
  end

  `pw groupdel #{groupname}`
end

#delete_user(user, removehome = false) ⇒ Object

deletes a user from the system



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/libcfruby/osmodules/freebsd.rb', line 283

def delete_user(user, removehome=false)
  username = nil
  if(user.respond_to?(:username))
    username = user.username
  else
    username = user.to_s
  end
  Cfruby.controller.attempt("Removing user \"#{username}\"", 'nonreversible', 'destructive') {
    if(removehome == true)
      `pw userdel #{username} -r`
    else
      `pw userdel #{username}`
    end
  }
end

#group?(group) ⇒ Boolean

returns true if group exists, false otherwise



221
222
223
# File 'lib/libcfruby/osmodules/freebsd.rb', line 221

def group?(group)
  return(infile(group, '/etc/group'))
end

#groupsObject

returns a list of all the groups on the system



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/libcfruby/osmodules/freebsd.rb', line 252

def groups()
  userlist = users()

  grouplist = GroupList.new()
  File.open('/etc/group', File::RDONLY) { |fp|
    regex = /^([a-zA-Z0-9-]+):[^:]+:([0-9]+):([^:]*)/
    fp.each_line() { |line|
      match = regex.match(line)
      if(match != nil)
        group = GroupInfo.new()
        group.groupname = match[1]
        group.gid = match[2].to_i()
        group.members = UserList.new()
        if(match[3] != nil)
          users = match[3].split(/,/)
          users.each() { |username|
            if(userlist.has_key?(username))
              group.members[username] = userlist[username]
            end
          }
        end
        grouplist[group.groupname] = group
      end
    }
  }

  return(grouplist)
end

#set_password(user, password) ⇒ Object

Set the password using the pw script



314
315
316
# File 'lib/libcfruby/osmodules/freebsd.rb', line 314

def set_password(user, password)
  `echo "#{shellescape(password)}" | /usr/sbin/pw usermod #{shellescape(user)} -h 0`
end

#user?(user) ⇒ Boolean

returns true if a user exists, false otherwise



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/libcfruby/osmodules/freebsd.rb', line 203

def user?(user)
  username = ""
  if(user.respond_to?(:username))
    username = user.username
  else
    username = user
  end

  output = Exec::exec("/usr/sbin/pw showuser '#{shellescape(username)}'")
  if(output[0][0] =~ /^#{Regexp.escape(username)}:/)
    return(true)
  else
    return(false)
  end
end

#usersObject

returns a list of all the users on the system



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/libcfruby/osmodules/freebsd.rb', line 227

def users()
  userlist = UserList.new()

  File.open('/etc/passwd', File::RDONLY) { |fp|
    regex = /^([a-zA-Z0-9-]+):[^:]+:([0-9]+):([0-9]+):([^:]*):([^:]*):([^:]*)$/
    fp.each_line() { |line|
      match = regex.match(line)
      if(match != nil)
        user = UserInfo.new()
        user.username = match[1]
        user.uid = match[2].to_i()
        user.gid = match[3].to_i()
        user.fullname = match[4]
        user.homedir = match[5]
        user.shell = match[6]
        userlist[user.username] = user
      end
    }
  }

  return(userlist)
end