| 1 | ############################################################################### | ||
| 2 | # Unix::Mgt | ||
| 3 | # | ||
| 4 | package Unix::Mgt; | ||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use IPC::System::Simple 'runx'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Capture::Tiny 'capture_merged'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use String::Util qw{define nocontent}; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | use Unix::SearchPathGuess 'cmd_path_guess'; | ||
| 10 | use Carp 'croak'; | ||
| 11 | |||
| 12 | # debug tools | ||
| 13 | # use Debug::ShowStuff ':all'; | ||
| 14 | # use Debug::ShowStuff::ShowVar; | ||
| 15 | |||
| 16 | # version | ||
| 17 | our $VERSION = '0.13'; | ||
| 18 | |||
| 19 | |||
| 20 | #------------------------------------------------------------------------------ | ||
| 21 | # export | ||
| 22 | # | ||
| 23 | use base 'Exporter'; | ||
| 24 | use vars qw[@EXPORT_OK %EXPORT_TAGS]; | ||
| 25 | push @EXPORT_OK, 'unix_mgt_err'; | ||
| 26 | %EXPORT_TAGS = ('all' => [@EXPORT_OK]); | ||
| 27 | # | ||
| 28 | # export | ||
| 29 | #------------------------------------------------------------------------------ | ||
| 30 | |||
| 31 | |||
| 32 | #------------------------------------------------------------------------------ | ||
| 33 | # opening POD | ||
| 34 | # | ||
| 35 | |||
| 36 | =head1 NAME | ||
| 37 | |||
| 38 | Unix::Mgt - lightweight Unix management tools | ||
| 39 | |||
| 40 | =head1 SYNOPSIS | ||
| 41 | |||
| 42 | # get user account | ||
| 43 | $user = Unix::Mgt::User->get('fred'); | ||
| 44 | |||
| 45 | # display some info | ||
| 46 | print 'uid: ', $user->uid, "\n"; | ||
| 47 | print join(', ', $user->groups()), "\n"; | ||
| 48 | |||
| 49 | # set some properties | ||
| 50 | $user->gid('websters'); | ||
| 51 | $user->shell('/bin/bash'); | ||
| 52 | $user->add_to_group('postgres'); | ||
| 53 | |||
| 54 | # create user account | ||
| 55 | $user = Unix::Mgt::User->create('vera'); | ||
| 56 | |||
| 57 | # get user account, creating it if necessary | ||
| 58 | $user = Unix::Mgt::User->ensure('molly'); | ||
| 59 | |||
| 60 | # get group | ||
| 61 | $group = Unix::Mgt::Group->get('www-data'); | ||
| 62 | |||
| 63 | # display some info | ||
| 64 | print 'gid: ', $group->gid, "\n"; | ||
| 65 | print join(', ', $group->members()), "\n"; | ||
| 66 | |||
| 67 | # add a member | ||
| 68 | $group->add_member('tucker'); | ||
| 69 | |||
| 70 | =head1 DESCRIPTION | ||
| 71 | |||
| 72 | Unix::Mgt provides simple object-oriented tools for managing your Unixish | ||
| 73 | system. Currently this module provides tools for managing users and groups. | ||
| 74 | Other tools may follow as they evolve. | ||
| 75 | |||
| 76 | Unix::Mgt does not directly manipulate any of the system files such as | ||
| 77 | C</etc/passwd>. This module uses Perl's built-in Unix functions such as | ||
| 78 | C<getgrent> to get information, and Unix's built-in programs such as | ||
| 79 | C<adduser>. | ||
| 80 | |||
| 81 | =head2 Early release | ||
| 82 | |||
| 83 | In the spirit of "release early, release often", I'm releasing this version | ||
| 84 | of Unix::Mgt before it has all the features that might be expected. This | ||
| 85 | version does not include methods for removing users from groups, renaming | ||
| 86 | users or groups, or several other methods. | ||
| 87 | |||
| 88 | =head2 Help with BSD development | ||
| 89 | |||
| 90 | This version does not work well on BSDish systems. Although the methods for | ||
| 91 | retrieving information such as C<get> and C<group> work well, this module | ||
| 92 | currently cannot create, modify, or delete users or group. If you'd like to | ||
| 93 | help fill in these features, here's what needs to be done. | ||
| 94 | |||
| 95 | Several places in the code is the comment "BSD code needed". In those places | ||
| 96 | what we need is to build an array consisting of an external command and the | ||
| 97 | arguments to be sent to the command. It would look something like this: | ||
| 98 | |||
| 99 | # build command | ||
| 100 | @cmd = ( | ||
| 101 | 'pw', | ||
| 102 | 'useradd', | ||
| 103 | $user->{'name'}, | ||
| 104 | '-G', | ||
| 105 | $group->{'name'}, | ||
| 106 | ); | ||
| 107 | |||
| 108 | The first element of the array is the program to be run. You don't need to put | ||
| 109 | the full path of the program, L<Unix::SearchPathGuess> should find it. (If it | ||
| 110 | doesn't please let me know, we may need to fix Unix::SearchPathGuess.) The | ||
| 111 | remaining elements are the params sent to the program using | ||
| 112 | L<IPC::System::Simple>. The program is not run in a shell, but instead is | ||
| 113 | called directly, so it isn't necessary to do any shell escaping. | ||
| 114 | |||
| 115 | The following methods currently need patching. In Unix::Mgt::User we need | ||
| 116 | create(), field_get_set(), group(), add_to_group(), and remove(). In | ||
| 117 | Unix::Mgt::Group we need create() and remove(). We will need more as we add | ||
| 118 | features to this module. | ||
| 119 | |||
| 120 | Please email me at miko@idocs.com if you've made these mods. | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | # | ||
| 125 | # opening POD | ||
| 126 | #------------------------------------------------------------------------------ | ||
| 127 | |||
| 128 | |||
| 129 | #------------------------------------------------------------------------------ | ||
| 130 | # error id and message globals | ||
| 131 | # | ||
| 132 | our $err_id; | ||
| 133 | our $err_msg; | ||
| 134 | # | ||
| 135 | # error id and message globals | ||
| 136 | #------------------------------------------------------------------------------ | ||
| 137 | |||
| 138 | |||
| 139 | #------------------------------------------------------------------------------ | ||
| 140 | # set_err, reset_err | ||
| 141 | # | ||
| 142 | sub set_err { | ||
| 143 | my ($class, $id, $msg) = @_; | ||
| 144 | $err_id = $id; | ||
| 145 | $err_msg = $msg; | ||
| 146 | return undef; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub reset_err { | ||
| 150 | undef $err_id; | ||
| 151 | undef $err_msg; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub unix_mgt_err { | ||
| 155 | if ($err_id) | ||
| 156 | { return $err_id . ': ' . define($err_msg) . "\n" } | ||
| 157 | else | ||
| 158 | { return '' } | ||
| 159 | } | ||
| 160 | # | ||
| 161 | # reset_err | ||
| 162 | #------------------------------------------------------------------------------ | ||
| 163 | |||
| 164 | |||
| 165 | #------------------------------------------------------------------------------ | ||
| 166 | # called_sub | ||
| 167 | # | ||
| 168 | sub called_sub { | ||
| 169 | my (@caller, $sub_name); | ||
| 170 | |||
| 171 | # TESTING | ||
| 172 | # println subname(class=>1); ##i | ||
| 173 | |||
| 174 | # get caller info | ||
| 175 | @caller = caller(1); | ||
| 176 | |||
| 177 | # get subroutine name and make it look like a method call | ||
| 178 | $sub_name = $caller[3]; | ||
| 179 | $sub_name =~ s|^(.*)\:\:|$1\-\>|s; | ||
| 180 | $sub_name .= '()'; | ||
| 181 | |||
| 182 | # return | ||
| 183 | return $sub_name; | ||
| 184 | } | ||
| 185 | # | ||
| 186 | # called_sub | ||
| 187 | #------------------------------------------------------------------------------ | ||
| 188 | |||
| 189 | |||
| 190 | #------------------------------------------------------------------------------ | ||
| 191 | # even_odd_params | ||
| 192 | # | ||
| 193 | sub even_odd_params { | ||
| 194 | my $class = shift(@_); | ||
| 195 | my $id = shift(@_); | ||
| 196 | my ($name, %opts); | ||
| 197 | |||
| 198 | # get params: even number means all params (except class) are options, | ||
| 199 | # odd number means first param is id | ||
| 200 | if (@_ % 2) { | ||
| 201 | ($name, %opts) = @_; | ||
| 202 | } | ||
| 203 | else { | ||
| 204 | %opts = @_; | ||
| 205 | $name = delete($opts{$id}); | ||
| 206 | } | ||
| 207 | |||
| 208 | # return | ||
| 209 | return ($name, %opts); | ||
| 210 | } | ||
| 211 | # | ||
| 212 | # even_odd_params | ||
| 213 | #------------------------------------------------------------------------------ | ||
| 214 | |||
| 215 | |||
| 216 | #------------------------------------------------------------------------------ | ||
| 217 | # run_cmd | ||
| 218 | # | ||
| 219 | sub run_cmd { | ||
| 220 | my ($class, $err_id_use, $cmd_id, @args) = @_; | ||
| 221 | my ($cmd, $out, $rv); | ||
| 222 | |||
| 223 | # TESTING | ||
| 224 | # println subname(method=>1); ##i | ||
| 225 | |||
| 226 | # get command | ||
| 227 | $cmd = cmd_path_guess($cmd_id); | ||
| 228 | $cmd or croak "do not find path for command $cmd_id"; | ||
| 229 | |||
| 230 | # run command | ||
| 231 | $out = capture_merged{ | ||
| 232 | $rv = runx(IPC::System::Simple::EXIT_ANY, $cmd, @args); | ||
| 233 | }; | ||
| 234 | |||
| 235 | # if error | ||
| 236 | if ($rv) { | ||
| 237 | return $class->set_err( | ||
| 238 | $err_id_use, | ||
| 239 | "error running program $cmd: " . $out, | ||
| 240 | ); | ||
| 241 | } | ||
| 242 | |||
| 243 | # return success | ||
| 244 | return 1; | ||
| 245 | } | ||
| 246 | # | ||
| 247 | # run_cmd | ||
| 248 | #------------------------------------------------------------------------------ | ||
| 249 | |||
| 250 | |||
| 251 | #------------------------------------------------------------------------------ | ||
| 252 | # use_bsd | ||
| 253 | # | ||
| 254 | my ($use_bsd); | ||
| 255 | |||
| 256 | sub use_bsd { | ||
| 257 | my ($ug) = @_; | ||
| 258 | my $use_bsd = \$Unix::Mgt::use_bsd; | ||
| 259 | |||
| 260 | # TESTING | ||
| 261 | # println subname(method=>1); ##i | ||
| 262 | |||
| 263 | # if cached, use that | ||
| 264 | if (defined $$use_bsd) | ||
| 265 | { return $$use_bsd } | ||
| 266 | |||
| 267 | # else get the command, cache, and return | ||
| 268 | $$use_bsd = cmd_path_guess('pw'); | ||
| 269 | return $$use_bsd; | ||
| 270 | } | ||
| 271 | # | ||
| 272 | # use_bsd | ||
| 273 | #------------------------------------------------------------------------------ | ||
| 274 | |||
| 275 | |||
| 276 | # | ||
| 277 | # Unix::Mgt | ||
| 278 | ############################################################################### | ||
| 279 | |||
| 280 | |||
| 281 | |||
| 282 | ############################################################################### | ||
| 283 | # Unix::Mgt::UGCommon | ||
| 284 | # | ||
| 285 | package Unix::Mgt::UGCommon; | ||
| 286 | use strict; | ||
| 287 | use String::Util ':all'; | ||
| 288 | use Carp 'croak'; | ||
| 289 | use Unix::SearchPathGuess 'cmd_path_guess'; | ||
| 290 | use base 'Unix::Mgt'; | ||
| 291 | |||
| 292 | # debug tools | ||
| 293 | # use Debug::ShowStuff ':all'; | ||
| 294 | |||
| 295 | |||
| 296 | #------------------------------------------------------------------------------ | ||
| 297 | # object overloading | ||
| 298 | # | ||
| 299 | use overload | ||
| 300 | '""' => sub{$_[0]->{'name'}}, # stringification | ||
| 301 | fallback => 1; # operations not defined here | ||
| 302 | # | ||
| 303 | # object overloading | ||
| 304 | #------------------------------------------------------------------------------ | ||
| 305 | |||
| 306 | |||
| 307 | #------------------------------------------------------------------------------ | ||
| 308 | # normalize_name | ||
| 309 | # | ||
| 310 | sub normalize_name { | ||
| 311 | my ($class, $name) = @_; | ||
| 312 | |||
| 313 | # if defined, remove spaces at beginning and end | ||
| 314 | if (defined $name) { | ||
| 315 | $name =~ s|^\s+||sg; | ||
| 316 | $name =~ s|\s+$||sg; | ||
| 317 | } | ||
| 318 | |||
| 319 | # return | ||
| 320 | return $name; | ||
| 321 | } | ||
| 322 | # | ||
| 323 | # normalize_name | ||
| 324 | #------------------------------------------------------------------------------ | ||
| 325 | |||
| 326 | |||
| 327 | #------------------------------------------------------------------------------ | ||
| 328 | # name_check | ||
| 329 | # | ||
| 330 | sub name_check { | ||
| 331 | my ($class, $name, $id) = @_; | ||
| 332 | |||
| 333 | # TESTING | ||
| 334 | # println subname(method=>1); ##i | ||
| 335 | |||
| 336 | # if name does not have content, that's an error | ||
| 337 | if (nocontent $name) { | ||
| 338 | return $class->set_err( | ||
| 339 | $id, | ||
| 340 | $class->called_sub() . ' requires a user name parameter' | ||
| 341 | ); | ||
| 342 | } | ||
| 343 | |||
| 344 | # normalize | ||
| 345 | $name = $class->normalize_name($name); | ||
| 346 | |||
| 347 | # return | ||
| 348 | return $name; | ||
| 349 | } | ||
| 350 | # | ||
| 351 | # name_check | ||
| 352 | #------------------------------------------------------------------------------ | ||
| 353 | |||
| 354 | |||
| 355 | #------------------------------------------------------------------------------ | ||
| 356 | # mod_only | ||
| 357 | # | ||
| 358 | sub mod_only { | ||
| 359 | my ($class, $name) = @_; | ||
| 360 | my ($only); | ||
| 361 | |||
| 362 | # TESTING | ||
| 363 | # println subname(method=>1); ##i | ||
| 364 | |||
| 365 | # get class | ||
| 366 | if (ref $class) | ||
| 367 | { $class = ref($class) } | ||
| 368 | |||
| 369 | # get hash with destrictions | ||
| 370 | # KLUDGE: This is an awkward way to get the variable, but I didn't want | ||
| 371 | # to remember how to work through package hashes. | ||
| 372 | if ( $class eq 'Unix::Mgt::User' ) | ||
| 373 | { $only = $Unix::Mgt::User::MOD_ONLY } | ||
| 374 | elsif ($class eq 'Unix::Mgt::Group') | ||
| 375 | { $only = $Unix::Mgt::Group::MOD_ONLY } | ||
| 376 | else | ||
| 377 | { croak qq|do not know package "$class" for mod restrictions | } | ||
| 378 | |||
| 379 | # if $only is defined, name must be in the hash | ||
| 380 | if ($only) { | ||
| 381 | # deref | ||
| 382 | if (ref $name) | ||
| 383 | { $name = $name->{'name'} } | ||
| 384 | |||
| 385 | # if no content in name, fail | ||
| 386 | if (nocontent $name) | ||
| 387 | { croak 'no content in $name' } | ||
| 388 | |||
| 389 | if (! exists($only->{$name})) { | ||
| 390 | croak qq|cannot modify user "$name"|; | ||
| 391 | } | ||
| 392 | } | ||
| 393 | |||
| 394 | # else it's ok to mod that user | ||
| 395 | return 1; | ||
| 396 | } | ||
| 397 | # | ||
| 398 | # mod_only | ||
| 399 | #------------------------------------------------------------------------------ | ||
| 400 | |||
| 401 | |||
| 402 | #------------------------------------------------------------------------------ | ||
| 403 | # ensure | ||
| 404 | # | ||
| 405 | sub ensure { | ||
| 406 | my $class = shift(@_); | ||
| 407 | my ($name, %opts) = $class->even_odd_params('name', @_); | ||
| 408 | |||
| 409 | # TESTING | ||
| 410 | # println subname(method=>1); ##i | ||
| 411 | |||
| 412 | # reset error globals | ||
| 413 | $class->reset_err(); | ||
| 414 | |||
| 415 | # check and normalize name | ||
| 416 | $name = $class->name_check($name, 'missing-user-name'); | ||
| 417 | $name or return undef; | ||
| 418 | |||
| 419 | # if user exists, return get method | ||
| 420 | if (my @fields = $class->fields($name)) { | ||
| 421 | return $class->get($name, fields=>\@fields) | ||
| 422 | } | ||
| 423 | |||
| 424 | # else return create | ||
| 425 | else { | ||
| 426 | return $class->create($name); | ||
| 427 | } | ||
| 428 | } | ||
| 429 | # | ||
| 430 | # ensure | ||
| 431 | #------------------------------------------------------------------------------ | ||
| 432 | |||
| 433 | |||
| 434 | # | ||
| 435 | # Unix::Mgt::UGCommon | ||
| 436 | ############################################################################### | ||
| 437 | |||
| 438 | |||
| 439 | |||
| 440 | ############################################################################### | ||
| 441 | # Unix::Mgt::User | ||
| 442 | # | ||
| 443 | package Unix::Mgt::User; | ||
| 444 | use strict; | ||
| 445 | use Carp 'croak'; | ||
| 446 | use String::Util ':all'; | ||
| 447 | use base 'Unix::Mgt::UGCommon'; | ||
| 448 | |||
| 449 | # debug tools | ||
| 450 | # use Debug::ShowStuff ':all'; | ||
| 451 | |||
| 452 | # safety mechanism for development | ||
| 453 | our $MOD_ONLY; | ||
| 454 | |||
| 455 | |||
| 456 | #------------------------------------------------------------------------------ | ||
| 457 | # POD | ||
| 458 | # | ||
| 459 | |||
| 460 | =head1 Unix::Mgt::User | ||
| 461 | |||
| 462 | A Unix::Mgt::User object represents a user in the Unix system. The object | ||
| 463 | allows you to get and set information about the user account. A user object | ||
| 464 | is created in one of three ways: C<get>, C<create>, or C<ensure>. Note that | ||
| 465 | there is no C<new> method. | ||
| 466 | |||
| 467 | Unix::Mgt::User objects stringify to the account's name. For example, the | ||
| 468 | following code would output C<miko>. | ||
| 469 | |||
| 470 | $user = Unix::Mgt::User->get('miko'); | ||
| 471 | print $user, "\n"; | ||
| 472 | |||
| 473 | =cut | ||
| 474 | |||
| 475 | # | ||
| 476 | # POD | ||
| 477 | #------------------------------------------------------------------------------ | ||
| 478 | |||
| 479 | |||
| 480 | #------------------------------------------------------------------------------ | ||
| 481 | # field_names | ||
| 482 | # | ||
| 483 | our @field_names = qw{ | ||
| 484 | name | ||
| 485 | passwd | ||
| 486 | uid | ||
| 487 | gid | ||
| 488 | quota | ||
| 489 | comment | ||
| 490 | gecos | ||
| 491 | dir | ||
| 492 | shell | ||
| 493 | expire | ||
| 494 | }; | ||
| 495 | # | ||
| 496 | # field_names | ||
| 497 | #------------------------------------------------------------------------------ | ||
| 498 | |||
| 499 | |||
| 500 | |||
| 501 | #------------------------------------------------------------------------------ | ||
| 502 | # get | ||
| 503 | # | ||
| 504 | |||
| 505 | =head2 get | ||
| 506 | |||
| 507 | Unix::Mgt::User->get() retrieves user account information using C<getpwnam> or | ||
| 508 | C<getpwuid>. The single param for this method is either the name or the uid of | ||
| 509 | the user. | ||
| 510 | |||
| 511 | $user = Unix::Mgt::User->get('vera'); | ||
| 512 | $user = Unix::Mgt::User->get('1010'); | ||
| 513 | |||
| 514 | If the user is not found then the C<do-not-have-user> error id is set in | ||
| 515 | C<$Unix::Mgt::err_id> and undef is returned. | ||
| 516 | |||
| 517 | =cut | ||
| 518 | |||
| 519 | sub get { | ||
| 520 | my $class = shift(@_); | ||
| 521 | my ($name, %opts) = $class->even_odd_params('name', @_); | ||
| 522 | my (@fields, $user); | ||
| 523 | |||
| 524 | # TESTING | ||
| 525 | # println subname(method=>1); ##i | ||
| 526 | |||
| 527 | # reset error globals | ||
| 528 | $class->reset_err(); | ||
| 529 | |||
| 530 | # check and normalize name | ||
| 531 | $name = $class->name_check($name, 'missing-user-name'); | ||
| 532 | $name or return undef; | ||
| 533 | |||
| 534 | # get fields | ||
| 535 | @fields = $class->fields($name); | ||
| 536 | |||
| 537 | # if user exists, get name, else throw error | ||
| 538 | if (@fields) { | ||
| 539 | $name = $fields[0]; | ||
| 540 | } | ||
| 541 | else { | ||
| 542 | return $class->set_err( | ||
| 543 | 'do-not-have-user', | ||
| 544 | $class->called_sub() . qq|: do not find a user with name "$name"|, | ||
| 545 | ); | ||
| 546 | } | ||
| 547 | |||
| 548 | # create object | ||
| 549 | $user = bless({}, $class); | ||
| 550 | |||
| 551 | # hold on to name | ||
| 552 | $user->{'name'} = $name; | ||
| 553 | |||
| 554 | # return | ||
| 555 | return $user; | ||
| 556 | } | ||
| 557 | # | ||
| 558 | # get | ||
| 559 | #------------------------------------------------------------------------------ | ||
| 560 | |||
| 561 | |||
| 562 | #------------------------------------------------------------------------------ | ||
| 563 | # entry | ||
| 564 | # | ||
| 565 | sub entry { | ||
| 566 | my ($user) = @_; | ||
| 567 | my (@fields, %entry); | ||
| 568 | |||
| 569 | # TESTING | ||
| 570 | # println subname(method=>1); ##i | ||
| 571 | |||
| 572 | # get fields | ||
| 573 | @fields = $user->fields($user->{'name'}); | ||
| 574 | |||
| 575 | # if no fields, set error and return undef | ||
| 576 | if (! @fields) { | ||
| 577 | return $user->set_err( | ||
| 578 | 'do-not-have-user-entry-anymore', | ||
| 579 | $user->called_sub() . ': do not have a user with name "' . $user->{'name'} . '"', | ||
| 580 | ); | ||
| 581 | } | ||
| 582 | |||
| 583 | # set hash | ||
| 584 | @entry{@field_names} = @fields; | ||
| 585 | |||
| 586 | # return | ||
| 587 | return \%entry; | ||
| 588 | } | ||
| 589 | # | ||
| 590 | # entry | ||
| 591 | #------------------------------------------------------------------------------ | ||
| 592 | |||
| 593 | |||
| 594 | #------------------------------------------------------------------------------ | ||
| 595 | # fields | ||
| 596 | # | ||
| 597 | sub fields { | ||
| 598 | my ($class, $name) = @_; | ||
| 599 | |||
| 600 | # TESTING | ||
| 601 | # println subname(method=>1); ##i | ||
| 602 | |||
| 603 | # return | ||
| 604 | if ($name =~ m|^\d+$|s) | ||
| 605 | { return getpwuid($name) } | ||
| 606 | else | ||
| 607 | { return getpwnam($name) } | ||
| 608 | } | ||
| 609 | # | ||
| 610 | # fields | ||
| 611 | #------------------------------------------------------------------------------ | ||
| 612 | |||
| 613 | |||
| 614 | #------------------------------------------------------------------------------ | ||
| 615 | # create | ||
| 616 | # | ||
| 617 | |||
| 618 | =head2 create | ||
| 619 | |||
| 620 | Unix::Mgt::User->create() creates a user account. The required param for this | ||
| 621 | method is the name for the new account. | ||
| 622 | |||
| 623 | $user = Unix::Mgt::User->create('vera'); | ||
| 624 | |||
| 625 | If the C<system> param is true, then the account is created as a system user, | ||
| 626 | like this: | ||
| 627 | |||
| 628 | $user = Unix::Mgt::User->create('lanny', system=>1); | ||
| 629 | |||
| 630 | create() uses the Unix C<adduser> program. | ||
| 631 | |||
| 632 | =cut | ||
| 633 | |||
| 634 | sub create { | ||
| 635 | my $class = shift(@_); | ||
| 636 | my ($name, %opts) = $class->even_odd_params('name', @_); | ||
| 637 | my ($user, @cmd); | ||
| 638 | |||
| 639 | # TESTING | ||
| 640 | # println subname(method=>1); ##i | ||
| 641 | |||
| 642 | # reset error globals | ||
| 643 | $class->reset_err(); | ||
| 644 | |||
| 645 | # check and normalize name | ||
| 646 | $name = $class->name_check($name, 'missing-user-name'); | ||
| 647 | $name or return undef; | ||
| 648 | |||
| 649 | # if user exists, throw error | ||
| 650 | if ($class->fields($name)) { | ||
| 651 | return $class->set_err( | ||
| 652 | 'already-have-user', | ||
| 653 | $class->called_sub() . qq|: already have a user with name "$name"|, | ||
| 654 | ); | ||
| 655 | } | ||
| 656 | |||
| 657 | # safety check | ||
| 658 | $class->mod_only($name); | ||
| 659 | |||
| 660 | # BSD style | ||
| 661 | if ($class->use_bsd) { | ||
| 662 | # BSD code needed | ||
| 663 | |||
| 664 | croak | ||
| 665 | 'We have not yet implemented for BSD. ' . | ||
| 666 | 'Please see "Help with BSD development" in the Unix::Mgt ' . | ||
| 667 | 'documents to help with this issue.'; | ||
| 668 | } | ||
| 669 | |||
| 670 | # else Linux style | ||
| 671 | else { | ||
| 672 | # build command | ||
| 673 | @cmd = ( | ||
| 674 | 'adduser', | ||
| 675 | '--disabled-password', | ||
| 676 | '--gecos', '', | ||
| 677 | ); | ||
| 678 | |||
| 679 | # if creating as system user | ||
| 680 | if ($opts{'system'}) | ||
| 681 | { push @cmd, '--system' } | ||
| 682 | |||
| 683 | # add name | ||
| 684 | push @cmd, $name; | ||
| 685 | } | ||
| 686 | |||
| 687 | # run command | ||
| 688 | $class->run_cmd('error-creating-user', @cmd) or return undef; | ||
| 689 | |||
| 690 | # create object | ||
| 691 | $user = bless({}, $class); | ||
| 692 | |||
| 693 | # hold on to name | ||
| 694 | $user->{'name'} = $name; | ||
| 695 | |||
| 696 | # return | ||
| 697 | return $user; | ||
| 698 | } | ||
| 699 | # | ||
| 700 | # create | ||
| 701 | #------------------------------------------------------------------------------ | ||
| 702 | |||
| 703 | |||
| 704 | #------------------------------------------------------------------------------ | ||
| 705 | # POD for ensure() | ||
| 706 | # | ||
| 707 | |||
| 708 | =head2 ensure | ||
| 709 | |||
| 710 | Unix::Mgt::User->ensure() gets a user account if it already exists, and | ||
| 711 | creates the account if it does not. For example, the following lines ensures | ||
| 712 | the C<molly> account: | ||
| 713 | |||
| 714 | $user = Unix::Mgt::User->ensure('molly'); | ||
| 715 | |||
| 716 | =cut | ||
| 717 | |||
| 718 | # | ||
| 719 | # POD for ensure() | ||
| 720 | #------------------------------------------------------------------------------ | ||
| 721 | |||
| 722 | |||
| 723 | #------------------------------------------------------------------------------ | ||
| 724 | # field gets | ||
| 725 | # | ||
| 726 | |||
| 727 | =head2 name | ||
| 728 | |||
| 729 | Returns the name of the user account. Currently this method cannot be used to | ||
| 730 | set the account name. | ||
| 731 | |||
| 732 | print $user->name(), "\n"; | ||
| 733 | |||
| 734 | =head2 uid | ||
| 735 | |||
| 736 | Returns the user's user id (uid). | ||
| 737 | |||
| 738 | print $user->uid(), "\n"; | ||
| 739 | |||
| 740 | =head2 passwd | ||
| 741 | |||
| 742 | Returns the password field from C<getpwname()>. This method will not actually | ||
| 743 | return a password, it will probably just return C<*>. | ||
| 744 | |||
| 745 | print $user->passwd(), "\n"; # probably outputs "*" | ||
| 746 | |||
| 747 | =cut | ||
| 748 | |||
| 749 | sub field_get { | ||
| 750 | my ($user, $key) = @_; | ||
| 751 | my ($entry); | ||
| 752 | |||
| 753 | # TESTING | ||
| 754 | # println subname(method=>1); ##i | ||
| 755 | |||
| 756 | # reset error | ||
| 757 | $user->reset_err(); | ||
| 758 | |||
| 759 | # get entry | ||
| 760 | $entry = $user->entry(); | ||
| 761 | $entry or return undef; | ||
| 762 | |||
| 763 | # return | ||
| 764 | return $entry->{$key}; | ||
| 765 | } | ||
| 766 | |||
| 767 | sub name { return shift->field_get('name') } | ||
| 768 | sub uid { return shift->field_get('uid') } | ||
| 769 | sub passwd { return shift->field_get('passwd') } | ||
| 770 | |||
| 771 | # | ||
| 772 | # field gets | ||
| 773 | #------------------------------------------------------------------------------ | ||
| 774 | |||
| 775 | |||
| 776 | #------------------------------------------------------------------------------ | ||
| 777 | # field get|sets | ||
| 778 | # | ||
| 779 | |||
| 780 | =head2 gid | ||
| 781 | |||
| 782 | Sets/gets the gid of the user's primary group. Called without params, it | ||
| 783 | returns the user's gid: | ||
| 784 | |||
| 785 | print $user->gid(), "\n"; | ||
| 786 | |||
| 787 | Called with a single param, gid() sets, then returns the user's primary | ||
| 788 | group id: | ||
| 789 | |||
| 790 | print $user->gid('1010'), "\n"; | ||
| 791 | |||
| 792 | If you want to get a Unix::Mgt::Group object representing the user's primary | ||
| 793 | group, use $user->group(). | ||
| 794 | |||
| 795 | =head2 dir | ||
| 796 | |||
| 797 | Sets/gets the user's home directory. Called without params, it returns the | ||
| 798 | directory name: | ||
| 799 | |||
| 800 | print $user->dir(), "\n"; | ||
| 801 | |||
| 802 | Called with a single param, dir() sets, then returns the user's home directory: | ||
| 803 | |||
| 804 | print $user->dir('/tmp'), "\n"; | ||
| 805 | |||
| 806 | =head2 shell | ||
| 807 | |||
| 808 | Sets/gets the user's default command line shell. Called without params, it | ||
| 809 | returns the shell name: | ||
| 810 | |||
| 811 | print $user->shell(), "\n"; | ||
| 812 | |||
| 813 | Called with a single param, shell() sets, then returns the user's shell: | ||
| 814 | |||
| 815 | print $user->shell('/bin/sh'), "\n"; | ||
| 816 | |||
| 817 | =cut | ||
| 818 | |||
| 819 | sub field_get_set { | ||
| 820 | my $user = shift(@_); | ||
| 821 | my $field = shift(@_); | ||
| 822 | my $option = shift(@_); | ||
| 823 | |||
| 824 | # TESTING | ||
| 825 | # println subname(method=>1); ##i | ||
| 826 | |||
| 827 | # if a value was sent, set the field to that value | ||
| 828 | if (@_) { | ||
| 829 | my ($value) = @_; | ||
| 830 | my (@cmd); | ||
| 831 | |||
| 832 | # safety check | ||
| 833 | $user->mod_only($user->{'name'}); | ||
| 834 | |||
| 835 | # BSD style | ||
| 836 | if ($user->use_bsd) { | ||
| 837 | # BSD code needed | ||
| 838 | |||
| 839 | croak | ||
| 840 | 'We have not yet implemented for BSD. ' . | ||
| 841 | 'Please see "Help with BSD development" in the Unix::Mgt ' . | ||
| 842 | 'documents to help with this issue.'; | ||
| 843 | } | ||
| 844 | |||
| 845 | # else Linux style | ||
| 846 | else { | ||
| 847 | # build command | ||
| 848 | @cmd = ( | ||
| 849 | 'usermod', | ||
| 850 | "--$option", | ||
| 851 | $value, | ||
| 852 | $user->{'name'}, | ||
| 853 | ); | ||
| 854 | } | ||
| 855 | |||
| 856 | # run command | ||
| 857 | $user->run_cmd("usermod-error-$field", @cmd) or return undef; | ||
| 858 | } | ||
| 859 | |||
| 860 | # return field | ||
| 861 | return $user->field_get($field); | ||
| 862 | } | ||
| 863 | |||
| 864 | |||
| 865 | sub gid { return shift->field_get_set('gid', 'gid', @_) } | ||
| 866 | sub dir { return shift->field_get_set('dir', 'home', @_) } | ||
| 867 | sub shell { return shift->field_get_set('shell', 'shell', @_) } | ||
| 868 | |||
| 869 | # sub quota { return shift->field_get_set('quota') } | ||
| 870 | # sub comment { return shift->field_get_set('comment', 'comment', @_) } | ||
| 871 | # sub expire { return shift->field_get_set('expire', 'expiredate', @_) } | ||
| 872 | # sub gecos { return shift->field_get_set('gecos') } | ||
| 873 | |||
| 874 | # | ||
| 875 | # field get|sets | ||
| 876 | #------------------------------------------------------------------------------ | ||
| 877 | |||
| 878 | |||
| 879 | #------------------------------------------------------------------------------ | ||
| 880 | # group | ||
| 881 | # | ||
| 882 | |||
| 883 | =head2 group | ||
| 884 | |||
| 885 | Sets/gets the user's primary group. When called without any params, C<group()> | ||
| 886 | returns a Unix::Mgt::Group object representing the user's primary group: | ||
| 887 | |||
| 888 | $group = $user->group(); | ||
| 889 | |||
| 890 | When called with a single param, C<group()> sets the user's primary group. The | ||
| 891 | param can be either the group's name or its gid: | ||
| 892 | |||
| 893 | $user->group('video'); | ||
| 894 | $user->group(44); | ||
| 895 | |||
| 896 | =cut | ||
| 897 | |||
| 898 | sub group { | ||
| 899 | my $user = shift(@_); | ||
| 900 | my ($new_group, %opts) = $user->even_odd_params('new', @_); | ||
| 901 | my ($entry, $gid, $group); | ||
| 902 | |||
| 903 | # TESTING | ||
| 904 | # println subname(method=>1); ##i | ||
| 905 | |||
| 906 | # default options | ||
| 907 | %opts = (object=>1, %opts); | ||
| 908 | |||
| 909 | # set new group | ||
| 910 | if (defined $new_group) { | ||
| 911 | my (@args, $success); | ||
| 912 | |||
| 913 | # reset error globals | ||
| 914 | $user->reset_err(); | ||
| 915 | |||
| 916 | # BSD style | ||
| 917 | if ($user->use_bsd) { | ||
| 918 | # BSD code needed | ||
| 919 | |||
| 920 | croak | ||
| 921 | 'We have not yet implemented for BSD. ' . | ||
| 922 | 'Please see "Help with BSD development" in the Unix::Mgt ' . | ||
| 923 | 'documents to help with this issue.'; | ||
| 924 | } | ||
| 925 | |||
| 926 | # else Linux style | ||
| 927 | else { | ||
| 928 | # build usermod arguments | ||
| 929 | @args = ( | ||
| 930 | 'usermod', | ||
| 931 | '-g', | ||
| 932 | "$new_group", | ||
| 933 | "$user" | ||
| 934 | ); | ||
| 935 | } | ||
| 936 | |||
| 937 | # change user's group | ||
| 938 | $success = $user->run_cmd('error-setting-user-group', @args); | ||
| 939 | $success or return 0; | ||
| 940 | } | ||
| 941 | |||
| 942 | # get gid | ||
| 943 | $gid = $user->gid(); | ||
| 944 | defined($gid) or return undef; | ||
| 945 | |||
| 946 | # get group | ||
| 947 | $group = Unix::Mgt::Group->get($gid); | ||
| 948 | |||
| 949 | # return | ||
| 950 | if ($opts{'object'}) | ||
| 951 | { return $group } | ||
| 952 | else | ||
| 953 | { return $group->name } | ||
| 954 | } | ||
| 955 | # | ||
| 956 | # group | ||
| 957 | #------------------------------------------------------------------------------ | ||
| 958 | |||
| 959 | |||
| 960 | #------------------------------------------------------------------------------ | ||
| 961 | # secondary_groups | ||
| 962 | # | ||
| 963 | |||
| 964 | =head2 secondary_groups | ||
| 965 | |||
| 966 | C<secondary_groups()> returns an array of the user's secondary groups. Each | ||
| 967 | element in the array is a Unix::Mgt::Group object. | ||
| 968 | |||
| 969 | @groups = $user->secondary_groups(); | ||
| 970 | |||
| 971 | =cut | ||
| 972 | |||
| 973 | sub secondary_groups { | ||
| 974 | my ($user, %opts) = @_; | ||
| 975 | my (%groups, @rv); | ||
| 976 | |||
| 977 | # TESTING | ||
| 978 | # println subname(method=>1); ##i | ||
| 979 | |||
| 980 | # default options | ||
| 981 | %opts = (object=>1, %opts); | ||
| 982 | |||
| 983 | # loop through all groups | ||
| 984 | while (my @fields = getgrent()) { | ||
| 985 | my (%group); | ||
| 986 | @group{@Unix::Mgt::Group::field_names} = @fields; | ||
| 987 | |||
| 988 | # if there are any members, of the group, see if this user is in it | ||
| 989 | if (my $member_str = $group{'members'}) { | ||
| 990 | my (%members); | ||
| 991 | |||
| 992 | # parse out members | ||
| 993 | $member_str = crunch($member_str); | ||
| 994 | @members{split m|\s+|, $member_str} = (); | ||
| 995 | |||
| 996 | # if this user is in the membership | ||
| 997 | if (exists $members{$user->{'name'}}) | ||
| 998 | { $groups{$group{'name'}} = 1 } | ||
| 999 | } | ||
| 1000 | } | ||
| 1001 | |||
| 1002 | # build return value | ||
| 1003 | foreach my $key (keys %groups) { | ||
| 1004 | my $group = Unix::Mgt::Group->get($key); | ||
| 1005 | |||
| 1006 | # set as just string if options indicate to do so | ||
| 1007 | if (! $opts{'object'}) | ||
| 1008 | { $group = $group->{'name'} } | ||
| 1009 | |||
| 1010 | # add to return array | ||
| 1011 | push @rv, $group; | ||
| 1012 | } | ||
| 1013 | |||
| 1014 | # return | ||
| 1015 | return @rv; | ||
| 1016 | } | ||
| 1017 | # | ||
| 1018 | # secondary_groups | ||
| 1019 | #------------------------------------------------------------------------------ | ||
| 1020 | |||
| 1021 | |||
| 1022 | #------------------------------------------------------------------------------ | ||
| 1023 | # groups | ||
| 1024 | # | ||
| 1025 | |||
| 1026 | =head2 groups | ||
| 1027 | |||
| 1028 | C<groups()> returns an array of all of the groups the user is a member of. The | ||
| 1029 | first element in the array will be the user's primary group. | ||
| 1030 | |||
| 1031 | @groups = $user->groups(); | ||
| 1032 | |||
| 1033 | =cut | ||
| 1034 | |||
| 1035 | sub groups { | ||
| 1036 | my ($user, %opts) = @_; | ||
| 1037 | my (@rv); | ||
| 1038 | |||
| 1039 | # TESTING | ||
| 1040 | # println subname(method=>1); ##i | ||
| 1041 | |||
| 1042 | # get user's primary group | ||
| 1043 | push @rv, $user->group(%opts); | ||
| 1044 | |||
| 1045 | # add user's secondary groups | ||
| 1046 | push @rv, $user->secondary_groups(%opts); | ||
| 1047 | |||
| 1048 | # return | ||
| 1049 | return @rv; | ||
| 1050 | } | ||
| 1051 | # | ||
| 1052 | # groups | ||
| 1053 | #------------------------------------------------------------------------------ | ||
| 1054 | |||
| 1055 | |||
| 1056 | #------------------------------------------------------------------------------ | ||
| 1057 | # add_to_group | ||
| 1058 | # | ||
| 1059 | |||
| 1060 | =head2 add_to_group | ||
| 1061 | |||
| 1062 | C<add_to_group()> adds the user to a group. The group will be one of the user's | ||
| 1063 | secondary groups, not the primary group. | ||
| 1064 | |||
| 1065 | $user->add_to_group('video'); | ||
| 1066 | |||
| 1067 | =cut | ||
| 1068 | |||
| 1069 | sub add_to_group { | ||
| 1070 | my ($user, $group) = @_; | ||
| 1071 | my (@args, $success); | ||
| 1072 | |||
| 1073 | # TESTING | ||
| 1074 | # println subname(method=>1); ##i | ||
| 1075 | |||
| 1076 | # BSD style | ||
| 1077 | if ($user->use_bsd) { | ||
| 1078 | # BSD code needed | ||
| 1079 | |||
| 1080 | croak | ||
| 1081 | 'We have not yet implemented for BSD. ' . | ||
| 1082 | 'Please see "Help with BSD development" in the Unix::Mgt ' . | ||
| 1083 | 'documents to help with this issue.'; | ||
| 1084 | } | ||
| 1085 | |||
| 1086 | # else Linux style | ||
| 1087 | else { | ||
| 1088 | # build command arguments | ||
| 1089 | @args = ( | ||
| 1090 | 'usermod', | ||
| 1091 | '--append', | ||
| 1092 | '--groups', | ||
| 1093 | "$group", | ||
| 1094 | "$user" | ||
| 1095 | ); | ||
| 1096 | } | ||
| 1097 | |||
| 1098 | # run command | ||
| 1099 | $success = $user->run_cmd('error-adding-user-to-group', @args); | ||
| 1100 | |||
| 1101 | # return success|failure | ||
| 1102 | return $success; | ||
| 1103 | } | ||
| 1104 | # | ||
| 1105 | # add_to_group | ||
| 1106 | #------------------------------------------------------------------------------ | ||
| 1107 | |||
| 1108 | |||
| 1109 | #------------------------------------------------------------------------------ | ||
| 1110 | # remove | ||
| 1111 | # | ||
| 1112 | |||
| 1113 | =head2 remove | ||
| 1114 | |||
| 1115 | C<remove> removes the user account from the system. C<remove> does not take any | ||
| 1116 | parameters. | ||
| 1117 | |||
| 1118 | $user->remove(); | ||
| 1119 | |||
| 1120 | =cut | ||
| 1121 | |||
| 1122 | sub remove { | ||
| 1123 | my ($user) = @_; | ||
| 1124 | |||
| 1125 | # TESTING | ||
| 1126 | # println subname(method=>1); ##i | ||
| 1127 | |||
| 1128 | # safety check | ||
| 1129 | $user->mod_only($user); | ||
| 1130 | |||
| 1131 | # reset error | ||
| 1132 | $user->reset_err(); | ||
| 1133 | |||
| 1134 | # BSD style | ||
| 1135 | if ($user->use_bsd) { | ||
| 1136 | # BSD code needed | ||
| 1137 | |||
| 1138 | croak | ||
| 1139 | 'We have not yet implemented for BSD. ' . | ||
| 1140 | 'Please see "Help with BSD development" in the Unix::Mgt ' . | ||
| 1141 | 'documents to help with this issue.'; | ||
| 1142 | } | ||
| 1143 | |||
| 1144 | # else use userdel | ||
| 1145 | else { | ||
| 1146 | $user->run_cmd('error-deleting-user', 'userdel', $user->name) or return undef; | ||
| 1147 | } | ||
| 1148 | |||
| 1149 | # return | ||
| 1150 | return 1; | ||
| 1151 | } | ||
| 1152 | # | ||
| 1153 | # remove | ||
| 1154 | #------------------------------------------------------------------------------ | ||
| 1155 | |||
| 1156 | |||
| 1157 | |||
| 1158 | # | ||
| 1159 | # Unix::Mgt::User | ||
| 1160 | ############################################################################### | ||
| 1161 | |||
| 1162 | |||
| 1163 | |||
| 1164 | ############################################################################### | ||
| 1165 | # Unix::Mgt::Group | ||
| 1166 | # | ||
| 1167 | package Unix::Mgt::Group; | ||
| 1168 | use strict; | ||
| 1169 | use String::Util ':all'; | ||
| 1170 | use Carp 'croak'; | ||
| 1171 | use base 'Unix::Mgt::UGCommon'; | ||
| 1172 | |||
| 1173 | |||
| 1174 | # debug tools | ||
| 1175 | # use Debug::ShowStuff ':all'; | ||
| 1176 | |||
| 1177 | # safety mechanism for development | ||
| 1178 | our $MOD_ONLY; | ||
| 1179 | |||
| 1180 | #------------------------------------------------------------------------------ | ||
| 1181 | # POD | ||
| 1182 | # | ||
| 1183 | |||
| 1184 | =head1 Unix::Mgt::Group | ||
| 1185 | |||
| 1186 | A Unix::Mgt::Group object represents a group in the Unix system. The object | ||
| 1187 | allows you to get and set information about the group. A group object is | ||
| 1188 | created in one of three ways: C<get>, C<create>, or C<ensure>. Note that | ||
| 1189 | there is no C<new> method. | ||
| 1190 | |||
| 1191 | Unix::Mgt::Group objects stringify to the groups's name. For example, the | ||
| 1192 | following code would output C<video>. | ||
| 1193 | |||
| 1194 | $group = Unix::Mgt::Group->get('video'); | ||
| 1195 | print $group, "\n"; | ||
| 1196 | |||
| 1197 | =cut | ||
| 1198 | |||
| 1199 | # | ||
| 1200 | # POD | ||
| 1201 | #------------------------------------------------------------------------------ | ||
| 1202 | |||
| 1203 | |||
| 1204 | #------------------------------------------------------------------------------ | ||
| 1205 | # field_names | ||
| 1206 | # | ||
| 1207 | our @field_names = qw{ | ||
| 1208 | name | ||
| 1209 | passwd | ||
| 1210 | gid | ||
| 1211 | members | ||
| 1212 | }; | ||
| 1213 | # | ||
| 1214 | # field_names | ||
| 1215 | #------------------------------------------------------------------------------ | ||
| 1216 | |||
| 1217 | |||
| 1218 | #------------------------------------------------------------------------------ | ||
| 1219 | # fields | ||
| 1220 | # | ||
| 1221 | sub fields { | ||
| 1222 | my ($class, $name) = @_; | ||
| 1223 | |||
| 1224 | # TESTING | ||
| 1225 | # println subname(method=>1); ##i | ||
| 1226 | |||
| 1227 | # return | ||
| 1228 | if ($name =~ m|^\d+$|s) | ||
| 1229 | { return getgrgid($name) } | ||
| 1230 | else | ||
| 1231 | { return getgrnam($name) } | ||
| 1232 | } | ||
| 1233 | # | ||
| 1234 | # fields | ||
| 1235 | #------------------------------------------------------------------------------ | ||
| 1236 | |||
| 1237 | |||
| 1238 | #------------------------------------------------------------------------------ | ||
| 1239 | # get | ||
| 1240 | # | ||
| 1241 | |||
| 1242 | =head2 get | ||
| 1243 | |||
| 1244 | Unix::Mgt::Group->get() retrieves group information using C<getgrnam> or | ||
| 1245 | C<getgrgid>. The single param for this method is either the name or the gid of | ||
| 1246 | the group. | ||
| 1247 | |||
| 1248 | $group = Unix::Mgt::Group->get('video'); | ||
| 1249 | $group = Unix::Mgt::Group->get('44'); | ||
| 1250 | |||
| 1251 | If the group is not found then the C<do-not-have-group> error id is set in | ||
| 1252 | C<$Unix::Mgt::err_id> and undef is returned. | ||
| 1253 | |||
| 1254 | =cut | ||
| 1255 | |||
| 1256 | sub get { | ||
| 1257 | my $class = shift(@_); | ||
| 1258 | my ($name, %opts) = $class->even_odd_params('name', @_); | ||
| 1259 | my (@fields, $group); | ||
| 1260 | |||
| 1261 | # TESTING | ||
| 1262 | # println subname(method=>1); ##i | ||
| 1263 | |||
| 1264 | # reset error globals | ||
| 1265 | $class->reset_err(); | ||
| 1266 | |||
| 1267 | # check and normalize name | ||
| 1268 | $name = $class->name_check($name, 'missing-group-name'); | ||
| 1269 | $name or return undef; | ||
| 1270 | |||
| 1271 | # get fields | ||
| 1272 | @fields = $class->fields($name); | ||
| 1273 | |||
| 1274 | # if group exists, set name, else throw error | ||
| 1275 | if (@fields) { | ||
| 1276 | $name = $fields[0]; | ||
| 1277 | } | ||
| 1278 | else { | ||
| 1279 | return $class->set_err( | ||
| 1280 | 'do-not-have-group', | ||
| 1281 | $class->called_sub() . qq|: do not find a group with name "$name"|, | ||
| 1282 | ); | ||
| 1283 | } | ||
| 1284 | |||
| 1285 | # create object | ||
| 1286 | $group = bless({}, $class); | ||
| 1287 | |||
| 1288 | # hold on to name | ||
| 1289 | $group->{'name'} = $name; | ||
| 1290 | |||
| 1291 | # return | ||
| 1292 | return $group; | ||
| 1293 | } | ||
| 1294 | # | ||
| 1295 | # get | ||
| 1296 | #------------------------------------------------------------------------------ | ||
| 1297 | |||
| 1298 | |||
| 1299 | #------------------------------------------------------------------------------ | ||
| 1300 | # create | ||
| 1301 | # | ||
| 1302 | |||
| 1303 | =head2 create | ||
| 1304 | |||
| 1305 | Unix::Mgt::Group->create() creates a group. The required param for this method | ||
| 1306 | is the name for the new group. | ||
| 1307 | |||
| 1308 | $group = Unix::Mgt::Group->create('websters'); | ||
| 1309 | |||
| 1310 | create() uses the Unix C<groupadd> program. | ||
| 1311 | |||
| 1312 | =cut | ||
| 1313 | |||
| 1314 | sub create { | ||
| 1315 | my $class = shift(@_); | ||
| 1316 | my ($name, %opts) = $class->even_odd_params('name', @_); | ||
| 1317 | my ($group, @cmd); | ||
| 1318 | |||
| 1319 | # TESTING | ||
| 1320 | # println subname(method=>1); ##i | ||
| 1321 | |||
| 1322 | # reset error globals | ||
| 1323 | $class->reset_err(); | ||
| 1324 | |||
| 1325 | # check and normalize name | ||
| 1326 | $name = $class->name_check($name, 'missing-group-name'); | ||
| 1327 | $name or return undef; | ||
| 1328 | |||
| 1329 | # if group exists, throw error | ||
| 1330 | if ($class->fields($name)) { | ||
| 1331 | return $class->set_err( | ||
| 1332 | 'already-have-group', | ||
| 1333 | $class->called_sub() . qq|: already have a group with name "$name"|, | ||
| 1334 | ); | ||
| 1335 | } | ||
| 1336 | |||
| 1337 | # safety check | ||
| 1338 | $class->mod_only($name); | ||
| 1339 | |||
| 1340 | # BSD style | ||
| 1341 | if ($class->use_bsd) { | ||
| 1342 | # BSD code needed | ||
| 1343 | |||
| 1344 | croak | ||
| 1345 | 'We have not yet implemented for BSD. ' . | ||
| 1346 | 'Please see "Help with BSD development" in the Unix::Mgt ' . | ||
| 1347 | 'documents to help with this issue.'; | ||
| 1348 | } | ||
| 1349 | |||
| 1350 | # else Linux style | ||
| 1351 | else { | ||
| 1352 | # command | ||
| 1353 | push @cmd, 'groupadd'; | ||
| 1354 | |||
| 1355 | # if creating as system group | ||
| 1356 | if ($opts{'system'}) | ||
| 1357 | { push @cmd, '--system' } | ||
| 1358 | |||
| 1359 | # add name | ||
| 1360 | push @cmd, $name; | ||
| 1361 | } | ||
| 1362 | |||
| 1363 | # run command | ||
| 1364 | $class->run_cmd('error-creating-group', @cmd) or return undef; | ||
| 1365 | |||
| 1366 | # create object | ||
| 1367 | $group = bless({}, $class); | ||
| 1368 | |||
| 1369 | # hold on to name | ||
| 1370 | $group->{'name'} = $name; | ||
| 1371 | |||
| 1372 | # return | ||
| 1373 | return $group; | ||
| 1374 | } | ||
| 1375 | # | ||
| 1376 | # create | ||
| 1377 | #------------------------------------------------------------------------------ | ||
| 1378 | |||
| 1379 | |||
| 1380 | #------------------------------------------------------------------------------ | ||
| 1381 | # POD for ensure() | ||
| 1382 | # | ||
| 1383 | |||
| 1384 | =head2 ensure | ||
| 1385 | |||
| 1386 | Unix::Mgt::Group->ensure() gets a group if it already exists, and creates the | ||
| 1387 | group if it does not. For example, the following lines ensures | ||
| 1388 | the C<wbesters> group: | ||
| 1389 | |||
| 1390 | $group = Unix::Mgt::User->ensure('wbesters'); | ||
| 1391 | |||
| 1392 | =cut | ||
| 1393 | |||
| 1394 | # | ||
| 1395 | # POD for ensure() | ||
| 1396 | #------------------------------------------------------------------------------ | ||
| 1397 | |||
| 1398 | |||
| 1399 | #------------------------------------------------------------------------------ | ||
| 1400 | # field gets | ||
| 1401 | # | ||
| 1402 | |||
| 1403 | =head2 name | ||
| 1404 | |||
| 1405 | Returns the name of the group. Currently this method cannot be used to set the | ||
| 1406 | group name. | ||
| 1407 | |||
| 1408 | print $group->name(), "\n"; | ||
| 1409 | |||
| 1410 | =head2 gid | ||
| 1411 | |||
| 1412 | Returns the groups's group id (gid). | ||
| 1413 | |||
| 1414 | print $group->gid(), "\n"; | ||
| 1415 | |||
| 1416 | =cut | ||
| 1417 | |||
| 1418 | sub field_get { | ||
| 1419 | my ($group, $key) = @_; | ||
| 1420 | my ($entry); | ||
| 1421 | |||
| 1422 | # TESTING | ||
| 1423 | # println subname(method=>1); ##i | ||
| 1424 | |||
| 1425 | # reset error | ||
| 1426 | $group->reset_err(); | ||
| 1427 | |||
| 1428 | # get entry | ||
| 1429 | $entry = $group->entry(); | ||
| 1430 | $entry or return undef; | ||
| 1431 | |||
| 1432 | # return | ||
| 1433 | return $entry->{$key}; | ||
| 1434 | } | ||
| 1435 | |||
| 1436 | sub name { return shift->field_get('name') } | ||
| 1437 | sub gid { return shift->field_get('gid') } | ||
| 1438 | |||
| 1439 | # | ||
| 1440 | # field gets | ||
| 1441 | #------------------------------------------------------------------------------ | ||
| 1442 | |||
| 1443 | |||
| 1444 | #------------------------------------------------------------------------------ | ||
| 1445 | # entry | ||
| 1446 | # | ||
| 1447 | sub entry { | ||
| 1448 | my ($group) = @_; | ||
| 1449 | my (@fields, %entry); | ||
| 1450 | |||
| 1451 | # TESTING | ||
| 1452 | # println subname(method=>1); ##i | ||
| 1453 | |||
| 1454 | # get fields | ||
| 1455 | @fields = $group->fields($group->{'name'}); | ||
| 1456 | |||
| 1457 | # if no fields, set error and return undef | ||
| 1458 | if (! @fields) { | ||
| 1459 | return $group->set_err( | ||
| 1460 | 'do-not-have-group-entry-anymore', | ||
| 1461 | $group->called_sub() . ': do not have a group with name "' . $group->{'name'} . '"', | ||
| 1462 | ); | ||
| 1463 | } | ||
| 1464 | |||
| 1465 | # set hash | ||
| 1466 | @entry{@field_names} = @fields; | ||
| 1467 | |||
| 1468 | # return | ||
| 1469 | return \%entry; | ||
| 1470 | } | ||
| 1471 | # | ||
| 1472 | # entry | ||
| 1473 | #------------------------------------------------------------------------------ | ||
| 1474 | |||
| 1475 | |||
| 1476 | #------------------------------------------------------------------------------ | ||
| 1477 | # members | ||
| 1478 | # | ||
| 1479 | |||
| 1480 | =head2 members | ||
| 1481 | |||
| 1482 | C<members()> returns an array of all members of the group. Both users for whom | ||
| 1483 | this is the primary group, and users for whom this is a secondary group are | ||
| 1484 | returned. | ||
| 1485 | |||
| 1486 | @members = $group->members(); | ||
| 1487 | |||
| 1488 | The elements in the array are Unix::Mgt::User objects. | ||
| 1489 | |||
| 1490 | =cut | ||
| 1491 | |||
| 1492 | sub members { | ||
| 1493 | my ($group, %opts) = @_; | ||
| 1494 | my (%members, @rv); | ||
| 1495 | |||
| 1496 | # add users for whom this is their primary group | ||
| 1497 | foreach my $user ($group->primary_members(%opts)) { | ||
| 1498 | $members{"$user"} = $user; | ||
| 1499 | } | ||
| 1500 | |||
| 1501 | # add users for whom this is a secondary group | ||
| 1502 | foreach my $user ($group->secondary_members(%opts)) { | ||
| 1503 | $members{"$user"} = $user; | ||
| 1504 | } | ||
| 1505 | |||
| 1506 | # build return value | ||
| 1507 | @rv = values(%members); | ||
| 1508 | |||
| 1509 | # return | ||
| 1510 | return @rv; | ||
| 1511 | } | ||
| 1512 | # | ||
| 1513 | # members | ||
| 1514 | #------------------------------------------------------------------------------ | ||
| 1515 | |||
| 1516 | |||
| 1517 | #------------------------------------------------------------------------------ | ||
| 1518 | # primary_members | ||
| 1519 | # | ||
| 1520 | |||
| 1521 | =head2 primary_members | ||
| 1522 | |||
| 1523 | C<primary_members()> returns an array of users for whom this is the primary | ||
| 1524 | group. | ||
| 1525 | |||
| 1526 | @members = $group->primary_members(); | ||
| 1527 | |||
| 1528 | The elements in the returned array are Unix::Mgt::User objects. | ||
| 1529 | |||
| 1530 | =cut | ||
| 1531 | |||
| 1532 | sub primary_members { | ||
| 1533 | my ($group, %opts) = @_; | ||
| 1534 | my ($gid, %members, @rv); | ||
| 1535 | |||
| 1536 | # TESTING | ||
| 1537 | # println subname(method=>1); ##i | ||
| 1538 | |||
| 1539 | # default options | ||
| 1540 | %opts = (object=>1, %opts); | ||
| 1541 | |||
| 1542 | # get gid | ||
| 1543 | $gid = $group->gid(); | ||
| 1544 | |||
| 1545 | # get users for whom this i | ||
| 1546 | while (my @fields = getpwent()) { | ||
| 1547 | my (%user); | ||
| 1548 | @user{@Unix::Mgt::User::field_names} = @fields; | ||
| 1549 | |||
| 1550 | # if the user is in the group, add to %members | ||
| 1551 | if ( defined($user{'gid'}) && ($user{'gid'} eq $gid) ) { | ||
| 1552 | $members{$user{'name'}} = 1; | ||
| 1553 | } | ||
| 1554 | } | ||
| 1555 | |||
| 1556 | # build return array of objects | ||
| 1557 | if ($opts{'object'}) { | ||
| 1558 | foreach my $name (keys %members) { | ||
| 1559 | push @rv, Unix::Mgt::User->get($name); | ||
| 1560 | } | ||
| 1561 | } | ||
| 1562 | |||
| 1563 | # else build return array of names | ||
| 1564 | else { | ||
| 1565 | @rv = keys(%members); | ||
| 1566 | } | ||
| 1567 | |||
| 1568 | # return | ||
| 1569 | return @rv; | ||
| 1570 | } | ||
| 1571 | # | ||
| 1572 | # primary_members | ||
| 1573 | #------------------------------------------------------------------------------ | ||
| 1574 | |||
| 1575 | |||
| 1576 | #------------------------------------------------------------------------------ | ||
| 1577 | # secondary_members | ||
| 1578 | # | ||
| 1579 | |||
| 1580 | =head2 secondary_members | ||
| 1581 | |||
| 1582 | C<secondary_members()> returns an array of users for whom this is a secondary group. | ||
| 1583 | |||
| 1584 | @members = $group->secondary_members(); | ||
| 1585 | |||
| 1586 | The elements in the returned array are Unix::Mgt::User objects. | ||
| 1587 | |||
| 1588 | =cut | ||
| 1589 | |||
| 1590 | sub secondary_members { | ||
| 1591 | my ($group, %opts) = @_; | ||
| 1592 | my ($gid, $members_str, %members, @rv); | ||
| 1593 | |||
| 1594 | # TESTING | ||
| 1595 | # println subname(method=>1); ##i | ||
| 1596 | |||
| 1597 | # default options | ||
| 1598 | %opts = (object=>1, %opts); | ||
| 1599 | |||
| 1600 | # get users for whom this is a secondary group | ||
| 1601 | $members_str = $group->entry->{'members'}; | ||
| 1602 | defined($members_str) or return (); | ||
| 1603 | |||
| 1604 | # loop through members | ||
| 1605 | NAME_LOOP: | ||
| 1606 | foreach my $name (split m|\s+|s, $members_str) { | ||
| 1607 | if (hascontent $name) { | ||
| 1608 | my $user = Unix::Mgt::User->get($name); | ||
| 1609 | $members{$user->{'name'}} = 1; | ||
| 1610 | } | ||
| 1611 | } | ||
| 1612 | |||
| 1613 | # build return array of objects | ||
| 1614 | if ($opts{'object'}) { | ||
| 1615 | foreach my $name (keys %members) { | ||
| 1616 | push @rv, Unix::Mgt::User->get($name); | ||
| 1617 | } | ||
| 1618 | } | ||
| 1619 | |||
| 1620 | # else build return array of names | ||
| 1621 | else { | ||
| 1622 | @rv = keys(%members); | ||
| 1623 | } | ||
| 1624 | |||
| 1625 | # return | ||
| 1626 | return @rv; | ||
| 1627 | } | ||
| 1628 | # | ||
| 1629 | # secondary_members | ||
| 1630 | #------------------------------------------------------------------------------ | ||
| 1631 | |||
| 1632 | |||
| 1633 | #------------------------------------------------------------------------------ | ||
| 1634 | # add_member | ||
| 1635 | # | ||
| 1636 | |||
| 1637 | =head2 add_member | ||
| 1638 | |||
| 1639 | C<add_member()> adds a user to the group as a secondary group. The single | ||
| 1640 | param can be a user name, uid, or Unix::Mgt::User object. | ||
| 1641 | |||
| 1642 | $group->add_member('miko'); | ||
| 1643 | |||
| 1644 | If the user is already a member of the group then nothing is done and no error | ||
| 1645 | is set. | ||
| 1646 | |||
| 1647 | =cut | ||
| 1648 | |||
| 1649 | sub add_member { | ||
| 1650 | my ($group, $user) = @_; | ||
| 1651 | |||
| 1652 | # TESTING | ||
| 1653 | # println subname(method=>1); ##i | ||
| 1654 | |||
| 1655 | # get user object | ||
| 1656 | if (! ref $user) | ||
| 1657 | { $user = Unix::Mgt::User->get($user) } | ||
| 1658 | |||
| 1659 | # add user to group | ||
| 1660 | return $user->add_to_group($group); | ||
| 1661 | } | ||
| 1662 | # | ||
| 1663 | # add_member | ||
| 1664 | #------------------------------------------------------------------------------ | ||
| 1665 | |||
| 1666 | |||
| 1667 | #------------------------------------------------------------------------------ | ||
| 1668 | # remove | ||
| 1669 | # | ||
| 1670 | |||
| 1671 | =head2 remove | ||
| 1672 | |||
| 1673 | C<remove> removes the group from the system. C<remove> does not take any | ||
| 1674 | parameters. | ||
| 1675 | |||
| 1676 | $group->remove(); | ||
| 1677 | |||
| 1678 | If any users have the group as a primary group then this method will fail. | ||
| 1679 | |||
| 1680 | =cut | ||
| 1681 | |||
| 1682 | sub remove { | ||
| 1683 | my ($group) = @_; | ||
| 1684 | |||
| 1685 | # TESTING | ||
| 1686 | # println subname(method=>1); ##i | ||
| 1687 | |||
| 1688 | # safety check | ||
| 1689 | $group->mod_only($group); | ||
| 1690 | |||
| 1691 | # reset error | ||
| 1692 | $group->reset_err(); | ||
| 1693 | |||
| 1694 | # cannot remove if any users have this group as primary group | ||
| 1695 | if (my @primaries = $group->primary_members) { | ||
| 1696 | my ($id, $msg); | ||
| 1697 | |||
| 1698 | # ste id | ||
| 1699 | $id = 'cannot-remove-primary-group'; | ||
| 1700 | |||
| 1701 | # begin message | ||
| 1702 | $msg = | ||
| 1703 | 'cannot remove the group "' . $group->name() . | ||
| 1704 | '" because it is the primary group for '; | ||
| 1705 | |||
| 1706 | # plural | ||
| 1707 | if (@primaries > 1) { | ||
| 1708 | $msg .= 'the following users: ' . join(', ', @primaries); | ||
| 1709 | } | ||
| 1710 | |||
| 1711 | # singular | ||
| 1712 | else { | ||
| 1713 | $msg .= 'the user "' . $primaries[0] . '"'; | ||
| 1714 | } | ||
| 1715 | |||
| 1716 | # return failure | ||
| 1717 | return $group->set_err($id, $msg); | ||
| 1718 | } | ||
| 1719 | |||
| 1720 | # if using pw command | ||
| 1721 | if ($group->use_bsd) { | ||
| 1722 | # BSD code needed | ||
| 1723 | |||
| 1724 | croak | ||
| 1725 | 'We have not yet implemented for BSD. ' . | ||
| 1726 | 'Please see "Help with BSD development" in the Unix::Mgt ' . | ||
| 1727 | 'documents to help with this issue.'; | ||
| 1728 | } | ||
| 1729 | |||
| 1730 | # else use groupdel | ||
| 1731 | else { | ||
| 1732 | $group->run_cmd('error-deleting-group', 'groupdel', $group->name) or return undef; | ||
| 1733 | } | ||
| 1734 | |||
| 1735 | # return | ||
| 1736 | return 1; | ||
| 1737 | } | ||
| 1738 | # | ||
| 1739 | # remove | ||
| 1740 | #------------------------------------------------------------------------------ | ||
| 1741 | |||
| 1742 | |||
| 1743 | # | ||
| 1744 | # Unix::Mgt::Group | ||
| 1745 | ############################################################################### | ||
| 1746 | |||
| 1747 | |||
| 1748 | |||
| 1749 | # return true | ||
| 1750 | 1; | ||
| 1751 | |||
| 1752 | __END__ | ||