| 1 | =head1 NAME | ||
| 2 | |||
| 3 | OSS::LDAPops - Perform operations on user accounts, groups and netgroups stored in an LDAP directory | ||
| 4 | |||
| 5 | =head1 SYSNOPSIS | ||
| 6 | |||
| 7 | #Define config hash | ||
| 8 | $GLOBAL::config = | ||
| 9 | { | ||
| 10 | LDAPHOST => 'ldap01.mydomain.net', | ||
| 11 | BINDDN => 'uid=webportal, ou=writeaccess, dc=auth, dc=mydomain,dc=net', | ||
| 12 | BASEDN => 'dc=auth,dc=mydomain,dc=net', | ||
| 13 | NISDOMAIN => 'auth.mydomain.net', | ||
| 14 | PASSWORD => 'xyzzy', | ||
| 15 | }; | ||
| 16 | #Instantiate new object and connect to server | ||
| 17 | my($ldapopsobj) = OSS::LDAPops->new($GLOBAL::config); | ||
| 18 | if (ref($ldapopsobj) !~ m/OSS::LDAPops/ ) {die("Error instantiating object: $ldapopsobj")}; | ||
| 19 | my($ret); | ||
| 20 | my(@retu); | ||
| 21 | |||
| 22 | #Bind server | ||
| 23 | $ldapopsobj->bind; | ||
| 24 | @retu = $ldapopsobj->searchuser($ARGV[1]); | ||
| 25 | die($retu[0]) if (($retu[0] ne undef) and (ref($retu[0]) !~ m/Net::LDAP::Entry/) ); | ||
| 26 | foreach my $entry (@retu) {$entry->dump; } | ||
| 27 | #if($ret) {die($ret);}; | ||
| 28 | exit; | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | This module manipulates user, group and netgroup objects within an LDAP directory. | ||
| 33 | |||
| 34 | Also included is ldapops.pl. This script implements a command-line utility using | ||
| 35 | OSS::LDAPops. | ||
| 36 | |||
| 37 | netgroupcache.pl is also included. This uses OSS::LDAPops to create a local cache of | ||
| 38 | LDAP-backed netgroups in /etc/netgroup. | ||
| 39 | |||
| 40 | =head1 AUTHOR | ||
| 41 | |||
| 42 | Simon <simon@hacknix.net> | ||
| 43 | |||
| 44 | =head1 ASSUMPTIONS ABOUT THE DIRECTORY | ||
| 45 | |||
| 46 | This module and associated sripts make some assumptions about how your directory is | ||
| 47 | configured. these include: | ||
| 48 | |||
| 49 | =over | ||
| 50 | |||
| 51 | =item * | ||
| 52 | |||
| 53 | Storage of maxuid | ||
| 54 | |||
| 55 | =item * | ||
| 56 | |||
| 57 | Conventions for use of netgroups | ||
| 58 | |||
| 59 | =item * | ||
| 60 | |||
| 61 | nis.schema is patched to allow equalityMatch on nisNetgroupTriple objects | ||
| 62 | |||
| 63 | =back | ||
| 64 | |||
| 65 | =head1 FILES TO HELP WITH SETTING UP A DIRECTORY | ||
| 66 | |||
| 67 | In the "examples" directory, there are several files to help you out, including: | ||
| 68 | |||
| 69 | =over | ||
| 70 | |||
| 71 | =item * | ||
| 72 | |||
| 73 | An example skeleton directory. | ||
| 74 | |||
| 75 | =item * | ||
| 76 | |||
| 77 | A patched version of nis.schema, suitable for use with OpenLDAP | ||
| 78 | |||
| 79 | =item * | ||
| 80 | |||
| 81 | An example of OpenLDAP's slapd.conf, showing example acls | ||
| 82 | |||
| 83 | =item * | ||
| 84 | |||
| 85 | changepassword.pl - an example simple CGI script using OSS::LDAPops | ||
| 86 | |||
| 87 | =item * | ||
| 88 | |||
| 89 | notifypasswordexpiry.pl - a script to send an email to users when their password expires. | ||
| 90 | |||
| 91 | =item * | ||
| 92 | |||
| 93 | ppolicy.ldif - an example password policy to use with the ppolicy overlay. | ||
| 94 | |||
| 95 | =item * | ||
| 96 | |||
| 97 | Several other example files, to asisst in setting up *NIX servers to use the directory are also included. | ||
| 98 | |||
| 99 | =back | ||
| 100 | |||
| 101 | =head1 USING THE DIRECTORY WITH *NIX SERVERS | ||
| 102 | |||
| 103 | The reader should consider looking at nss_ldap and pam_ldap, here: | ||
| 104 | |||
| 105 | http://www.padl.com/Contents/OpenSourceSoftware.html | ||
| 106 | |||
| 107 | =head1 METHODS | ||
| 108 | |||
| 109 | This section describes the methods that are implemented and their use. | ||
| 110 | |||
| 111 | =head2 new | ||
| 112 | |||
| 113 | #Define config hash | ||
| 114 | $GLOBAL::config = | ||
| 115 | { | ||
| 116 | LDAPHOST => 'ldap01.mydomain.net', | ||
| 117 | BINDDN => 'uid=webportal, ou=writeaccess, dc=auth, dc=lastminute,dc=com', | ||
| 118 | BASEDN => 'dc=auth,dc=mydomain,dc=net', | ||
| 119 | NISDOMAIN => 'auth.mydomain.net', | ||
| 120 | PASSWORD => 'xyzzy', | ||
| 121 | }; | ||
| 122 | #Instantiate new object and connect to server | ||
| 123 | my($ldapopsobj) = OSS::LDAPops->new($GLOBAL::config); | ||
| 124 | if (ref($ldapopsobj) !~ m/OSS::LDAPops/ ) {die("Error instantiating object: $ldapopsobj")}; | ||
| 125 | |||
| 126 | Instantiates an object and connects to the LDAP server. Returns an object on success and false on error. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | #Define version | ||
| 131 | $OSS::LDAPops::VERSION = '1.033'; | ||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | #Please also note, proper error checking MUST be used to ensure | ||
| 136 | #the integrity of the directory. | ||
| 137 | # | ||
| 138 | #Most of the methods in this package return 0 on sucess | ||
| 139 | #and an error string on fail. | ||
| 140 | # | ||
| 141 | #You can therefore test for truth of the return. The string | ||
| 142 | #describes the error. | ||
| 143 | # | ||
| 144 | #Exceptions to this are described in the comments for the methods | ||
| 145 | #themselves. | ||
| 146 | # | ||
| 147 | #The only exceptions to this are the new() method, which returns an object | ||
| 148 | #or false on error and the salt() method which only ever returns a salt. | ||
| 149 | # | ||
| 150 | #********N.B.*********** | ||
| 151 | # | ||
| 152 | #A change to the file /etc/openldap/nis.schema is required to make | ||
| 153 | #deleting of single entries from nisNetgroupTriple attributes within nisNetgroups | ||
| 154 | #work. | ||
| 155 | # | ||
| 156 | #This is to allow equality matching on these attributes. | ||
| 157 | |||
| 158 | use strict; | ||
| 159 | |||
| 160 | package OSS::LDAPops; | ||
| 161 | |||
| 162 | use Net::LDAP; | ||
| 163 | use Digest::SHA1; | ||
| 164 | use MIME::Base64; | ||
| 165 | |||
| 166 | #New object | ||
| 167 | # | ||
| 168 | ##requires the config hash to be passed. | ||
| 169 | #(see example in ldapops.pl) | ||
| 170 | # | ||
| 171 | #Returns an object of OSS::LDAPops on success | ||
| 172 | #and flase on error. | ||
| 173 | sub new | ||
| 174 | { | ||
| 175 | my($class) = shift; | ||
| 176 | my($self) = shift; | ||
| 177 | bless($self, $class); | ||
| 178 | $self->{LDAPOBJ} = Net::LDAP->new($self->{LDAPHOST},debug => 0) or return($@); | ||
| 179 | return('binddn missing') if (!$self->{BINDDN}); | ||
| 180 | return('basedn missing') if (!$self->{BASEDN}); | ||
| 181 | return('nisdomain missing') if (!$self->{NISDOMAIN}); | ||
| 182 | return('password missing') if (!$self->{PASSWORD}); | ||
| 183 | return($self); | ||
| 184 | |||
| 185 | }; | ||
| 186 | |||
| 187 | =head2 bind | ||
| 188 | |||
| 189 | Bind to LDAP server with supplied credentials. | ||
| 190 | |||
| 191 | No arguments are accepted as the pre-supplied config | ||
| 192 | values are used. | ||
| 193 | |||
| 194 | =cut | ||
| 195 | sub bind | ||
| 196 | { | ||
| 197 | my($self) = shift; | ||
| 198 | my($msg); | ||
| 199 | $msg = $self->{LDAPOBJ}->bind( | ||
| 200 | $self->{BINDDN}, | ||
| 201 | password => $self->{PASSWORD} | ||
| 202 | ); | ||
| 203 | $msg->code && return ($msg->error); | ||
| 204 | }; | ||
| 205 | |||
| 206 | =head2 groupexists | ||
| 207 | |||
| 208 | Check to see if a group exists. | ||
| 209 | |||
| 210 | |||
| 211 | $obj->groupexists(<group>); | ||
| 212 | |||
| 213 | Returns 0 when the group does not exist. | ||
| 214 | Returns 2 when the group does exists. | ||
| 215 | Returns a text string on error. | ||
| 216 | |||
| 217 | =cut | ||
| 218 | sub groupexists | ||
| 219 | { | ||
| 220 | my($self) = shift; | ||
| 221 | my($group) = shift; | ||
| 222 | my($msg); | ||
| 223 | $msg = $self->{LDAPOBJ}->search( | ||
| 224 | base => 'ou=netgroups,'.$self->{BASEDN}, | ||
| 225 | scope => 'one', | ||
| 226 | filter => "(cn=$group)", | ||
| 227 | ); | ||
| 228 | $msg->code && return ($msg->error); | ||
| 229 | if ($msg->entries) {return(2)} | ||
| 230 | else {return(0);}; | ||
| 231 | }; | ||
| 232 | |||
| 233 | =head2 userexists | ||
| 234 | |||
| 235 | Check if user exists. | ||
| 236 | |||
| 237 | $obj->userexists(<user>); | ||
| 238 | |||
| 239 | Returns 0 when the group does not exist. | ||
| 240 | Returns 2 when the group does exists. | ||
| 241 | Returns a text string on error. | ||
| 242 | |||
| 243 | =cut | ||
| 244 | sub userexists | ||
| 245 | { | ||
| 246 | my($self) = shift; | ||
| 247 | my($user) = shift; | ||
| 248 | my($msg); | ||
| 249 | $msg = $self->{LDAPOBJ}->search( | ||
| 250 | base => 'ou=people,'.$self->{BASEDN}, | ||
| 251 | scope => 'one', | ||
| 252 | filter => "(uid=$user)", | ||
| 253 | ); | ||
| 254 | $msg->code && return ($msg->error); | ||
| 255 | if ($msg->entries) {return(2)} | ||
| 256 | else {return(0);}; | ||
| 257 | }; | ||
| 258 | |||
| 259 | =head2 searchuser | ||
| 260 | |||
| 261 | Search for a users entry in the directory. | ||
| 262 | |||
| 263 | $obj->searchuser(<userid>); | ||
| 264 | |||
| 265 | (the wildcard * can be used) | ||
| 266 | |||
| 267 | Returns an array of Net::LDAP:Entry objects on success | ||
| 268 | Returns false on no results. | ||
| 269 | Returns an error string on error. | ||
| 270 | |||
| 271 | =cut | ||
| 272 | sub searchuser | ||
| 273 | { | ||
| 274 | my($self) = shift; | ||
| 275 | my($userid) = shift; | ||
| 276 | my($msg); | ||
| 277 | $msg = $self->{LDAPOBJ}->search( | ||
| 278 | base => 'ou=people,'.$self->{BASEDN}, | ||
| 279 | scope => 'one', | ||
| 280 | filter => "(uid=$userid)", | ||
| 281 | ); | ||
| 282 | $msg->code && return ($msg->error); | ||
| 283 | return($msg->entries); | ||
| 284 | |||
| 285 | |||
| 286 | }; | ||
| 287 | |||
| 288 | =head2 searchnetgroup | ||
| 289 | |||
| 290 | Search for a netgroup entry in the directory. | ||
| 291 | |||
| 292 | #$obj->searchnetgroup(<group>); | ||
| 293 | |||
| 294 | (the wildcard * can be used) | ||
| 295 | |||
| 296 | Returns an array of Net::LDAP:Entry objects on success | ||
| 297 | Returns false on no results. | ||
| 298 | Returns an error string on error. | ||
| 299 | |||
| 300 | =cut | ||
| 301 | sub searchnetgroup | ||
| 302 | { | ||
| 303 | my($self) = shift; | ||
| 304 | my($srch) = shift; | ||
| 305 | my($msg); | ||
| 306 | $msg = $self->{LDAPOBJ}->search( | ||
| 307 | base => 'ou=netgroups,'.$self->{BASEDN}, | ||
| 308 | scope => 'one', | ||
| 309 | filter => "(cn=$srch)" | ||
| 310 | ); | ||
| 311 | $msg->code && ($msg->error); | ||
| 312 | return($msg->entries); | ||
| 313 | #foreach my $entry ($msg->entries) {$entry->dump;}; | ||
| 314 | |||
| 315 | }; | ||
| 316 | |||
| 317 | |||
| 318 | =head2 searchunixgroup | ||
| 319 | |||
| 320 | Search for a unix (posix) group entry in the directory. | ||
| 321 | |||
| 322 | #$obj->searchunixgroup(<group>); | ||
| 323 | |||
| 324 | (the wildcard * can be used) | ||
| 325 | |||
| 326 | Returns an array of Net::LDAP:Entry objects on success | ||
| 327 | Returns false on no results. | ||
| 328 | Returns an error string on error. | ||
| 329 | |||
| 330 | =cut | ||
| 331 | sub searchunixgroup | ||
| 332 | { | ||
| 333 | my($self) = shift; | ||
| 334 | my($srch) = shift; | ||
| 335 | my($msg); | ||
| 336 | $msg = $self->{LDAPOBJ}->search( | ||
| 337 | base => 'ou=unixgroups,'.$self->{BASEDN}, | ||
| 338 | scope => 'one', | ||
| 339 | filter => "(cn=$srch)" | ||
| 340 | ); | ||
| 341 | $msg->code && ($msg->error); | ||
| 342 | return($msg->entries); | ||
| 343 | #foreach my $entry ($msg->entries) {$entry->dump;}; | ||
| 344 | |||
| 345 | }; | ||
| 346 | |||
| 347 | =head2 addhost | ||
| 348 | |||
| 349 | Add a host entry to the directory | ||
| 350 | |||
| 351 | $obj->addhost(<hostname>); | ||
| 352 | |||
| 353 | Returns a text string on error | ||
| 354 | Returns false on success | ||
| 355 | |||
| 356 | =cut | ||
| 357 | sub addhost | ||
| 358 | { | ||
| 359 | my($self) =shift; | ||
| 360 | my($hostname) = shift; | ||
| 361 | my($msg); | ||
| 362 | $msg = $self->{LDAPOBJ}->add( | ||
| 363 | "cn=$hostname-hug, ou=netgroups,".$self->{BASEDN}, | ||
| 364 | attr => [ | ||
| 365 | 'cn' => $hostname.'-hug', | ||
| 366 | 'description' => "Host User Group for $hostname", | ||
| 367 | 'objectclass' => ['top','nisNetgroup'] | ||
| 368 | ] | ||
| 369 | ); | ||
| 370 | $msg->code && return ($msg->error); | ||
| 371 | |||
| 372 | }; | ||
| 373 | |||
| 374 | =head2 addhostgroup | ||
| 375 | |||
| 376 | Add a host group entry to the directory | ||
| 377 | |||
| 378 | $obj->addhostgroup(<hostname>); | ||
| 379 | |||
| 380 | Returns a text string on error | ||
| 381 | Returns false on success | ||
| 382 | |||
| 383 | =cut | ||
| 384 | sub addhostgroup | ||
| 385 | { | ||
| 386 | my($self) =shift; | ||
| 387 | my($hostname) = shift; | ||
| 388 | my($msg); | ||
| 389 | $msg = $self->{LDAPOBJ}->add( | ||
| 390 | "cn=$hostname-hg, ou=netgroups,".$self->{BASEDN}, | ||
| 391 | attr => [ | ||
| 392 | 'cn' => $hostname.'-hg', | ||
| 393 | 'description' => "Host Group for $hostname", | ||
| 394 | 'objectclass' => ['top','nisNetgroup'] | ||
| 395 | ] | ||
| 396 | ); | ||
| 397 | $msg->code && return ($msg->error); | ||
| 398 | |||
| 399 | }; | ||
| 400 | |||
| 401 | =head2 addusergroup | ||
| 402 | |||
| 403 | Add a user group entry to the directory | ||
| 404 | |||
| 405 | $obj->addusergroup(<groupname>); | ||
| 406 | |||
| 407 | Returns a text string on error | ||
| 408 | Returns false on success | ||
| 409 | |||
| 410 | =cut | ||
| 411 | sub addusergroup | ||
| 412 | { | ||
| 413 | my($self) =shift; | ||
| 414 | my($groupname) = shift; | ||
| 415 | my($msg); | ||
| 416 | $msg = $self->{LDAPOBJ}->add( | ||
| 417 | "cn=$groupname-ug, ou=netgroups,".$self->{BASEDN}, | ||
| 418 | attr => [ | ||
| 419 | 'cn' => $groupname.'-ug', | ||
| 420 | 'description' => "User Group: $groupname", | ||
| 421 | 'objectclass' => ['top','nisNetgroup'] | ||
| 422 | ] | ||
| 423 | ); | ||
| 424 | $msg->code && return ($msg->error); | ||
| 425 | |||
| 426 | }; | ||
| 427 | |||
| 428 | =head2 addunixgroup | ||
| 429 | |||
| 430 | Add a unix group to the directory | ||
| 431 | |||
| 432 | $obj->addunixgroup(<groupname>,<gid>); | ||
| 433 | |||
| 434 | returns a text string on error | ||
| 435 | returns false on success | ||
| 436 | =cut | ||
| 437 | sub addunixgroup | ||
| 438 | { | ||
| 439 | my($self) =shift; | ||
| 440 | my($groupname) = shift; | ||
| 441 | my($gid) = shift; | ||
| 442 | my($msg); | ||
| 443 | $msg = $self->{LDAPOBJ}->add( | ||
| 444 | "cn=$groupname, ou=unixgroups,".$self->{BASEDN}, | ||
| 445 | attr => [ | ||
| 446 | 'cn' => $groupname, | ||
| 447 | 'description' => "Unix Group: $groupname", | ||
| 448 | 'objectclass' => ['top','posixGroup'], | ||
| 449 | 'gidNumber' => $gid | ||
| 450 | ] | ||
| 451 | ); | ||
| 452 | $msg->code && return ($msg->error); | ||
| 453 | |||
| 454 | }; | ||
| 455 | |||
| 456 | #Generate salt for SHA1. | ||
| 457 | sub salt8 { | ||
| 458 | my($self) = shift; | ||
| 459 | my $salt = join '', ('a'..'z')[rand 26,rand 26,rand 26,rand 26,rand 26,rand 26,rand 26,rand 26]; | ||
| 460 | return($salt); | ||
| 461 | } | ||
| 462 | |||
| 463 | |||
| 464 | sub password_ssha { | ||
| 465 | my($self) = shift; | ||
| 466 | my $pass=shift; | ||
| 467 | my ($hashedPasswd,$salt); | ||
| 468 | $salt = &salt8; | ||
| 469 | my $ctx = Digest::SHA1->new; | ||
| 470 | $ctx->add($pass); | ||
| 471 | $ctx->add($salt); | ||
| 472 | $hashedPasswd = '{SSHA}' . encode_base64($ctx->digest . $salt,''); | ||
| 473 | return($hashedPasswd); | ||
| 474 | } | ||
| 475 | |||
| 476 | =head2 adduser | ||
| 477 | |||
| 478 | Add a user entry to the directory | ||
| 479 | |||
| 480 | $obj->adduser(<username>); | ||
| 481 | |||
| 482 | Returns a text string on error | ||
| 483 | Returns false on success | ||
| 484 | |||
| 485 | =cut | ||
| 486 | sub adduser | ||
| 487 | { | ||
| 488 | |||
| 489 | my($self) = shift; | ||
| 490 | my($uid, $givenname, $sn, $cn, $mail, $password, $gid, $homedir, $loginshell,$shadowmax, $shadowmin, $shadowwarn, $shadowinactive, $employeenumber,$mailmessagestore) = @_; | ||
| 491 | my($msg); | ||
| 492 | srand; | ||
| 493 | my($salt) = $self->salt8; | ||
| 494 | |||
| 495 | $msg = $self->{LDAPOBJ}->search( | ||
| 496 | base => 'uid=maxUid,'.$self->{BASEDN}, | ||
| 497 | scope => 'sub', | ||
| 498 | filter => "(uid=maxUid)" | ||
| 499 | ); | ||
| 500 | $msg->code && return ($msg->error); | ||
| 501 | my($entry) = $msg->entry; | ||
| 502 | return('Problem: could maxUid be missing from the directory?') if (!$entry); | ||
| 503 | my($maxuid) = $entry->get_value('uidNumber'); | ||
| 504 | my($newmaxuid); | ||
| 505 | my($i) = 0; | ||
| 506 | until ($i) | ||
| 507 | { | ||
| 508 | $newmaxuid = $maxuid+1; | ||
| 509 | $msg = $self->{LDAPOBJ}->modify( | ||
| 510 | "uid=maxUid,".$self->{BASEDN}, | ||
| 511 | delete => { | ||
| 512 | 'uidNumber' => $maxuid | ||
| 513 | }, | ||
| 514 | add => { | ||
| 515 | 'uidNumber' => $newmaxuid | ||
| 516 | } | ||
| 517 | ); | ||
| 518 | $msg->code && next; | ||
| 519 | $i++; | ||
| 520 | }; | ||
| 521 | $msg = $self->{LDAPOBJ}->add( | ||
| 522 | "uid=$uid, ou=people,".$self->{BASEDN}, | ||
| 523 | attr => [ | ||
| 524 | 'uid' => $uid, | ||
| 525 | 'uidNumber' => $newmaxuid, | ||
| 526 | 'cn' => $cn, | ||
| 527 | 'givenname' => $givenname, | ||
| 528 | 'sn' => $sn, | ||
| 529 | 'mail' => $mail, | ||
| 530 | 'gidNumber' => $gid, | ||
| 531 | 'homeDirectory' => $homedir, | ||
| 532 | 'loginshell' => $loginshell, | ||
| 533 | 'userpassword' => $self->password_ssha($password,$salt), | ||
| 534 | 'description' => "User entry for $cn - $uid", | ||
| 535 | 'objectclass' => ['top','person','inetOrgPerson','posixAccount','shadowAccount','qmailUser'], | ||
| 536 | 'shadowMax' => $shadowmax, | ||
| 537 | 'shadowMin' => $shadowmin, | ||
| 538 | 'shadowWarning' => $shadowwarn, | ||
| 539 | 'employeeNumber'=> $employeenumber, | ||
| 540 | 'shadowInactive'=> $shadowinactive, | ||
| 541 | 'shadowLastChange'=> 0, | ||
| 542 | 'mailMessageStore' => $mailmessagestore | ||
| 543 | ] | ||
| 544 | ); | ||
| 545 | $msg->code && return ($msg->error); | ||
| 546 | }; | ||
| 547 | |||
| 548 | |||
| 549 | |||
| 550 | =head2 updatepw | ||
| 551 | |||
| 552 | Add a user entry to the directory | ||
| 553 | |||
| 554 | $obj->updatepw(<username>,<password>,<force reset on login [1|0]>); | ||
| 555 | |||
| 556 | Returns a text string on error | ||
| 557 | Returns false on success | ||
| 558 | |||
| 559 | =cut | ||
| 560 | sub updatepw | ||
| 561 | { | ||
| 562 | my($self) = shift; | ||
| 563 | my($yw,$newpw,$forcereset,$ou) = @_; | ||
| 564 | my($msg); | ||
| 565 | my($salt) = $self->salt8; | ||
| 566 | if ($forcereset) | ||
| 567 | { | ||
| 568 | $msg = $self->{LDAPOBJ}->modify( | ||
| 569 | "uid=$yw, ou=$ou,".$self->{BASEDN}, | ||
| 570 | replace => { | ||
| 571 | 'userpassword' => $self->password_ssha($newpw,$salt), | ||
| 572 | 'shadowLastChange' => 0 | ||
| 573 | } | ||
| 574 | ); | ||
| 575 | |||
| 576 | } else | ||
| 577 | { | ||
| 578 | $msg = $self->{LDAPOBJ}->modify( | ||
| 579 | "uid=$yw, ou=$ou,".$self->{BASEDN}, | ||
| 580 | replace => { | ||
| 581 | 'userpassword' => $self->password_ssha($newpw,$salt) | ||
| 582 | } | ||
| 583 | ); | ||
| 584 | } | ||
| 585 | $msg->code && return ($msg->error); | ||
| 586 | }; | ||
| 587 | |||
| 588 | =head2 lockacct | ||
| 589 | |||
| 590 | Lock a user account by setting shadowExpire to 1 | ||
| 591 | |||
| 592 | $obj->lockacct(<uid>,<lock [1|0]>,<ppolicy [1|0]>); | ||
| 593 | |||
| 594 | |||
| 595 | =cut | ||
| 596 | |||
| 597 | sub lockacct { | ||
| 598 | my($self) = shift; | ||
| 599 | my($uid) = shift; | ||
| 600 | my($lock) = shift; | ||
| 601 | my($useppolicy) = shift; | ||
| 602 | my($msg); | ||
| 603 | if ($lock) | ||
| 604 | { | ||
| 605 | $msg = $self->{LDAPOBJ}->modify( | ||
| 606 | "uid=$uid, ou=people,".$self->{BASEDN}, | ||
| 607 | replace => { | ||
| 608 | 'shadowExpire' => 1 | ||
| 609 | } | ||
| 610 | ); | ||
| 611 | |||
| 612 | } else | ||
| 613 | { | ||
| 614 | $msg = $self->{LDAPOBJ}->modify( | ||
| 615 | "uid=$uid, ou=people,".$self->{BASEDN}, | ||
| 616 | replace => { | ||
| 617 | 'shadowExpire' => -1 | ||
| 618 | } | ||
| 619 | ); | ||
| 620 | } | ||
| 621 | $msg->code && return ($msg->error); | ||
| 622 | }; | ||
| 623 | |||
| 624 | =head2 addusertoug | ||
| 625 | |||
| 626 | Add a user entry to a user group | ||
| 627 | |||
| 628 | $obj->addusertoug(<username>,<group>); | ||
| 629 | |||
| 630 | Returns a text string on error | ||
| 631 | Returns false on success | ||
| 632 | |||
| 633 | =cut | ||
| 634 | sub addusertoug | ||
| 635 | { | ||
| 636 | my($self) = shift; | ||
| 637 | my($yw,$group) = @_; | ||
| 638 | my($msg); | ||
| 639 | my ($ue) = $self->userexists("$yw"); | ||
| 640 | return ('User to add does not exist') if (!$ue); | ||
| 641 | return($ue) if ($ue ne '2'); | ||
| 642 | $msg = $self->{LDAPOBJ}->modify( | ||
| 643 | "cn=$group-ug, ou=netgroups,".$self->{BASEDN}, | ||
| 644 | add => { | ||
| 645 | 'nisNetgroupTriple' => "(,$yw,$self->{NISDOMAIN})" | ||
| 646 | } | ||
| 647 | ); | ||
| 648 | $msg->code && return ($msg->error); | ||
| 649 | }; | ||
| 650 | |||
| 651 | =head2 deluserfromug | ||
| 652 | |||
| 653 | Del a user from a user gorup | ||
| 654 | |||
| 655 | $obj->deluserfromug(<username>,<group>); | ||
| 656 | |||
| 657 | Returns a text string on error | ||
| 658 | Returns false on success | ||
| 659 | |||
| 660 | =cut | ||
| 661 | sub deluserfromug | ||
| 662 | { | ||
| 663 | my($self) = shift; | ||
| 664 | my($yw,$group) = @_; | ||
| 665 | my($msg); | ||
| 666 | $msg = $self->{LDAPOBJ}->modify( | ||
| 667 | "cn=$group-ug, ou=netgroups,".$self->{BASEDN}, | ||
| 668 | delete => { | ||
| 669 | 'nisNetgroupTriple' => "(,$yw,$self->{NISDOMAIN})" | ||
| 670 | } | ||
| 671 | ); | ||
| 672 | $msg->code && return ($msg->error); | ||
| 673 | }; | ||
| 674 | |||
| 675 | =head2 addhosttohg | ||
| 676 | |||
| 677 | Add a host to a host group | ||
| 678 | |||
| 679 | $obj->addhosttohg(<host>,<group>); | ||
| 680 | |||
| 681 | Returns a text string on error | ||
| 682 | Returns false on success | ||
| 683 | |||
| 684 | =cut | ||
| 685 | sub addhosttohg | ||
| 686 | { | ||
| 687 | my($self) = shift; | ||
| 688 | my($host,$group) = @_; | ||
| 689 | my($msg); | ||
| 690 | my ($he) = $self->groupexists("$host-hug"); | ||
| 691 | return ('Host to add does not exist') if (!$he); | ||
| 692 | return($he) if ($he ne '2'); | ||
| 693 | $msg = $self->{LDAPOBJ}->modify( | ||
| 694 | "cn=$group-hg, ou=netgroups,".$self->{BASEDN}, | ||
| 695 | add => { | ||
| 696 | 'nisNetgroupTriple' => "($host,,$self->{NISDOMAIN})" | ||
| 697 | } | ||
| 698 | ); | ||
| 699 | $msg->code && return ($msg->error); | ||
| 700 | }; | ||
| 701 | |||
| 702 | =head2 delhostfromhg | ||
| 703 | |||
| 704 | Delete host from host group | ||
| 705 | |||
| 706 | $obj->delhostfromhg(<host>,<group>); | ||
| 707 | |||
| 708 | Returns a text string on error | ||
| 709 | Returns false on success | ||
| 710 | |||
| 711 | =cut | ||
| 712 | sub delhostfromhg | ||
| 713 | { | ||
| 714 | my($self) = shift; | ||
| 715 | my($host,$group) = @_; | ||
| 716 | my($msg); | ||
| 717 | $msg = $self->{LDAPOBJ}->modify( | ||
| 718 | "cn=$group-hg, ou=netgroups,".$self->{BASEDN}, | ||
| 719 | delete => { | ||
| 720 | 'nisNetgroupTriple' => "($host,,$self->{NISDOMAIN})" | ||
| 721 | } | ||
| 722 | ); | ||
| 723 | $msg->code && return ($msg->error); | ||
| 724 | }; | ||
| 725 | |||
| 726 | =head2 addusertohug | ||
| 727 | |||
| 728 | add user to host user group | ||
| 729 | |||
| 730 | $obj->addusertohug(<host>,<group>); | ||
| 731 | |||
| 732 | Returns a text string on error | ||
| 733 | Returns false on success | ||
| 734 | |||
| 735 | =cut | ||
| 736 | sub addusertohug | ||
| 737 | { | ||
| 738 | my($self) = shift; | ||
| 739 | my($yw,$host) = @_; | ||
| 740 | my($msg); | ||
| 741 | my ($ue) = $self->userexists("$yw"); | ||
| 742 | return ('User to add does not exist') if (!$ue); | ||
| 743 | return($ue) if ($ue ne '2'); | ||
| 744 | $msg = $self->{LDAPOBJ}->modify( | ||
| 745 | "cn=$host-hug, ou=netgroups,".$self->{BASEDN}, | ||
| 746 | add => { | ||
| 747 | 'nisNetgroupTriple' => "($host,$yw,$self->{NISDOMAIN})" | ||
| 748 | } | ||
| 749 | ); | ||
| 750 | $msg->code && return ($msg->error); | ||
| 751 | }; | ||
| 752 | |||
| 753 | =head2 deluserfromhug | ||
| 754 | |||
| 755 | delete user from host user group | ||
| 756 | |||
| 757 | $obj->deluserfromhug(<host>,<group>); | ||
| 758 | |||
| 759 | Returns a text string on error | ||
| 760 | Returns false on success | ||
| 761 | |||
| 762 | =cut | ||
| 763 | sub deluserfromhug | ||
| 764 | { | ||
| 765 | my($self) = shift; | ||
| 766 | my($yw,$host) = @_; | ||
| 767 | my($msg); | ||
| 768 | $msg = $self->{LDAPOBJ}->modify( | ||
| 769 | "cn=$host-hug, ou=netgroups,".$self->{BASEDN}, | ||
| 770 | delete => { | ||
| 771 | 'nisNetgroupTriple' => "($host,$yw,$self->{NISDOMAIN})" | ||
| 772 | } | ||
| 773 | ); | ||
| 774 | $msg->code && return ($msg->error); | ||
| 775 | }; | ||
| 776 | |||
| 777 | =head2 addgrouptogroup | ||
| 778 | |||
| 779 | Add a group to a group | ||
| 780 | |||
| 781 | $obj->addggrouptogroup(<ug|hg>,<host>,<group>); | ||
| 782 | |||
| 783 | Returns a text string on error | ||
| 784 | Returns false on success | ||
| 785 | |||
| 786 | =cut | ||
| 787 | sub addgrouptogroup | ||
| 788 | { | ||
| 789 | my($self) = shift; | ||
| 790 | my($type,$addgroup,$togroup) = @_; | ||
| 791 | return('Type not ug or hg') if ($type !~ m/[uh]g/); | ||
| 792 | return('Cannot add a group to itself') if ($addgroup eq $togroup); | ||
| 793 | my ($ge) = $self->groupexists("$addgroup-$type"); | ||
| 794 | return ('Group to add does not exist') if (!$ge); | ||
| 795 | return($ge) if ($ge ne '2'); | ||
| 796 | my($msg); | ||
| 797 | $msg = $self->{LDAPOBJ}->modify( | ||
| 798 | "cn=$togroup-$type, ou=netgroups,".$self->{BASEDN}, | ||
| 799 | add => { | ||
| 800 | 'memberNisNetgroup' => "$addgroup-$type" | ||
| 801 | } | ||
| 802 | ); | ||
| 803 | $msg->code && return ($msg->error); | ||
| 804 | }; | ||
| 805 | |||
| 806 | =head2 delgroupfromgroup | ||
| 807 | |||
| 808 | delete group from group | ||
| 809 | |||
| 810 | $obj->delgroupfromgroup(<ug|hg>,<host>,<group>); | ||
| 811 | |||
| 812 | Returns a text string on error | ||
| 813 | Returns false on success | ||
| 814 | |||
| 815 | =cut | ||
| 816 | sub delgroupfromgroup | ||
| 817 | { | ||
| 818 | my($self) = shift; | ||
| 819 | my($type,$delgroup,$fromgroup) = @_; | ||
| 820 | return('Type not ug or hg') if ($type !~ m/[uh]g/); | ||
| 821 | my($msg); | ||
| 822 | $msg = $self->{LDAPOBJ}->modify( | ||
| 823 | "cn=$fromgroup-$type, ou=netgroups,".$self->{BASEDN}, | ||
| 824 | delete => { | ||
| 825 | 'memberNisNetgroup' => "$delgroup-$type" | ||
| 826 | } | ||
| 827 | ); | ||
| 828 | $msg->code && return ($msg->error); | ||
| 829 | }; | ||
| 830 | |||
| 831 | =head2 addusertounixgroup | ||
| 832 | |||
| 833 | add user to a unix group | ||
| 834 | |||
| 835 | $obj->addusertounixgroup(<user>,<unix group>); | ||
| 836 | |||
| 837 | Returns a text string on error | ||
| 838 | Returns false on success | ||
| 839 | |||
| 840 | =cut | ||
| 841 | |||
| 842 | sub addusertounixgroup | ||
| 843 | { | ||
| 844 | my($self) = shift; | ||
| 845 | my($user,$group) = @_; | ||
| 846 | my($msg); | ||
| 847 | $msg = $self->{LDAPOBJ}->modify( | ||
| 848 | "cn=$group, ou=unixgroups,".$self->{BASEDN}, | ||
| 849 | add => { | ||
| 850 | 'memberUid' => $user | ||
| 851 | } | ||
| 852 | ); | ||
| 853 | $msg->code && return ($msg->error); | ||
| 854 | }; | ||
| 855 | |||
| 856 | =head2 deluserfromunixgroup | ||
| 857 | |||
| 858 | delete user from a unix group | ||
| 859 | |||
| 860 | $obj->deluserfromunixgroup(<user>,<unix group>); | ||
| 861 | |||
| 862 | Returns a text string on error | ||
| 863 | Returns false on success | ||
| 864 | |||
| 865 | =cut | ||
| 866 | |||
| 867 | sub deluserfromunixgroup | ||
| 868 | { | ||
| 869 | my($self) = shift; | ||
| 870 | my($user,$group) = @_; | ||
| 871 | my($msg); | ||
| 872 | $msg = $self->{LDAPOBJ}->modify( | ||
| 873 | "cn=$group, ou=unixgroups,".$self->{BASEDN}, | ||
| 874 | delete => { | ||
| 875 | 'memberUid' => $user | ||
| 876 | } | ||
| 877 | ); | ||
| 878 | $msg->code && return ($msg->error); | ||
| 879 | }; | ||
| 880 | |||
| 881 | =head2 deletedn | ||
| 882 | |||
| 883 | Delete an entry by DN (use with caution) | ||
| 884 | |||
| 885 | Used to remove users and groups by DN | ||
| 886 | |||
| 887 | WARNING: it's possible to damage the tree stucture | ||
| 888 | this way!!!! get it right!! | ||
| 889 | |||
| 890 | $obj=>deletedn($dn); | ||
| 891 | |||
| 892 | Returns a text string on error. | ||
| 893 | Returns false on success | ||
| 894 | |||
| 895 | =cut | ||
| 896 | sub deletedn | ||
| 897 | { | ||
| 898 | my($self) = shift; | ||
| 899 | my($dn) = shift; | ||
| 900 | my($msg); | ||
| 901 | $msg = $self->{LDAPOBJ}->delete($dn); | ||
| 902 | $msg->code && return ($msg->error); | ||
| 903 | }; | ||
| 904 | #Do not remove the below '1'. It is needed for correct functioning. | ||
| 905 | 1; | ||
| 906 | |||
| 907 | #EOF | ||
| 908 |