gdipadmin.pl 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/perl
  2. #####################################################
  3. # gdipadmin.pl
  4. #
  5. # This script adds an admin user to the GnuDIP
  6. # database.
  7. #
  8. # See COPYING for licensing information.
  9. #
  10. #####################################################
  11. # PERL modules
  12. use strict;
  13. use Getopt::Std;
  14. # global variables
  15. use vars qw($conf $gnudipdir);
  16. # locate ourselves
  17. use FindBin;
  18. BEGIN {
  19. $gnudipdir = '';
  20. if ($FindBin::Bin =~ /(.*)\/.+?/) {
  21. $gnudipdir = $1;
  22. }
  23. }
  24. use lib "$gnudipdir/lib";
  25. # GnuDIP common subroutines
  26. use gdiplib;
  27. use dbusers;
  28. use gdiplinemode;
  29. # process command line
  30. sub usage {
  31. print STDERR <<"EOQ";
  32. usage: gdipadmin.pl [ -h | [ -u ] userid password ]
  33. usage: Add GnuDIP administrative user \"user\" with password \"password\".
  34. usage: -h: Print this usage message.
  35. usage: -u: Update user if it already exists.
  36. EOQ
  37. }
  38. use vars qw/ $opt_h $opt_u /;
  39. if (!getopts('hu')) {
  40. usage();
  41. exit 1;
  42. }
  43. if ($opt_h) {
  44. usage();
  45. exit;
  46. }
  47. if (@ARGV ne 2) {
  48. usage();
  49. exit 1;
  50. }
  51. my $username = $ARGV[0];
  52. my $password = $ARGV[1];
  53. # get preferences from config file
  54. $conf = getconf();
  55. # user exists?
  56. my $userinfo = getuser($username, '');
  57. # user exists
  58. if ($userinfo) {
  59. # did not say to update
  60. if (!$opt_u) {
  61. print STDOUT "User $username already exists - use \"-u\" to update\n";
  62. # supposed to update
  63. } else {
  64. $$userinfo{'password'} = md5_hex($password);
  65. $$userinfo{'level'} = 'ADMIN';
  66. updateuser($userinfo);
  67. print STDOUT
  68. "Updated username $username with password $password and set as ADMIN\n";
  69. }
  70. # user does not exist yet
  71. } else {
  72. createuser(
  73. $username,
  74. '', # domain
  75. md5_hex($password),
  76. 'ADMIN',
  77. '' # E-mail
  78. );
  79. print STDOUT "Added username $username with password $password and set as ADMIN\n";
  80. }
  81. exit;