Class: Cfruby::Users::OSXUserManager

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

Overview

Implementation of the UserManger for OS X

Instance Method Summary collapse

Methods inherited from UserManager

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

Instance Method Details

#add_group(groupname, gid = nil) ⇒ Object

adds a group to the system with an optional fixed uid



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/libcfruby/osmodules/osx.rb', line 136

def add_group(groupname, gid=nil)
	Cfruby.controller.attempt("Adding group \"#{groupname}\"", 'destructive') {
		`/usr/bin/niutil -create . /groups/#{groupname}`

		newgroupid = gid
		if(newgroupid == nil)
			lastgid = `/usr/bin/nidump group . | /usr/bin/cut -d: -f3 | /usr/bin/sort -n | /usr/bin/tail -n 1`
			newgroupid = lastgid.to_i() + 1
		end				

		`/usr/bin/niutil -createprop . /groups/#{groupname} gid #{newgroupid}`
		`/usr/bin/niutil -createprop . /groups/#{groupname} users`
	}
end

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

adds a user to the system. If userinfo is a string then it will add the named user with an optional uid. If userinfo is an actual UserInfo object then uid is ignored and everything possible will be used from userinfo (or generated if not supplied)



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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/libcfruby/osmodules/osx.rb', line 76

def add_user(userinfo, password=nil, uid=nil)
	newuser = UserInfo.new()
	newuser.username = nil
	newuser.uid = nil
	newuser.gid = nil
	newuser.fullname = nil
	newuser.shell = nil
	newuser.homedir = nil

	if(userinfo.respond_to?(:username))
		if(userinfo.username == nil)
			raise(BadUsernameError, "No username given")
		end
		newuser.username = userinfo.username
		newuser.uid = userinfo.uid
		newuser.gid = userinfo.gid
		newuser.fullname = userinfo.fullname
		newuser.shell = userinfo.shell
		newuser.homedir = userinfo.homedir
	else
		newuser.username = userinfo.to_s()
		newuser.uid = uid
	end

	Cfruby.controller.attempt("Adding user \"#{newuser.username}\"", 'destructive') {
		if(newuser.uid == nil)
			lastuid = `/usr/bin/nidump passwd . | /usr/bin/cut -d: -f3 | /usr/bin/sort -n | /usr/bin/tail -n 1`
			newuser.uid = lastuid.to_i() + 1
			if(newuser.uid == 0)
				raise(AddUserError, "Error generating new uid")
			end
		end

		if(newuser.gid == nil)
			newuser.gid = newuser.uid
		end

		`/usr/bin/niutil -create . /users/#{newuser.username}`
		`/usr/bin/niutil -createprop . /users/#{newuser.username} passwd`
		`/usr/bin/niutil -createprop . /users/#{newuser.username} gid #{newuser.gid.to_i()}`
		`/usr/bin/niutil -createprop . /users/#{newuser.username} uid #{newuser.uid.to_i()}`
		`/usr/bin/niutil -createprop . /users/#{newuser.username} shell #{newuser.shell.to_s()}`
		`/usr/bin/niutil -createprop . /users/#{newuser.username} home "#{newuser.homedir.to_s()}"`
		`/usr/bin/niutil -createprop . /users/#{newuser.username} realname "#{newuser.fullname.to_s()}"`
		`/usr/bin/niutil -createprop . /users/#{newuser.username} _shadow_passwd`

		# make the home directory
		if(newuser.homedir != nil and !File.exist?(newuser.homedir.to_s()))
			FileUtils.mkdir_p(newuser.homedir.to_s())
		end

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

#delete_group(group) ⇒ Object

deletes a group from the system



178
179
# File 'lib/libcfruby/osmodules/osx.rb', line 178

def delete_group(group)
end

#delete_user(username, removehome = false) ⇒ Object

deletes a user from the system



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/osx.rb', line 153

def delete_user(username, removehome=false)
	Cfruby.controller.attempt("Removing user \"#{username}\"", 'nonreversible', 'destructive') {
		# get the homedir before we destroy the user if we are going to delete it
		# don't actually destroy the directory yet because we may fail to actually
		# delete the user
		homedir = nil
		if(removehome)
			homedir = `/usr/bin/niutil -readprop . /users/#{username} home`.strip()
		end

		# FIXME - add check for error return code
		output = Cfruby::Exec.exec("/usr/bin/niutil -destroy . /users/#{username}")
		if(output[1].join().strip() != '')
			print(output[1].join("\n"))
			raise(DeleteUserError, "Unable to delete user \"#{username}\"")
		end

		if(homedir != nil)
			FileUtils.rmdir(homedir)
		end
	}
end

#group?(group) ⇒ Boolean

returns true if group exists, false otherwise

Returns:

  • (Boolean)


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

def group?(group)
	if(groups.has_key?(group))
		Cfruby.controller.inform('debug', "group \"#{group}\" exists")
		return(true)
	end

	Cfruby.controller.inform('debug', "group \"#{group}\" does not exist")
	return(false)
end

#groupsObject

returns a list of all the groups on the system



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/libcfruby/osmodules/osx.rb', line 237

def groups()
	groups = GroupList.new()

	# get the list of users using niutil
	textlist = `niutil -list . /groups`
	textlist.each() { |line|
		line.strip!()
		group = GroupInfo.new()
		group.groupname = line[/^[0-9]+\s+(\S+)$/, 1]
		grouplist = `niutil -read . /groups/#{group.groupname}`
		grouplist.each() { |subline|
			subline.strip!()
			case(subline)
				when(/^gid:/)
					group.gid = subline[/:\s*(.*)$/, 1].to_i()
			end
		}

		groups[group.groupname] = group
	}

	return(groups)
end

#user?(username) ⇒ Boolean

returns true if a user exists, false otherwise

Returns:

  • (Boolean)


183
184
185
186
187
188
189
# File 'lib/libcfruby/osmodules/osx.rb', line 183

def user?(username)
	if(users.has_key?(username))
		return(true)
	end

	return(false)
end

#usersObject

returns a list of all the users on the system



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/libcfruby/osmodules/osx.rb', line 205

def users()
	users = UserList.new()

	# get the list of users using niutil
	textlist = `niutil -list . /users`
	textlist.each() { |line|
		line.strip!()
		user = UserInfo.new()
		user.username = line[/^[0-9]+\s+(\S+)$/, 1]
		userlist = `niutil -read . /users/#{user.username}`
		userlist.each() { |subline|
			subline.strip!()
			case(subline)
				when(/^home:/)
					user.homedir = subline[/:\s*(.*)$/, 1]
				when(/^uid:/)
					user.uid = subline[/:\s*(.*)$/, 1].to_i()
				when(/^gid:/)
					user.gid = subline[/:\s*(.*)$/, 1].to_i()
				when(/^realname:/)
					user.fullname = subline[/:\s*(.*)$/, 1]
			end
		}

		users[user.username] = user
	}

	return(users)
end