~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/dist/util/sgeCA/sge_ca

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
#
 
3
# Grid Engine CA framework script
 
4
#
 
5
#___INFO__MARK_BEGIN__
 
6
##########################################################################
 
7
#
 
8
#  The Contents of this file are made available subject to the terms of
 
9
#  the Sun Industry Standards Source License Version 1.2
 
10
#
 
11
#  Sun Microsystems Inc., March, 2001
 
12
#
 
13
#
 
14
#  Sun Industry Standards Source License Version 1.2
 
15
#  =================================================
 
16
#  The contents of this file are subject to the Sun Industry Standards
 
17
#  Source License Version 1.2 (the "License"); You may not use this file
 
18
#  except in compliance with the License. You may obtain a copy of the
 
19
#  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
20
#
 
21
#  Software provided under this License is provided on an "AS IS" basis,
 
22
#  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
23
#  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
24
#  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
25
#  See the License for the specific provisions governing your rights and
 
26
#  obligations concerning the Software.
 
27
#
 
28
#  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
29
#
 
30
#  Copyright: 2001 by Sun Microsystems, Inc.
 
31
#
 
32
#  All Rights Reserved.
 
33
#
 
34
##########################################################################
 
35
#___INFO__MARK_END__
 
36
#
 
37
# set -x
 
38
 
 
39
# Reset PATH to a safe value  
 
40
#
 
41
PATH=/bin:/usr/bin:/usr/sbin:/usr/bsd:/usr/ucb
 
42
 
 
43
# Easy way to prevent clearing of screen
 
44
#
 
45
CLEAR=clear
 
46
#CLEAR=:
 
47
 
 
48
 
 
49
#-------------------------------------------------------------------------
 
50
# USEFUL LOCAL SHELL PROCEDURES
 
51
 
 
52
#-------------------------------------------------------------------------
 
53
# PrintError
 
54
PrintError() 
 
55
{
 
56
   fmt="Error: $1"
 
57
   shift
 
58
   $INFOTEXT -e "$fmt" $*
 
59
   $INFOTEXT -log "$fmt" $*
 
60
}
 
61
 
 
62
PrintErrorAndExit()
 
63
{
 
64
   exitcode=$1
 
65
   shift
 
66
   fmt="$1"
 
67
   shift
 
68
   PrintError "$fmt" $*
 
69
   exit $exitcode
 
70
}
 
71
 
 
72
#-------------------------------------------------------------------------
 
73
# ErrUsage: print usage string, exit
 
74
#
 
75
ErrUsage()
 
