Class: Cfruby::Users::OpenBSDUserManager

Inherits:
UserManager show all
Defined in:
lib/libcfruby/osmodules/openbsd.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



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/libcfruby/osmodules/openbsd.rb', line 114

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/libcfruby/osmodules/openbsd.rb', line 65

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



129
130
131
132
133
134
135
# File 'lib/libcfruby/osmodules/openbsd.rb', line 129

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



237
238
239
240
241
242
243
244
245
246
# File 'lib/libcfruby/osmodules/openbsd.rb', line 237

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



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/libcfruby/osmodules/openbsd.rb', line 219

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

Returns:

  • (Boolean)


157
158
159
# File 'lib/libcfruby/osmodules/openbsd.rb', line 157

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

#groupsObject

returns a list of all the groups on the system



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/libcfruby/osmodules/openbsd.rb', line 188

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



250
251
252
# File 'lib/libcfruby/osmodules/openbsd.rb', line 250

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

Returns:

  • (Boolean)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/libcfruby/osmodules/openbsd.rb', line 139

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



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/libcfruby/osmodules/openbsd.rb', line 163

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