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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
#!/bin/sh
#
# Grid Engine CA framework script
#
#___INFO__MARK_BEGIN__
##########################################################################
#
#  The Contents of this file are made available subject to the terms of
#  the Sun Industry Standards Source License Version 1.2
#
#  Sun Microsystems Inc., March, 2001
#
#
#  Sun Industry Standards Source License Version 1.2
#  =================================================
#  The contents of this file are subject to the Sun Industry Standards
#  Source License Version 1.2 (the "License"); You may not use this file
#  except in compliance with the License. You may obtain a copy of the
#  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
#
#  Software provided under this License is provided on an "AS IS" basis,
#  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
#  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
#  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
#  See the License for the specific provisions governing your rights and
#  obligations concerning the Software.
#
#  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
#
#  Copyright: 2001 by Sun Microsystems, Inc.
#
#  All Rights Reserved.
#
##########################################################################
#___INFO__MARK_END__
#
# set -x

# Reset PATH to a safe value  
#
PATH=/bin:/usr/bin:/usr/sbin:/usr/bsd:/usr/ucb

# Easy way to prevent clearing of screen
#
CLEAR=clear
#CLEAR=:


#-------------------------------------------------------------------------
# USEFUL LOCAL SHELL PROCEDURES

#-------------------------------------------------------------------------
# PrintError
PrintError() 
{
   fmt="Error: $1"
   shift
   $INFOTEXT -e "$fmt" $*
   $INFOTEXT -log "$fmt" $*
}

PrintErrorAndExit()
{
   exitcode=$1
   shift
   fmt="$1"
   shift
   PrintError "$fmt" $*
   exit $exitcode
}