76
{
 
77
   if [ $# -gt 0 ]; then
 
78
      PrintError $*
 
79
   fi
 
80
   myname=`basename $0`
 
81
   $INFOTEXT -e \
 
82
   "\nUsage: %s <command>\n" \
 
83
   "   -adminuser <user>   set admin user\n" \
 
84
   "   -init               initialize CA infrastructure\n" \
 
85
   "   -req                generate a certificate request and private key\n" \
 
86
   "   -sign               sign a certificate request\n" \
 
87
   "   -copy               install user certificate and private key\n" \
 
88
   "   -verify <cert>      verify a <cert>\n" \
 
89
   "   -print <cert>       print a <cert>\n" \
 
90
   "   -printkey <key>     print a <key>\n" \
 
91
   "   -printcrl <crl>     print a <crl>\n" \
 
92
   "   -renew <user>       extend the certificate of <user>\n" \
 
93
   "   -renew_ca           extend the CA certificate\n" \
 
94
   "   -renew_sys          extend the daemon certificate\n" \
 
95
   "   -renew_sdm <g>      renew certificate of a SDM daemon with\n" \
 
96
   "                       g=Common Name of the SDM daemon\n" \
 
97
   "   -days <days>        days of validity of the certificate\n" \
 
98
   "   -sha1               use sha-1 instead of md5 as message digest\n" \
 
99
   "   -encryptkey         use des to encrypt the generated key with a passphrase\n" \
 
100
   "   -outdir <dir>       write to directory <dir>\n" \
 
101
   "   -cahost <host>      define CA hostname (CA master host)\n" \
 
102
   "   -cadir <dir>        define CALOCALTOP and CATOP settings\n" \
 
103
   "   -calocaltop <dir>   define CALOCALTOP setting\n" \
 
104
   "   -catop <dir>        define CATOP setting\n" \
 
105
   "   -pkcs12 <user>      generate pkcs12 format file for user <user> \n" \
 
106
   "   -pkcs12pwf <file>   pkcs12 password file\n" \
 
107
   "   -pkcs12dir <dir>    pkcs12 output directory\n" \
 
108
   "   -usercert <file>    generate certificates and keys for the users in <file>\n" \
 
109
   "   -userks             generate keystore for existing users\n" \
 
110
   "   -user <u:g:e>       generate certificates and keys for <u:g:e>\n" \
 
111
   "                       with u=Unix User, g=Common Name, e=email\n" \
 
112
   "   -sdm_daemon <u:g:e> generate certificate and key for a SDM daemon\n" \
 
113
   "                       with u=Unix User, g=Common Name, e=email\n" \
 
114
   "   -sdm_pkcs12 <g>     generate pkcs12 format file for SDM daemon\n" \
 
115
   "                       with g=Common Name of the SDM daemon\n" \
 
116
   "   -sys_pkcs12 <g>     generate pkcs12 format file for SGE daemon\n" \
 
117
   "                       with g=Common Name of the SGE daemon\n" \
 
118
   "   -ks <user>          generate a keystore file for <user>\n" \
 
119
   "   -kspwf <file>       keystore pw file\n" \
 
120
   "   -ksout <file>       keystore output file\n" \
 
121
   "   -sysks              generate keystore for SGE daemon\n" \
 
122
   "   -showCaTop          echo caTop path\n" \
 
123
   "   -showCaLocalTop     echo caLocalTop path\n" \
 
124
   $myname
 
125
 
 
126
   exit 1
 
127
}
 
128
 
 
129
#-------------------------------------------------------------------------
 
130
# Enter: input is read and returned to stdout. If input is empty echo $1
 
131
#
 
132
# USES: variable "$autoinst" 
 
133
#
 
134
Enter()
 
135
{
 
136
   if [ "$autoinst" = true ]; then
 
137
      $INFOTEXT $1
 
138
   else
 
139
      read INP
 
140
      if [ "$INP" = "" ]; then
 
141
         $INFOTEXT $1
 
142
      else
 
143
         $INFOTEXT $INP
 
144
      fi
 
145
   fi
 
146
}
 
147
 
 
148
 
 
149
#-------------------------------------------------------------------------
 
150
# Execute command as user $ADMINUSER and exit if exit status != 0
 
151
# if ADMINUSER = default then execute command unchanged
 
152
#
 
153
# uses binary "adminrun" form SGE distribution
 
154
#
 
155
# USES: variables "$verbose"    (if set to "true" print arguments)
 
156
#                  $ADMINUSER   (if set to "default" do not use "adminrun)
 
157
#                 "$V5UTILBIN"  (path to the binary in utilbin)
 
158
#
 
159
ExecuteAsAdmin()
 
160
{
 
161
   if [ "$verbose" = true ]; then
 
162
      $ECHO $*
 
163
   fi
 
164
 
 
165
   if [ $ADMINUSER = default ]; then
 
166
      $*
 
167
   else
 
168
      $V5UTILBIN/adminrun $ADMINUSER "$@"
 
169
   fi
 
170
 
 
171
   return $?
 
172
}
 
173
 
 
174
#-------------------------------------------------------------------------
 
175
# Execute command and return exit status
 
176
#
 
177
Execute()
 
178
{
 
179
   if [ "$verbose" = true ]; then
 
180
      $ECHO $*
 
181
   fi
 
182
   $*
 
183
   return $?
 
184
}
 
185
 
 
186
ExecRm() {
 
187
  Execute $RM -rf $* > /dev/null 2>&1
 
188
  files=""
 
189
  for i in $*; do
 
190
     if [ -f $i -o -d $i ]; then
 
191
        files="$files $i" 
 
192
     fi 
 
193
  done
 
194
  if [ "$files" != "" ]; then
 
195
    sleep 1
 
196
    for i in $files; do
 
197
       Execute $RM -rf $i  > /dev/null 2>&1
 
198
       if [ $? -ne 0 ]; then
 
199
          return 1
 
200
       fi
 
201
    done
 
202
  fi
 
203
  return 0
 
204
}
 
205
 
 
206
ExecRmAsAdmin() {
 
207
  ExecuteAsAdmin $RM -rf $*  > /dev/null 2>&1
 
208
  files=""
 
209
  for i in $*; do
 
210
     if [ -f $i -o -d $i ]; then
 
211
        files="$files $i" 
 
212
     fi 
 
213
  done
 
214
  if [ "$files" != "" ]; then
 
215
    sleep 1
 
216
    for i in $files; do
 
217
       ExecuteAsAdmin $RM -rf $i  > /dev/null 2>&1
 
218
       if [ $? -ne 0 ]; then
 
219
          return 1
 
220
       fi
 
221
    done
 
222
  fi
 
223
  return 0
 
224
}
 
225
 
 
226
#-------------------------------------------------------------------------
 
227
# Change the ownership of a file or directory
 
228
# Is only executed as root
 
229
#
 
230
ExecChown()
 
231
{
 
232
   if [ $rootinstalls = true ]; then
 
233
      Execute $CHOWN $*
 
234
      return $?
 
235
   else
 
236
      return 0
 
237
   fi
 
238
   
 
239
}
 
240
 
 
241
#--------------------------------------------------------------------------
 
242
# InitCA Init CA directories and get DN info
 
243
#
 
244
InitCA()
 
245
{
 
246
   $CLEAR
 
247
   $INFOTEXT -u "\nInitializing Certificate Authority (CA) for OpenSSL security framework"
 
248
 
 
249
   if [ -d $CATOP -a -d $CALOCALTOP ]; then
 
250
      $INFOTEXT -e "\nThere are already directories of the CA infrastructure in\n   %s\n   or\n   %s\n" "$CATOP" "$CALOCALTOP"
 
251
      $INFOTEXT -auto $AUTO -ask "y" "n" -def "y" -n \
 
252
         "Do you want to recreate your SGE CA infrastructure (y/n) [y] >> "
 
253
      if [ $? != 0 ]; then
 
254
         $INFOTEXT "We will not reinitialize your SGE CA infrastructure."
 
255
         exit 0
 
256
      fi
 
257
   fi
 
258
 
 
259
   MakeCADirs
 
260
   if [ $? != 0 ]; then
 
261
      PrintErrorAndExit 1 "CA initialization failed. Exit."
 
262
   fi
 
263
   MakeCAcert
 
264
   if [ $? -ne 0 ]; then
 
265
      PrintErrorAndExit 1 "CA initialization failed. Exit."
 
266
   fi
 
267
 
 
268
   MakeCert daemon $ME "SGE Daemon" none
 
269
   if [ $? -ne 0 ]; then
 
270
      PrintErrorAndExit 1 "CA initialization failed. Exit."
 
271
   fi
 
272
   
 
273
   MakeCert user $ME "SGE install user" none
 
274
   if [ $? -ne 0 ]; then
 
275
      PrintErrorAndExit 1 "CA initialization failed. Exit."
 
276
   fi
 
277
 
 
278
   if [ "$ADMINUSER" != default ]; then
 
279
      MakeCert user $ADMINUSER "SGE admin user" none
 
280
      if [ $? -ne 0 ]; then
 
281
         PrintErrorAndExit 1 "CA initialization failed. Exit."
 
282
      fi
 
283
   fi
 
284
}
 
285
 
 
286
#---------------------------------------------------------------------------
 
287
#  create a certificate request
 
288
#
 
289
RequestCert()
 
290
{
 
291
    # create a certificate request
 
292
    $REQ -new -keyout $outdir/$newkey -out $outdir/$newreq $DAYS
 
293
    RET=$?
 
294
    if [ $RET = 0 ]; then
 
295
      $INFOTEXT "Request is in %s" $outdir/$newreq
 
296
      $INFOTEXT "Private key is in %s" $outdir/$newkey
 
297
    else
 
298
      PrintErrorAndExit 1 "Creating a certificate request failed"
 
299
    fi
 
300
}
 
301
 
 
302
#---------------------------------------------------------------------------
 
303
#  Sign a certificate
 
304
#
 
305
SignCert()
 
306
{
 
307
    $CA -config $SGE_ROOT/util/sgeCA/sge_ssl.cnf -policy policy_anything $DAYS $BATCHMODE -notext -out $outdir/$newcert -infiles $indir/$newreq
 
308
    RET=$?
 
309
    if [ $RET = 0 ]; then
 
310
      $INFOTEXT "Signed certificate is in %s" $outdir/$newcert
 
311
    else
 
312
      PrintErrorAndExit 1 "Signing a certificate failed"
 
313
    fi
 
314
}
 
315
 
 
316
#---------------------------------------------------------------------------
 
317
#  verify a certificate
 
318
#
 
319
VerifyCert() 
 
320
{
 
321
   $VERIFY -CAfile $CATOP/$CACERT $newcert
 
322
   if [ $? != 0 ]; then
 
323
      PrintErrorAndExit 1 "Verification of certificate failed"
 
324
   fi
 
325
}
 
326
 
 
327
PrintX509() 
 
328
{
 
329
   if [ $1 = "cert" ]; then
 
330
      $X509 -in $2 -text
 
331
   elif [ $1 = "key" ]; then
 
332
      $X509KEY -in $newcert -text
 
333
   elif [ $1 = "crl" ]; then
 
334
      $X509CRL -in $newcert -text
 
335
   else
 
336
      PrintErrorAndExit 1 "Can not print %s" $1
 
337
   fi
 
338
   
 
339
   if [ $? != 0 ]; then
 
340
      PrintErrorAndExit 1 "Printing %s failed (%s)" $1 $2
 
341
   fi
 
342
}
 
343
 
 
344
 
 
345
#---------------------------------------------------------------------------
 
346
# CheckIfCaHost
 
347
#    If our hostname given in $1 is the same as "CAHOST"
 
348
#    echo "true" else echo "false"
 
349
#
 
350
# $1 = hostname
 
351
#
 
352
CheckIfCaHost()
 
353
{
 
354
   host=$1
 
355
 
 
356
   if [ "$host" = "$CAHOST" ]; then
 
357
      echo true
 
358
   else
 
359
      echo false
 
360
   fi
 
361
}
 
362
 
 
363
#--------------------------------------------------------------------------
 
364
# InstallKey
 
365
#
 
366
# $1 = user | daemon
 
367
# $2 = $ME ( username )
 
368
#
 
369
InstallKey()
 
370
{
 
371
   HOST=`$V5UTILBIN/gethostname -aname`
 
372
   result=`CheckIfCaHost $HOST`
 
373
   if [ "$result" != "true" ]; then 
 
374
      PrintError "You can install your private key and certificate only on the master host."
 
375
      return 1
 
376
   fi 
 
377
 
 
378
   if [ -d $CALOCALTOP/userkeys ]; then
 
379
      userkeydir=$CALOCALTOP/userkeys
 
380
   else
 
381
      PrintError "Can not find local userkey directory."
 
382
      return 1
 
383
   fi   
 
384
 
 
385
   if [ $1 = daemon ]; then
 
386
      keyfile=$CALOCALTOP/private/key.pem
 
387
      certfile=$CATOP/certs/cert.pem
 
388
      randfile=$CALOCALTOP/private/rand.seed
 
389
   else
 
390
      keyfile=$userkeydir/$ME/key.pem
 
391
      certfile=$userkeydir/$ME/cert.pem
 
392
      randfile=$userkeydir/$ME/rand.seed
 
393
      HOMEDIR=$HOME
 
394
   fi
 
395
 
 
396
   basedir=$CAHOMEKEYDIR
 
397
   if [ ! -d $basedir ]; then
 
398
      $V5UTILBIN/adminrun $2 $MKDIR -p $basedir
 
399
   fi
 
400
 
 
401
   if [ ! -d $basedir/certs ]; then
 
402
      $V5UTILBIN/adminrun $2 $MKDIR $basedir/certs
 
403
   fi
 
404
 
 
405
   if [ ! -d $basedir/private ]; then
 
406
      $V5UTILBIN/adminrun $2 $MKDIR $basedir/private
 
407
   fi
 
408
 
 
409
   if [ -f $basedir/private/key.pem ]; then
 
410
      $V5UTILBIN/adminrun $2 rm -f $basedir/private/key.pem
 
411
   fi
 
412
 
 
413
   if [ -f $basedir/private/rand.seed ]; then
 
414
      $V5UTILBIN/adminrun $2 rm -f $basedir/private/rand.seed
 
415
   fi
 
416
 
 
417
   if [ -f $basedir/certs/cert.pem ]; then
 
418
      $V5UTILBIN/adminrun $2 rm -f $basedir/certs/cert.pem
 
419
   fi
 
420
 
 
421
   $V5UTILBIN/adminrun $2 $CP $keyfile $basedir/private
 
422
   if [ $? -ne 0 ]; then
 
423
      PrintError "Could not copy key file (%s -> %s)" $keyfile $basedir/private
 
424
      return 1
 
425
   fi
 
426
   $V5UTILBIN/adminrun $2 $CP $randfile $basedir/private
 
427
   if [ $? -ne 0 ]; then
 
428
      PrintError "Could not copy rand file (%s -> %s)" $randfile $basedir/private
 
429
      return 1
 
430
   fi
 
431
   
 
432
   $V5UTILBIN/adminrun $2 $CHMOD 700 $basedir/private
 
433
   if [ $? -ne 0 ]; then
 
434
      PrintError "chmod for %s on private dir failed (%s)" $2 $basedir/private
 
435
      return 1
 
436
   fi
 
437
   
 
438
   $V5UTILBIN/adminrun $2 $CHMOD 600 $basedir/private/*
 
439
   if [ $? -ne 0 ]; then
 
440
      PrintError "chmod for %s on private files failed (%s/\*)" $2 $basedir/private
 
441
      return 1
 
442
   fi
 
443
 
 
444
   $V5UTILBIN/adminrun $2 $CP $certfile $basedir/certs
 
445
   if [ $? -ne 0 ]; then
 
446
      PrintError "Could not copy cert file (%s -> %s)" $certfile $basedir/certs
 
447
      return 1
 
448
   fi
 
449
   
 
450
   $V5UTILBIN/adminrun $2 $CHMOD 755 $basedir/certs
 
451
   if [ $? -ne 0 ]; then
 
452
      PrintError "chmod for %s on certs dir failed (%s)" $2 $basedir/certs
 
453
      return 1
 
454
   fi
 
455
   
 
456
   $V5UTILBIN/adminrun $2 $CHMOD 644 $basedir/certs/*
 
457
   if [ $? -ne 0 ]; then
 
458
      PrintError "chmod for %s on cert files failed (%s/\*)" $2 $basedir/certs
 
459
      return 1
 
460
   fi
 
461
 
 
462
   $INFOTEXT "Certificate and private key for user %s have been installed" $ME
 
463
 
 
464
   return 0
 
465
}
 
466
 
 
467
 
 
468
#--------------------------------------------------------------------------
 
469
# MakeCAcert create CA certificate and private key
 
470
#
 
471
#
 
472
MakeCAcert()
 
473
{
 
474
   $INFOTEXT -u "\nCreating CA certificate and private key"
 
475
   $INFOTEXT -e "Please give some basic parameters to create the distinguished name (DN)\n" \
 
476
   "for the certificates.\n\n" \
 
477
   "We will ask for\n" \
 
478
   "   - the two letter country code\n" \
 
479
   "   - the state\n" \
 
480
   "   - the location, e.g city or your buildingcode\n" \
 
481
   "   - the organization (e.g. your company name)\n" \
 
482
   "   - the organizational unit, e.g. your department\n" \
 
483
   "   - the email address of the CA administrator (you!)\n"
 
484
 
 
485
   $INFOTEXT -wait -auto $AUTO -n "\nHit <RETURN> to continue >> "
 
486
   # $CLEAR
 
487
 
 
488
   done=false
 
489
   while [ $done = false ]; do
 
490
 
 
491
      if [ "$AUTO" = "true" ]; then
 
492
            CA_C=`echo $CSP_COUNTRY_CODE | env LC_ALL=C tr "[a-z]" "[A-Z]"`
 
493
            CA_ST="$CSP_STATE"
 
494
            CA_L="$CSP_LOCATION"
 
495
            CA_O="$CSP_ORGA"
 
496
            CA_OU="$CSP_ORGA_UNIT"
 
497
            CA_EMAIL="$CSP_MAIL_ADDRESS"
 
498
      else
 
499
         dndone=false
 
500
         while [ $dndone = false ]; do
 
501
            $INFOTEXT -n "Please enter your two letter country code, e.g. 'US' >> "
 
502
            INP=`Enter ""`
 
503
            if [ "$INP != " -a `echo $INP | wc -c` = 3 ]; then
 
504
               CA_C=`echo $INP | env LC_ALL=C tr "[a-z]" "[A-Z]"`
 
505
               dndone=true
 
506
            fi
 
507
         done
 
508
 
 
509
         dndone=false
 
510
         while [ $dndone = false ]; do
 
511
            $INFOTEXT -n "Please enter your state >> "
 
512
            INP=`Enter ""`
 
513
            CA_ST="$INP"
 
514
            if [ "$INP" != "" ]; then
 
515
               CA_ST="$INP"
 
516
               dndone=true
 
517
            fi
 
518
         done
 
519
 
 
520
         dndone=false
 
521
         while [ $dndone = false ]; do
 
522
            $INFOTEXT -n "Please enter your location, e.g city or buildingcode >> "
 
523
            INP=`Enter ""`
 
524
            CA_L="$INP"
 
525
            if [ "$INP" != "" ]; then
 
526
               CA_L="$INP"
 
527
               dndone=true
 
528
            fi
 
529
         done
 
530
 
 
531
         dndone=false
 
532
         while [ $dndone = false ]; do
 
533
            $INFOTEXT -n "Please enter the name of your organization >> "
 
534
            INP=`Enter ""`   
 
535
            if [ "$INP" != "" ]; then
 
536
               CA_O="$INP"
 
537
               dndone=true
 
538
            fi
 
539
         done
 
540
 
 
541
         dndone=false
 
542
         while [ $dndone = false ]; do
 
543
            $INFOTEXT -n "Please enter your organizational unit, e.g. your department >> "
 
544
            INP=`Enter ""`    
 
545
            CA_OU="$INP"
 
546
            if [ "$INP" != "" ]; then
 
547
               CA_OU="$INP"
 
548
               dndone=true
 
549
            fi
 
550
         done
 
551
 
 
552
         dndone=false
 
553
         while [ $dndone = false ]; do
 
554
            $INFOTEXT -n "Please enter the email address of the CA administrator >> "
 
555
            INP=`Enter ""`   
 
556
            if [ "$INP" != "" ]; then
 
557
               CA_EMAIL="$INP"
 
558
               dndone=true
 
559
            fi
 
560
         done
 
561
      fi
 
562
 
 
563
 
 
564
      $CLEAR
 
565
      $INFOTEXT -e "\nYou selected the following basic data for the distinguished name of\n" \
 
566
      "your certificates:\n\n" \
 
567
      "Country code:         %s=%s\n" \
 
568
      "State:                %s=%s\n" \
 
569
      "Location:             %s=%s\n" \
 
570
      "Organization:         %s=%s\n" \
 
571
      "Organizational unit:  %s=%s\n" \
 
572
      "CA email address:     %s=%s\n" \
 
573
      C "$CA_C" ST "$CA_ST" L "$CA_L" O "$CA_O" OU "$CA_OU" emailAddress "$CA_EMAIL"
 
574
      
 
575
      $INFOTEXT -auto $AUTO -ask "y" "n" -def "y" -n \
 
576
         "Do you want to use these data (y/n) [y] >> "
 
577
      if [ $? = 0 ]; then
 
578
         done=true
 
579
 
 
580
         TMPFILE=/tmp/sge_ca$$.tmp
 
581
         TMPFILE1=/tmp/sge_ca1$$.tmp
 
582
         ExecRm $TMPFILE $TMPFILE1
 
583
         if [ $? -ne 0 ]; then
 
584
            PrintError "Could not delete file %s" $TMPFILE
 
585
            return 1
 
586
         fi
 
587
         
 
588
         ExecRm $TMPFILE1
 
589
         if [ $? -ne 0 ]; then
 
590
            PrintError "Could not delete file %s" $TMPFILE1
 
591
            return 1
 
592
         fi
 
593
         
 
594
         $TOUCH $TMPFILE
 
595
         if [ $? -ne 0 ]; then
 
596
            PrintError "Could not touch file %s" $TMPFILE
 
597
            return 1
 
598
         fi
 
599
         $TOUCH $TMPFILE1
 
600
         if [ $? -ne 0 ]; then
 
601
            PrintError "Could not touch file %s" $TMPFILE1
 
602
            return 1
 
603
         fi
 
604
         
 
605
         echo C="$CA_C" >> $TMPFILE
 
606
         echo ST="$CA_ST" >> $TMPFILE
 
607
         echo L="$CA_L" >> $TMPFILE
 
608
         echo O="$CA_O" >> $TMPFILE
 
609
         echo OU="$CA_OU" >> $TMPFILE
 
610
         ExecuteAsAdmin $CP $TMPFILE $CATOP/dn.info
 
611
         if [ $? -ne 0 ]; then
 
612
            PrintError "Could not copy file (%s -> %s)" $TMPFILE $CATOP/dn.info
 
613
            return 1
 
614
         fi
 
615
         
 
616
         echo CN="SGE Certificate Authority" >> $TMPFILE
 
617
         echo userId=CA >> $TMPFILE
 
618
         echo emailAddress=$CA_EMAIL >> $TMPFILE
 
619
 
 
620
         export CATOP
 
621
         Execute cat $CONFIG_DIR/sge_ssl_template.cnf $TMPFILE > $TMPFILE1
 
622
         if [ $? -ne 0 ]; then
 
623
            PrintError "Could not cat file (%s)" $CONFIG_DIR/sge_ssl_template.cnf
 
624
            return 1
 
625
         fi
 
626
 
 
627
         if [ $ADMINUSER != default -a $rootinstalls = true ]; then
 
628
            # echo +++ $ADMINUSER
 
629
            MakeRandFile $CALOCALTOP/private/rand.seed $ADMINUSER
 
630
         else
 
631
            # echo +++ $ME
 
632
            MakeRandFile $CALOCALTOP/private/rand.seed $ME
 
633
         fi
 
634
         if [ $? -ne 0 ]; then
 
635
            return 1
 
636
         fi
 
637
         RANDFILE=$CALOCALTOP/private/rand.seed; export RANDFILE
 
638
 
 
639
         umask 077
 
640
         $INFOTEXT "Creating CA certificate and private key"
 
641
         ExecuteAsAdmin $REQ -config $TMPFILE1 -new -x509 \
 
642
              -keyout ${CALOCALTOP}/private/$CAKEY -out ${CATOP}/$CACERT $DAYS
 
643
         status=$?
 
644
         umask 022
 
645
 
 
646
         ExecuteAsAdmin $CHMOD 644 ${CATOP}/$CACERT
 
647
         if [ $? -ne 0 ]; then
 
648
            PrintError "chmod as %s failed (%s)" $ADMINUSER ${CATOP}/$CACERT
 
649
            return 1
 
650
         fi
 
651
         rm -f $TMPFILE $TMPFILE1
 
652
 
 
653
         if [ $status != 0 ]; then
 
654
            PrintError "Failed to create CA certificate and private key. Exit"
 
655
            return 1
 
656
         fi
 
657
 
 
658
      else
 
659
         $CLEAR
 
660
      fi
 
661
   done
 
662
 
 
663
   $INFOTEXT -wait -auto $AUTO -n "\nHit <RETURN> to continue >> "
 
664
   $CLEAR
 
665
   return 0
 
666
}
 
667
 
 
668
#--------------------------------------------------------------------------
 
669
# MakeRandFile create a random data file
 
670
#  
 
671
# $1 = <randfilename>
 
672
# $2 = <owner>
 
673
#
 
674
MakeRandFile()
 
675
{
 
676
 
 
677
   # OpenSSL uses /dev/urandom by default so if it's there, don't
 
678
   # polute the PRNG with predictable data.
 
679
   rfile="/dev/urandom"
 
680
   if [ ! -r /dev/urandom ]; then
 
681
      if [ -r /dev/random ]; then 
 
682
         rfile=/dev/random
 
683
      elif [ -r /bin/vi ]; then
 
684
         rfile="/bin/vi"
 
685
      fi
 
686
   fi
 
687
   # $INFOTEXT "Creating RANDFILE from '%s' in '%s'" $rfile $1
 
688
 
 
689
   RANDFILE=/tmp/.rand.$$; export RANDFILE;
 
690
   $V5UTILBIN/adminrun $2 dd if=$rfile of=$RANDFILE count=2048 > /dev/null 2>&1
 
691
   if [ $? -lt 0 ]; then
 
692
       PrintError "Could not create random number for (%s -> %s)" $rfile $RANDFILE
 
693
       return 1
 
694
   fi
 
695
   $V5UTILBIN/adminrun $2 $OPENSSL rand -rand $RANDFILE -out $1 2048  > /dev/null 2>&1
 
696
   if [ $? -ne 0 ]; then
 
697
       PrintError "bootstraping of rand command as user %s failed (%s -> %s)" $2 $RANDFILE $1
 
698
       return 1
 
699
   fi
 
700
   
 
701
   $V5UTILBIN/adminrun $2 $OPENSSL rand -rand $rfile -out $1 2048  > /dev/null 2>&1
 
702
   if [ $? -ne 0 ]; then
 
703
       PrintError "openssl rand command as user %s failed (%s -> %s)" $2 $rfile $1
 
704
       return 1
 
705
   fi
 
706
 
 
707
   $V5UTILBIN/adminrun $2 $RM $RANDFILE > /dev/null 2>&1
 
708
   if [ $? -ne 0 ]; then
 
709
       PrintError "Could not delete file %s as user %s" $RANDFILE $2
 
710
       return 1
 
711
   fi
 
712
   
 
713
   $V5UTILBIN/adminrun $2 $CHMOD 644 $1 > /dev/null 2>&1
 
714
   if [ $? -ne 0 ]; then
 
715
       PrintError "chmod as user %s on file %s failed" $2 $1
 
716
       return 1
 
717
   fi
 
718
   
 
719
   # echo "--------------"
 
720
   # ls -l $rfile $1
 
721
   # echo "--------------"
 
722
 
 
723
}
 
724
 
 
725
 
 
726
#--------------------------------------------------------------------------
 
727
# MakeUserCert create user certificates and keys for Windows Admin user
 
728
#
 
729
# $1 = certificate type "user" or "sdm_daemon"
 
730
# $2 = <user:gecos:email>
 
731
#
 
732
MakeUserCert()
 
733
{
 
734
   cert_type=$1
 
735
   line="$2"
 
736
 
 
737
   unixuser=`echo $line|cut -d: -f1`
 
738
   gecos=`echo $line|cut -d: -f2`
 
739
   email=`echo $line|cut -d: -f3`
 
740
 
 
741
   # echo $unixuser:$gecos:$email
 
742
   if [ "$unixuser" = "" ]; then
 
743
      PrintErrorAndExit 1 "no Unix user specified. Exit."
 
744
   fi   
 
745
 
 
746
   if [ "$gecos" = "" ]; then
 
747
      gecos=$unixuser
 
748
   fi   
 
749
   if [ "$email" = "" ]; then
 
750
      email=$unixuser
 
751
   fi   
 
752
 
 
753
   if [ "$ADMINUSER" = default ]; then
 
754
      entries=`grep "CN=$gecos" $CATOP/index.txt|grep '^V'|wc -l`
 
755
   else 
 
756
      entries=`$V5UTILBIN/adminrun $ADMINUSER grep "CN=$gecos" $CATOP/index.txt|grep '^V'|wc -l`
 
757
   fi
 
758
   if [ $entries = 0 ]; then
 
759
      $INFOTEXT "Generating %s certificate and key for '%s' ('%s','%s')." \
 
760
         "$cert_type" "$unixuser" "$gecos" "$email"
 
761
      MakeCert $cert_type "$unixuser" "$gecos" "$email"
 
762
      if [ $? -ne 0 ]; then
 
763
         return 1
 
764
      fi
 
765
   else
 
766
      PrintError "Please renew %s certificate for '%s' ('%s','%s') instead." \
 
767
                 "$cert_type" "$unixuser" "$gecos" "$email"
 
768
      # RenewCert user $unixuser
 
769
      return 1
 
770
   fi
 
771
   return 0
 
772
}
 
773
 
 
774
#--------------------------------------------------------------------------
 
775
# MakeUserCerts create user certificates and keys from userfile
 
776
# userfile contains a list of entries of the following format per user.
 
777
#
 
778
# <unix username>:<gecos field>:<user's email address>
 
779
#
 
780
# $1 = <userfile>
 
781
#
 
782
MakeUserCerts()
 
783
{
 
784
   if [ ! -f "$1" ]; then
 
785
      PrintErrorAndExit 1 "no valid userfile '%s'. Exit." $1
 
786
   fi
 
787
 
 
788
   userfile=$1
 
789
 
 
790
   cat $userfile | while read line; do
 
791
      MakeUserCert user "$line"
 
792
   done   
 
793
}
 
794
 
 
795
#--------------------------------------------------------------------------
 
796
# MakeUserKeystores create keystores for existing users in 
 
797
# $CALOCALTOP/userkeys 
 
798
#
 
799
MakeUserKeystores()
 
800
{
 
801
   if [ "$ksoutfile" = "" ]; then
 
802
      ksoutfile_saved=$ksoutfile
 
803
   fi
 
804
 
 
805
   remove_kspwfile=false
 
806
   if [ "$kspwfile" = "" ]; then
 
807
      touch /tmp/tmp.$$
 
808
      kspwfile=/tmp/tmp.$$
 
809
      remove_kspwfile=true
 
810
   fi   
 
811
   for user in `ls $CALOCALTOP/userkeys`; do
 
812
      certfile=$CALOCALTOP/userkeys/$user/cert.pem
 
813
      if [ -r $certfile ]; then 
 
814
         ksoutfile=$CALOCALTOP/userkeys/$user/keystore
 
815
         touch $ksoutfile
 
816
         $CHOWN $user $ksoutfile
 
817
         $CHMOD 600 $ksoutfile
 
818
         MakeKeystore user "$user"
 
819
      fi
 
820
   done   
 
821
   if [ "$remove_kspwfile" = "true" ]; then
 
822
      rm $kspwfile
 
823
   fi
 
824
   ksoutfile=$ksoutfile_saved
 
825
}
 
826
 
 
827
#--------------------------------------------------------------------------
 
828
# MakeSysKeystore create keystore for SGE Daemon
 
829
# $CALOCALTOP/private 
 
830
#
 
831
MakeSysKeystore()
 
832
{
 
833
   if [ "$ksoutfile" = "" ]; then
 
834
      ksoutfile_saved=$ksoutfile
 
835
   fi
 
836
 
 
837
   remove_kspwfile=false
 
838
   if [ "$kspwfile" = "" ]; then
 
839
      touch /tmp/tmp.$$
 
840
      kspwfile=/tmp/tmp.$$
 
841
      remove_kspwfile=true
 
842
   fi   
 
843
   certfile=$CATOP/certs/cert.pem
 
844
   if [ -r $certfile ]; then 
 
845
      ksoutfile=$CALOCALTOP/private/keystore
 
846
      ExecuteAsAdmin touch $ksoutfile
 
847
      ExecuteAsAdmin chmod 600 $ksoutfile
 
848
      MakeKeystore sge_daemon "SGEDaemon"
 
849
   fi
 
850
   if [ "$remove_kspwfile" = "true" ]; then
 
851
      rm $kspwfile
 
852
   fi
 
853
   ksoutfile=$ksoutfile_saved
 
854
}
 
855
 
 
856
 
 
857
 
 
858
#--------------------------------------------------------------------------
 
859
# MakeKeystore create keystore file of user's certificate and key
 
860
#
 
861
# $1 = user or sge_daemon
 
862
# $2 = <unix user>
 
863
#
 
864
MakeKeystore()
 
865
{
 
866
   type=$1
 
867
   if [ "$type" != "user" -a "$type" != "sge_daemon" ]; then
 
868
      PrintError "unknown type $type only user or sge_daemon allowed"
 
869
      return 1
 
870
   fi
 
871
   myuser=$2
 
872
   if [ "$myuser" = "" ]; then 
 
873
      PrintError "username is empty"
 
874
      return 1
 
875
   fi
 
876
   CA_ARGS="-catop $CATOP"
 
877
   CA_ARGS="$CA_ARGS -calocaltop $CALOCALTOP"
 
878
   CA_ARGS="$CA_ARGS -cascript $ROOT_PATH/util/sgeCA/sge_ca"
 
879
   CA_ARGS="$CA_ARGS -cahost $CAHOST -adminuser $ADMINUSER"
 
880
   CA_ARGS="$CA_ARGS initks $type $myuser $ksoutfile"
 
881
   if [ "$kspwfile" != "" ]; then
 
882
      CA_ARGS="$CA_ARGS $kspwfile"
 
883
   fi
 
884
 
 
885
   if [ "$JAVA_HOME" = "" ]; then 
 
886
      PrintError "JAVA_HOME not set"
 
887
      return 1
 
888
   else   
 
889
      JAVA=$JAVA_HOME/bin/java
 
890
   fi
 
891
 
 
892
   JVM_ARGS="-cp $ROOT_PATH/lib/juti.jar"
 
893
   JVM_ARGS="$JVM_ARGS -Djava.util.logging.config.file=$ROOT_PATH/util/sgeCA/logging.properties"
 
894
 
 
895
   $JAVA $JVM_ARGS com.sun.grid.ca.Main $CA_ARGS 
 
896
}
 
897
 
 
898
#--------------------------------------------------------------------------
 
899
# MakePKCS12 create pkcs12 file of user's certificate and key
 
900
#
 
901
# $1 = "user" or "sdm_daemon" or "sge_daemon"
 
902
# $2 = <unix user> or <sdm daemon>
 
903
#
 
904
MakePKCS12()
 
905
{
 
906
   if [ "$1" = "user" ]; then
 
907
      if [ "$2" = "" ]; then
 
908
         PrintError "no valid user '%s'." $1
 
909
         return 1
 
910
      fi
 
911
      keyowner=$2
 
912
      unixuser=$2
 
913
      KEYFILE=$CALOCALTOP/userkeys/$keyowner/key.pem
 
914
      CERTFILE=$CALOCALTOP/userkeys/$keyowner/cert.pem
 
915
   elif [ "$1" = "sdm_daemon" ]; then
 
916
      if [ "$2" = "" ]; then
 
917
         PrintError "no valid sdm daemon '%s'." $1
 
918
         return 1
 
919
      fi
 
920
      keyowner=$2
 
921
      unixuser=$ADMINUSER
 
922
      KEYFILE=$CALOCALTOP/daemons/$keyowner/key.pem
 
923
      CERTFILE=$CALOCALTOP/daemons/$keyowner/cert.pem
 
924
   elif [ "$1" = "sge_daemon" ]; then
 
925
      keyowner=$2
 
926
      unixuser=$ADMINUSER
 
927
      KEYFILE=$CALOCALTOP/private/key.pem
 
928
      CERTFILE=$CATOP/certs/cert.pem
 
929
   else
 
930
      PrintError "valid argument: %s" $1
 
931
      return 1
 
932
   fi
 
933
   
 
934
 
 
935
   pwoptions=""
 
936
   if [ "$passinfile" != "" ]; then
 
937
      pwoptions="-passout file:$passinfile"
 
938
   fi
 
939
 
 
940
   myoutdir="."
 
941
   if [ "$pkcs12outdir" != "" ]; then
 
942
      myoutdir="$pkcs12outdir"
 
943
   fi
 
944
 
 
945
   $INFOTEXT "Generating %s/%s.p12." "$myoutdir" "$keyowner"
 
946
   # create rand.seed file
 
947
   RSFILE=/tmp/rand.seed.$$
 
948
   MakeRandFile $RSFILE $unixuser
 
949
   if [ $? -ne 0 ]; then
 
950
      return 1
 
951
   fi
 
952
   RANDFILE=$RSFILE; export RANDFILE
 
953
 
 
954
   $P12 $pwoptions -export -nodes -inkey $KEYFILE -out $myoutdir/$keyowner.p12 -in $CERTFILE -certfile $CATOP/cacert.pem -caname "SGE CA"
 
955
   if [ $? -ne 0 ]; then
 
956
      PrintError "openssl pkcs12 command failed"
 
957
      return 1
 
958
   fi
 
959
   $RM -f $RANDFILE
 
960
}
 
961
 
 
962
#--------------------------------------------------------------------------
 
963
# MakePKCS12ForUsers create pkcs12 file of user certificate and keys from userfile
 
964
# userfile contains a list of entries of the following format per user.
 
965
#
 
966
# <unix username>:<gecos field>:<user's email address>
 
967
#
 
968
# $1 = <userfile>
 
969
#
 
970
MakePKCS12ForUsers()
 
971
{
 
972
   if [ ! -f "$1" ]; then
 
973
      PrintErrorAndExit 1 "no valid userfile '%s'. Exit." $1
 
974
   fi
 
975
   
 
976
   cat $1 | while read line; do
 
977
      unixuser=`echo $line|cut -d: -f1`
 
978
      $INFOTEXT "Generating %s/%s.p12." \
 
979
         "$pkcs12outdir" "$unixuser"
 
980
      pkcs12outdir=$CALOCALTOP/userkeys/$unixuser
 
981
      MakePKCS12 user "$unixuser"
 
982
   done   
 
983
}
 
984
 
 
985
#--------------------------------------------------------------------------
 
986
# DumpUsers dump user info 
 
987
#
 
988
DumpUsers()
 
989
{
 
990
   CERTDIR=$CALOCALTOP/userkeys
 
991
   RSFILE=/tmp/rand.$$
 
992
 
 
993
   # create rand.seed file
 
994
   MakeRandFile $RSFILE $USER
 
995
   if [ $? -ne 0 ]; then
 
996
      exit 1
 
997
   fi
 
998
   RANDFILE=$RSFILE; export RANDFILE
 
999
 
 
1000
   $INFOTEXT "Dumping users to $outdir/dumped_users.txt"
 
1001
   for i in `ls $CERTDIR`; do
 
1002
      $X509 -in $CERTDIR/$i/cert.pem -subject -noout|$AWK -F '/' '{print $8 ":" $7 ":" $9}' | sed -e 's/UID=//' -e 's/CN=//' -e 's/emailAddress=//' >> $outdir/dumped_users.txt
 
1003
      if [ $? -ne 0 ]; then
 
1004
         PrintError "openssl x509 command for user %s failed" $i
 
1005
      fi
 
1006
   done
 
1007
}
 
1008
 
 
1009
#--------------------------------------------------------------------------
 
1010
# MakeCert create certificate and private key for daemon
 
1011
#
 
1012
# $1 = certificate type ("daemon" or "user"  or "sdm_daemon")
 
1013
# $2 = userId   (Unix user name)
 
1014
# $3 = commonname         (e.g. passwd gecos field or name of sdm_daemon)
 
1015
# $4 = email address
 
1016
#
 
1017
MakeCert()
 
1018
{
 
1019
   $INFOTEXT -u "\nCreating '%s' certificate and key for %s" "$1" "$3"
 
1020
 
 
1021
   TMPFILE=/tmp/sge_ca$$.tmp
 
1022
   TMPFILE1=/tmp/sge_ca1$$.tmp
 
1023
   $RM -f $TMPFILE $TMPFILE1
 
1024
   Execute cp $CATOP/dn.info $TMPFILE
 
1025
   if [ $? -ne 0 ]; then
 
1026
      PrintError "Could not copy file (%s -> %s)" $CATOP/dn.info $TMPFILE
 
1027
      return 1
 
1028
   fi
 
1029
 
 
1030
   # 
 
1031
   #  For SDM daemon the uid is always "sdm_daemon"
 
1032
   #  the SdmCATrustManagerLoginModule needs this information
 
1033
   #  to distingush between daemon and user certificates
 
1034
   #
 
1035
   if [ $1 = sdm_daemon ]; then
 
1036
      echo userId=sdm_daemon_$2 >> $TMPFILE
 
1037
   else
 
1038
      echo userId=$2 >> $TMPFILE
 
1039
   fi
 
1040
   echo CN=$3 >> $TMPFILE
 
1041
   echo emailAddress=$4 >> $TMPFILE
 
1042
 
 
1043
   Execute cat $CONFIG_DIR/sge_ssl_template.cnf $TMPFILE > $TMPFILE1
 
1044
   if [ $? -ne 0 ]; then
 
1045
      PrintError "Could not cat file (%s, %s -> %s)" $CONFIG_DIR/sge_ssl_template.cnf $TMPFILE $TMPFILE1
 
1046
      return 1
 
1047
   fi
 
1048
 
 
1049
   if [ $1 = daemon ]; then
 
1050
      KEYDIR=$CALOCALTOP/private
 
1051
      KEYFILE=$CALOCALTOP/private/key.pem
 
1052
      REQFILE=$CALOCALTOP/private/req.pem
 
1053
      CERTFILE=$CATOP/certs/cert.pem
 
1054
      RSFILE=$CALOCALTOP/private/rand.seed
 
1055
   elif [ $1 = user ]; then
 
1056
      KEYDIR=$CALOCALTOP/userkeys/$2
 
1057
      KEYFILE=$KEYDIR/key.pem
 
1058
      REQFILE=$KEYDIR/req.pem
 
1059
      CERTFILE=$KEYDIR/cert.pem
 
1060
      CERTFILE_PUBLIC_DIR=$CATOP/usercerts/$2
 
1061
      RSFILE=$KEYDIR/rand.seed
 
1062
      ExecRm $KEYDIR
 
1063
      ExecuteAsAdmin $MKDIR $KEYDIR
 
1064
   elif [ $1 = sdm_daemon ]; then
 
1065
      KEYDIR=$CALOCALTOP/daemons/$3
 
1066
      KEYFILE=$KEYDIR/key.pem
 
1067
      REQFILE=$KEYDIR/req.pem
 
1068
      CERTFILE=$KEYDIR/cert.pem
 
1069
      RSFILE=$KEYDIR/rand.seed
 
1070
      ExecRm $KEYDIR
 
1071
      ExecuteAsAdmin $MKDIR $KEYDIR
 
1072
   else 
 
1073
      PrintError "Unknown certificate type %s" $1
 
1074
      return 1
 
1075
   fi
 
1076
 
 
1077
   # create rand.seed file
 
1078
   if [ "$ADMINUSER" = default ]; then
 
1079
      MakeRandFile $RSFILE  root
 
1080
   else
 
1081
      MakeRandFile $RSFILE $ADMINUSER
 
1082
   fi
 
1083
   if [ $? -ne 0 ]; then
 
1084
      return 1
 
1085
   fi
 
1086
 
 
1087
   RANDFILE=$RSFILE; export RANDFILE
 
1088
 
 
1089
   umask 077
 
1090
 
 
1091
   # create a certificate request
 
1092
   ExecuteAsAdmin $REQ -config $TMPFILE1 -new -keyout $KEYFILE -out $REQFILE $DAYS
 
1093
   if [ $? != 0 ]; then
 
1094
      PrintError "Can't create %s or %s. Exit." $KEYFILE $REQFILE
 
1095
      return 1
 
1096
   fi  
 
1097
 
 
1098
   # sign certificate request
 
1099
   ExecuteAsAdmin $CA -config $TMPFILE1 -policy policy_anything -batch $DAYS \
 
1100
                      -notext -out $CERTFILE -infiles $REQFILE
 
1101
   if [ $? != 0 ]; then
 
1102
      PrintError "Can't sign certificate request %s. Exit." $REQFILE 
 
1103
      return 1
 
1104
   fi
 
1105
   ExecuteAsAdmin $CHMOD 644 $CERTFILE
 
1106
   if [ $? != 0 ]; then
 
1107
      PrintError "chmod as %s failed (%s)" $ADMINUSER $CERTFILE
 
1108
      return 1
 
1109
   fi
 
1110
   
 
1111
   if [ "$1" = user ]; then
 
1112
      ExecuteAsAdmin $MKDIR -m 755 -p $CERTFILE_PUBLIC_DIR
 
1113
      ExecuteAsAdmin $CP $CERTFILE $CERTFILE_PUBLIC_DIR
 
1114
      ExecuteAsAdmin $CHMOD 644 $CERTFILE_PUBLIC_DIR/cert.pem
 
1115
   fi
 
1116
   
 
1117
   $RM -f $TMPFILE $TMPFILE1 
 
1118
 
 
1119
   Execute $CHMOD 700 $KEYDIR
 
1120
   if [ $? != 0 ]; then
 
1121
      PrintError "chmod failed (%s)" $KEYDIR
 
1122
      return 1
 
1123
   fi
 
1124
   Execute $CHMOD 600 $KEYDIR/*
 
1125
   if [ $? != 0 ]; then
 
1126
      PrintError "chmod failed (%s/\*)" $KEYDIR
 
1127
      return 1
 
1128
   fi
 
1129
   Execute $CHMOD 600 $KEYDIR/rand.seed
 
1130
   if [ $? != 0 ]; then
 
1131
      PrintError "chmod failed (%s)" $KEYDIR/rand.seed
 
1132
      return 1
 
1133
   fi
 
1134
   if [ $1 = daemon ]; then
 
1135
      $INFOTEXT "created and signed certificate for SGE daemons" 
 
1136
   elif [ $1 = sdm_daemon ]; then
 
1137
      $INFOTEXT "created and signed certificate for sdm_daemon '%s' in '%s'" $3 $KEYDIR
 
1138
   else
 
1139
      # check if user exists and chown if yes
 
1140
      id $2  > /dev/null 2>&1
 
1141
      if [ $? = 0 ]; then
 
1142
         ExecChown -R $2 $KEYDIR
 
1143
         if [ $? != 0 ]; then
 
1144
            PrintError "chown to %s failed (%s)" $2 $KEYDIR
 
1145
            return 1
 
1146
         fi
 
1147
      fi   
 
1148
      $INFOTEXT "created and signed certificate for user '%s' in '%s'" $2 $KEYDIR
 
1149
   fi
 
1150
 
 
1151
   umask 022
 
1152
   return 0
 
1153
}
 
1154
 
 
1155
#--------------------------------------------------------------------------
 
1156
# RenewCA renew a selfsigned CA certificate
 
1157
#
 
1158
RenewCA()
 
1159
{
 
1160
   $INFOTEXT -u "\nRenewing CA certificate"
 
1161
 
 
1162
   CAKEYDIR=$CALOCALTOP/private
 
1163
   CAKEYFILE=$CALOCALTOP/private/cakey.pem
 
1164
   CACERTFILE=$CATOP/cacert.pem
 
1165
   RSFILE=$CALOCALTOP/private/rand.seed
 
1166
   NEWCACERTFILE=$CATOP/cacert.pem.new
 
1167
   OLDCACERTFILE=$CATOP/cacert.pem.`date | tr " " "_"`
 
1168
   RANDFILE=$RSFILE; export RANDFILE
 
1169
 
 
1170
   ExecuteAsAdmin $X509 -in $CACERTFILE -signkey $CAKEYFILE $DAYS -out $NEWCACERTFILE
 
1171
   if [ $? != 0 ]; then
 
1172
      PrintError "openssl x509 as %s failed (RenewCA)" $ADMINUSER
 
1173
      return 1
 
1174
   fi
 
1175
 
 
1176
   ExecuteAsAdmin $CP $CACERTFILE $OLDCACERTFILE
 
1177
   if [ $? != 0 ]; then
 
1178
      PrintError "Could not copy file as %s (%s -> %s)" $ADMINUSER $CACERTFILE $OLDCACERTFILE
 
1179
      return 1
 
1180
   fi
 
1181
   ExecuteAsAdmin $CHMOD 644 $CACERTFILE
 
1182
   if [ $? != 0 ]; then
 
1183
      PrintError "chmod as %s failed (%s)" $ADMINUSER $CACERTFILE
 
1184
      return 1
 
1185
   fi
 
1186
   ExecuteAsAdmin $MV $NEWCACERTFILE $CACERTFILE
 
1187
   if [ $? != 0 ]; then
 
1188
      PrintError "Could not move file as %s (%s -> %s)" $ADMINUSER $NEWCACERTFILE $CACERTFILE
 
1189
      return 1
 
1190
   fi
 
1191
   ExecuteAsAdmin $CHMOD 644 $CACERTFILE
 
1192
   if [ $? != 0 ]; then
 
1193
      PrintError "chmod as %s failed (%s)" $ADMINUSER $CACERTFILE
 
1194
      return 1
 
1195
   fi
 
1196
   return 0
 
1197
}
 
1198
 
 
1199
 
 
1200
#--------------------------------------------------------------------------
 
1201
# RenewCert renew a certificate
 
1202
#
 
1203
# $1 = certificate type ("daemon" or "user" or "sdm_daemon"
 
1204
# $2 = userId   (Unix user name of common name of sdm_damon)
 
1205
#
 
1206
RenewCert()
 
1207
{
 
1208
   ADMINRUN_NO_EXIT=true # ExecuteAsAdmin should not exit, 
 
1209
                         # we need to cleanup the file permissions
 
1210
 
 
1211
   # in the gridengine environment only root user is allowed
 
1212
   # to renew certificates
 
1213
   # for haithabu the admin user can also renew certificates
 
1214
   if [ "$rootinstalls" != true -a $SGE_CNF = true ]; then
 
1215
      PrintError "Only root user can renew certificates!"
 
1216
      return 1
 
1217
   fi
 
1218
  
 
1219
   if [ "$1" = daemon ]; then
 
1220
      $INFOTEXT -u "\nRenewing daemon certificate"
 
1221
      if [ "$2" = default ]; then
 
1222
         $2=root
 
1223
      fi
 
1224
   else
 
1225
      $INFOTEXT -u "\nRenewing  '%s' '%s' certificate" "$2" "$1"
 
1226
   fi
 
1227
 
 
1228
   TMPFILE=/tmp/sge_ca$$.tmp
 
1229
   TMPFILE1=/tmp/sge_ca1$$.tmp
 
1230
   $RM -f $TMPFILE $TMPFILE1
 
1231
   Execute cp $CATOP/dn.info $TMPFILE
 
1232
   if [ $? != 0 ]; then
 
1233
      PrintError "Could not copy file (%s -> %s)" $CATOP/dn.info $TMPFILE
 
1234
      return 1
 
1235
   fi
 
1236
   echo userId=$2 >> $TMPFILE
 
1237
   echo CN="dummy" >> $TMPFILE
 
1238
   echo emailAddress="dummy" >> $TMPFILE
 
1239
   Execute cat $CONFIG_DIR/sge_ssl_template.cnf $TMPFILE > $TMPFILE1
 
1240
   if [ $? != 0 ]; then
 
1241
      PrintError "Could not cat file (%s, %s -> %s)" $CONFIG_DIR/sge_ssl_template.cnf $TMPFILE > $TMPFILE1
 
1242
      return 1
 
1243
   fi
 
1244
 
 
1245
 
 
1246
   if [ "$1" = daemon ]; then
 
1247
      KEYDIR=$CALOCALTOP/private
 
1248
      KEYFILE=$CALOCALTOP/private/key.pem
 
1249
      REQFILE=$CALOCALTOP/private/req.pem
 
1250
      CERTFILE=$CATOP/certs/cert.pem
 
1251
      NEWCERTFILE=$CATOP/certs/cert.pem.new
 
1252
      OLDCERTFILE=$CATOP/certs/cert.pem.old
 
1253
      RSFILE=$CALOCALTOP/private/rand.seed
 
1254
      CRLFILE=$CATOP/ca-crl.pem
 
1255
   elif [ $1 = sdm_daemon ]; then
 
1256
      KEYDIR=$CALOCALTOP/daemons/$2
 
1257
      NEWKEYBASEDIR=/tmp/sge_ca_dir_$$
 
1258
      NEWKEYDIR=$NEWKEYBASEDIR/daemons/$2
 
1259
      KEYFILE=$NEWKEYDIR/key.pem
 
1260
      REQFILE=$NEWKEYDIR/req.pem
 
1261
      CERTFILE=$NEWKEYDIR/cert.pem
 
1262
      NEWCERTFILE=$NEWKEYDIR/cert.pem.new
 
1263
      OLDCERTFILE=$NEWKEYDIR/cert.pem.old
 
1264
      RSFILE=$NEWKEYDIR/rand.seed
 
1265
      CRLFILE=$CATOP/ca-crl.pem
 
1266
   elif [ $1 = user ]; then
 
1267
      KEYDIR=$CALOCALTOP/userkeys/$2
 
1268
      NEWKEYBASEDIR=/tmp/sge_ca_dir_$$
 
1269
      NEWKEYDIR=$NEWKEYBASEDIR/userkeys/$2
 
1270
      KEYFILE=$NEWKEYDIR/key.pem
 
1271
      REQFILE=$NEWKEYDIR/req.pem
 
1272
      CERTFILE=$NEWKEYDIR/cert.pem
 
1273
      NEWCERTFILE=$NEWKEYDIR/cert.pem.new
 
1274
      OLDCERTFILE=$NEWKEYDIR/cert.pem.old
 
1275
      RSFILE=$NEWKEYDIR/rand.seed
 
1276
      CRLFILE=$CATOP/ca-crl.pem
 
1277
      CERTFILE_PUBLIC_DIR=$CATOP/usercerts/$2
 
1278
   else 
 
1279
      PrintError "Unknown certificate type %s" $1
 
1280
      return 1
 
1281
   fi
 
1282
 
 
1283
   # create rand.seed file
 
1284
   # rand.seed already exists, therefore no MakeRandFile $RSFILE $2
 
1285
   RANDFILE=$RSFILE; export RANDFILE
 
1286
 
 
1287
   if [ "$1" = daemon ]; then
 
1288
      Execute $CHMOD 644 $CERTFILE
 
1289
      if [ $? != 0 ]; then
 
1290
         PrintError "chmod failed (%s)" $CERTFILE
 
1291
         return 1
 
1292
      fi
 
1293
   else
 
1294
      Execute $MKDIR -p $NEWKEYDIR
 
1295
      if [ $? != 0 ]; then
 
1296
         PrintError "mkdir failed (%s)" $NEWKEYDIR
 
1297
         return 1
 
1298
      fi
 
1299
      Execute $CP $KEYDIR/* $NEWKEYDIR
 
1300
      if [ $? != 0 ]; then
 
1301
         PrintError "Could not copy files (%s/\* -> %s)" $KEYDIR/* $NEWKEYDIR
 
1302
         return 1
 
1303
      fi
 
1304
      if [ "$ADMINUSER" = default ]; then
 
1305
         ExecChown -R root $NEWKEYDIR
 
1306
      else
 
1307
         ExecChown -R $ADMINUSER $NEWKEYDIR
 
1308
      fi
 
1309
      if [ $? != 0 ]; then
 
1310
         PrintError "chown failed (%s)" $NEWKEYDIR
 
1311
         return 1
 
1312
      fi
 
1313
      if [ $? != 0 ]; then
 
1314
         PrintError "chown failed (%s)" $NEWKEYDIR
 
1315
         return 1
 
1316
      fi
 
1317
      Execute $CHMOD 644 $NEWKEYDIR/*
 
1318
      if [ $? != 0 ]; then
 
1319
         PrintError "chmod failed (%s/\*)" $NEWKEYDIR
 
1320
         return 1
 
1321
      fi
 
1322
      
 
1323
   fi
 
1324
 
 
1325
   umask 077
 
1326
 
 
1327
   # revoke the old certificate and create the certificate revocation list
 
1328
   ExecuteAsAdmin $CA -config $TMPFILE1 -policy policy_anything \
 
1329
                      -batch $DAYS \
 
1330
                      -revoke $CERTFILE 
 
1331
 
 
1332
   if [ $? != 0 ]; then
 
1333
      PrintError "Can't revoke %s." $CERTFILE
 
1334
      return 1
 
1335
   else
 
1336
 
 
1337
      # sign certificate request
 
1338
      ExecuteAsAdmin $CA -config $TMPFILE1 \
 
1339
                         -policy policy_anything -batch $DAYS \
 
1340
                         -notext -out $NEWCERTFILE -infiles $REQFILE
 
1341
 
 
1342
      if [ $? != 0 ]; then
 
1343
         PrintError "Can't renew %s." $CERTFILE
 
1344
         return 1
 
1345
      else  
 
1346
      
 
1347
         # create the certificate revocation list
 
1348
         ExecuteAsAdmin $CA -config $TMPFILE1 -policy policy_anything \
 
1349
                            -batch $DAYS \
 
1350
                            -gencrl -out ${CRLFILE}.tmp
 
1351
 
 
1352
         if [ $? != 0 ]; then
 
1353
            PrintError "Can't generate revocation list %s.." $CRLFILE
 
1354
            return 1
 
1355
         else
 
1356
            if [ $1 = daemon ]; then
 
1357
               $INFOTEXT "renewed certificate for SGE daemons" 
 
1358
            else
 
1359
               $INFOTEXT "renewed certificate for user '%s' in '%s'" $2 $KEYDIR
 
1360
            fi
 
1361
         fi  
 
1362
 
 
1363
         if [ -f $OLDCERTFILE ]; then
 
1364
            ExecuteAsAdmin $RM -f $OLDCERTFILE
 
1365
            if [ $? != 0 ]; then
 
1366
               PrintError "Can not delete old cert file (%s)"  $OLDCERTFILE
 
1367
               return 1
 
1368
            fi
 
1369
         fi
 
1370
 
 
1371
         ExecuteAsAdmin $CP $CERTFILE $OLDCERTFILE
 
1372
         if [ $? != 0 ]; then
 
1373
            PrintError "Could not copy file (%s -> %s)"  $CERTFILE $OLDCERTFILE
 
1374
            return 1
 
1375
         fi
 
1376
         ExecuteAsAdmin $MV $NEWCERTFILE $CERTFILE
 
1377
         if [ $? != 0 ]; then
 
1378
            PrintError "Could not move file (%s -> %s)"  $NEWCERTFILE $CERTFILE
 
1379
            return 1
 
1380
         fi
 
1381
         ExecuteAsAdmin $CHMOD 644 $CERTFILE
 
1382
         if [ $? != 0 ]; then
 
1383
            PrintError "chmod failed (%s)" $CERTFILE
 
1384
            return 1
 
1385
         fi
 
1386
 
 
1387
         # copy renewed cert.pem to $CERTFILE_PUBLIC_DIR
 
1388
         if [ "$1" = user ]; then
 
1389
            umask 022
 
1390
            if [ ! -d "$CERTFILE_PUBLIC_DIR" ]; then
 
1391
               ExecuteAsAdmin $MKDIR -m 755 -p $CERTFILE_PUBLIC_DIR
 
1392
               if [ $? != 0 ]; then
 
1393
                  umask 077
 
1394
                  PrintError "Could not create %s"  $CERTFILE_PUBLIC_DIR
 
1395
                  return 1
 
1396
               fi
 
1397
            fi
 
1398
            ExecuteAsAdmin $CP $CERTFILE $CERTFILE_PUBLIC_DIR
 
1399
            if [ $? != 0 ]; then
 
1400
               umask 077
 
1401
               PrintError "Could not copy file (%s -> %s)"  $CERTFILE $CERTFILE_PUBLIC_DIR
 
1402
               return 1
 
1403
            fi
 
1404
            ExecuteAsAdmin $CHMOD 644 $CERTFILE_PUBLIC_DIR/cert.pem
 
1405
            if [ $? != 0 ]; then
 
1406
               umask 077
 
1407
               PrintError "Could not chown 644 file %s"  $CERTFILE_PUBLIC_DIR/cert.pem
 
1408
               return 1
 
1409
            fi
 
1410
            umask 077
 
1411
         fi
 
1412
      
 
1413
         if [ "$1" != daemon ]; then
 
1414
            Execute $CP -f $NEWKEYDIR/* $KEYDIR
 
1415
            if [ $? != 0 ]; then
 
1416
               PrintError "Could not copy files (%s/\* -> %s)"  $NEWKEYDIR $KEYDIR
 
1417
               return 1
 
1418
            fi
 
1419
         fi
 
1420
 
 
1421
         ExecuteAsAdmin $MV ${CRLFILE}.tmp $CRLFILE
 
1422
         if [ $? != 0 ]; then
 
1423
            PrintError "Could not move file (%s -> %s)"  ${CRLFILE}.tmp $CRLFILE
 
1424
            return 1
 
1425
         fi
 
1426
         ExecuteAsAdmin $CHMOD 644 $CRLFILE 
 
1427
         if [ $? != 0 ]; then
 
1428
            PrintError "chmod failed (%s)" $CERTFILE
 
1429
            return 1
 
1430
         fi
 
1431
      fi
 
1432
 
 
1433
   fi
 
1434
 
 
1435
   Execute $RM -f $TMPFILE $TMPFILE1 
 
1436
 
 
1437
   if [ "$1" != daemon ]; then
 
1438
      ExecRm $NEWKEYBASEDIR
 
1439
      Execute $CHMOD 500 $KEYDIR
 
1440
      if [ $? != 0 ]; then
 
1441
         PrintError "chmod failed (%s)" $KEYDIR
 
1442
         return 1
 
1443
      fi
 
1444
      Execute $CHMOD 600 $KEYDIR/*
 
1445
      if [ $? != 0 ]; then
 
1446
         PrintError "chmod failed (%s/\*)" $KEYDIR
 
1447
         return 1
 
1448
      fi
 
1449
      # The chown can only be done for user certificates
 
1450
      # sdm_daemon certificate are owned by $ADMINUSER
 
1451
      if [ $1 = "user" ]; then
 
1452
         id $2  > /dev/null 2>&1
 
1453
         if [ $? = 0 ]; then
 
1454
            ExecChown -R $2 $KEYDIR
 
1455
            if [ $? != 0 ]; then
 
1456
               PrintError "chown to %s failed (%s)" $2 $KEYDIR
 
1457
               return 1
 
1458
            fi
 
1459
         fi
 
1460
      fi
 
1461
   fi
 
1462
 
 
1463
   umask 022
 
1464
   return 0
 
1465
}
 
1466
 
 
1467
 
 
1468
#--------------------------------------------------------------------------
 
1469
# MakeCADirs
 
1470
#    create all directories for CA infrastructure
 
1471
#
 
1472
 
1473
MakeCADirs()
 
1474
{
 
1475
 
 
1476
 
 
1477
   if [ -f $CATOP -o -f $CALOCALTOP ]; then
 
1478
      $INFOTEXT "The CA directories\n   %s or %s\n seem to be regular files" \
 
1479
            $CATOP $CALOCALTOP
 
1480
      $INFOTEXT "CA initialization failed. Exit."
 
1481
      exit 1
 
1482
   fi
 
1483
 
 
1484
   ExecRm $CALOCALTOP 
 
1485
   if [ -f $CALOCALTOP -o -d $CALOCALTOP ]; then
 
1486
      PrintError "Can't delete the CA local directories\n   %s" \
 
1487
                 $CALOCALTOP
 
1488
      return 1
 
1489
   fi
 
1490
 
 
1491
   ExecRmAsAdmin $CATOP 
 
1492
   if [ -f $CATOP -o -d $CATOP ]; then
 
1493
      PrintError "Can't delete the CA directories\n   %s" \
 
1494
                 $CATOP 
 
1495
      return 1
 
1496
   fi
 
1497
 
 
1498
 
 
1499
   $INFOTEXT "Creating %s" $CATOP
 
1500
   ExecuteAsAdmin $MKDIR $CATOP
 
1501
   if [ $? != 0 ]; then
 
1502
      PrintError "mkdir failed (%s)" $CATOP
 
1503
      return 1
 
1504
   fi
 
1505
   $INFOTEXT "Creating %s" $CALOCALTOP
 
1506
   Execute $MKDIR -p $CALOCALTOP
 
1507
   if [ $? != 0 ]; then
 
1508
      PrintError "mkdir failed (%s)" $CALOCALTOP
 
1509
      return 1
 
1510
   fi
 
1511
 
 
1512
   if [ $ADMINUSER != default -a $rootinstalls = true ]; then
 
1513
      ExecChown $ADMINUSER $CALOCALTOP
 
1514
      if [ $? != 0 ]; then
 
1515
         PrintError "chown to %s failed (%s)" $ADMINUSER $CALOCALTOP
 
1516
         return 1
 
1517
      fi
 
1518
   fi
 
1519
 
 
1520
   $INFOTEXT "Creating %s" ${CATOP}/certs
 
1521
   ExecuteAsAdmin $MKDIR ${CATOP}/certs
 
1522
   if [ $? != 0 ]; then
 
1523
      PrintError "mkdir failed (%s)" ${CATOP}/certs
 
1524
      return 1
 
1525
   fi
 
1526
 
 
1527
   $INFOTEXT "Creating %s" ${CATOP}/crl
 
1528
   $INFOTEXT -log "Creating %s" ${CATOP}/crl
 
1529
   ExecuteAsAdmin $MKDIR ${CATOP}/crl
 
1530
   if [ $? != 0 ]; then
 
1531
      PrintError "mkdir failed (%s)" ${CATOP}/crl
 
1532
      return 1
 
1533
   fi
 
1534
 
 
1535
   $INFOTEXT "Creating %s" ${CATOP}/newcerts
 
1536
   $INFOTEXT -log "Creating %s" ${CATOP}/newcerts
 
1537
   ExecuteAsAdmin $MKDIR ${CATOP}/newcerts
 
1538
   if [ $? != 0 ]; then
 
1539
      PrintError "mkdir failed (%s)" ${CATOP}/newcerts
 
1540
      return 1
 
1541
   fi
 
1542
 
 
1543
   $INFOTEXT "Creating %s" ${CATOP}/serial
 
1544
   $INFOTEXT -log "Creating %s" ${CATOP}/serial
 
1545
   # TruncCreateAndMakeWriteable ${CATOP}/serial
 
1546
   ExecuteAsAdmin $TOUCH ${CATOP}/serial
 
1547
   ExecuteAsAdmin $CHMOD 666 ${CATOP}/serial
 
1548
   if [ $? != 0 ]; then
 
1549
      PrintError "Could not touch file as %s (%s)" $ADMINUSER ${CATOP}/serial
 
1550
      return 1
 
1551
   fi
 
1552
   
 
1553
   echo 01 > ${CATOP}/serial
 
1554
   ExecuteAsAdmin $CHMOD 644 ${CATOP}/serial
 
1555
   if [ $? != 0 ]; then
 
1556
      PrintError "chmod failed (%s)" ${CATOP}/serial
 
1557
      return 1
 
1558
   fi
 
1559
 
 
1560
   $INFOTEXT "Creating %s" ${CATOP}/index.txt
 
1561
   $INFOTEXT -log "Creating %s" ${CATOP}/index.txt
 
1562
   ExecuteAsAdmin $TOUCH ${CATOP}/index.txt   
 
1563
   if [ $? != 0 ]; then
 
1564
      PrintError "Could not touch file as %s (%s)" $ADMINUSER ${CATOP}/index.txt
 
1565
      return 1
 
1566
   fi
 
1567
 
 
1568
   $INFOTEXT "Creating %s" ${CATOP}/usercerts
 
1569
   $INFOTEXT -log "Creating %s" ${CATOP}/usercerts
 
1570
   ExecuteAsAdmin $MKDIR -m 755 -p ${CATOP}/usercerts
 
1571
   if [ $? != 0 ]; then
 
1572
      PrintError "mkdir failed (%s)" ${CATOP}/usercerts
 
1573
      return 1
 
1574
   fi
 
1575
 
 
1576
   $INFOTEXT "Creating %s" ${CALOCALTOP}/userkeys
 
1577
   $INFOTEXT -log "Creating %s" ${CALOCALTOP}/userkeys
 
1578
   ExecuteAsAdmin $MKDIR ${CALOCALTOP}/userkeys   
 
1579
   if [ $? != 0 ]; then
 
1580
      PrintError "mkdir failed (%s)" ${CALOCALTOP}/userkeys
 
1581
      return 1
 
1582
   fi
 
1583
 
 
1584
   if [ $SGE_CNF = false ]; then
 
1585
      # haithabu store the daemon certificates in a seperate
 
1586
      # daemons directory
 
1587
      $INFOTEXT "Creating %s" ${CALOCALTOP}/daemons
 
1588
      $INFOTEXT -log "Creating %s" ${CALOCALTOP}/daemons
 
1589
      ExecuteAsAdmin $MKDIR ${CALOCALTOP}/daemons   
 
1590
      if [ $? != 0 ]; then
 
1591
         PrintError "mkdir failed (%s)" ${CATOP}/daemons
 
1592
         return 1
 
1593
      fi
 
1594
   fi
 
1595
   
 
1596
   umask 077
 
1597
   $INFOTEXT "Creating %s" ${CALOCALTOP}/private
 
1598
   $INFOTEXT -log "Creating %s" ${CALOCALTOP}/private
 
1599
   ExecuteAsAdmin $MKDIR ${CALOCALTOP}/private
 
1600
   if [ $? != 0 ]; then
 
1601
      PrintError "mkdir failed (%s)" ${CATOP}/private
 
1602
      return 1
 
1603
   fi
 
1604
   umask 022
 
1605
 
 
1606
   $INFOTEXT -wait -auto $AUTO -n "\nHit <RETURN> to continue >> "
 
1607
   $CLEAR
 
1608
 
 
1609
   return 0
 
1610
}
 
1611
 
 
1612
 
 
1613
#--------------------------------------------------------------------------
 
1614
# ReadConfig
 
1615
#    sources the sge_ca.conf file, if the -nosge option is not set
 
1616
#
 
1617
 
1618
ReadConfig() {
 
1619
   
 
1620
   i=0
 
1621
   read_it=true
 
1622
   while [ $# -gt 0 ]; do
 
1623
     if [ "$1" = "-nosge" ]; then
 
1624
        read_it=false
 
1625
        break
 
1626
     fi
 
1627
     shift
 
1628
   done
 
1629
   if [ "$read_it" = "true" ]; then
 
1630
      if [ -f  $ROOT_PATH/util/sgeCA/sge_ca.cnf ]; then
 
1631
         . $ROOT_PATH/util/sgeCA/sge_ca.cnf
 
1632
      fi
 
1633
      return 1
 
1634
   else
 
1635
      return 0
 
1636
   fi
 
1637
}
 
1638
 
 
1639
#--------------------------------------------------------------------------
 
1640
# THE MAIN PROCEDURE
 
1641
#--------------------------------------------------------------------------
 
1642
TOUCH=touch
 
1643
RM=rm
 
1644
MKDIR=mkdir
 
1645
CHMOD=chmod
 
1646
CHOWN=chown
 
1647
CP=cp
 
1648
MV=mv
 
1649
 
 
1650
umask 022
 
1651
 
 
1652
SGE_CNF=true
 
1653
ROOT_PATH=`dirname $0`/../..
 
1654
ROOT_PATH=`cd $ROOT_PATH; pwd`
 
1655
CATOP=""
 
1656
CALOCALTOP=""
 
1657
CAHOST=""
 
1658
CAHOMEKEYDIR=""
 
1659
 
 
1660
ADMINUSER="default"
 
1661
CONFIG_DIR=$ROOT_PATH/util/sgeCA
 
1662
ARCHSCRIPT=$ROOT_PATH/util/arch
 
1663
if [ ! -f "$ARCHSCRIPT" ]; then
 
1664
   echo
 
1665
   echo Error: The shell script \"$ARCHSCRIPT\" does not exist.
 
1666
   echo Please verify your setup and restart this script. Exit.
 
1667
   echo
 
1668
   exit 1
 
1669
fi
 
1670
 
 
1671
ARCH=`$ARCHSCRIPT`
 
1672
V5UTILBIN=$ROOT_PATH/utilbin/$ARCH              # adminrun, infotext, openssl, uidgid
 
1673
 
 
1674
if [ ! -d "$V5UTILBIN" ]; then
 
1675
   echo
 
1676
   echo "Error: The utilbin directory "$V5UTILBIN" does not exist"
 
1677
   echo "Please verify your setup and restart this script. Exit."
 
1678
   exit 1
 
1679
fi
 
1680
 
 
1681
if [ ! -d "$ROOT_PATH/lib/$ARCH" ]; then
 
1682
    echo
 
1683
    echo "Error: The lib directory \"$ROOT_PATH/lib/$ARCH\" does not exist"
 
1684
    echo "Please verify your setup and restart this script. Exit."
 
1685
    exit 1
 
1686
fi
 
1687
 
 
1688
if [ "$LD_LIBRARY_PATH"  = "" ]; then
 
1689
   LD_LIBRARY_PATH=$ROOT_PATH/lib/$ARCH
 
1690
else
 
1691
   LD_LIBRARY_PATH="$ROOT_PATH/lib/${ARCH}:${LD_LIBRARY_PATH}"
 
1692
fi
 
1693
export LD_LIBRARY_PATH
 
1694
 
 
1695
#---------------------------------------
 
1696
# setup INFOTEXT begin
 
1697
#---------------------------------------
 
1698
 
 
1699
# INFOTXT_DUMMY is needed by message parsing script
 
1700
# which is looking for $INFOTEXT and would report
 
1701
# errors in the next command. Please use INFOTXT_DUMMY
 
1702
# instead of using $INFOTEXT
 
1703
 
 
1704
INFOTXT_DUMMY=$V5UTILBIN/infotext
 
1705
INFOTEXT=$INFOTXT_DUMMY
 
1706
if [ ! -x $INFOTXT_DUMMY ]; then
 
1707
   echo "Error: Can't find binary \"$INFOTXT_DUMMY\""
 
1708
   echo "Please verify your setup and restart this script. Exit."
 
1709
   exit 1
 
1710
fi
 
1711
 
 
1712
# Test the infotext binary
 
1713
tmp=`$INFOTEXT test 2>&1`
 
1714
if [ $? -ne 0 ]; then
 
1715
   echo "Error: Execution of $INFOTEXT failed: $tmp"
 
1716
   echo "Please verify your setup and restart this script. Exit."
 
1717
   exit 1
 
1718
fi
 
1719
 
 
1720
#
 
1721
# From now on we can use PrintError methods to write error messages
 
1722
#
 
1723
 
 
1724
SGE_INFOTEXT_MAX_COLUMN=5000; export SGE_INFOTEXT_MAX_COLUMN
 
1725
 
 
1726
#---------------------------------------
 
1727
# setup INFOTEXT end
 
1728
#---------------------------------------
 
1729
 
 
1730
#
 
1731
#  Check wether all needed binaries are exists
 
1732
#
 
1733
for i in adminrun openssl uidgid; do
 
1734
   if [ ! -x $V5UTILBIN/$i ]; then
 
1735
      PrintErrorAndExit 1 "Error: Can't find binary \"%s\"" $V5UTILBIN/$i
 
1736
   fi
 
1737
done
 
1738
 
 
1739
euid=`$V5UTILBIN/uidgid -euid 2>&1`
 
1740
if [ $? -ne 0 ]; then
 
1741
   PrintErrorAndExit 1 "Execution of $V5UTILBIN/uidgid failed: $euid"
 
1742
fi
 
1743
if [ $euid = 0 ]; then
 
1744
   rootinstalls=true
 
1745
else
 
1746
   rootinstalls=false
 
1747
fi
 
1748
 
 
1749
ReadConfig $*
 
1750
 
 
1751
#--------------------------------------------------------------------------
 
1752
# SGE specific settings to keep previous behavior
 
1753
#--------------------------------------------------------------------------
 
1754
 
 
1755
ME=`whoami`
 
1756
if [ "$ME" = "" ]; then
 
1757
   PrintErrorAndExit 1 "Can't determine your username with \"%s\" command. Exit" whoami
 
1758
fi
 
1759
 
 
1760
CAKEY=cakey.pem
 
1761
CACERT=cacert.pem
 
1762
OPENSSL=$V5UTILBIN/openssl
 
1763
 
 
1764
AUTO="false"
 
1765
#-----------------------------
 
1766
# CommandLine Argument Parsing
 
1767
#
 
1768
WHICH="undef"
 
1769
newkey=newkey.pem
 
1770
newreq=newreq.pem
 
1771
newcert=newcert.pem
 
1772
user=""
 
1773
outdir=.
 
1774
indir=.
 
1775
userfile=""
 
1776
passinfile=""
 
1777
pkcs12outdir=""
 
1778
md="-md md5"
 
1779
nodes="-nodes"
 
1780
ksoutfile=""
 
1781
kspwfile=""
 
1782
 
 
1783
ARGC=$#
 
1784
while [ $ARGC != 0 ]; do
 
1785
   case $1 in
 
1786
   -adminuser)
 
1787
      ADMINUSER="$2"
 
1788
      shift
 
1789
      ARGC=`expr $ARGC - 1`
 
1790
      ;;
 
1791
   -auto)
 
1792
      AUTO="true"
 
1793
      . $2
 
1794
      shift
 
1795
      ARGC=`expr $ARGC - 1`
 
1796
      ;;
 
1797
   -init)
 
1798
      WHICH="init"
 
1799
      ;;
 
1800
   -req)
 
1801
      WHICH="req"
 
1802
      ;;
 
1803
   -sign)
 
1804
      WHICH="sign"
 
1805
      ;;
 
1806
   -copy)
 
1807
      WHICH="copy"
 
1808
      ;;
 
1809
   -nosge)
 
1810
      SGE_CNF=false
 
1811
      ;;
 
1812
   -verify)
 
1813
      if [ $ARGC -lt 2 ]; then
 
1814
         PrintError "-verify needs argument"
 
1815
         ErrUsage
 
1816
      fi
 
1817
      WHICH="verify"
 
1818
      newcert=$2
 
1819
      shift
 
1820
      ARGC=`expr $ARGC - 1`
 
1821
      ;;
 
1822
   -print)
 
1823
      if [ $ARGC -lt 2 ]; then
 
1824
         PrintError "-print needs argument"
 
1825
         ErrUsage
 
1826
      fi
 
1827
      WHICH="print"
 
1828
      newcert=$2
 
1829
      shift
 
1830
      ARGC=`expr $ARGC - 1`
 
1831
      ;;
 
1832
   -showCaTop)
 
1833
      WHICH="showCaTop"
 
1834
      ;;
 
1835
   -showCaLocalTop)
 
1836
      WHICH="showCaLocalTop"
 
1837
      ;;
 
1838
   -printkey)
 
1839
      if [ $ARGC -lt 2 ]; then
 
1840
         PrintError "-printkey needs argument"
 
1841
         ErrUsage
 
1842
      fi
 
1843
      WHICH="printkey"
 
1844
      newcert=$2
 
1845
      shift
 
1846
      ARGC=`expr $ARGC - 1`
 
1847
      ;;
 
1848
   -printcrl)
 
1849
      if [ $ARGC -lt 2 ]; then
 
1850
         PrintError "-printcrl needs argument"
 
1851
         ErrUsage
 
1852
      fi
 
1853
      WHICH="printcrl"
 
1854
      newcert=$2
 
1855
      shift
 
1856
      ARGC=`expr $ARGC - 1`
 
1857
      ;;
 
1858
   -days)
 
1859
      if [ $ARGC -lt 2 ]; then
 
1860
         PrintError "-days needs argument"
 
1861
         ErrUsage
 
1862
      fi
 
1863
      DAYS="-days $2"
 
1864
      shift
 
1865
      ARGC=`expr $ARGC - 1`
 
1866
      ;;
 
1867
   -outdir)
 
1868
      if [ $ARGC -lt 2 ]; then
 
1869
         PrintError "-outdir needs argument"
 
1870
         ErrUsage
 
1871
      fi
 
1872
      outdir="$2"
 
1873
      shift
 
1874
      ARGC=`expr $ARGC - 1`
 
1875
      ;;
 
1876
   -cahost)
 
1877
      if [ $ARGC -lt 2 ]; then
 
1878
         PrintError "-cahost needs argument"
 
1879
         ErrUsage
 
1880
      fi
 
1881
      CAHOST="$2"
 
1882
      shift
 
1883
      ARGC=`expr $ARGC - 1`
 
1884
      ;;
 
1885
   -cadir)
 
1886
      if [ $ARGC -lt 2 ]; then
 
1887
         PrintError "-cadir needs argument"
 
1888
         ErrUsage
 
1889
      fi
 
1890
      CATOP="$2"
 
1891
      CALOCALTOP="$2"
 
1892
      shift
 
1893
      ARGC=`expr $ARGC - 1`
 
1894
      ;;
 
1895
   -calocaltop)
 
1896
      if [ $ARGC -lt 2 ]; then
 
1897
         PrintError "-calocaltop needs argument"
 
1898
         ErrUsage
 
1899
      fi
 
1900
      CALOCALTOP="$2"
 
1901
      shift
 
1902
      ARGC=`expr $ARGC - 1`
 
1903
      ;;
 
1904
   -catop)
 
1905
      if [ $ARGC -lt 2 ]; then
 
1906
         PrintError "-catop needs argument"
 
1907
         ErrUsage
 
1908
      fi
 
1909
      CATOP="$2"
 
1910
      shift
 
1911
      ARGC=`expr $ARGC - 1`
 
1912
      ;;
 
1913
   -pkcs12)
 
1914
      WHICH="pkcs12"
 
1915
      if [ $ARGC -lt 2 ]; then
 
1916
         PrintError "-pkcs12 needs argument"
 
1917
         ErrUsage
 
1918
      fi
 
1919
      user="$2"
 
1920
      shift
 
1921
      ARGC=`expr $ARGC - 1`
 
1922
      ;;
 
1923
   -sdm_pkcs12)
 
1924
      WHICH="sdm_pkcs12"
 
1925
      if [ $ARGC -lt 2 ]; then
 
1926
         PrintError "-sdm_pkcs12 needs argument"
 
1927
         ErrUsage
 
1928
      fi
 
1929
      user="$2"
 
1930
      shift
 
1931
      ARGC=`expr $ARGC - 1`
 
1932
      ;;
 
1933
   -sys_pkcs12)
 
1934
      WHICH="sys_pkcs12"
 
1935
      if [ $ARGC -lt 2 ]; then
 
1936
         PrintError "-sys_pkcs12 needs argument"
 
1937
         ErrUsage
 
1938
      fi
 
1939
      user="$2"
 
1940
      shift
 
1941
      ARGC=`expr $ARGC - 1`
 
1942
      ;;
 
1943
   -ks)
 
1944
      WHICH="ks"
 
1945
      if [ $ARGC -lt 2 ]; then
 
1946
         PrintError "-ks needs argument"
 
1947
         ErrUsage
 
1948
      fi
 
1949
      user="$2"
 
1950
      shift
 
1951
      ARGC=`expr $ARGC - 1`
 
1952
      ;;
 
1953
   -ksout)   
 
1954
      if [ $ARGC -lt 2 ]; then
 
1955
         PrintError "-ksout needs argument"
 
1956
         ErrUsage
 
1957
      fi
 
1958
      ksoutfile="$2"
 
1959
      shift
 
1960
      ARGC=`expr $ARGC - 1`
 
1961
      ;;
 
1962
   -kspwf)   
 
1963
      if [ $ARGC -lt 2 ]; then
 
1964
         PrintError "-kspwf needs argument"
 
1965
         ErrUsage
 
1966
      fi
 
1967
      kspwfile="$2"
 
1968
      shift
 
1969
      ARGC=`expr $ARGC - 1`
 
1970
      ;;
 
1971
   -pkcs12pwf)   
 
1972
      if [ $ARGC -lt 2 ]; then
 
1973
         PrintError "-pkcs12pwf needs argument"
 
1974
         ErrUsage
 
1975
      fi
 
1976
      passinfile="$2"
 
1977
      shift
 
1978
      ARGC=`expr $ARGC - 1`
 
1979
      ;;
 
1980
   -pkcs12dir)   
 
1981
      if [ $ARGC -lt 2 ]; then
 
1982
         PrintError "-pkcs12dir needs argument"
 
1983
         ErrUsage
 
1984
      fi
 
1985
      pkcs12outdir="$2"
 
1986
      shift
 
1987
      ARGC=`expr $ARGC - 1`
 
1988
      ;;
 
1989
   -sha1)
 
1990
      md="-md sha1"
 
1991
      ;;
 
1992
   -encryptkey)
 
1993
      nodes=""
 
1994
      ;;
 
1995
   -usercert)
 
1996
      WHICH="usercert"
 
1997
      if [ $ARGC -lt 2 ]; then
 
1998
         PrintError "-usercert needs argument"
 
1999
         ErrUsage
 
2000
      fi
 
2001
      userfile=$2
 
2002
      shift
 
2003
      ARGC=`expr $ARGC - 1`
 
2004
      ;;
 
2005
   -userks)
 
2006
      WHICH="userks"
 
2007
      ;;
 
2008
   -sysks)
 
2009
      WHICH="sysks"
 
2010
      ;;
 
2011
   -user)
 
2012
      WHICH="oneuser"
 
2013
      if [ $ARGC -lt 2 ]; then
 
2014
         PrintError "-user needs argument"
 
2015
         ErrUsage
 
2016
      fi
 
2017
      user=$2
 
2018
      shift
 
2019
      ARGC=`expr $ARGC - 1`
 
2020
      ;;
 
2021
   -sdm_daemon)
 
2022
      WHICH="sdm_daemon"
 
2023
      if [ $ARGC -lt 2 ]; then
 
2024
         PrintError "-sdm_daemon needs argument"
 
2025
         ErrUsage
 
2026
      fi
 
2027
      user=$2
 
2028
      shift
 
2029
      ARGC=`expr $ARGC - 1`
 
2030
      ;;
 
2031
   -renew)
 
2032
      if [ $ARGC -lt 2 ]; then
 
2033
         PrintError "-renew needs argument"
 
2034
         ErrUsage
 
2035
      fi
 
2036
      WHICH="renew"
 
2037
      user=$2
 
2038
      shift
 
2039
      ARGC=`expr $ARGC - 1`
 
2040
      ;;
 
2041
   -renew_sdm)
 
2042
      if [ $ARGC -lt 2 ]; then
 
2043
         PrintError "-renew_sdm needs argument"
 
2044
         ErrUsage
 
2045
      fi
 
2046
      WHICH="renew_sdm"
 
2047
      user=$2
 
2048
      shift
 
2049
      ARGC=`expr $ARGC - 1`
 
2050
      ;;
 
2051
   -renew_sys)
 
2052
      WHICH="renew_sys"
 
2053
      ;;
 
2054
   -renew_ca)
 
2055
      WHICH="renew_ca"
 
2056
      ;;
 
2057
   -version)
 
2058
      WHICH="version"
 
2059
      ;;
 
2060
   -dumpusers)
 
2061
      WHICH="dumpusers"
 
2062
      ;;
 
2063
   *)
 
2064
      PrintError "Unknown option %s" "$1"
 
2065
      ErrUsage
 
2066
      ;;
 
2067
   esac
 
2068
   shift
 
2069
   ARGC=`expr $ARGC - 1`
 
2070
done
 
2071
 
 
2072
if [ "$WHICH" = "undef" ]; then
 
2073
   ErrUsage
 
2074
fi
 
2075
 
 
2076
#echo "ADMINUSER: $ADMINUSER" 
 
2077
#echo "CATOP: $CATOP" 
 
2078
#echo "CALOCALTOP: $CALOCALTOP" 
 
2079
#echo "CAHOST: $CAHOST" 
 
2080
 
 
2081
if [ "$CATOP" = "" ]; then
 
2082
   ErrUsage "catop not set"
 
2083
fi
 
2084
if [ "$CALOCALTOP" = "" ]; then
 
2085
   ErrUsage "calocaltop not set"
 
2086
fi
 
2087
if [ "$ADMINUSER" = "" ]; then
 
2088
   ErrUsage "adminuser not set"
 
2089
fi
 
2090
if [ "$CAHOST" = "" ]; then
 
2091
   ErrUsage "cahost not set"
 
2092
fi
 
2093
 
 
2094
 
 
2095
export ADMINUSER CATOP CALOCALTOP CAHOST
 
2096
 
 
2097
REQ="$OPENSSL req $nodes"
 
2098
 
 
2099
CA="$OPENSSL ca $md -keyfile $CALOCALTOP/private/$CAKEY -cert $CATOP/$CACERT \
 
2100
             -outdir $CATOP/newcerts"
 
2101
 
 
2102
VERIFY="$OPENSSL verify"
 
2103
 
 
2104
X509="$OPENSSL x509"
 
2105
X509KEY="$OPENSSL rsa"
 
2106
X509CRL="$OPENSSL crl"
 
2107
 
 
2108
P12="$OPENSSL pkcs12"
 
2109
 
 
2110
case $WHICH in
 
2111
init)
 
2112
   InitCA  
 
2113
   ;;
 
2114
req)
 
2115
   RequestCert
 
2116
   ;;
 
2117
sign)
 
2118
   SignCert
 
2119
    ;;
 
2120
copy) 
 
2121
    InstallKey user $ME
 
2122
    ;;
 
2123
usercert) 
 
2124
    MakeUserCerts $userfile
 
2125
    ;;
 
2126
userks) 
 
2127
    MakeUserKeystores
 
2128
    ;;
 
2129
sysks) 
 
2130
    MakeSysKeystore
 
2131
    ;;
 
2132
oneuser)
 
2133
    MakeUserCert user "$user"
 
2134
    if [ $? != 0 ]; then
 
2135
       exit 1
 
2136
    fi
 
2137
    ;;
 
2138
sdm_daemon)
 
2139
    MakeUserCert sdm_daemon $user
 
2140
    if [ $? != 0 ]; then
 
2141
       exit 1
 
2142
    fi
 
2143
    ;;
 
2144
verify)
 
2145
    VerifyCert
 
2146
    ;;
 
2147
print)
 
2148
    PrintX509 cert $newcert
 
2149
    ;;
 
2150
printkey) 
 
2151
    PrintX509 key $newcert
 
2152
    ;;
 
2153
printcrl) 
 
2154
    PrintX509 crl $newcert
 
2155
    ;;
 
2156
renew) 
 
2157
    RenewCert user $user
 
2158
    if [ $? != 0 ]; then
 
2159
       exit 1
 
2160
    fi
 
2161
    ;;
 
2162
renew_sdm) 
 
2163
    RenewCert sdm_daemon $user
 
2164
    if [ $? != 0 ]; then
 
2165
       exit 1
 
2166
    fi
 
2167
    ;;
 
2168
renew_sys) 
 
2169
    RenewCert daemon $ADMINUSER
 
2170
    if [ $? != 0 ]; then
 
2171
       exit 1
 
2172
    fi
 
2173
    ;;
 
2174
renew_ca) 
 
2175
    RenewCA
 
2176
    if [ $? != 0 ]; then
 
2177
       exit 1
 
2178
    fi
 
2179
    ;;
 
2180
pkcs12) 
 
2181
    MakePKCS12 user $user
 
2182
    if [ $? != 0 ]; then
 
2183
       exit 1
 
2184
    fi
 
2185
    ;;
 
2186
ks) 
 
2187
    MakeKeystore user $user
 
2188
    if [ $? != 0 ]; then
 
2189
       exit 1
 
2190
    fi
 
2191
    ;;
 
2192
sdm_pkcs12) 
 
2193
    MakePKCS12 sdm_daemon $user
 
2194
    if [ $? != 0 ]; then
 
2195
       exit 1
 
2196
    fi
 
2197
    ;;
 
2198
sys_pkcs12) 
 
2199
    MakePKCS12 sge_daemon $user
 
2200
    if [ $? != 0 ]; then
 
2201
       exit 1
 
2202
    fi
 
2203
    ;;
 
2204
showCaTop)
 
2205
    echo "$CATOP"
 
2206
    ;;
 
2207
showCaLocalTop)
 
2208
    echo "$CALOCALTOP"
 
2209
    ;;
 
2210
version)
 
2211
    echo "sge_ca 6.5"
 
2212
    ;;
 
2213
dumpusers)
 
2214
    DumpUsers
 
2215
    ;;
 
2216
help)
 
2217
    ErrUsage
 
2218
    ;;
 
2219
esac
 
2220
 
 
2221
exit 0