Guest User

Untitled

a guest
Jun 21st, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. #
  3. # Creates a new repository in the database with input data
  4.  
  5. use DBI;
  6. use Sys::Hostname;
  7.  
  8. sub identify_hostname;
  9. sub identify_admin;
  10. sub read_path_parameter;
  11. sub create_repo;
  12.  
  13. my $DB = 'DBI:mysql:Repository:localhost';
  14. my $USER = 'wwwrun';
  15. my $PASSWORD = 'SeNh4Z';
  16.  
  17. my $HOSTNAME = 'luist';
  18. my $ADMIN_GROUP = 'grouptest';
  19.  
  20. identify_hostname();
  21. identify_admin();
  22.  
  23. my $dbh = DBI->connect($DB, $USER, $PASSWORD)
  24. or die "Couldn't connect to $DB\n";
  25.  
  26. my $name = $ARGV[0];
  27. my $path = $ARGV[1];
  28. my $url = $ARGV[2];
  29.  
  30. read_path_parameter($path);
  31. create_repo($dbh, $name, $path, $url);
  32.  
  33. sub identify_hostname {
  34. my $this_host = hostname;
  35.  
  36. if ($this_host ne $HOSTNAME) {
  37. die "${this_host}: Invalid hostname.\n";
  38. }
  39. return 1;
  40. }
  41.  
  42. sub identify_admin {
  43. my $login = getlogin();
  44. my ($name, $passwd, $gid, $members) = getgrnam($ADMIN_GROUP);
  45. #print "Name: ${name}\nPasswd: ${passwd}\nGID: ${gid}\nMembers ${members}\n";
  46.  
  47. unless (defined($name)) {
  48. die "${ADMIN_GROUP}: Inexistent group.\n";
  49. }
  50.  
  51. my @member_list = split(/ /,$members);
  52. if(grep { $_ eq $login } @member_list) {
  53. #print "Login: ${login}\n";
  54. return 1;
  55. }
  56. else {
  57. die "${login}: Permission denied for this user.\n";
  58. }
  59. }
  60.  
  61. sub read_path_parameter {
  62. my ($my_path) = @_;
  63.  
  64. unless (-d $my_path) {
  65. die "${my_path}: Invalid directory\n"
  66. }
  67. }
  68.  
  69. sub create_repo {
  70. my ($my_dbh, $my_name, $my_path, $my_url) = @_;
  71. my ($sth);
  72.  
  73. my $statement = 'INSERT INTO Repositories (`Name`, `InsertBy`, `UpdateBy`, `InsertTime`, `UpdateTime`, `Path`, `Url`) VALUES ';
  74. $statement .= "(\"${my_name}\", \"${USER}\", \"${USER}\", now(), now(), \"${my_path}\", \"${my_url}\")";
  75.  
  76. unless ($sth = $my_dbh->prepare($statement)) {
  77. print STDERR __LINE__.": Can't prepare \"$statement\": ".$my_dbh->errstr."\n";
  78. $my_dbh->disconnect;
  79. exit 2;
  80. }
  81. unless ($sth->execute()) {
  82. print STDERR __LINE__.": Can't execute \"$statement\": ".$my_dbh->errstr."\n";
  83. $dbh->disconnect;
  84. exit 2;
  85. }
  86.  
  87. #print "Name: ${my_name}\nPath: ${my_path}\nUrl: ${my_url}\n";
  88. }
Add Comment
Please, Sign In to add comment