Debian Codegraph @

myproxy-admin-addservice

raw
1 #!/usr/bin/perl
2
3 # myproxy admin adduser/addservice script
4 # generates a certificate request using grid-cert-request
5 # signs the request using grid-ca-sign
6 # and stores the credential in the repository using
7 # myproxy-admin-load-credential
8
9 use File::Temp qw(tempdir);
10 use File::Basename;
11 use IPC::Open3;
12
13 # check for program dependencies
14
15 chomp($grid_cert_request = `which grid-cert-request 2>/dev/null`);
16 if (!(-x $grid_cert_request)) {
17 print STDERR "Error: grid-cert-request not found.\n";
18 exit 1;
19 }
20 chomp($grid_ca_sign = `which grid-ca-sign 2>/dev/null`);
21 if (!(-x $grid_ca_sign)) {
22 print STDERR "Error: grid-ca-sign not found.\n";
23 print STDERR "Is the Simple CA package installed?\n";
24 exit 1;
25 }
26 chomp($mpalc = `which myproxy-admin-load-credential 2>/dev/null`);
27 if (!(-x $mpalc)) {
28 print STDERR "Error: myproxy-admin-load-credential not found.\n";
29 print STDERR "Is MyProxy installed in $ENV{GLOBUS_LOCATION}?\n";
30 exit 1;
31 }
32 chomp($grid_cert_info = `which grid-cert-info 2>/dev/null`);
33 if (!(-x $grid_cert_info)) {
34 print STDERR "Error: grid-cert-info not found.\n";
35 exit 1;
36 }
37
38 $cmdname = basename($0);
39
40 # handle arguments
41
42 use Getopt::Long;
43
44 our ($opt_h, $opt_u, $opt_v, $opt_c, $opt_p, $opt_s, $opt_l, $opt_t, $opt_a,
45 $opt_A, $opt_x, $opt_X, $opt_r, $opt_R, $opt_n, $opt_d, $opt_k, $opt_K,
46 $opt_Z, $opt_E);
47
48 $result = GetOptions('h', 'u', 'v', 'c=s', 'p=s', 'l=s', 't=i', 'a',
49 'A', 'x', 'X', 'r=s', 'R=s', 'n', 'd', 'k=s', 'K=s', 'Z=s', 'E=s');
50
51 if ((!$result) || $opt_h || $opt_u) {
52 print STDERR <<"EOF";
53 Syntax: $0 [-c cn] [-l username] ...
54 $0 [-usage|-help]
55 Options
56 -h Displays usage
57 -u
58 -v Display debugging messages
59 -c <cn> Common Name for new credential
60 -s <directory> Credential storage directory
61 -l <username> Credential username
62 -t <hours> Max. lifetime of delegated proxies
63 -p <CA password> Set CA private key password using
64 openssl format (see the PASS PHRASE ARGUMENTS
65 section in the openssl(1) man page)
66 -a Allow credentials to be retrieved with
67 just username/passphrase
68 -A Allow credentials to be renewed by
69 any client (not recommended)
70 -x Set regular expression matching mode
71 for following policy options
72 -X Set CN matching mode (default)
73 for following policy options
74 -r <dn> Allow specified entity to retrieve credential
75 -R <dn> Allow specified entity to renew credential
76 -Z <dn> Allow specified entity to retrieve credential
77 w/o passphrase
78 -E <dn> Allow specified entity to retrieve credential key
79 -n Disable passphrase authentication
80 -d Use the proxy certificate subject as username
81 -k <name> Specifies credential name
82 -K <desc> Specifies credential description
83 EOF
84 exit(1);
85 }
86
87 #grid-cert-request
88
89 if (!$opt_c) {
90 print "Enter common name for the certificate: ";
91 chop($opt_c = <STDIN>);
92 }
93
94 $tmp_dir_name = tempdir("myproxy_adduser_XXXXXX", TMPDIR => 1, CLEANUP => 1);
95 $prefix="myproxy_adduser_";
96 $certfile = "${tmp_dir_name}/${prefix}cert.pem";
97 $reqfile = "${tmp_dir_name}/${prefix}cert_request.pem";
98 $keyfile = "${tmp_dir_name}/${prefix}key.pem";
99
100 if ($opt_v) {
101 print "temporary directory is: $tmp_dir_name\n";
102 }
103
104 @args = ("grid-cert-request", "-cn", $opt_c, "-prefix", $prefix, "-dir",
105 $tmp_dir_name, "-force");
106 push(@args, "-nopassphrase") if ($opt_n);
107 &runcmd(@args);
108
109 #grid-ca-sign
110
111 @args = ("grid-ca-sign", "-in", $reqfile, "-out", $certfile, "-force");
112 if ($opt_p) {
113 push(@args, "-passin"); push(@args, $opt_p);
114 }
115
116 if ($opt_v) {
117 print "running command:\n@args\n";
118 }
119
120 if (system(@args)) {
121 print STDERR "grid-ca-sign failed.\n";
122 &cleanup();
123 exit 1;
124 }
125
126 #myproxy-alcf
127
128 if (!$opt_l && !$opt_d) {
129 print "Enter username [use DN by default]: ";
130 chop ($opt_l = <STDIN>);
131 if (length $opt_l == 0) {
132 $opt_d = 1;
133 }
134 }
135
136 @args = ("myproxy-admin-load-credential", "-c", $certfile, "-y", $keyfile);
137 if ($opt_s) {
138 push(@args, "-s"); push(@args, $opt_s);
139 }
140 if ($opt_l) {
141 push(@args, "-l"); push(@args, $opt_l);
142 }
143 if ($opt_t) {
144 push(@args, "-t"); push(@args, $opt_t);
145 }
146 push(@args, "-a") if ($opt_a);
147 push(@args, "-A") if ($opt_A);
148 push(@args, "-x") if ($opt_x);
149 push(@args, "-X") if ($opt_X);
150 if ($opt_r) {
151 push(@args, "-r"); push(@args, $opt_r);
152 }
153 if ($opt_R) {
154 push(@args, "-R"); push(@args, $opt_R);
155 }
156 if ($opt_Z) {
157 push(@args, "-Z"); push(@args, $opt_Z);
158 }
159 if ($opt_E) {
160 push(@args, "-E"); push(@args, $opt_E);
161 } elsif ($cmdname eq "myproxy-admin-addservice") {
162 push(@args, "-x"); push(@args, "-E"); push(@args, "\*");
163 }
164 push(@args, "-d") if ($opt_d);
165 if ($opt_k) {
166 push(@args, "-k"); push(@args, $opt_k);
167 } elsif ($opt_c && $cmdname eq "myproxy-admin-addservice") {
168 push(@args, "-k"); push(@args, $opt_c);
169 }
170 if ($opt_K) {
171 push(@args, "-K"); push(@args, $opt_K);
172 }
173
174 if ($opt_v) {
175 print "running command:\n@args\n";
176 }
177
178 if (system(@args)) {
179 print STDERR "myproxy-admin-load-credential failed.\n";
180 &cleanup();
181 exit 1;
182 }
183
184 print "Certificate subject is:\n";
185 @args = ("grid-cert-info", "-subject", "-file", $certfile);
186 system(@args);
187
188 &cleanup();
189 exit 0;
190
191 sub cleanup {
192 unlink($certfile) if (defined($certfile));
193 unlink($reqfile) if (defined($reqfile));
194 if (defined($keyfile)) {
195 &wipefile($keyfile);
196 unlink($keyfile);
197 }
198 # temporary directory is automatically removed by File::Temp
199 }
200
201 sub wipefile {
202 local($filename) = @_;
203 $size = (stat($filename))[7];
204 return if (!defined($size) || !$size);
205 return if (!open(WIPEFILE, '>', $filename));
206 for ($i = 0; $i < $size; $i++) {
207 print WIPEFILE "\0";
208 }
209 close(WIPEFILE);
210 }
211
212 sub runcmd {
213 @args = @_;
214 if ($opt_v) {
215 print "running command:\n@args\n";
216 }
217 $pid = open3(*Writer, *Reader, '', @args);
218 close(Writer);
219 @output = <Reader>;
220 $output = join('', @output);
221 close(Reader);
222 waitpid($pid, 0);
223 if ($?) {
224 print STDERR $args[0], " failed:\n";
225 print STDERR $output;
226 exit 1;
227 } elsif ($opt_v) {
228 print "command output:\n$output\n";
229 }
230 }