5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
23 # ident "%Z%%M% %I% %E% SMI"
25 # Copyright 2007 Sun Microsystems, Inc. All rights reserved.
26 # Use is subject to license terms.
29 # signit [-q] [-i dir][-o dir] [-l user]
31 # Client program for use with code signing server.
32 # Reads a list of signing credential names and file pathnames
33 # from standard input. Each file is read from the input directory,
34 # sent to the signing server, signed with the specified credential,
35 # and written to the output directory.
38 # -q quiet operation: avoid printing files successfully signed
39 # -i dir input directory (defaults to current dir)
40 # -o dir output directory (defautls to input dir)
41 # -l user user account on signing server (defaults to current user)
43 # The CODESIGN_SERVER environment variable can be used to
44 # specify the hostname or IP address of the signing server
45 # (defaults to quill.sfbay).
49 use File
::Temp
'tempdir';
56 my ($Indir, $Outdir); # Input and output directories (may be the same)
57 my $Server; # Signing server hostname
58 my $Quiet; # Suppress printing each file successfully signed
59 my ($pid); # Process id for ssh client
60 my @cred_rules; # Array of path prefixes and credentials to use
61 my $Tmpdir = tempdir
(CLEANUP
=> 1); # Temporary directory
62 my $Warnings = 0; # Count of warnings returned
69 $Server = $ENV{CODESIGN_SERVER
} || "quill.sfbay";
71 # Get command-line arguments
72 our($opt_c, $opt_i, $opt_o, $opt_l, $opt_q);
73 if (!getopts
("i:o:c:l:q")) {
74 die "Usage: $0 [-i dir] [-o dir] [-l user]\n";
78 # Get input/output directories
79 $Indir = $opt_i || getcwd
(); # default to current dir
80 $Outdir = $opt_o || $Indir; # default to input dir
81 $Indir = getcwd
() . "/$Indir" if (substr($Indir, 0, 1) ne "/");
82 $Outdir = getcwd
() . "/$Outdir" if (substr($Outdir, 0, 1) ne "/");
84 # Ignore SIGPIPE to allow proper error messages
85 $SIG{PIPE
} = 'IGNORE';
87 # Create ssh connection to server
89 if (defined($opt_l)) {
90 push @args, "-l", $opt_l;
92 push @args, "-s", $Server, "codesign";
93 $pid = open2
(*SRV_OUT
, *SRV_IN
, "/usr/bin/ssh", @args) or
94 die "ERROR Connection to server $Server failed\n";
95 select(SRV_IN
); $| = 1; select(STDOUT
); # unbuffered writes
97 # Sign each file with the specified credential
100 my ($cred, $path) = split;
102 sign_file
($cred, $path);
109 # Clean up after normal or abnormal exit.
117 waitpid($pid, 0) if ($pid);
119 print STDERR
"ERROR Connection to server $Server failed\n";
122 $?
= $old_status if ($?
== 0);
128 # Print debug message to standard error.
131 print STDERR
"### @_";
135 # check_response(str)
137 # Validate response from server. Print messages for warnings or errors,
138 # and exit in the case of an error. If the response indicates a successful
139 # signing operation, return the size of the output data.
144 if ($str =~ /^OK SIGN (\d+)/) {
147 elsif ($str =~ /^OK/) {
150 elsif ($str =~ /^WARNING/) {
155 elsif ($str =~ /^ERROR/) {
160 printf STDERR
"ERROR Protocol failure (%d)\n", length($str);
166 # sign_file(credential, filename)
168 # Send the file to the server for signing. Package the file into a
169 # ZIP archive, send to the server, and extract the ZIP archive that
170 # is returned. The input ZIP archive always contains a single file,
171 # but the returned archive may contain one or more files.
174 my ($cred, $path) = @_;
177 $path =~ s
:^\
./::g; # remove leading "./"
178 unlink("$Tmpdir/in.zip
");
179 system("cd
$Indir; /usr/bin
/zip -q $Tmpdir/in
.zip
$path");
181 sendfile("$Tmpdir/in.zip
", "$cred $path") || return;
184 $size = check_response($res);
186 recvfile("$Tmpdir/out
.zip
", $size) || return;
188 if (system("cd
$Outdir; /usr/bin
/unzip -qo $Tmpdir/out
.zip
")) {
191 print "$cred\t$path\n" unless $Quiet;
197 # sendfile(file, args)
199 # Send a ZIP archive file to the signing server. This involves
200 # sending a SIGN command with the given arguments, followed by
201 # the contents of the archive itself.
204 my ($file, $args) = @_;
208 print SRV_IN "SIGN
$size $args\n";
209 if (!open(F, "<$file")) {
210 print STDERR "$file: $!\n";
213 read(F, $bytes, $size);
215 if (!syswrite(SRV_IN, $bytes, $size)) {
216 print STDERR "Can
't send to server: $!\n";
223 # recvfile(file, size)
225 # Receive a ZIP archive from the signing server. The caller
226 # provides the size argument previously obtained from the
230 my ($file, $size) = @_;
233 if (!read(SRV_OUT, $bytes, $size)) {
234 print STDERR "Can't
read from server
: $!\n";
237 if (!open(F, ">$file")) {
238 print STDERR "$file: $!\n";
241 syswrite(F, $bytes, $size);