#-------------------------------------------------------------------------
# ErrUsage: print usage string, exit
#
ErrUsage()
{
   if [ $# -gt 0 ]; then
      PrintError $*
   fi
   myname=`basename $0`
   $INFOTEXT -e \
   "\nUsage: %s <command>\n" \
   "   -adminuser <user>   set admin user\n" \
   "   -init               initialize CA infrastructure\n" \
   "   -req                generate a certificate request and private key\n" \
   "   -sign               sign a certificate request\n" \
   "   -copy               install user certificate and private key\n" \
   "   -verify <cert>      verify a <cert>\n" \
   "   -print <cert>       print a <cert>\n" \
   "   -printkey <key>     print a <key>\n" \
   "   -printcrl <crl>     print a <crl>\n" \
   "   -renew <user>       extend the certificate of <user>\n" \
   "   -renew_ca           extend the CA certificate\n" \
   "   -renew_sys          extend the daemon certificate\n" \
   "   -renew_sdm <g>      renew certificate of a SDM daemon with\n" \
   "                       g=Common Name of the SDM daemon\n" \
   "   -days <days>        days of validity of the certificate\n" \
   "   -sha1               use sha-1 instead of md5 as message digest\n" \
   "   -encryptkey         use des to encrypt the generated key with a passphrase\n" \
   "   -outdir <dir>       write to directory <dir>\n" \
   "   -cahost <host>      define CA hostname (CA master host)\n" \
   "   -cadir <dir>        define CALOCALTOP and CATOP settings\n" \
   "   -calocaltop <dir>   define CALOCALTOP setting\n" \
   "   -catop <dir>        define CATOP setting\n" \
   "   -pkcs12 <user>      generate pkcs12 format file for user <user> \n" \
   "   -pkcs12pwf <file>   pkcs12 password file\n" \
   "   -pkcs12dir <dir>    pkcs12 output directory\n" \
   "   -usercert <file>    generate certificates and keys for the users in <file>\n" \
   "   -userks             generate keystore for existing users\n" \
   "   -user <u:g:e>       generate certificates and keys for <u:g:e>\n" \
   "                       with u=Unix User, g=Common Name, e=email\n" \
   "   -sdm_daemon <u:g:e> generate certificate and key for a SDM daemon\n" \
   "                       with u=Unix User, g=Common Name, e=email\n" \
   "   -sdm_pkcs12 <g>     generate pkcs12 format file for SDM daemon\n" \
   "                       with g=Common Name of the SDM daemon\n" \
   "   -sys_pkcs12 <g>     generate pkcs12 format file for SGE daemon\n" \
   "                       with g=Common Name of the SGE daemon\n" \
   "   -ks <user>          generate a keystore file for <user>\n" \
   "   -kspwf <file>       keystore pw file\n" \
   "   -ksout <file>       keystore output file\n" \
   "   -sysks              generate keystore for SGE daemon\n" \
   "   -showCaTop          echo caTop path\n" \
   "   -showCaLocalTop     echo caLocalTop path\n" \
   $myname

   exit 1
}

#-------------------------------------------------------------------------
# Enter: input is read and returned to stdout. If input is empty echo $1
#
# USES: variable "$autoinst" 
#
Enter()
{
   if [ "$autoinst" = true ]; then
      $INFOTEXT $1
   else
      read INP
      if [ "$INP" = "" ]; then
         $INFOTEXT $1
      else
         $INFOTEXT $INP
      fi
   fi
}


#-------------------------------------------------------------------------
# Execute command as user $ADMINUSER and exit if exit status != 0
# if ADMINUSER = default then execute command unchanged
#
# uses binary "adminrun" form SGE distribution
#
# USES: variables "$verbose"    (if set to "true" print arguments)
#                  $ADMINUSER   (if set to "default" do not use "adminrun)
#                 "$V5UTILBIN"  (path to the binary in utilbin)
#
ExecuteAsAdmin()
{
   if [ "$verbose" = true ]; then
      $ECHO $*
   fi

   if [ $ADMINUSER = default ]; then
      $*
   else
      $V5UTILBIN/adminrun $ADMINUSER "$@"
   fi

   return $?
}

#-------------------------------------------------------------------------
# Execute command and return exit status
#
Execute()
{
   if [ "$verbose" = true ]; then
      $ECHO $*
   fi
   $*
   return $?
}

ExecRm() {
  Execute $RM -rf $* > /dev/null 2>&1
  files=""
  for i in $*; do
     if [ -f $i -o -d $i ]; then
        files="$files $i" 
     fi 
  done
  if [ "$files" != "" ]; then
    sleep 1
    for i in $files; do
       Execute $RM -rf $i  > /dev/null 2>&1
       if [ $? -ne 0 ]; then
          return 1
       fi
    done
  fi
  return 0
}

ExecRmAsAdmin() {
  ExecuteAsAdmin $RM -rf $*  > /dev/null 2>&1
  files=""
  for i in $*; do
     if [ -f $i -o -d $i ]; then
        files="$files $i" 
     fi 
  done
  if [ "$files" != "" ]; then
    sleep 1
    for i in $files; do
       ExecuteAsAdmin $RM -rf $i  > /dev/null 2>&1
       if [ $? -ne 0 ]; then
          return 1
       fi
    done
  fi
  return 0
}

#-------------------------------------------------------------------------
# Change the ownership of a file or directory
# Is only executed as root
#
ExecChown()
{
   if [ $rootinstalls = true ]; then
      Execute $CHOWN $*
      return $?
   else
      return 0
   fi
   
}

#--------------------------------------------------------------------------
# InitCA Init CA directories and get DN info
#
InitCA()
{
   $CLEAR
   $INFOTEXT -u "\nInitializing Certificate Authority (CA) for OpenSSL security framework"

   if [ -d $CATOP -a -d $CALOCALTOP ]; then
      $INFOTEXT -e "\nThere are already directories of the CA infrastructure in\n   %s\n   or\n   %s\n" "$CATOP" "$CALOCALTOP"
      $INFOTEXT -auto $AUTO -ask "y" "n" -def "y" -n \
         "Do you want to recreate your SGE CA infrastructure (y/n) [y] >> "
      if [ $? != 0 ]; then
         $INFOTEXT "We will not reinitialize your SGE CA infrastructure."
         exit 0
      fi
   fi

   MakeCADirs
   if [ $? != 0 ]; then
      PrintErrorAndExit 1 "CA initialization failed. Exit."
   fi
   MakeCAcert
   if [ $? -ne 0 ]; then
      PrintErrorAndExit 1 "CA initialization failed. Exit."
   fi
 
   MakeCert daemon $ME "SGE Daemon" none
   if [ $? -ne 0 ]; then
      PrintErrorAndExit 1 "CA initialization failed. Exit."
   fi
   
   MakeCert user $ME "SGE install user" none
   if [ $? -ne 0 ]; then
      PrintErrorAndExit 1 "CA initialization failed. Exit."
   fi

   if [ "$ADMINUSER" != default ]; then
      MakeCert user $ADMINUSER "SGE admin user" none
      if [ $? -ne 0 ]; then
         PrintErrorAndExit 1 "CA initialization failed. Exit."
      fi
   fi
}

#---------------------------------------------------------------------------
#  create a certificate request
#
RequestCert()
{
    # create a certificate request
    $REQ -new -keyout $outdir/$newkey -out $outdir/$newreq $DAYS
    RET=$?
    if [ $RET = 0 ]; then
      $INFOTEXT "Request is in %s" $outdir/$newreq
      $INFOTEXT "Private key is in %s" $outdir/$newkey
    else
      PrintErrorAndExit 1 "Creating a certificate request failed"
    fi
}

#---------------------------------------------------------------------------
#  Sign a certificate
#
SignCert()
{
    $CA -config $SGE_ROOT/util/sgeCA/sge_ssl.cnf -policy policy_anything $DAYS $BATCHMODE -notext -out $outdir/$newcert -infiles $indir/$newreq
    RET=$?
    if [ $RET = 0 ]; then
      $INFOTEXT "Signed certificate is in %s" $outdir/$newcert
    else
      PrintErrorAndExit 1 "Signing a certificate failed"
    fi
}

#---------------------------------------------------------------------------
#  verify a certificate
#
VerifyCert() 
{
   $VERIFY -CAfile $CATOP/$CACERT $newcert
   if [ $? != 0 ]; then
      PrintErrorAndExit 1 "Verification of certificate failed"
   fi
}

PrintX509() 
{
   if [ $1 = "cert" ]; then
      $X509 -in $2 -text
   elif [ $1 = "key" ]; then
      $X509KEY -in $newcert -text
   elif [ $1 = "crl" ]; then
      $X509CRL -in $newcert -text
   else
      PrintErrorAndExit 1 "Can not print %s" $1
   fi
   
   if [ $? != 0 ]; then
      PrintErrorAndExit 1 "Printing %s failed (%s)" $1 $2
   fi
}


#---------------------------------------------------------------------------
# CheckIfCaHost
#    If our hostname given in $1 is the same as "CAHOST"
#    echo "true" else echo "false"
#
# $1 = hostname
#
CheckIfCaHost()
{
   host=$1

   if [ "$host" = "$CAHOST" ]; then
      echo true
   else
      echo false
   fi
}

#--------------------------------------------------------------------------
# InstallKey
#
# $1 = user | daemon
# $2 = $ME ( username )
#
InstallKey()
{
   HOST=`$V5UTILBIN/gethostname -aname`
   result=`CheckIfCaHost $HOST`
   if [ "$result" != "true" ]; then 
      PrintError "You can install your private key and certificate only on the master host."
      return 1
   fi 

   if [ -d $CALOCALTOP/userkeys ]; then
      userkeydir=$CALOCALTOP/userkeys
   else
      PrintError "Can not find local userkey directory."
      return 1
   fi   

   if [ $1 = daemon ]; then
      keyfile=$CALOCALTOP/private/key.pem
      certfile=$CATOP/certs/cert.pem
      randfile=$CALOCALTOP/private/rand.seed
   else
      keyfile=$userkeydir/$ME/key.pem
      certfile=$userkeydir/$ME/cert.pem
      randfile=$userkeydir/$ME/rand.seed
      HOMEDIR=$HOME
   fi

   basedir=$CAHOMEKEYDIR
   if [ ! -d $basedir ]; then
      $V5UTILBIN/adminrun $2 $MKDIR -p $basedir
   fi

   if [ ! -d $basedir/certs ]; then
      $V5UTILBIN/adminrun $2 $MKDIR $basedir/certs
   fi

   if [ ! -d $basedir/private ]; then
      $V5UTILBIN/adminrun $2 $MKDIR $basedir/private
   fi

   if [ -f $basedir/private/key.pem ]; then
      $V5UTILBIN/adminrun $2 rm -f $basedir/private/key.pem
   fi

   if [ -f $basedir/private/rand.seed ]; then
      $V5UTILBIN/adminrun $2 rm -f $basedir/private/rand.seed
   fi

   if [ -f $basedir/certs/cert.pem ]; then
      $V5UTILBIN/adminrun $2 rm -f $basedir/certs/cert.pem
   fi

   $V5UTILBIN/adminrun $2 $CP $keyfile $basedir/private
   if [ $? -ne 0 ]; then
      PrintError "Could not copy key file (%s -> %s)" $keyfile $basedir/private
      return 1
   fi
   $V5UTILBIN/adminrun $2 $CP $randfile $basedir/private
   if [ $? -ne 0 ]; then
      PrintError "Could not copy rand file (%s -> %s)" $randfile $basedir/private
      return 1
   fi
   
   $V5UTILBIN/adminrun $2 $CHMOD 700 $basedir/private
   if [ $? -ne 0 ]; then
      PrintError "chmod for %s on private dir failed (%s)" $2 $basedir/private
      return 1
   fi
   
   $V5UTILBIN/adminrun $2 $CHMOD 600 $basedir/private/*
   if [ $? -ne 0 ]; then
      PrintError "chmod for %s on private files failed (%s/\*)" $2 $basedir/private
      return 1
   fi

   $V5UTILBIN/adminrun $2 $CP $certfile $basedir/certs
   if [ $? -ne 0 ]; then
      PrintError "Could not copy cert file (%s -> %s)" $certfile $basedir/certs
      return 1
   fi
   
   $V5UTILBIN/adminrun $2 $CHMOD 755 $basedir/certs
   if [ $? -ne 0 ]; then
      PrintError "chmod for %s on certs dir failed (%s)" $2 $basedir/certs
      return 1
   fi
   
   $V5UTILBIN/adminrun $2 $CHMOD 644 $basedir/certs/*
   if [ $? -ne 0 ]; then
      PrintError "chmod for %s on cert files failed (%s/\*)" $2 $basedir/certs
      return 1
   fi

   $INFOTEXT "Certificate and private key for user %s have been installed" $ME

   return 0
}


#--------------------------------------------------------------------------
# MakeCAcert create CA certificate and private key
#
#
MakeCAcert()
{
   $INFOTEXT -u "\nCreating CA certificate and private key"
   $INFOTEXT -e "Please give some basic parameters to create the distinguished name (DN)\n" \
   "for the certificates.\n\n" \
   "We will ask for\n" \
   "   - the two letter country code\n" \
   "   - the state\n" \
   "   - the location, e.g city or your buildingcode\n" \
   "   - the organization (e.g. your company name)\n" \
   "   - the organizational unit, e.g. your department\n" \
   "   - the email address of the CA administrator (you!)\n"

   $INFOTEXT -wait -auto $AUTO -n "\nHit <RETURN> to continue >> "
   # $CLEAR

   done=false
   while [ $done = false ]; do

      if [ "$AUTO" = "true" ]; then
            CA_C=`echo $CSP_COUNTRY_CODE | env LC_ALL=C tr "[a-z]" "[A-Z]"`
            CA_ST="$CSP_STATE"
            CA_L="$CSP_LOCATION"
            CA_O="$CSP_ORGA"
            CA_OU="$CSP_ORGA_UNIT"
            CA_EMAIL="$CSP_MAIL_ADDRESS"
      else
         dndone=false
         while [ $dndone = false ]; do
            $INFOTEXT -n "Please enter your two letter country code, e.g. 'US' >> "
            INP=`Enter ""`
            if [ "$INP != " -a `echo $INP | wc -c` = 3 ]; then
               CA_C=`echo $INP | env LC_ALL=C tr "[a-z]" "[A-Z]"`
               dndone=true
            fi
         done

         dndone=false
         while [ $dndone = false ]; do
            $INFOTEXT -n "Please enter your state >> "
            INP=`Enter ""`
            CA_ST="$INP"
            if [ "$INP" != "" ]; then
               CA_ST="$INP"
               dndone=true
            fi
         done

         dndone=false
         while [ $dndone = false ]; do
            $INFOTEXT -n "Please enter your location, e.g city or buildingcode >> "
            INP=`Enter ""`
            CA_L="$INP"
            if [ "$INP" != "" ]; then
               CA_L="$INP"
               dndone=true
            fi
         done

         dndone=false
         while [ $dndone = false ]; do
            $INFOTEXT -n "Please enter the name of your organization >> "
            INP=`Enter ""`   
            if [ "$INP" != "" ]; then
               CA_O="$INP"
               dndone=true
            fi
         done

         dndone=false
         while [ $dndone = false ]; do
            $INFOTEXT -n "Please enter your organizational unit, e.g. your department >> "
            INP=`Enter ""`    
            CA_OU="$INP"
            if [ "$INP" != "" ]; then
               CA_OU="$INP"
               dndone=true
            fi
         done

         dndone=false
         while [ $dndone = false ]; do
            $INFOTEXT -n "Please enter the email address of the CA administrator >> "
            INP=`Enter ""`   
            if [ "$INP" != "" ]; then
               CA_EMAIL="$INP"
               dndone=true
            fi
         done
      fi


      $CLEAR
      $INFOTEXT -e "\nYou selected the following basic data for the distinguished name of\n" \
      "your certificates:\n\n" \
      "Country code:         %s=%s\n" \
      "State:                %s=%s\n" \
      "Location:             %s=%s\n" \
      "Organization:         %s=%s\n" \
      "Organizational unit:  %s=%s\n" \
      "CA email address:     %s=%s\n" \
      C "$CA_C" ST "$CA_ST" L "$CA_L" O "$CA_O" OU "$CA_OU" emailAddress "$CA_EMAIL"
      
      $INFOTEXT -auto $AUTO -ask "y" "n" -def "y" -n \
         "Do you want to use these data (y/n) [y] >> "
      if [ $? = 0 ]; then
         done=true
 
         TMPFILE=/tmp/sge_ca$$.tmp
         TMPFILE1=/tmp/sge_ca1$$.tmp
         ExecRm $TMPFILE $TMPFILE1
         if [ $? -ne 0 ]; then
            PrintError "Could not delete file %s" $TMPFILE
            return 1
         fi
         
         ExecRm $TMPFILE1
         if [ $? -ne 0 ]; then
            PrintError "Could not delete file %s" $TMPFILE1
            return 1
         fi
         
         $TOUCH $TMPFILE
         if [ $? -ne 0 ]; then
            PrintError "Could not touch file %s" $TMPFILE
            return 1
         fi
         $TOUCH $TMPFILE1
         if [ $? -ne 0 ]; then
            PrintError "Could not touch file %s" $TMPFILE1
            return 1
         fi
         
         echo C="$CA_C" >> $TMPFILE
         echo ST="$CA_ST" >> $TMPFILE
         echo L="$CA_L" >> $TMPFILE
         echo O="$CA_O" >> $TMPFILE
         echo OU="$CA_OU" >> $TMPFILE
         ExecuteAsAdmin $CP $TMPFILE $CATOP/dn.info
         if [ $? -ne 0 ]; then
            PrintError "Could not copy file (%s -> %s)" $TMPFILE $CATOP/dn.info
            return 1
         fi
         
         echo CN="SGE Certificate Authority" >> $TMPFILE
         echo userId=CA >> $TMPFILE
         echo emailAddress=$CA_EMAIL >> $TMPFILE

         export CATOP
         Execute cat $CONFIG_DIR/sge_ssl_template.cnf $TMPFILE > $TMPFILE1
         if [ $? -ne 0 ]; then
            PrintError "Could not cat file (%s)" $CONFIG_DIR/sge_ssl_template.cnf
            return 1
         fi

         if [ $ADMINUSER != default -a $rootinstalls = true ]; then
            # echo +++ $ADMINUSER
            MakeRandFile $CALOCALTOP/private/rand.seed $ADMINUSER
         else
            # echo +++ $ME
            MakeRandFile $CALOCALTOP/private/rand.seed $ME
         fi
         if [ $? -ne 0 ]; then
            return 1
         fi
         RANDFILE=$CALOCALTOP/private/rand.seed; export RANDFILE

         umask 077
         $INFOTEXT "Creating CA certificate and private key"
         ExecuteAsAdmin $REQ -config $TMPFILE1 -new -x509 \
              -keyout ${CALOCALTOP}/private/$CAKEY -out ${CATOP}/$CACERT $DAYS
         status=$?
         umask 022

         ExecuteAsAdmin $CHMOD 644 ${CATOP}/$CACERT
         if [ $? -ne 0 ]; then
            PrintError "chmod as %s failed (%s)" $ADMINUSER ${CATOP}/$CACERT
            return 1
         fi
         rm -f $TMPFILE $TMPFILE1

         if [ $status != 0 ]; then
            PrintError "Failed to create CA certificate and private key. Exit"
            return 1
         fi

      else
         $CLEAR
      fi
   done

   $INFOTEXT -wait -auto $AUTO -n "\nHit <RETURN> to continue >> "
   $CLEAR
   return 0
}

#--------------------------------------------------------------------------
# MakeRandFile create a random data file
#  
# $1 = <randfilename>
# $2 = <owner>
#
MakeRandFile()
{

   # OpenSSL uses /dev/urandom by default so if it's there, don't
   # polute the PRNG with predictable data.
   rfile="/dev/urandom"
   if [ ! -r /dev/urandom ]; then
      if [ -r /dev/random ]; then 
         rfile=/dev/random
      elif [ -r /bin/vi ]; then
         rfile="/bin/vi"
      fi
   fi
   # $INFOTEXT "Creating RANDFILE from '%s' in '%s'" $rfile $1

   RANDFILE=/tmp/.rand.$$; export RANDFILE;
   $V5UTILBIN/adminrun $2 dd if=$rfile of=$RANDFILE count=2048 > /dev/null 2>&1
   if [ $? -lt 0 ]; then
       PrintError "Could not create random number for (%s -> %s)" $rfile $RANDFILE
       return 1
   fi
   $V5UTILBIN/adminrun $2 $OPENSSL rand -rand $RANDFILE -out $1 2048  > /dev/null 2>&1
   if [ $? -ne 0 ]; then
       PrintError "bootstraping of rand command as user %s failed (%s -> %s)" $2 $RANDFILE $1
       return 1
   fi
   
   $V5UTILBIN/adminrun $2 $OPENSSL rand -rand $rfile -out $1 2048  > /dev/null 2>&1
   if [ $? -ne 0 ]; then
       PrintError "openssl rand command as user %s failed (%s -> %s)" $2 $rfile $1
       return 1
   fi

   $V5UTILBIN/adminrun $2 $RM $RANDFILE > /dev/null 2>&1
   if [ $? -ne 0 ]; then
       PrintError "Could not delete file %s as user %s" $RANDFILE $2
       return 1
   fi
   
   $V5UTILBIN/adminrun $2 $CHMOD 644 $1 > /dev/null 2>&1
   if [ $? -ne 0 ]; then
       PrintError "chmod as user %s on file %s failed" $2 $1
       return 1
   fi
   
   # echo "--------------"
   # ls -l $rfile $1
   # echo "--------------"

}


#--------------------------------------------------------------------------
# MakeUserCert create user certificates and keys for Windows Admin user
#
# $1 = certificate type "user" or "sdm_daemon"
# $2 = <user:gecos:email>
#
MakeUserCert()
{
   cert_type=$1
   line="$2"

   unixuser=`echo $line|cut -d: -f1`
   gecos=`echo $line|cut -d: -f2`
   email=`echo $line|cut -d: -f3`

   # echo $unixuser:$gecos:$email
   if [ "$unixuser" = "" ]; then
      PrintErrorAndExit 1 "no Unix user specified. Exit."
   fi   

   if [ "$gecos" = "" ]; then
      gecos=$unixuser
   fi   
   if [ "$email" = "" ]; then
      email=$unixuser
   fi   

   if [ "$ADMINUSER" = default ]; then
      entries=`grep "CN=$gecos" $CATOP/index.txt|grep '^V'|wc -l`
   else 
      entries=`$V5UTILBIN/adminrun $ADMINUSER grep "CN=$gecos" $CATOP/index.txt|grep '^V'|wc -l`
   fi
   if [ $entries = 0 ]; then
      $INFOTEXT "Generating %s certificate and key for '%s' ('%s','%s')." \
         "$cert_type" "$unixuser" "$gecos" "$email"
      MakeCert $cert_type "$unixuser" "$gecos" "$email"
      if [ $? -ne 0 ]; then
         return 1
      fi
   else
      PrintError "Please renew %s certificate for '%s' ('%s','%s') instead." \
                 "$cert_type" "$unixuser" "$gecos" "$email"
      # RenewCert user $unixuser
      return 1
   fi
   return 0
}

#--------------------------------------------------------------------------
# MakeUserCerts create user certificates and keys from userfile
# userfile contains a list of entries of the following format per user.
#
# <unix username>:<gecos field>:<user's email address>
#
# $1 = <userfile>
#
MakeUserCerts()
{
   if [ ! -f "$1" ]; then
      PrintErrorAndExit 1 "no valid userfile '%s'. Exit." $1
   fi

   userfile=$1

   cat $userfile | while read line; do
      MakeUserCert user "$line"
   done   
}

#--------------------------------------------------------------------------
# MakeUserKeystores create keystores for existing users in 
# $CALOCALTOP/userkeys 
#
MakeUserKeystores()
{
   if [ "$ksoutfile" = "" ]; then
      ksoutfile_saved=$ksoutfile
   fi

   remove_kspwfile=false
   if [ "$kspwfile" = "" ]; then
      touch /tmp/tmp.$$
      kspwfile=/tmp/tmp.$$
      remove_kspwfile=true
   fi   
   for user in `ls $CALOCALTOP/userkeys`; do
      certfile=$CALOCALTOP/userkeys/$user/cert.pem
      if [ -r $certfile ]; then 
         ksoutfile=$CALOCALTOP/userkeys/$user/keystore
         touch $ksoutfile
         $CHOWN $user $ksoutfile
         $CHMOD 600 $ksoutfile
         MakeKeystore user "$user"
      fi
   done   
   if [ "$remove_kspwfile" = "true" ]; then
      rm $kspwfile
   fi
   ksoutfile=$ksoutfile_saved
}

#--------------------------------------------------------------------------
# MakeSysKeystore create keystore for SGE Daemon
# $CALOCALTOP/private 
#
MakeSysKeystore()
{
   if [ "$ksoutfile" = "" ]; then
      ksoutfile_saved=$ksoutfile
   fi

   remove_kspwfile=false
   if [ "$kspwfile" = "" ]; then
      touch /tmp/tmp.$$
      kspwfile=/tmp/tmp.$$
      remove_kspwfile=true
   fi   
   certfile=$CATOP/certs/cert.pem
   if [ -r $certfile ]; then 
      ksoutfile=$CALOCALTOP/private/keystore
      ExecuteAsAdmin touch $ksoutfile
      ExecuteAsAdmin chmod 600 $ksoutfile
      MakeKeystore sge_daemon "SGEDaemon"
   fi
   if [ "$remove_kspwfile" = "true" ]; then
      rm $kspwfile
   fi
   ksoutfile=$ksoutfile_saved
}



#--------------------------------------------------------------------------
# MakeKeystore create keystore file of user's certificate and key
#
# $1 = user or sge_daemon
# $2 = <unix user>
#
MakeKeystore()
{
   type=$1
   if [ "$type" != "user" -a "$type" != "sge_daemon" ]; then
      PrintError "unknown type $type only user or sge_daemon allowed"
      return 1
   fi
   myuser=$2
   if [ "$myuser" = "" ]; then 
      PrintError "username is empty"
      return 1
   fi
   CA_ARGS="-catop $CATOP"
   CA_ARGS="$CA_ARGS -calocaltop $CALOCALTOP"
   CA_ARGS="$CA_ARGS -cascript $ROOT_PATH/util/sgeCA/sge_ca"
   CA_ARGS="$CA_ARGS -cahost $CAHOST -adminuser $ADMINUSER"
   CA_ARGS="$CA_ARGS initks $type $myuser $ksoutfile"
   if [ "$kspwfile" != "" ]; then
      CA_ARGS="$CA_ARGS $kspwfile"
   fi

   if [ "$JAVA_HOME" = "" ]; then 
      PrintError "JAVA_HOME not set"
      return 1
   else   
      JAVA=$JAVA_HOME/bin/java
   fi

   JVM_ARGS="-cp $ROOT_PATH/lib/juti.jar"
   JVM_ARGS="$JVM_ARGS -Djava.util.logging.config.file=$ROOT_PATH/util/sgeCA/logging.properties"

   $JAVA $JVM_ARGS com.sun.grid.ca.Main $CA_ARGS 
}

#--------------------------------------------------------------------------
# MakePKCS12 create pkcs12 file of user's certificate and key
#
# $1 = "user" or "sdm_daemon" or "sge_daemon"
# $2 = <unix user> or <sdm daemon>
#
MakePKCS12()
{
   if [ "$1" = "user" ]; then
      if [ "$2" = "" ]; then
         PrintError "no valid user '%s'." $1
         return 1
      fi
      keyowner=$2
      unixuser=$2
      KEYFILE=$CALOCALTOP/userkeys/$keyowner/key.pem
      CERTFILE=$CALOCALTOP/userkeys/$keyowner/cert.pem
   elif [ "$1" = "sdm_daemon" ]; then
      if [ "$2" = "" ]; then
         PrintError "no valid sdm daemon '%s'." $1
         return 1
      fi
      keyowner=$2
      unixuser=$ADMINUSER
      KEYFILE=$CALOCALTOP/daemons/$keyowner/key.pem
      CERTFILE=$CALOCALTOP/daemons/$keyowner/cert.pem
   elif [ "$1" = "sge_daemon" ]; then
      keyowner=$2
      unixuser=$ADMINUSER
      KEYFILE=$CALOCALTOP/private/key.pem
      CERTFILE=$CATOP/certs/cert.pem
   else
      PrintError "valid argument: %s" $1
      return 1
   fi
   

   pwoptions=""
   if [ "$passinfile" != "" ]; then
      pwoptions="-passout file:$passinfile"
   fi

   myoutdir="."
   if [ "$pkcs12outdir" != "" ]; then
      myoutdir="$pkcs12outdir"
   fi

   $INFOTEXT "Generating %s/%s.p12." "$myoutdir" "$keyowner"
   # create rand.seed file
   RSFILE=/tmp/rand.seed.$$
   MakeRandFile $RSFILE $unixuser
   if [ $? -ne 0 ]; then
      return 1
   fi
   RANDFILE=$RSFILE; export RANDFILE

   $P12 $pwoptions -export -nodes -inkey $KEYFILE -out $myoutdir/$keyowner.p12 -in $CERTFILE -certfile $CATOP/cacert.pem -caname "SGE CA"
   if [ $? -ne 0 ]; then
      PrintError "openssl pkcs12 command failed"
      return 1
   fi
   $RM -f $RANDFILE
}

#--------------------------------------------------------------------------
# MakePKCS12ForUsers create pkcs12 file of user certificate and keys from userfile
# userfile contains a list of entries of the following format per user.
#
# <unix username>:<gecos field>:<user's email address>
#
# $1 = <userfile>
#
MakePKCS12ForUsers()
{
   if [ ! -f "$1" ]; then
      PrintErrorAndExit 1 "no valid userfile '%s'. Exit." $1
   fi
   
   cat $1 | while read line; do
      unixuser=`echo $line|cut -d: -f1`
      $INFOTEXT "Generating %s/%s.p12." \
         "$pkcs12outdir" "$unixuser"
      pkcs12outdir=$CALOCALTOP/userkeys/$unixuser
      MakePKCS12 user "$unixuser"
   done   
}

#--------------------------------------------------------------------------
# DumpUsers dump user info 
#
DumpUsers()
{
   CERTDIR=$CALOCALTOP/userkeys
   RSFILE=/tmp/rand.$$

   # create rand.seed file
   MakeRandFile $RSFILE $USER
   if [ $? -ne 0 ]; then
      exit 1
   fi
   RANDFILE=$RSFILE; export RANDFILE

   $INFOTEXT "Dumping users to $outdir/dumped_users.txt"
   for i in `ls $CERTDIR`; do
      $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
      if [ $? -ne 0 ]; then
         PrintError "openssl x509 command for user %s failed" $i
      fi
   done
}

#--------------------------------------------------------------------------
# MakeCert create certificate and private key for daemon
#
# $1 = certificate type ("daemon" or "user"  or "sdm_daemon")
# $2 = userId   (Unix user name)
# $3 = commonname         (e.g. passwd gecos field or name of sdm_daemon)
# $4 = email address
#
MakeCert()
{
   $INFOTEXT -u "\nCreating '%s' certificate and key for %s" "$1" "$3"

   TMPFILE=/tmp/sge_ca$$.tmp
   TMPFILE1=/tmp/sge_ca1$$.tmp
   $RM -f $TMPFILE $TMPFILE1
   Execute cp $CATOP/dn.info $TMPFILE
   if [ $? -ne 0 ]; then
      PrintError "Could not copy file (%s -> %s)" $CATOP/dn.info $TMPFILE
      return 1
   fi

   # 
   #  For SDM daemon the uid is always "sdm_daemon"
   #  the SdmCATrustManagerLoginModule needs this information
   #  to distingush between daemon and user certificates
   #
   if [ $1 = sdm_daemon ]; then
      echo userId=sdm_daemon_$2 >> $TMPFILE
   else
      echo userId=$2 >> $TMPFILE
   fi
   echo CN=$3 >> $TMPFILE
   echo emailAddress=$4 >> $TMPFILE

   Execute cat $CONFIG_DIR/sge_ssl_template.cnf $TMPFILE > $TMPFILE1
   if [ $? -ne 0 ]; then
      PrintError "Could not cat file (%s, %s -> %s)" $CONFIG_DIR/sge_ssl_template.cnf $TMPFILE $TMPFILE1
      return 1
   fi

   if [ $1 = daemon ]; then
      KEYDIR=$CALOCALTOP/private
      KEYFILE=$CALOCALTOP/private/key.pem
      REQFILE=$CALOCALTOP/private/req.pem
      CERTFILE=$CATOP/certs/cert.pem
      RSFILE=$CALOCALTOP/private/rand.seed
   elif [ $1 = user ]; then
      KEYDIR=$CALOCALTOP/userkeys/$2
      KEYFILE=$KEYDIR/key.pem
      REQFILE=$KEYDIR/req.pem
      CERTFILE=$KEYDIR/cert.pem
      CERTFILE_PUBLIC_DIR=$CATOP/usercerts/$2
      RSFILE=$KEYDIR/rand.seed
      ExecRm $KEYDIR
      ExecuteAsAdmin $MKDIR $KEYDIR
   elif [ $1 = sdm_daemon ]; then
      KEYDIR=$CALOCALTOP/daemons/$3
      KEYFILE=$KEYDIR/key.pem
      REQFILE=$KEYDIR/req.pem
      CERTFILE=$KEYDIR/cert.pem
      RSFILE=$KEYDIR/rand.seed
      ExecRm $KEYDIR
      ExecuteAsAdmin $MKDIR $KEYDIR
   else 
      PrintError "Unknown certificate type %s" $1
      return 1
   fi

   # create rand.seed file
   if [ "$ADMINUSER" = default ]; then
      MakeRandFile $RSFILE  root
   else
      MakeRandFile $RSFILE $ADMINUSER
   fi
   if [ $? -ne 0 ]; then
      return 1
   fi

   RANDFILE=$RSFILE; export RANDFILE

   umask 077

   # create a certificate request
   ExecuteAsAdmin $REQ -config $TMPFILE1 -new -keyout $KEYFILE -out $REQFILE $DAYS
   if [ $? != 0 ]; then
      PrintError "Can't create %s or %s. Exit." $KEYFILE $REQFILE
      return 1
   fi  

   # sign certificate request
   ExecuteAsAdmin $CA -config $TMPFILE1 -policy policy_anything -batch $DAYS \
                      -notext -out $CERTFILE -infiles $REQFILE
   if [ $? != 0 ]; then
      PrintError "Can't sign certificate request %s. Exit." $REQFILE 
      return 1
   fi
   ExecuteAsAdmin $CHMOD 644 $CERTFILE
   if [ $? != 0 ]; then
      PrintError "chmod as %s failed (%s)" $ADMINUSER $CERTFILE
      return 1
   fi
   
   if [ "$1" = user ]; then
      ExecuteAsAdmin $MKDIR -m 755 -p $CERTFILE_PUBLIC_DIR
      ExecuteAsAdmin $CP $CERTFILE $CERTFILE_PUBLIC_DIR
      ExecuteAsAdmin $CHMOD 644 $CERTFILE_PUBLIC_DIR/cert.pem
   fi
   
   $RM -f $TMPFILE $TMPFILE1 

   Execute $CHMOD 700 $KEYDIR
   if [ $? != 0 ]; then
      PrintError "chmod failed (%s)" $KEYDIR
      return 1
   fi
   Execute $CHMOD 600 $KEYDIR/*
   if [ $? != 0 ]; then
      PrintError "chmod failed (%s/\*)" $KEYDIR
      return 1
   fi
   Execute $CHMOD 600 $KEYDIR/rand.seed
   if [ $? != 0 ]; then
      PrintError "chmod failed (%s)" $KEYDIR/rand.seed
      return 1
   fi
   if [ $1 = daemon ]; then
      $INFOTEXT "created and signed certificate for SGE daemons" 
   elif [ $1 = sdm_daemon ]; then
      $INFOTEXT "created and signed certificate for sdm_daemon '%s' in '%s'" $3 $KEYDIR
   else
      # check if user exists and chown if yes
      id $2  > /dev/null 2>&1
      if [ $? = 0 ]; then
         ExecChown -R $2 $KEYDIR
         if [ $? != 0 ]; then
            PrintError "chown to %s failed (%s)" $2 $KEYDIR
            return 1
         fi
      fi   
      $INFOTEXT "created and signed certificate for user '%s' in '%s'" $2 $KEYDIR
   fi

   umask 022
   return 0
}

#--------------------------------------------------------------------------
# RenewCA renew a selfsigned CA certificate
#
RenewCA()
{
   $INFOTEXT -u "\nRenewing CA certificate"

   CAKEYDIR=$CALOCALTOP/private
   CAKEYFILE=$CALOCALTOP/private/cakey.pem
   CACERTFILE=$CATOP/cacert.pem
   RSFILE=$CALOCALTOP/private/rand.seed
   NEWCACERTFILE=$CATOP/cacert.pem.new
   OLDCACERTFILE=$CATOP/cacert.pem.`date | tr " " "_"`
   RANDFILE=$RSFILE; export RANDFILE

   ExecuteAsAdmin $X509 -in $CACERTFILE -signkey $CAKEYFILE $DAYS -out $NEWCACERTFILE
   if [ $? != 0 ]; then
      PrintError "openssl x509 as %s failed (RenewCA)" $ADMINUSER
      return 1
   fi

   ExecuteAsAdmin $CP $CACERTFILE $OLDCACERTFILE
   if [ $? != 0 ]; then
      PrintError "Could not copy file as %s (%s -> %s)" $ADMINUSER $CACERTFILE $OLDCACERTFILE
      return 1
   fi
   ExecuteAsAdmin $CHMOD 644 $CACERTFILE
   if [ $? != 0 ]; then
      PrintError "chmod as %s failed (%s)" $ADMINUSER $CACERTFILE
      return 1
   fi
   ExecuteAsAdmin $MV $NEWCACERTFILE $CACERTFILE
   if [ $? != 0 ]; then
      PrintError "Could not move file as %s (%s -> %s)" $ADMINUSER $NEWCACERTFILE $CACERTFILE
      return 1
   fi
   ExecuteAsAdmin $CHMOD 644 $CACERTFILE
   if [ $? != 0 ]; then
      PrintError "chmod as %s failed (%s)" $ADMINUSER $CACERTFILE
      return 1
   fi
   return 0
}


#--------------------------------------------------------------------------
# RenewCert renew a certificate
#
# $1 = certificate type ("daemon" or "user" or "sdm_daemon"
# $2 = userId   (Unix user name of common name of sdm_damon)
#
RenewCert()
{
   ADMINRUN_NO_EXIT=true # ExecuteAsAdmin should not exit, 
                         # we need to cleanup the file permissions

   # in the gridengine environment only root user is allowed
   # to renew certificates
   # for haithabu the admin user can also renew certificates
   if [ "$rootinstalls" != true -a $SGE_CNF = true ]; then
      PrintError "Only root user can renew certificates!"
      return 1
   fi
  
   if [ "$1" = daemon ]; then
      $INFOTEXT -u "\nRenewing daemon certificate"
      if [ "$2" = default ]; then
         $2=root
      fi
   else
      $INFOTEXT -u "\nRenewing  '%s' '%s' certificate" "$2" "$1"
   fi

   TMPFILE=/tmp/sge_ca$$.tmp
   TMPFILE1=/tmp/sge_ca1$$.tmp
   $RM -f $TMPFILE $TMPFILE1
   Execute cp $CATOP/dn.info $TMPFILE
   if [ $? != 0 ]; then
      PrintError "Could not copy file (%s -> %s)" $CATOP/dn.info $TMPFILE
      return 1
   fi
   echo userId=$2 >> $TMPFILE
   echo CN="dummy" >> $TMPFILE
   echo emailAddress="dummy" >> $TMPFILE
   Execute cat $CONFIG_DIR/sge_ssl_template.cnf $TMPFILE > $TMPFILE1
   if [ $? != 0 ]; then
      PrintError "Could not cat file (%s, %s -> %s)" $CONFIG_DIR/sge_ssl_template.cnf $TMPFILE > $TMPFILE1
      return 1
   fi


   if [ "$1" = daemon ]; then
      KEYDIR=$CALOCALTOP/private
      KEYFILE=$CALOCALTOP/private/key.pem
      REQFILE=$CALOCALTOP/private/req.pem
      CERTFILE=$CATOP/certs/cert.pem
      NEWCERTFILE=$CATOP/certs/cert.pem.new
      OLDCERTFILE=$CATOP/certs/cert.pem.old
      RSFILE=$CALOCALTOP/private/rand.seed
      CRLFILE=$CATOP/ca-crl.pem
   elif [ $1 = sdm_daemon ]; then
      KEYDIR=$CALOCALTOP/daemons/$2
      NEWKEYBASEDIR=/tmp/sge_ca_dir_$$
      NEWKEYDIR=$NEWKEYBASEDIR/daemons/$2
      KEYFILE=$NEWKEYDIR/key.pem
      REQFILE=$NEWKEYDIR/req.pem
      CERTFILE=$NEWKEYDIR/cert.pem
      NEWCERTFILE=$NEWKEYDIR/cert.pem.new
      OLDCERTFILE=$NEWKEYDIR/cert.pem.old
      RSFILE=$NEWKEYDIR/rand.seed
      CRLFILE=$CATOP/ca-crl.pem
   elif [ $1 = user ]; then
      KEYDIR=$CALOCALTOP/userkeys/$2
      NEWKEYBASEDIR=/tmp/sge_ca_dir_$$
      NEWKEYDIR=$NEWKEYBASEDIR/userkeys/$2
      KEYFILE=$NEWKEYDIR/key.pem
      REQFILE=$NEWKEYDIR/req.pem
      CERTFILE=$NEWKEYDIR/cert.pem
      NEWCERTFILE=$NEWKEYDIR/cert.pem.new
      OLDCERTFILE=$NEWKEYDIR/cert.pem.old
      RSFILE=$NEWKEYDIR/rand.seed
      CRLFILE=$CATOP/ca-crl.pem
      CERTFILE_PUBLIC_DIR=$CATOP/usercerts/$2
   else 
      PrintError "Unknown certificate type %s" $1
      return 1
   fi

   # create rand.seed file
   # rand.seed already exists, therefore no MakeRandFile $RSFILE $2
   RANDFILE=$RSFILE; export RANDFILE

   if [ "$1" = daemon ]; then
      Execute $CHMOD 644 $CERTFILE
      if [ $? != 0 ]; then
         PrintError "chmod failed (%s)" $CERTFILE
         return 1
      fi
   else
      Execute $MKDIR -p $NEWKEYDIR
      if [ $? != 0 ]; then
         PrintError "mkdir failed (%s)" $NEWKEYDIR
         return 1
      fi
      Execute $CP $KEYDIR/* $NEWKEYDIR
      if [ $? != 0 ]; then
         PrintError "Could not copy files (%s/\* -> %s)" $KEYDIR/* $NEWKEYDIR
         return 1
      fi
      if [ "$ADMINUSER" = default ]; then
         ExecChown -R root $NEWKEYDIR
      else
         ExecChown -R $ADMINUSER $NEWKEYDIR
      fi
      if [ $? != 0 ]; then
         PrintError "chown failed (%s)" $NEWKEYDIR
         return 1
      fi
      if [ $? != 0 ]; then
         PrintError "chown failed (%s)" $NEWKEYDIR
         return 1
      fi
      Execute $CHMOD 644 $NEWKEYDIR/*
      if [ $? != 0 ]; then
         PrintError "chmod failed (%s/\*)" $NEWKEYDIR
         return 1
      fi
      
   fi

   umask 077

   # revoke the old certificate and create the certificate revocation list
   ExecuteAsAdmin $CA -config $TMPFILE1 -policy policy_anything \
                      -batch $DAYS \
                      -revoke $CERTFILE 

   if [ $? != 0 ]; then
      PrintError "Can't revoke %s." $CERTFILE
      return 1
   else

      # sign certificate request
      ExecuteAsAdmin $CA -config $TMPFILE1 \
                         -policy policy_anything -batch $DAYS \
                         -notext -out $NEWCERTFILE -infiles $REQFILE

      if [ $? != 0 ]; then
         PrintError "Can't renew %s." $CERTFILE
         return 1
      else  
      
         # create the certificate revocation list
         ExecuteAsAdmin $CA -config $TMPFILE1 -policy policy_anything \
                            -batch $DAYS \
                            -gencrl -out ${CRLFILE}.tmp

         if [ $? != 0 ]; then
            PrintError "Can't generate revocation list %s.." $CRLFILE
            return 1
         else
            if [ $1 = daemon ]; then
               $INFOTEXT "renewed certificate for SGE daemons" 
            else
               $INFOTEXT "renewed certificate for user '%s' in '%s'" $2 $KEYDIR
            fi
         fi  

         if [ -f $OLDCERTFILE ]; then
            ExecuteAsAdmin $RM -f $OLDCERTFILE
            if [ $? != 0 ]; then
               PrintError "Can not delete old cert file (%s)"  $OLDCERTFILE
               return 1
            fi
         fi

         ExecuteAsAdmin $CP $CERTFILE $OLDCERTFILE
         if [ $? != 0 ]; then
            PrintError "Could not copy file (%s -> %s)"  $CERTFILE $OLDCERTFILE
            return 1
         fi
         ExecuteAsAdmin $MV $NEWCERTFILE $CERTFILE
         if [ $? != 0 ]; then
            PrintError "Could not move file (%s -> %s)"  $NEWCERTFILE $CERTFILE
            return 1
         fi
         ExecuteAsAdmin $CHMOD 644 $CERTFILE
         if [ $? != 0 ]; then
            PrintError "chmod failed (%s)" $CERTFILE
            return 1
         fi

         # copy renewed cert.pem to $CERTFILE_PUBLIC_DIR
         if [ "$1" = user ]; then
            umask 022
            if [ ! -d "$CERTFILE_PUBLIC_DIR" ]; then
               ExecuteAsAdmin $MKDIR -m 755 -p $CERTFILE_PUBLIC_DIR
               if [ $? != 0 ]; then
                  umask 077
                  PrintError "Could not create %s"  $CERTFILE_PUBLIC_DIR
                  return 1
               fi
            fi
            ExecuteAsAdmin $CP $CERTFILE $CERTFILE_PUBLIC_DIR
            if [ $? != 0 ]; then
               umask 077
               PrintError "Could not copy file (%s -> %s)"  $CERTFILE $CERTFILE_PUBLIC_DIR
               return 1
            fi
            ExecuteAsAdmin $CHMOD 644 $CERTFILE_PUBLIC_DIR/cert.pem
            if [ $? != 0 ]; then
               umask 077
               PrintError "Could not chown 644 file %s"  $CERTFILE_PUBLIC_DIR/cert.pem
               return 1
            fi
            umask 077
         fi
      
         if [ "$1" != daemon ]; then
            Execute $CP -f $NEWKEYDIR/* $KEYDIR
            if [ $? != 0 ]; then
               PrintError "Could not copy files (%s/\* -> %s)"  $NEWKEYDIR $KEYDIR
               return 1
            fi
         fi

         ExecuteAsAdmin $MV ${CRLFILE}.tmp $CRLFILE
         if [ $? != 0 ]; then
            PrintError "Could not move file (%s -> %s)"  ${CRLFILE}.tmp $CRLFILE
            return 1
         fi
         ExecuteAsAdmin $CHMOD 644 $CRLFILE 
         if [ $? != 0 ]; then
            PrintError "chmod failed (%s)" $CERTFILE
            return 1
         fi
      fi
 
   fi

   Execute $RM -f $TMPFILE $TMPFILE1 

   if [ "$1" != daemon ]; then
      ExecRm $NEWKEYBASEDIR
      Execute $CHMOD 500 $KEYDIR
      if [ $? != 0 ]; then
         PrintError "chmod failed (%s)" $KEYDIR
         return 1
      fi
      Execute $CHMOD 600 $KEYDIR/*
      if [ $? != 0 ]; then
         PrintError "chmod failed (%s/\*)" $KEYDIR
         return 1
      fi
      # The chown can only be done for user certificates
      # sdm_daemon certificate are owned by $ADMINUSER
      if [ $1 = "user" ]; then
         id $2  > /dev/null 2>&1
         if [ $? = 0 ]; then
            ExecChown -R $2 $KEYDIR
            if [ $? != 0 ]; then
               PrintError "chown to %s failed (%s)" $2 $KEYDIR
               return 1
            fi
         fi
      fi
   fi

   umask 022
   return 0
}


#--------------------------------------------------------------------------
# MakeCADirs
#    create all directories for CA infrastructure
#
# 
MakeCADirs()
{


   if [ -f $CATOP -o -f $CALOCALTOP ]; then
      $INFOTEXT "The CA directories\n   %s or %s\n seem to be regular files" \
            $CATOP $CALOCALTOP
      $INFOTEXT "CA initialization failed. Exit."
      exit 1
   fi

   ExecRm $CALOCALTOP 
   if [ -f $CALOCALTOP -o -d $CALOCALTOP ]; then
      PrintError "Can't delete the CA local directories\n   %s" \
                 $CALOCALTOP
      return 1
   fi

   ExecRmAsAdmin $CATOP 
   if [ -f $CATOP -o -d $CATOP ]; then
      PrintError "Can't delete the CA directories\n   %s" \
                 $CATOP 
      return 1
   fi


   $INFOTEXT "Creating %s" $CATOP
   ExecuteAsAdmin $MKDIR $CATOP
   if [ $? != 0 ]; then
      PrintError "mkdir failed (%s)" $CATOP
      return 1
   fi
   $INFOTEXT "Creating %s" $CALOCALTOP
   Execute $MKDIR -p $CALOCALTOP
   if [ $? != 0 ]; then
      PrintError "mkdir failed (%s)" $CALOCALTOP
      return 1
   fi

   if [ $ADMINUSER != default -a $rootinstalls = true ]; then
      ExecChown $ADMINUSER $CALOCALTOP
      if [ $? != 0 ]; then
         PrintError "chown to %s failed (%s)" $ADMINUSER $CALOCALTOP
         return 1
      fi
   fi

   $INFOTEXT "Creating %s" ${CATOP}/certs
   ExecuteAsAdmin $MKDIR ${CATOP}/certs
   if [ $? != 0 ]; then
      PrintError "mkdir failed (%s)" ${CATOP}/certs
      return 1
   fi

   $INFOTEXT "Creating %s" ${CATOP}/crl
   $INFOTEXT -log "Creating %s" ${CATOP}/crl
   ExecuteAsAdmin $MKDIR ${CATOP}/crl
   if [ $? != 0 ]; then
      PrintError "mkdir failed (%s)" ${CATOP}/crl
      return 1
   fi

   $INFOTEXT "Creating %s" ${CATOP}/newcerts
   $INFOTEXT -log "Creating %s" ${CATOP}/newcerts
   ExecuteAsAdmin $MKDIR ${CATOP}/newcerts
   if [ $? != 0 ]; then
      PrintError "mkdir failed (%s)" ${CATOP}/newcerts
      return 1
   fi

   $INFOTEXT "Creating %s" ${CATOP}/serial
   $INFOTEXT -log "Creating %s" ${CATOP}/serial
   # TruncCreateAndMakeWriteable ${CATOP}/serial
   ExecuteAsAdmin $TOUCH ${CATOP}/serial
   ExecuteAsAdmin $CHMOD 666 ${CATOP}/serial
   if [ $? != 0 ]; then
      PrintError "Could not touch file as %s (%s)" $ADMINUSER ${CATOP}/serial
      return 1
   fi
   
   echo 01 > ${CATOP}/serial
   ExecuteAsAdmin $CHMOD 644 ${CATOP}/serial
   if [ $? != 0 ]; then
      PrintError "chmod failed (%s)" ${CATOP}/serial
      return 1
   fi

   $INFOTEXT "Creating %s" ${CATOP}/index.txt
   $INFOTEXT -log "Creating %s" ${CATOP}/index.txt
   ExecuteAsAdmin $TOUCH ${CATOP}/index.txt   
   if [ $? != 0 ]; then
      PrintError "Could not touch file as %s (%s)" $ADMINUSER ${CATOP}/index.txt
      return 1
   fi

   $INFOTEXT "Creating %s" ${CATOP}/usercerts
   $INFOTEXT -log "Creating %s" ${CATOP}/usercerts
   ExecuteAsAdmin $MKDIR -m 755 -p ${CATOP}/usercerts
   if [ $? != 0 ]; then
      PrintError "mkdir failed (%s)" ${CATOP}/usercerts
      return 1
   fi

   $INFOTEXT "Creating %s" ${CALOCALTOP}/userkeys
   $INFOTEXT -log "Creating %s" ${CALOCALTOP}/userkeys
   ExecuteAsAdmin $MKDIR ${CALOCALTOP}/userkeys   
   if [ $? != 0 ]; then
      PrintError "mkdir failed (%s)" ${CALOCALTOP}/userkeys
      return 1
   fi

   if [ $SGE_CNF = false ]; then
      # haithabu store the daemon certificates in a seperate
      # daemons directory
      $INFOTEXT "Creating %s" ${CALOCALTOP}/daemons
      $INFOTEXT -log "Creating %s" ${CALOCALTOP}/daemons
      ExecuteAsAdmin $MKDIR ${CALOCALTOP}/daemons   
      if [ $? != 0 ]; then
         PrintError "mkdir failed (%s)" ${CATOP}/daemons
         return 1
      fi
   fi
   
   umask 077
   $INFOTEXT "Creating %s" ${CALOCALTOP}/private
   $INFOTEXT -log "Creating %s" ${CALOCALTOP}/private
   ExecuteAsAdmin $MKDIR ${CALOCALTOP}/private
   if [ $? != 0 ]; then
      PrintError "mkdir failed (%s)" ${CATOP}/private
      return 1
   fi
   umask 022

   $INFOTEXT -wait -auto $AUTO -n "\nHit <RETURN> to continue >> "
   $CLEAR

   return 0
}


#--------------------------------------------------------------------------
# ReadConfig
#    sources the sge_ca.conf file, if the -nosge option is not set
#
# 
ReadConfig() {
   
   i=0
   read_it=true
   while [ $# -gt 0 ]; do
     if [ "$1" = "-nosge" ]; then
        read_it=false
        break
     fi
     shift
   done
   if [ "$read_it" = "true" ]; then
      if [ -f  $ROOT_PATH/util/sgeCA/sge_ca.cnf ]; then
         . $ROOT_PATH/util/sgeCA/sge_ca.cnf
      fi
      return 1
   else
      return 0
   fi
}

#--------------------------------------------------------------------------
# THE MAIN PROCEDURE
#--------------------------------------------------------------------------
TOUCH=touch
RM=rm
MKDIR=mkdir
CHMOD=chmod
CHOWN=chown
CP=cp
MV=mv

umask 022

SGE_CNF=true
ROOT_PATH=`dirname $0`/../..
ROOT_PATH=`cd $ROOT_PATH; pwd`
CATOP=""
CALOCALTOP=""
CAHOST=""
CAHOMEKEYDIR=""

ADMINUSER="default"
CONFIG_DIR=$ROOT_PATH/util/sgeCA
ARCHSCRIPT=$ROOT_PATH/util/arch
if [ ! -f "$ARCHSCRIPT" ]; then
   echo
   echo Error: The shell script \"$ARCHSCRIPT\" does not exist.
   echo Please verify your setup and restart this script. Exit.
   echo
   exit 1
fi

ARCH=`$ARCHSCRIPT`
V5UTILBIN=$ROOT_PATH/utilbin/$ARCH              # adminrun, infotext, openssl, uidgid

if [ ! -d "$V5UTILBIN" ]; then
   echo
   echo "Error: The utilbin directory "$V5UTILBIN" does not exist"
   echo "Please verify your setup and restart this script. Exit."
   exit 1
fi

if [ ! -d "$ROOT_PATH/lib/$ARCH" ]; then
    echo
    echo "Error: The lib directory \"$ROOT_PATH/lib/$ARCH\" does not exist"
    echo "Please verify your setup and restart this script. Exit."
    exit 1
fi

if [ "$LD_LIBRARY_PATH"  = "" ]; then
   LD_LIBRARY_PATH=$ROOT_PATH/lib/$ARCH
else
   LD_LIBRARY_PATH="$ROOT_PATH/lib/${ARCH}:${LD_LIBRARY_PATH}"
fi
export LD_LIBRARY_PATH

#---------------------------------------
# setup INFOTEXT begin
#---------------------------------------

# INFOTXT_DUMMY is needed by message parsing script
# which is looking for $INFOTEXT and would report
# errors in the next command. Please use INFOTXT_DUMMY
# instead of using $INFOTEXT

INFOTXT_DUMMY=$V5UTILBIN/infotext
INFOTEXT=$INFOTXT_DUMMY
if [ ! -x $INFOTXT_DUMMY ]; then
   echo "Error: Can't find binary \"$INFOTXT_DUMMY\""
   echo "Please verify your setup and restart this script. Exit."
   exit 1
fi

# Test the infotext binary
tmp=`$INFOTEXT test 2>&1`
if [ $? -ne 0 ]; then
   echo "Error: Execution of $INFOTEXT failed: $tmp"
   echo "Please verify your setup and restart this script. Exit."
   exit 1
fi

#
# From now on we can use PrintError methods to write error messages
#

SGE_INFOTEXT_MAX_COLUMN=5000; export SGE_INFOTEXT_MAX_COLUMN

#---------------------------------------
# setup INFOTEXT end
#---------------------------------------

#
#  Check wether all needed binaries are exists
#
for i in adminrun openssl uidgid; do
   if [ ! -x $V5UTILBIN/$i ]; then
      PrintErrorAndExit 1 "Error: Can't find binary \"%s\"" $V5UTILBIN/$i
   fi
done

euid=`$V5UTILBIN/uidgid -euid 2>&1`
if [ $? -ne 0 ]; then
   PrintErrorAndExit 1 "Execution of $V5UTILBIN/uidgid failed: $euid"
fi
if [ $euid = 0 ]; then
   rootinstalls=true
else
   rootinstalls=false
fi

ReadConfig $*

#--------------------------------------------------------------------------
# SGE specific settings to keep previous behavior
#--------------------------------------------------------------------------

ME=`whoami`
if [ "$ME" = "" ]; then
   PrintErrorAndExit 1 "Can't determine your username with \"%s\" command. Exit" whoami
fi

CAKEY=cakey.pem
CACERT=cacert.pem
OPENSSL=$V5UTILBIN/openssl

AUTO="false"
#-----------------------------
# CommandLine Argument Parsing
#
WHICH="undef"
newkey=newkey.pem
newreq=newreq.pem
newcert=newcert.pem
user=""
outdir=.
indir=.
userfile=""
passinfile=""
pkcs12outdir=""
md="-md md5"
nodes="-nodes"
ksoutfile=""
kspwfile=""

ARGC=$#
while [ $ARGC != 0 ]; do
   case $1 in
   -adminuser)
      ADMINUSER="$2"
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -auto)
      AUTO="true"
      . $2
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -init)
      WHICH="init"
      ;;
   -req)
      WHICH="req"
      ;;
   -sign)
      WHICH="sign"
      ;;
   -copy)
      WHICH="copy"
      ;;
   -nosge)
      SGE_CNF=false
      ;;
   -verify)
      if [ $ARGC -lt 2 ]; then
         PrintError "-verify needs argument"
         ErrUsage
      fi
      WHICH="verify"
      newcert=$2
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -print)
      if [ $ARGC -lt 2 ]; then
         PrintError "-print needs argument"
         ErrUsage
      fi
      WHICH="print"
      newcert=$2
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -showCaTop)
      WHICH="showCaTop"
      ;;
   -showCaLocalTop)
      WHICH="showCaLocalTop"
      ;;
   -printkey)
      if [ $ARGC -lt 2 ]; then
         PrintError "-printkey needs argument"
         ErrUsage
      fi
      WHICH="printkey"
      newcert=$2
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -printcrl)
      if [ $ARGC -lt 2 ]; then
         PrintError "-printcrl needs argument"
         ErrUsage
      fi
      WHICH="printcrl"
      newcert=$2
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -days)
      if [ $ARGC -lt 2 ]; then
         PrintError "-days needs argument"
         ErrUsage
      fi
      DAYS="-days $2"
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -outdir)
      if [ $ARGC -lt 2 ]; then
         PrintError "-outdir needs argument"
         ErrUsage
      fi
      outdir="$2"
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -cahost)
      if [ $ARGC -lt 2 ]; then
         PrintError "-cahost needs argument"
         ErrUsage
      fi
      CAHOST="$2"
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -cadir)
      if [ $ARGC -lt 2 ]; then
         PrintError "-cadir needs argument"
         ErrUsage
      fi
      CATOP="$2"
      CALOCALTOP="$2"
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -calocaltop)
      if [ $ARGC -lt 2 ]; then
         PrintError "-calocaltop needs argument"
         ErrUsage
      fi
      CALOCALTOP="$2"
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -catop)
      if [ $ARGC -lt 2 ]; then
         PrintError "-catop needs argument"
         ErrUsage
      fi
      CATOP="$2"
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -pkcs12)
      WHICH="pkcs12"
      if [ $ARGC -lt 2 ]; then
         PrintError "-pkcs12 needs argument"
         ErrUsage
      fi
      user="$2"
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -sdm_pkcs12)
      WHICH="sdm_pkcs12"
      if [ $ARGC -lt 2 ]; then
         PrintError "-sdm_pkcs12 needs argument"
         ErrUsage
      fi
      user="$2"
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -sys_pkcs12)
      WHICH="sys_pkcs12"
      if [ $ARGC -lt 2 ]; then
         PrintError "-sys_pkcs12 needs argument"
         ErrUsage
      fi
      user="$2"
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -ks)
      WHICH="ks"
      if [ $ARGC -lt 2 ]; then
         PrintError "-ks needs argument"
         ErrUsage
      fi
      user="$2"
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -ksout)   
      if [ $ARGC -lt 2 ]; then
         PrintError "-ksout needs argument"
         ErrUsage
      fi
      ksoutfile="$2"
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -kspwf)   
      if [ $ARGC -lt 2 ]; then
         PrintError "-kspwf needs argument"
         ErrUsage
      fi
      kspwfile="$2"
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -pkcs12pwf)   
      if [ $ARGC -lt 2 ]; then
         PrintError "-pkcs12pwf needs argument"
         ErrUsage
      fi
      passinfile="$2"
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -pkcs12dir)   
      if [ $ARGC -lt 2 ]; then
         PrintError "-pkcs12dir needs argument"
         ErrUsage
      fi
      pkcs12outdir="$2"
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -sha1)
      md="-md sha1"
      ;;
   -encryptkey)
      nodes=""
      ;;
   -usercert)
      WHICH="usercert"
      if [ $ARGC -lt 2 ]; then
         PrintError "-usercert needs argument"
         ErrUsage
      fi
      userfile=$2
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -userks)
      WHICH="userks"
      ;;
   -sysks)
      WHICH="sysks"
      ;;
   -user)
      WHICH="oneuser"
      if [ $ARGC -lt 2 ]; then
         PrintError "-user needs argument"
         ErrUsage
      fi
      user=$2
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -sdm_daemon)
      WHICH="sdm_daemon"
      if [ $ARGC -lt 2 ]; then
         PrintError "-sdm_daemon needs argument"
         ErrUsage
      fi
      user=$2
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -renew)
      if [ $ARGC -lt 2 ]; then
         PrintError "-renew needs argument"
         ErrUsage
      fi
      WHICH="renew"
      user=$2
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -renew_sdm)
      if [ $ARGC -lt 2 ]; then
         PrintError "-renew_sdm needs argument"
         ErrUsage
      fi
      WHICH="renew_sdm"
      user=$2
      shift
      ARGC=`expr $ARGC - 1`
      ;;
   -renew_sys)
      WHICH="renew_sys"
      ;;
   -renew_ca)
      WHICH="renew_ca"
      ;;
   -version)
      WHICH="version"
      ;;
   -dumpusers)
      WHICH="dumpusers"
      ;;
   *)
      PrintError "Unknown option %s" "$1"
      ErrUsage
      ;;
   esac
   shift
   ARGC=`expr $ARGC - 1`
done

if [ "$WHICH" = "undef" ]; then
   ErrUsage
fi

#echo "ADMINUSER: $ADMINUSER" 
#echo "CATOP: $CATOP" 
#echo "CALOCALTOP: $CALOCALTOP" 
#echo "CAHOST: $CAHOST" 

if [ "$CATOP" = "" ]; then
   ErrUsage "catop not set"
fi
if [ "$CALOCALTOP" = "" ]; then
   ErrUsage "calocaltop not set"
fi
if [ "$ADMINUSER" = "" ]; then
   ErrUsage "adminuser not set"
fi
if [ "$CAHOST" = "" ]; then
   ErrUsage "cahost not set"
fi


export ADMINUSER CATOP CALOCALTOP CAHOST

REQ="$OPENSSL req $nodes"

CA="$OPENSSL ca $md -keyfile $CALOCALTOP/private/$CAKEY -cert $CATOP/$CACERT \
             -outdir $CATOP/newcerts"

VERIFY="$OPENSSL verify"

X509="$OPENSSL x509"
X509KEY="$OPENSSL rsa"
X509CRL="$OPENSSL crl"

P12="$OPENSSL pkcs12"

case $WHICH in
init)
   InitCA  
   ;;
req)
   RequestCert
   ;;
sign)
   SignCert
    ;;
copy) 
    InstallKey user $ME
    ;;
usercert) 
    MakeUserCerts $userfile
    ;;
userks) 
    MakeUserKeystores
    ;;
sysks) 
    MakeSysKeystore
    ;;
oneuser)
    MakeUserCert user "$user"
    if [ $? != 0 ]; then
       exit 1
    fi
    ;;
sdm_daemon)
    MakeUserCert sdm_daemon $user
    if [ $? != 0 ]; then
       exit 1
    fi
    ;;
verify)
    VerifyCert
    ;;
print)
    PrintX509 cert $newcert
    ;;
printkey) 
    PrintX509 key $newcert
    ;;
printcrl) 
    PrintX509 crl $newcert
    ;;
renew) 
    RenewCert user $user
    if [ $? != 0 ]; then
       exit 1
    fi
    ;;
renew_sdm) 
    RenewCert sdm_daemon $user
    if [ $? != 0 ]; then
       exit 1
    fi
    ;;
renew_sys) 
    RenewCert daemon $ADMINUSER
    if [ $? != 0 ]; then
       exit 1
    fi
    ;;
renew_ca) 
    RenewCA
    if [ $? != 0 ]; then
       exit 1
    fi
    ;;
pkcs12) 
    MakePKCS12 user $user
    if [ $? != 0 ]; then
       exit 1
    fi
    ;;
ks) 
    MakeKeystore user $user
    if [ $? != 0 ]; then
       exit 1
    fi
    ;;
sdm_pkcs12) 
    MakePKCS12 sdm_daemon $user
    if [ $? != 0 ]; then
       exit 1
    fi
    ;;
sys_pkcs12) 
    MakePKCS12 sge_daemon $user
    if [ $? != 0 ]; then
       exit 1
    fi
    ;;
showCaTop)
    echo "$CATOP"
    ;;
showCaLocalTop)
    echo "$CALOCALTOP"
    ;;
version)
    echo "sge_ca 6.5"
    ;;
dumpusers)
    DumpUsers
    ;;
help)
    ErrUsage
    ;;
esac

exit 0