script/ldapops.pl untested

filter: Statementstmt 0.0% 0 / 302 Branchbran 0.0% 0 / 172 Conditioncond 0.0% 0 / 60 MC/DCmcdc 0.0% 0 / 39 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 580 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2 =head1 NAME
3  
4 ldapops.pl - perform operations on an LDAP directory from the command line
5  
6 =head1 SYNOPISIS
7  
8 Perform operations on the OSS ldap directory
9  
10 This code uses the module for OSS directory actions, "OSS::LDAPops." Please see OSS::LDAPops for more details.
11  
12 This program will return nothing on sucess and will die with
13 an error message to STDERR on fail.
14  
15 All operations required to administer the directory are avaliable
16 via this program apart from changing objects outside of the user and group
17 space. These must be altered manuaelly.
18  
19 This code does not have to run on the LDAP server as it is network enabled
20 and can be used over the network from a suitable location, ACL permitting of course!
21  
22 =head1 CONFIG
23  
24 A configuration file is required in /etc/ldapops.conf or ~/.ldapopsrc. An example is below:
25  
26 $GLOBAL::config =
27 {
28 LDAPHOST => 'ldap01.mydomain.net',
29 BINDDN => 'uid=webportal, ou=writeaccess, dc=auth, dc=mydomain,dc=net',
30 BASEDN => 'dc=auth,dc=mydomain,dc=net',
31 NISDOMAIN => 'auth.mydomain.net',
32 PASSWORD => 'xyzzy',
33 };
34  
35 #These config options are used within this script
36 $GLOBAL::localconfig =
37 {
38 SHADOWMAX => 90,
39 SHADOWMIN => 10,
40 SHADOWWARNING => 10,
41 SHELL => '/bin/bash',
42 #Trailing '/' please!
43 HOMEPREFIX => '/home/',
44 GID => 300
45 };
46  
47 #This 1 is required!
48 1;
49  
50 This example file is also included in the source distribution.
51  
52 =head1 USAGE
53  
54 ldapops.pl -su <string> | search user
55 ldapops.pl -sg <string> | search netgroup
56 ldapops.pl -sx <string> | search unix/posix group
57 ldapops.pl -ah <hostname> | add host
58 ldapops.pl -ahg <group> | add hostgroup
59 ldapops.pl -aug <user> | add usergroup
60 ldapops.pl -axg <group> <gid> |add unix/posix group
61 ldapops.pl -auug <userid> <group> | add user to user group
62 ldapops.pl -duug <userid> <group> | delete user from user group
63 ldapops.l -auh <userid> <host> | add user to host
64 ldapops.pl -duh <userid> <host> | delete user from host
65 ldapops.pl -ahhg <host> <group> | add host to host group
66 ldapops.pl -dhhg <host> <group> | delete host from host group
67 ldapops.pl -augug <addgroup> <togroup> | add user group to user group
68 ldapops.pl -dugug <delgroup> <fromgroup> | delete user group from user group
69 ldapops.pl -ahghg <addgroup> <togroup> | add host group to host group
70 ldapops.pl -dhghg <delgroup> <fromgroup> | delete host group from host group
71 ldapops.pl -aung <userid> <unix group> | add user to unix group
72 ldapops.pl -dung <userid> <unix group> | del user from unix group
73 ldapops.pl -au | add user
74 ldapops.pl -up <userid> <password> | update password for user
75 ldapops.pl -upr <userid> <password> | update password for user and force reset on next login
76 ldapops.pl -upo <userid> <password> <ou> | update password for user in non-standard ou
77 ldapops.pl -b <csv file> | batch add users from CSV file (see batchadd.csv for format)
78 ldapops.pl -d '<dn>' | delete dn (note the quotes)
79  
80 Note: the wildcard '*' can be used, but must be escaped as \*
81  
82  
83 =cut
84 BEGIN
85 {
86 unshift(@INC, './lib/');
87 };
88  
89 #use strict pragma.
90 use strict;
91  
92 #Use OSS::LDAPops object.
93 use OSS::LDAPops 1.033;
94  
95 warn ("Your OSS::LDAPops is version $OSS::LDAPops::VERSION. This version lf ldapops.pl is designed\nto work with 1.0.33. Upgrade this script!\n") unless ($OSS::LDAPops::VERSION == 1.033);
96  
97 #Use file operations
98 use OSS::Fileops;
99  
100 #Load config
101  
102 if (-r "$ENV{HOME}/.ldapopsrc")
103 {
104 require "$ENV{HOME}/.ldapopsrc";
105 }
106 else
107 {
108 require '/etc/ldapops.conf'
109 };
110 #Instantiate new object.
111 my($ldapopsobj);
112 if ($ARGV[0]) {
113 $ldapopsobj = OSS::LDAPops->new($GLOBAL::config);
114 if (ref($ldapopsobj) !~ m/OSS::LDAPops/ ) {die("Error instantiating object: $ldapopsobj")};
115 };
116 my($ret);
117 my(@retu);
118 #If argument x received....
119 #Search for user
120 if ($ARGV[0] eq '-su')
121 {
122 if (!$ARGV[1])
123 {
124 print("\nUsage: ldaops.pl -su <search string>\n");
125 exit;
126 };
127 $ldapopsobj->bind;
128 @retu = $ldapopsobj->searchuser($ARGV[1]);
129 die($retu[0]) if (($retu[0] ne undef) and (ref($retu[0]) !~ m/Net::LDAP::Entry/) );
130 foreach my $entry (@retu) {$entry->dump; }
131 #if($ret) {die($ret);};
132 exit;
133 }
134 #Search for netgroup
135 elsif ($ARGV[0] eq '-sg')
136 {
137 if (!$ARGV[1])
138 {
139 print("\nUsage: ldaops.pl -sg <search string>\n");
140 exit;
141 };
142 $ldapopsobj->bind;
143 @retu = $ldapopsobj->searchnetgroup($ARGV[1]);
144 die($retu[0]) if (($retu[0] ne undef) and (ref($retu[0]) !~ m/Net::LDAP::Entry/) );
145 foreach my $entry (@retu) {$entry->dump; }
146 exit;
147 }
148 #Search for unix group
149 elsif ($ARGV[0] eq '-sx')
150 {
151 if (!$ARGV[1])
152 {
153 print("\nUsage: ldaops.pl -sx <search string>\n");
154 exit;
155 };
156 $ldapopsobj->bind;
157 @retu = $ldapopsobj->searchunixgroup($ARGV[1]);
158 die($retu[0]) if (($retu[0] ne undef) and (ref($retu[0]) !~ m/Net::LDAP::Entry/) );
159 foreach my $entry (@retu) {$entry->dump; }
160 exit;
161 }
162 #Add host user group
163 elsif ($ARGV[0] eq '-ah')
164 {
165 if (!$ARGV[1])
166 {
167 print("\nUsage: ldapops.pl -ah <hostname>\n");
168 exit;
169 };
170 $ldapopsobj->bind;
171 $ret = $ldapopsobj->addhost($ARGV[1]);
172 if($ret) {die($ret);};
173 exit;
174 }
175 #Add host group
176 elsif ($ARGV[0] eq '-ahg')
177 {
178 if (!$ARGV[1])
179 {
180 print("\nUsage: ldapops.pl -ahg <group>\n");
181 exit;
182 };
183 $ldapopsobj->bind;
184 $ret = $ldapopsobj->addhostgroup($ARGV[1]);
185 if($ret) {die($ret);};
186 exit;
187 }
188 #Add user group
189 elsif ($ARGV[0] eq '-aug')
190 {
191 if (!$ARGV[1])
192 {
193 print("\nUsage: ldapops.pl -aug <group>\n");
194 exit;
195 };
196 $ldapopsobj->bind;
197 $ret = $ldapopsobj->addusergroup($ARGV[1]);
198 if($ret) {die($ret);};
199 exit;
200 }
201 #Add unix group
202 elsif ($ARGV[0] eq '-axg')
203 {
204 if (!$ARGV[1] and !$ARGV[2])
205 {
206 print("\nUsage: ldapops.pl -axg <group> <gid>\n");
207 exit;
208 };
209 $ldapopsobj->bind;
210 $ret = $ldapopsobj->addunixgroup($ARGV[1],$ARGV[2]);
211 if($ret) {die($ret);};
212 exit;
213 }
214 #Add user to user group
215 elsif ($ARGV[0] eq '-auug')
216 {
217 if (!$ARGV[1]or !$ARGV[2])
218 {
219 print("\nUsage: ldapops.pl -auug <userid> <group>\n");
220 exit;
221 };
222 $ldapopsobj->bind;
223 $ret = $ldapopsobj->addusertoug($ARGV[1],$ARGV[2]);
224 if($ret) {die($ret);};
225 exit;
226 }
227 #Delete user from user group
228 elsif ($ARGV[0] eq '-duug')
229 {
230 if (!$ARGV[1]or !$ARGV[2])
231 {
232 print("\nUsage: ldapops.pl -auug <userid> <group>\n");
233 exit;
234 };
235 $ldapopsobj->bind;
236 $ret = $ldapopsobj->deluserfromug($ARGV[1],$ARGV[2]);
237 if($ret) {die($ret);};
238 exit;
239 }
240 #Add host to host group
241 elsif ($ARGV[0] eq '-ahhg')
242 {
243 if (!$ARGV[1] or !$ARGV[2])
244 {
245 print("\nUsage: ldapops.pl -ahhg <host> <group>\n");
246 exit;
247 };
248 $ldapopsobj->bind;
249 $ret = $ldapopsobj->addhosttohg($ARGV[1],$ARGV[2]);
250 if($ret) {die($ret);};
251 exit;
252 }
253 #Delete host from host group
254 elsif ($ARGV[0] eq '-dhhg')
255 {
256 if (!$ARGV[1] or !$ARGV[2])
257 {
258 print("\nUsage: ldapops.pl -ahhg <host> <group>\n");
259 exit;
260 };
261 $ldapopsobj->bind;
262 $ret = $ldapopsobj->delhostfromhg($ARGV[1],$ARGV[2]);
263 if($ret) {die($ret);};
264 exit;
265 }
266 #Add user group to user group
267 elsif ($ARGV[0] eq '-augug')
268 {
269 if (!$ARGV[1] or !$ARGV[2])
270 {
271 print("\nUsage: ldapops.pl -augug <addgroup> <togroup>\n");
272 exit;
273 };
274 $ldapopsobj->bind;
275 $ret = $ldapopsobj->addgrouptogroup('ug',$ARGV[1],$ARGV[2]);
276 if($ret) {die($ret);};
277 exit;
278 }
279 #Del ug from ug
280 elsif ($ARGV[0] eq '-dugug')
281 {
282 if (!$ARGV[1] or !$ARGV[2])
283 {
284 print("\nUsage: ldapops.pl -augug <delgroup> <fromgroup>\n");
285 exit;
286 };
287 $ldapopsobj->bind;
288 $ret = $ldapopsobj->delgroupfromgroup('ug',$ARGV[1],$ARGV[2]);
289 if($ret) {die($ret);};
290 exit;
291 }
292 #add hg to hg
293 elsif ($ARGV[0] eq '-ahghg')
294 {
295 if (!$ARGV[1] or !$ARGV[2])
296 {
297 print("\nUsage: ldapops.pl -augug <addgroup> <togroup>\n");
298 exit;
299 };
300 $ldapopsobj->bind;
301 $ret = $ldapopsobj->addgrouptogroup('hg',$ARGV[1],$ARGV[2]);
302 if($ret) {die($ret);};
303 exit;
304 }
305 #Del hg from hg
306 elsif ($ARGV[0] eq '-dhghg')
307 {
308 if (!$ARGV[1] or !$ARGV[2])
309 {
310 print("\nUsage: ldapops.pl -augug <delgroup> <fromgroup>\n");
311 exit;
312 };
313 $ldapopsobj->bind;
314 $ret = $ldapopsobj->delgroupfromgroup('hg',$ARGV[1],$ARGV[2]);
315 if($ret) {die($ret);};
316 exit;
317 }
318 #add user to host user group
319 elsif ($ARGV[0] eq '-auh')
320 {
321 if (!$ARGV[1] or !$ARGV[2])
322 {
323 print("\nUsage: ldapops.pl -auh <userid> <host>\n");
324 exit;
325 };
326 $ldapopsobj->bind;
327 $ret = $ldapopsobj->addusertohug($ARGV[1],$ARGV[2]);
328 if($ret) {die($ret);};
329 exit;
330 }
331 #Delete user from host user group
332 elsif ($ARGV[0] eq '-duh')
333 {
334 if (!$ARGV[1] or !$ARGV[2])
335 {
336 print("\nUsage: ldapops.pl -auh <userid> <host>\n");
337 exit;
338 };
339 $ldapopsobj->bind;
340 $ret = $ldapopsobj->deluserfromhug($ARGV[1],$ARGV[2]);
341 if($ret) {die($ret);};
342 exit;
343 }
344  
345 #Add user to unix group
346 elsif ($ARGV[0] eq '-aung')
347 {
348 if (!$ARGV[1] or !$ARGV[2])
349 {
350 print("\nUsage: ldapops.pl -aung <userid> <unix group>\n");
351 exit;
352 };
353 $ldapopsobj->bind;
354 $ret = $ldapopsobj->addusertounixgroup($ARGV[1],$ARGV[2]);
355 if($ret) {die($ret);};
356 exit;
357 }
358 #Delete user from unix group
359 elsif ($ARGV[0] eq '-dung')
360 {
361 if (!$ARGV[1] or !$ARGV[2])
362 {
363 print("\nUsage: ldapops.pl -aung <userid> <unix group>\n");
364 exit;
365 };
366 $ldapopsobj->bind;
367 $ret = $ldapopsobj->deluserfromunixgroup($ARGV[1],$ARGV[2]);
368 if($ret) {die($ret);};
369 exit;
370 }
371 #Update passworf for user
372 elsif ($ARGV[0] eq '-up')
373 {
374 if (!$ARGV[1] or !$ARGV[2])
375 {
376 print("\nUsage: ldapops.pl -up <userid> <password>\n");
377 exit;
378 };
379 $ldapopsobj->bind;
380 $ret = $ldapopsobj->updatepw($ARGV[1],$ARGV[2],0,'people');
381 if($ret) {die($ret);};
382 exit;
383 }
384 #Update passworf for user and force reset on next login
385 elsif ($ARGV[0] eq '-upr')
386 {
387 if (!$ARGV[1] or !$ARGV[2])
388 {
389 print("\nUsage: ldapops.pl -upr <userid> <password>\n");
390 exit;
391 };
392 $ldapopsobj->bind;
393 $ret = $ldapopsobj->updatepw($ARGV[1],$ARGV[2],1,'people');
394 if($ret) {die($ret);};
395 exit;
396 }
397 #Lock account
398 elsif ($ARGV[0] eq '-la')
399 {
400 if (!$ARGV[1])
401 {
402 print("\nUsage: ldapops.pl -la <userid>\n");
403 exit;
404 };
405 $ldapopsobj->bind;
406 $ret = $ldapopsobj->lockacct($ARGV[1],1);
407 if($ret) {die($ret);};
408 }
409 #Unlock account
410 elsif ($ARGV[0] eq '-ua')
411 {
412 if (!$ARGV[1])
413 {
414 print("\nUsage: ldapops.pl -ua <userid>\n");
415 exit;
416 };
417 $ldapopsobj->bind;
418 $ret = $ldapopsobj->lockacct($ARGV[1],0);
419 if($ret) {die($ret);};
420 }
421 #Update password in non-standard ou
422 elsif ($ARGV[0] eq '-upo')
423 {
424 if (!$ARGV[1] or !$ARGV[2] or !$ARGV[3])
425 {
426 print("\nUsage: ldapops.pl -upr <userid> <password> <ou>\n");
427 exit;
428 };
429 $ldapopsobj->bind;
430 $ret = $ldapopsobj->updatepw($ARGV[1],$ARGV[2],0, $ARGV[3]);
431 if($ret) {die($ret);};
432 exit;
433 }
434  
435 #Add user
436 elsif ($ARGV[0] eq '-au')
437 {
438 print("\nAdd User:\n\nUsername:");
439 my($uid,$givenname,$sn,$mail,$pw,$cn,$gid,$homedir,$loginshell,$employeenumber);
440 $uid = <STDIN>;
441 chomp($uid);
442 print("\nFirst name:");
443 $givenname = <STDIN>;
444 chomp($givenname);
445 print("\nSurname:");
446 $sn = <STDIN>;
447 chomp($sn);
448 print("\nEmail address:");
449 $mail = <STDIN>;
450 chomp($mail);
451 print("\nEmployee Number:");
452 $employeenumber = <STDIN>;
453 chomp($employeenumber);
454 print("\nNumeric GID or enter for default [$$GLOBAL::localconfig{GID}]:");
455 $gid = <STDIN>;
456 chomp($gid);
457 if ($gid !~ /^\d+/)
458 {
459 $gid = $$GLOBAL::localconfig{GID};
460 };
461 print("\nPassword:");
462 $pw = <STDIN>;
463 chomp($pw);
464 $ldapopsobj->bind;
465 $ret = $ldapopsobj->adduser($uid,$givenname,$sn,$givenname.' '.$sn,$mail,$pw,$gid,$$GLOBAL::localconfig{HOMEPREFIX}.$uid,$$GLOBAL::localconfig{SHELL},$$GLOBAL::localconfig{SHADOWMAX},$$GLOBAL::localconfig{SHADOWMIN},$$GLOBAL::localconfig{SHADOWWARNING},$$GLOBAL::localconfig{SHADOWINACTIVE},$employeenumber,$$GLOBAL::localconfig{MAILMESSAGESTORE}.$mail.'/.maildir/');
466 if($ret) {die($ret);};
467 exit;
468 }
469 #Batch add users
470 elsif ($ARGV[0] eq '-b')
471 {
472 if (!$ARGV[1])
473 {
474 print("\nUsage: ldapops.pl -upr <csv file>\n");
475 exit;
476 };
477 my($fileobj) = OSS::Fileops->new;
478 my(@csvfile) = $fileobj->read_file($ARGV[1]);
479 if (shift(@csvfile) !~ /^username,given name,surname,email,employeenumber,password, numeric gid/)
480 {
481 die("'Error: CSV headings incorrect");
482  
483 }
484 $ldapopsobj->bind;
485 my(@linesplit);
486 foreach my $line (@csvfile)
487 {
488 chomp($line);
489 @linesplit = split(/,/,$line);
490 my($ret) = $ldapopsobj->adduser($linesplit[0],$linesplit[1],$linesplit[2],$linesplit[1].' '.$linesplit[2],$linesplit[3],$linesplit[5],$linesplit[6],$$GLOBAL::localconfig{HOMEPREFIX}.$linesplit[0],$$GLOBAL::localconfig{SHELL},$$GLOBAL::localconfig{SHADOWMAX},$$GLOBAL::localconfig{SHADOWMIN},$$GLOBAL::localconfig{SHADOWWARNING},$$GLOBAL::localconfig{SHADOWINACTIVE},$linesplit[4],$$GLOBAL::localconfig{MAILMESSAGESTORE}.$linesplit[7].'/.maildir/');
491 if($ret) {print("User $linesplit[0] not added: $ret\n");};
492
493 };
494 exit;
495 }
496 #Delete DN
497 elsif ($ARGV[0] eq '-d')
498 {
499 if (!$ARGV[1])
500 {
501 print("\nUsage: ldapops.pl -d \'<dn>\'\n");
502 exit;
503 };
504 $ldapopsobj->bind;
505 $ret = $ldapopsobj->deletedn($ARGV[1]);
506 if($ret) {die($ret);};
507 }
508 else
509 #Print usage information
510 {
511 print("\nUsage:\n\nldapops.pl -su <string>\t\t\t\t| search user\n");
512 print("ldapops.pl -sg <string>\t\t\t\t| search netgroup\n");
513 print("ldapops.pl -sx <string>\t\t\t\t| search unix/posix group\n");
514 print("ldapops.pl -ah <hostname>\t\t\t| add host\n");
515 print("ldapops.pl -ahg <group>\t\t\t\t| add hostgroup\n");
516 print("ldapops.pl -aug <user>\t\t\t\t| add usergroup\n");
517 print("ldapops.pl -axg <group> <gid> \t\t\t| add unix/posix group\n");
518 print("ldapops.pl -auug <userid> <group>\t\t| add user to user group\n");
519 print("ldapops.pl -duug <userid> <group>\t\t| delete user from user group\n");
520 print("ldapops.pl -auh <userid> <host>\t\t\t| add user to host\n");
521 print("ldapops.pl -duh <userid> <host>\t\t\t| delete user from host\n");
522 print("ldapops.pl -ahhg <host> <group>\t\t\t| add host to host group\n");
523 print("ldapops.pl -dhhg <host> <group>\t\t\t| delete host from host group\n");
524 print("ldapops.pl -augug <addgroup> <togroup>\t\t| add user group to user group\n");
525 print("ldapops.pl -dugug <delgroup> <fromgroup>\t| delete user group from user group\n");
526 print("ldapops.pl -ahghg <addgroup> <togroup>\t\t| add host group to host group\n");
527 print("ldapops.pl -dhghg <delgroup> <fromgroup>\t| delete host group from host group\n");
528 print("ldapops.pl -aung <userid> <unix group>\t\t| add user to unix group\n");
529 print("ldapops.pl -dung <userid> <unix group>\t\t| del user from unix group\n");
530 print("ldapops.pl -au \t\t\t\t\t| add user\n");
531 print("ldapops.pl -up <userid> <password>\t\t| update password for user\n");
532 print("ldapops.pl -upr <userid> <password>\t\t| update password for user and force reset on next login\n");
533 print("ldapops.pl -upo <userid> <password> <ou>\t| update password for user in non-standard ou\n");
534 print("ldapops.pl -la <userid>\t\t\t\t| Lock (expire) user account\n");
535 print("ldapops.pl -ul <userid>\t\t\t\t| unlock user accout\n");
536 print("ldapops.pl -b <csv file> \t\t\t| batch add users from CSV file (see batchadd.csv for format)\n");
537 print("ldapops.pl -d \'<dn>\'\t\t\t\t| delete dn (note the quotes)\n");
538 print("\nNote: the wildcard \'*\' can be used, but must be escaped as \\*\n");
539  
540 };