ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/scripts/dbpass
Revision: 24
Committed: Tue Jul 26 21:46:39 2011 UTC (8 years, 2 months ago) by gpertea
File size: 5629 byte(s)
Log Message:
Line File contents
1 #!/usr/bin/perl
2 # this is just a library of several commonly used DBI/Sybase subroutines/wrappers
3 use strict;
4 use FindBin;use lib $FindBin::Bin;
5 use POSIX qw(:termios_h);
6 use dbSession;
7 use Getopt::Std;
8 umask(0077);
9 my $usage=q{
10 Utility for Sybase database/server authentication file management.
11 Unless otherwise specified with -f option, the current user authentication
12 file ~/.db_pass is used.
13
14 Usage:
15 dbpass [-b <db>[@<SERVER>][/<servertype>]]
16 [-s <SERVER>[/<servertype>][:<user>]]
17 [-f <auth_file>]
18
19 -b update the list of databases for server <SERVER> by adding
20 database <db>, which can be a comma delimited list
21 -s update/add user authentication for server <SERVER>
22 if <user> is not provided, the current Unix login name
23 is assumed
24 -f <auth_file> is used for update instead of
25 ~/.db_pass
26 };
27
28
29 getopts('b:s:f:') || die $usage;
30
31 my $d_server; #default Sybase server to use
32 my $auth_section='Authentication';
33 my $d_user=$ENV{'USER'};
34 my $homedir=$ENV{'HOME'};
35 my $authfile=$Getopt::Std::opt_f || $homedir.'/.db_pass';
36 my $srv_op=$Getopt::Std::opt_b; #server/db operation request
37 my $auth_op=$Getopt::Std::opt_s;#authentication operation request
38 unless ($auth_op || $srv_op) {
39 print STDERR $usage;
40 exit(1);
41 }
42 my $fcreate;
43 unless (-e $authfile) {
44 print STDERR "Cannot locate authentication file ($authfile)\n. Creating new file.";
45 $fcreate=1;
46 }
47 #=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=
48 my $s_body;
49 my $section;
50 my (%sections, @sorder);
51 unless ($fcreate) {
52 open (AUTHFILE, '<'.$authfile) || die "Cannot open file $authfile. Please create it first.\n";
53 $/="\n"; #Read one line at once, whole file
54 while (<AUTHFILE>) {
55 s/\s+$//;
56 next unless $_;
57 if (m/^\s*\[\s*(\S+)\s*\]/) {
58 $sections{$section}.=$s_body if $section;
59 $section=$1;
60 push(@sorder, $section) unless $section eq 'Authentication';
61 $s_body='';
62 }
63 else { #not a [section] title line
64 if ($section eq 'Default' && !$d_server) {
65 my $line=$_;
66 $line=~tr/ //d;
67 my ($var, $value)=split(/=/);
68 $var=lc($var);
69 $d_server=$value if ($var eq 'server');
70 }
71 $s_body.=$_."\n";
72 }
73 }
74 $sections{$section}.=$s_body if $section;
75 close AUTHFILE;
76 }
77 #=======================
78 my $msgbuf;
79 my $do_update=1;
80 if ($srv_op) { #database/server addition requested
81 my ($mydb,$mysrv);
82 if ($srv_op =~ m/^([\w\,]+)@(\w+)$/) {
83 ($mydb, $mysrv)=($1,$2);
84 }
85 else {
86 die "Error: no server given and no default server was found.\n"
87 unless $d_server;
88 ($mydb, $mysrv)=($srv_op, $d_server);
89 unless (&confirm("[Confirm:] Sure you want to add $mydb to server '$mysrv'?")) {
90 print STDERR "Operation cancelled\n";
91 exit(1);
92 }
93 }
94 local $_=0;
95 my @dbs=split(/,/, $mydb);
96 foreach my $mdb (@dbs) {
97 if ($_=$sections{$mysrv}) { #server entry already there
98 if (m/\b$mdb\b/m) { # db already there
99 print "Database $mdb already declared for server $mysrv\n";
100 $do_update=0;
101 }
102 else { #add the db to this server's list (last line)
103 my ($lastline)=(m/\n?(.*?)\Z/m);
104 my $repl=(length($lastline)+length($mdb)<78) ? ($lastline.', '.$mdb."\n") : ($lastline."\n".$mdb."\n");
105 s/$lastline\n/$repl/m;
106 $sections{$mysrv}=$_;
107 $msgbuf.="Database $mdb added to server $mysrv\n";
108 }
109 }
110 else {
111 $sections{$mysrv}=$mdb."\n";
112 push(@sorder, $mysrv);
113 $msgbuf.="Database $mdb added to server $mysrv\n";
114 }
115 }
116 }
117
118 if ($auth_op) { #authentication update requested:
119 $do_update=1;
120 my ($myuser, $mysrv);
121 if ($auth_op =~ m/^([\w\/]+):(\w+)$/) {
122 ($mysrv, $myuser)=($1,$2);
123 }
124 else { #user not provided, assuming default
125 ($mysrv, $myuser)=($auth_op, $d_user);
126 }
127 TYPEAGAIN:
128 my $pass=&ask_pass("Enter password for user '$myuser' on server '$mysrv': ");
129 my ($srv, $stype)=split(/\//, $mysrv);
130 my $cryptpass = scrypt($pass, $srv);
131 my $passck=&ask_pass("Please confirm this password by typing it again: ");
132 if ($pass ne $passck) {
133 print STDERR "No matching passwords. Please try again.\n";
134 goto TYPEAGAIN;
135 }
136 local $_=0;
137 if ($_=$sections{$auth_section}) { #Authentication section is there
138 #delete the line containing the given server and user, if any
139 s/[ \t]*$mysrv\s*\:\s*$myuser\/\S+\s*//m;
140 #add the new entry to the remaining
141 $_.="\n" unless m/\n\Z/m;
142 $_.="$mysrv:$myuser/$cryptpass\n";
143 $sections{$auth_section}=$_;
144 }
145 else {
146 $sections{$auth_section}="$mysrv:$myuser/$cryptpass\n";
147 }
148 $msgbuf.="Updated user '$myuser' credentials for server '$mysrv'.\n";
149 #
150 }
151
152
153 #=#=#=#=#= write to the file:
154 #first, the default server:
155 unless ($do_update) {
156 print "No update needed.\n";
157 exit(0)
158 }
159 rename($authfile, $authfile.'..tmp');
160 unless (open(WFILE, '>'.$authfile)) {
161 print STDERR "Error: cannot open file $authfile for write\nFile was not updated.\n";
162 rename($authfile.'..tmp', $authfile);
163 exit(1);
164 }
165 #if (my $dbs=$sections{$d_server}) {
166 # print WFILE "[$d_server]\n$dbs";
167 # delete $sections{$d_server};
168 # }
169 my $auth_data=$sections{$auth_section};
170 delete $sections{$auth_section};
171 #scalar(keys(%sections)); #reset iterator
172 foreach my $section (@sorder) {
173 my $sect_data=$sections{$section};
174 next unless $sect_data;
175 print WFILE "[$section]\n$sect_data\n";
176 }
177 #finally, add the authentication data:
178 print WFILE "[$auth_section]\n$auth_data\n";
179 close WFILE;
180 unlink($authfile.'..tmp');
181 print $msgbuf;
182
183 #==================================================
184 sub prn {
185 print STDERR join("\n",@_),"\n";
186 }

Properties

Name Value
svn:executable *