This file is indexed.

/usr/bin/grid-cert-request is in globus-gsi-cert-utils-progs 8.6-2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/bin/sh

# 
# Copyright 1999-2006 University of Chicago
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 


printDirections ()
{
  printThis=0
  if [ -f $DIRECTIONS_FILE ] ; then
    while read line || test ! -z "${line}" ; do
      if test -n "`echo $line | ${GLOBUS_SH_GREP-grep} \"END $1 TEXT\"`" ; then
        break
      fi
      if [ "$printThis" = "1" ]; then
        echo "`eval echo \"$line\"`"
      fi
      if test -n "`echo $line | ${GLOBUS_SH_GREP-grep} \"BEGIN $1 TEXT\"`" ; then
        printThis=1
      fi
    done < $DIRECTIONS_FILE
  fi

  return $printThis
}



if test -n "${GLOBUS_LOCATION}" ; then
    prefix="${GLOBUS_LOCATION}"
else
    prefix="/usr"
fi

exec_prefix="${prefix}"
sbindir="${exec_prefix}/sbin"
bindir="${exec_prefix}/bin"
libdir="${prefix}/lib"
includedir="${prefix}/include/globus"
datarootdir="${prefix}/share"
datadir="${datarootdir}"
libexecdir="${datadir}/globus"
sysconfdir="/etc"
sharedstatedir="/var/lib"
localstatedir="/var"
aclocaldir="${datadir}/globus/aclocal"

. ${libexecdir}/globus-script-initializer

globus_source ${libexecdir}/globus-sh-tools.sh

if [ ! -z "${GRID_SECURITY_DIR}" ] ; then
    if [ -r "${GRID_SECURITY_DIR}/globus-user-ssl.conf" ] && 
       [ -r "${GRID_SECURITY_DIR}/globus-host-ssl.conf" ] ; then
        secconfdir="${GRID_SECURITY_DIR}"
        if test ! -z "${X509_CERT_DIR}" ; then
            trusted_certs_dir="${X509_CERT_DIR}"
        elif test "X`echo ${GRID_SECURITY_DIR}| sed 's/\/\//\//'`" \
                = "X`echo ${sysconfdir}|sed 's/\/\//\//'`" ; then
            trusted_certs_dir="${datadir}/certificates"
        elif test -d "${secconfdir}/certificates"; then
            trusted_certs_dir="${secconfdir}/certificates"
        else
            echo "ERROR: Cannot determine trusted certificate directory. Please set X509_CERT_DIR" 1>&2
            exit 1
        fi
    fi
elif [ -r "/etc/grid-security/globus-user-ssl.conf" ] && 
     [ -r "/etc/grid-security/globus-host-ssl.conf" ] ; then
    secconfdir="/etc/grid-security"
    if test ! -z "${X509_CERT_DIR}" ; then
        trusted_certs_dir="${X509_CERT_DIR}"
    else
        trusted_certs_dir="${secconfdir}/certificates"
    fi
elif [ -r "${sysconfdir}/globus-user-ssl.conf" ] &&
     [ -r "${sysconfdir}/globus-host-ssl.conf" ] ; then
    secconfdir="${sysconfdir}"
    if test ! -z "${X509_CERT_DIR}" ; then
        trusted_certs_dir="${X509_CERT_DIR}"
    else
        trusted_certs_dir="${datadir}/certificates"
    fi
else
    secconfdir="/etc/grid-security"
    if test ! -z "${X509_CERT_DIR}" ; then
        trusted_certs_dir="${X509_CERT_DIR}"
    else
        trusted_certs_dir="${secconfdir}/certificates"
    fi

fi

#
# Sample script to generate a certificate request which can be sent 
# to the Globus CA, who will sign it. This script uses the 
# globus-ssl.conf file.
#
# When generating a certificate request and private key for a 
# globus gatekeeper daemon, use the -nopw option, so the
# key is not protected by pass phrase. 
#
PROGRAM_NAME=`echo $0 | ${GLOBUS_SH_SED-sed} 's|.*/||g'`

PROGRAM_VERSION=`echo '$Revision: 1.26.14.2 $'| ${GLOBUS_SH_SED-sed} -e 's|\\$||g' -e 's|Revision: \(.*\)|\1|'`

VERSION="8.6"

PACKAGE="globus_gsi_cert_utils"

DIRT_TIMESTAMP="1381426153"
DIRT_BRANCH_ID="83"

short_usage="$PROGRAM_NAME [-help] [ options ...]"


###########################################################
# long_usage
#   Provide usage instructions to the end user
###########################################################

long_usage ()
{
    ${GLOBUS_SH_CAT-cat} >&2 <<EOF

${short_usage}

  Example Usage:

    Creating a user certifcate:
      grid-cert-request

    Creating a host or gatekeeper certifcate:
      grid-cert-request -host [my.host.fqdn]

    Creating a LDAP server certificate:
      grid-cert-request -service ldap -host [my.host.fqdn]

  Options:
    
    -version           : Display version
    -?, -h, -help,     : Display usage
    -usage 
    -cn <name>,        : Common name of the user
    -commonname <name>
    -service <service> : Create certificate for a service. Requires
                         the -host option and implies that the generated 
                         key will not be password protected (ie implies -nopw).
    -host <FQDN>       : Create certificate for a host named <FQDN>
    -dns <FQDN>,...    : Create certificate with the dNSName extension
    -ip <IP-ADDRESS>,...
                       : Create certificate with the iPAddress extension
    -dir <dir_name>    : Changes the directory the private key and certificate 
                         request will be placed in. By default user 
                         certificates are placed in $HOME/.globus, host 
                         certificates are placed in /etc/grid-security and 
                         service certificates are place in 
                         /etc/grid-security/<service>.
    -prefix <prefix>   : Causes the generated files to be named 
                         <prefix>cert.pem, <prefix>key.pem and
                         <prefix>cert_request.pem
    -nopw,             : Create certificate without a passwd
    -nodes,
    -nopassphrase,
    -verbose           : Don't clear the screen
    -int[eractive]     : Prompt user for each component of the DN
    -force             : Overwrites preexisting certifictes
    -ca                : Will ask which CA is to be used (interactive)
    -ca <hash>         : Will use the CA with hash value <hash>
EOF
}

globus_source ${libexecdir}/globus-args-parser-header $@

###########################################################
# set_non_default_ca
#   Change the CA used for generating the certificate
###########################################################

set_non_default_ca() 
{
    if [ "${nondefaultca}" = "true" ]; then

        INSTALLED_CERTS="`echo ${trusted_certs_dir}/*.0`"

        if [ -z "${INSTALLED_CERTS}" ]; then
            echo "No CA's have been installed on this host!"
            echo "To run grid-cert-request, a CA must first"
            echo "be installed"
            exit 1;
        else
            echo
            echo "The available CA configurations installed on this host are:"
            echo
        fi
    
        index=1
        
        for cert in ${trusted_certs_dir}/*.0; do
            if test -r "${cert}" ; then
                eval "CA${index}=${cert}"
                TEMP_SUBJECT=`${SSL_EXEC} x509 -in ${cert} -noout -subject`
                eval "CA_SUBJECT${index}=\"`echo ${TEMP_SUBJECT} | ${GLOBUS_SH_SED-sed} -e \"s|subject= ||\"`\""
                eval "CA_HASH${index}=`${SSL_EXEC} x509 -in ${cert} -noout -hash`"
                eval "echo \"$index)  \${CA_HASH${index}} - \${CA_SUBJECT${index}}\""
                index=`expr $index + 1`
            fi
        done
    
        echo
        echo -n "Enter the index number of the CA you want to sign your cert request: "
        read CA_CHOSEN_INDEX
    
        if [ 0 -ge $CA_CHOSEN_INDEX ] || [ $index -le $CA_CHOSEN_INDEX ] ; then
            echo "${CA_CHOSEN_INDEX} is not a valid index!"
            exit 1
        fi
    
        eval "CA_SUBJECT=\${CA_SUBJECT${CA_CHOSEN_INDEX}}"
        eval "CA_HASH=\${CA_HASH${CA_CHOSEN_INDEX}}"

    elif [ ! -z "${nondefaultca}" ] ; then
        CA_HASH=${nondefaultca}
        if test ! -r "${trusted_certs_dir}/${CA_HASH}.0" ; then
            echo
            echo "Cannot find a CA with hash ${CA_HASH}."
            echo "Use grid-cert-request -ca to pick a CA interactively."
            echo 
            exit 1
        fi
        TEMP_SUBJECT=`${SSL_EXEC} x509 -in ${trusted_certs_dir}/${CA_HASH}.0 -noout -subject`
        eval "CA_SUBJECT=\"`echo ${TEMP_SUBJECT} | ${GLOBUS_SH_SED-sed} -e \"s|subject= ||\"`\""
    fi

    echo
    echo "Using CA: ${CA_HASH} - ${CA_SUBJECT}"
    echo

    SSL_CONFIG=${trusted_certs_dir}/globus-user-ssl.conf.${CA_HASH}
    SSL_USER_CONFIG=${trusted_certs_dir}/globus-user-ssl.conf.${CA_HASH}
    SSL_HOST_CONFIG=${trusted_certs_dir}/globus-host-ssl.conf.${CA_HASH}
    DIRECTIONS_FILE=${trusted_certs_dir}/directions.${CA_HASH}
    security_conf=${trusted_certs_dir}/grid-security.conf.${CA_HASH}
}


###########################################################
# abort_cleanup
#   Remove temp files generated by this script
###########################################################

abort_cleanup () 
{
    ${GLOBUS_SH_RM-rm} -f ${CERT_FILE} ${KEY_FILE} \
                          ${REQUEST_FILE} ${RAND_TEMP} \
                          ${REQ_HEAD} ${REQ_INPUT} ${REQ_OUTPUT} ${REQ_CONF}
}


###########################################################
# absolute_path
#    Make these absolute file names if they are not
###########################################################

absolute_path () 
{
   _file_name="$1"

   case $_file_name in
      /*)
        echo ${_file_name}
        ;;
      *)
        echo ${PWD}/${_file_name}
    esac
}


###########################################################
# read_command_line
#   Command line parsing
###########################################################

read_command_line () 
{
    # Expects $* from the shell invocation

    while [ "X$1" !=  "X" ]
    do
        case $1 in
            -\?|-h|-help|-usage)
                long_usage
                exit 0
                ;;
            -cn | -commonname)
                COMMON_NAME="$2"
                shift ; shift
                ;;
            -host)
                SERVICE_HOST="$2"
                SERVICE_HOST=`echo ${SERVICE_HOST}|${GLOBUS_SH_TR-tr} 'A-Z' 'a-z'`
                if echo ${SERVICE_HOST}|grep "localhost" >/dev/null || \
                   echo ${SERVICE_HOST}|grep "localdomain" >/dev/null; then
                    globus_args_option_error "$1" "$2 may not contain localhost or localdomain"
                fi
                NO_DES="-nodes"
                shift ; shift
                ;;
            -dns)
                DNS_NAME_EXTENSIONS="$2"
                shift; shift;
                ;;
            -ip)
                IP_ADDRESS_EXTENSIONS="$2"
                shift; shift;
                ;;
            -service)
                SERVICE="$2"
                SERVICE=`echo ${SERVICE}|${GLOBUS_SH_TR-tr} 'A-Z' 'a-z'`
                shift ; shift
                ;;
            -dir)
                TARGET_DIR="$2"
                TARGET_DIR="`absolute_path ${TARGET_DIR}`"
                shift ; shift
                ;;
            -prefix)
                PREFIX="$2"
                shift ; shift
                ;;
            -nopw|-nodes|-nopassphrase)
                NO_DES="-nodes"
                shift
                ;;
            -verbose)
                VERBOSE="yes"
                shift
                ;;
            -int|-interactive)
                INTERACTIVE="TRUE"
                shift
                ;;
            -force)
                FORCE="TRUE"
                shift
                ;;
            -ca)
                if [ ! -z "$2" ] && \
                   [ -z "`echo $2 | ${GLOBUS_SH_SED-sed} -e \"s|[^-]*||g\"`" ]
                then
                    nondefaultca=$2
                    shift
                else
                    echo "nondefaultca=true"
                    nondefaultca="true"
                fi
                set_non_default_ca
                shift
                ;;
            *)
                globus_args_unrecognized_option "$1"
                ;;
        esac
    done

    if test -z "${SERVICE}"; then 
        if test -z "${SERVICE_HOST}"; then
            SERVICE="user"
        else
            SERVICE="host"
        fi
    else
        if test -z "${SERVICE_HOST}"; then
            globus_args_option_error "-service" "Requires thes -host option"
        fi
    fi

    if test -z "${TARGET_DIR}"; then
        case ${SERVICE} in
            user)
                TARGET_DIR="$HOME/.globus"
                ;;
            host)
                TARGET_DIR="${secconfdir}"
                ;;
            *)
                TARGET_DIR="${secconfdir}/${SERVICE}"
                ;;
        esac
    fi

    if test -z "${PREFIX}"; then
        REQUEST_FILE="${TARGET_DIR}/${SERVICE}cert_request.pem"
        CERT_FILE="${TARGET_DIR}/${SERVICE}cert.pem"
        KEY_FILE="${TARGET_DIR}/${SERVICE}key.pem"
    else
        REQUEST_FILE="${TARGET_DIR}/${PREFIX}cert_request.pem"
        CERT_FILE="${TARGET_DIR}/${PREFIX}cert.pem"
        KEY_FILE="${TARGET_DIR}/${PREFIX}key.pem"
    fi
}



###########################################################
# create_input_file
#   Generate a inputfile to be given to SSL that fully 
#   specifies the DN of the user
#   files
###########################################################

create_input_file () 
{
    _common_name="$1"
    _config_file="$2"

    # Parse the ssleay configuration file, to determine the
    # correct default parameters

    ${GLOBUS_SH_AWK-awk}  < $_config_file '
 
  /^\[ req_distinguished_name \]/ {
     start_parsing=1;
     next;
  }

  /^\[ .*/ {
     start_parsing=0;
     next;
  }

  /^[a-zA-Z0-9\.]*_default[ \t]*=/ && start_parsing==1 {
     split($0, a, "=");
     # default value is in a[2], but we should strip of leading ws
     for(i=1;substr(a[2],i,1) == " " || substr(a[2],i,1) == "\t"; i++);
     print substr(a[2], i);
     next;
}
'

    echo ${_common_name}
}


create_subjectAltName()
{
    _dns_extensions=""
    _ip_extensions=""

    if [ -n "$DNS_NAME_EXTENSIONS" ]; then
        _dns_extensions="`echo \"$DNS_NAME_EXTENSIONS\" | sed -e 's/^/DNS:/' -e 's/,/,DNS:/g'`"
    fi
    if [ -n "$IP_ADDRESS_EXTENSIONS" ]; then
        _ip_extensions="`echo \"$IP_ADDRESS_EXTENSIONS\" | sed -e 's/^/IP:/' -e 's/,/,IP:/g'`"
    fi

    if [ -n "$_dns_extensions" -a -n "$_ip_extensions" ]; then
        echo "subjectAltName=$_dns_extensions,$_ip_extensions"
    elif [ -n "$_dns_extensions" ]; then
        echo "subjectAltName=$_dns_extensions"
    else
        echo "subjectAltName=$_ip_extensions"
    fi
}

create_conf_file ()
{
    _config_file="$1"

    # Add $DNS_NAME_EXTENSIONS and $IP_ADDRESS_EXTENSIONS to the v3_req section
    while read line; do
        echo "$line"

        if echo "$line" | ${GLOBUS_SH_GREP-grep} '\[ v3_req \]' > /dev/null 2>&1 ; then
            create_subjectAltName
        fi
    done < $_config_file
}

###########################################################
# create_request_header
###########################################################

create_request_header () 
{
  if printDirections "REQUEST HEADER" ; then
    ${GLOBUS_SH_CAT-cat} <<EOF
This is a Certificate Request file:

It should be mailed to ${GSI_CA_EMAIL_ADDR}


=========================================================================
Certificate Subject:

    ${SUBJECT}

The above string is known as your ${SERVICE} certificate subject, and it
uniquely identifies this ${SERVICE}.

To install this ${SERVICE} certificate, please save this e-mail message
into the following file.


${CERT_FILE}


      You need not edit this message in any way. Simply
      save this e-mail message to the file.



If you have any questions about the certificate contact
the ${GSI_CA_NAME} at ${GSI_CA_EMAIL_ADDR}

EOF
  fi
}

###########################################################
# check4certs:  
#   Ensure that the user does not overwrite their
#   security files.
###########################################################
check4certs () 
{
    _exists="FALSE"

    if [ -r ${REQUEST_FILE} ] ; then
        ${GLOBUS_SH_PRINTF-printf} "\n    ${REQUEST_FILE} already exists" 1>&2
        ${GLOBUS_SH_CHMOD-chmod} u+w ${REQUEST_FILE}
        _exists=TRUE
    fi
    
    if [ -r ${CERT_FILE} ] ; then
        ${GLOBUS_SH_PRINTF-printf} "\n    ${CERT_FILE} already exists" 1>&2
        ${GLOBUS_SH_CHMOD-chmod} u+w ${CERT_FILE}
        _exists=TRUE
    fi
    
    if [ -r ${KEY_FILE} ] ; then
        ${GLOBUS_SH_PRINTF-printf} "\n    ${KEY_FILE} already exists" 1>&2
        ${GLOBUS_SH_CHMOD-chmod} u+w ${KEY_FILE}
        _exists=TRUE
    fi

  
    if [ "X$_exists" = "XTRUE" ] ; then
        if [ "X$FORCE" = "XTRUE" ] ; then
            ${GLOBUS_SH_RM-rm} -f ${CERT_FILE} ${KEY_FILE} ${REQUEST_FILE}
            echo
            echo
        else 
            ${GLOBUS_SH_PRINTF-printf} "\n\nIf you wish to overwrite, run the script again with -force.\n" 1>&2
            exit 1
        fi
    fi
}


###########################################################
# setup_target_dir:  
#   Create a directory in the HOME directory of the user
#   to store globus related stuff.
###########################################################

setup_target_dir () 
{
    if test ! -d ${TARGET_DIR} ; then
        ${GLOBUS_SH_MKDIR-mkdir} ${TARGET_DIR}
        if [ $? -ne 0 ] ; then
            ${GLOBUS_SH_PRINTF-printf} "\n\nUnable to create ${TARGET_DIR}. Please make sure that you have the right to create this directory before running this script.\n" 1>&2
            exit $?
        fi 
    fi

    if test ! -w ${TARGET_DIR} ; then
        ${GLOBUS_SH_PRINTF-printf} "\n\nUnable to access ${TARGET_DIR}. Please make sure that you have the right to create files in this directory before running this script.\n" 1>&2
        exit 1
    fi
}


###########################################################
# get_user_CN
#   Determine the name of the user
###########################################################
get_user_CN () 
{
    _common_name="${COMMON_NAME}"

    # 1. Command line argument
    # 2. Query the system
    # 3. Prompt the user

    if [ -z "${_common_name}" ] ; then 
        ${GLOBUS_SH_FINGER-finger} -lm ${USERID}  >/dev/null 2>&1
        if [ $? -eq 0 ] ; then 
            _common_name="`${GLOBUS_SH_FINGER-finger} -lm ${USERID}      |\
                           ${GLOBUS_SH_GREP-grep} ${USERID}              |\
                           ${GLOBUS_SH_AWK-awk} -F: '{ print $3; exit }' |\
                           ${GLOBUS_SH_CUT-cut} -c2- `"
        fi
    fi

   if [ -z "${_common_name}" ] || [ "${_common_name}" = "(null)" ];  then
      ${GLOBUS_SH_PRINTF-printf} "Enter your name, e.g., John Smith: " 1>&2
      read _common_name
   fi

   echo ${_common_name}
}



###########################################################
# get_host_CN
#   Determine the name of the host for a host certificate
###########################################################

get_host_CN () 
{
   _common_name="${COMMON_NAME}"

   # 1. Command line -cn argument
   # 2. Create from -host argument

   if [ -z "${_common_name}" ] ; then 
        # Check SERVICE_HOST and make sure it looks like a FQDN
        ${GLOBUS_SH_ECHO-echo} ${SERVICE_HOST} | \
            ${GLOBUS_SH_GREP-grep} "\." >/dev/null 2>&1
        if [ $? -eq 1 ] ; then
            ${GLOBUS_SH_ECHO-echo} "The hostname ${SERVICE_HOST} does not appear to be fully qualified." 1>&2
            ${GLOBUS_SH_PRINTF-printf} "Do you wish to continue? [n] " 1>&2
            read _response

            case X${_response} in
                Xy|XY|Xyes|XYES|XYes)
                    ;;

                *)
                    ${GLOBUS_SH_ECHO-echo} "Aborting" 1>&2
                    return 1
                    ;;
            esac
        fi
      
        _common_name="${SERVICE}/${SERVICE_HOST}"
    fi

    echo ${_common_name}
    return 0
}



###########################################################
# create_certs
#   Create the certificate, key, and certificate request
#   files
###########################################################
create_certs () {
  
    if [ -z "${NO_DES}" ] ; then
        echo "A certificate request and private key is being created."
        echo "You will be asked to enter a PEM pass phrase."
        echo "This pass phrase is akin to your account password, "
        echo "and is used to protect your key file."
        echo "If you forget your pass phrase, you will need to"
        echo "obtain a new certificate."
        echo
    fi

    #------------------------
    # Create the Certificate File
    umask 022
    ${GLOBUS_SH_TOUCH-touch} ${CERT_FILE}


    #------------------------
    # Create some semi random data for key generation 
    umask 066
    ${GLOBUS_SH_TOUCH-touch} ${RAND_TEMP}
    if [ -r /dev/urandom ] ; then
        head -1000 /dev/urandom >> ${RAND_TEMP} 2>&1
    fi
    date >> ${RAND_TEMP} 2>&1
    netstat -in >> ${RAND_TEMP} 2>&1
    ps -ef >> ${RAND_TEMP} 2>&1
    ls -ln ${HOME} >> ${RAND_TEMP} 2>&1
    ls -ln /tmp >> ${RAND_TEMP} 2>&1

    umask 266
    #------------------------
    # Create the Key and Request Files

    if test "${SERVICE}" = "user" ; then
        used_config="${SSL_USER_CONFIG}"
    elif [ -z "$DNS_NAME_EXTENSIONS" -a -z "$IP_ADDRESS_EXTENSIONS" ]; then
        used_config="${SSL_HOST_CONFIG}"
    else
        used_config="${REQ_CONF}"
        create_conf_file "${SSL_HOST_CONFIG}" > "${REQ_CONF}"
    fi

    if test ! -z "${INTERACTIVE}" ; then
        ${SSL_EXEC} req -new -keyout ${KEY_FILE} -out ${REQ_OUTPUT} \
            -rand ${RAND_TEMP}:/var/adm/wtmp:/var/log/messages \
            -config ${used_config} ${NO_DES}
        RET=$?
        ${GLOBUS_SH_RM-rm} -f ${RAND_TEMP}
    else
        create_input_file "${COMMON_NAME}" "${used_config}" > ${REQ_INPUT}

        ${SSL_EXEC} req -new -keyout ${KEY_FILE} \
              -rand ${RAND_TEMP}:/var/adm/wtmp:/var/log/messages \
              -out ${REQ_OUTPUT} -config ${used_config} \
              ${NO_DES} < ${REQ_INPUT} 
        RET=$?

        ${GLOBUS_SH_RM-rm} -f ${RAND_TEMP}
        ${GLOBUS_SH_RM-rm} -f ${REQ_INPUT}

        # You can't separate the SSL output, it all goes to stderr
        # including the prompts.
        # Don't remove ssleay output on error as it may be useful
        
        if test ${RET} -eq 0 -a -z "${VERBOSE}" ; then
            ${GLOBUS_SH_CLEAR-clear}
        fi
    fi

    if [ ${RET} -ne 0 ] ; then
        echo "Error number ${RET} was returned by " 1>&2
        echo "   ${SSL_EXEC}"                    1>&2
        exit ${RET}
    fi

    umask 022
    
    #------------------------
    # Insert instructions into the request file


    SUBJECT="`${SSL_EXEC} req -text -noout < ${REQ_OUTPUT} 2>&1 |\
              ${GLOBUS_SH_GREP-grep} 'Subject:' | ${GLOBUS_SH_AWK-awk} -F: '{print $2}' |\
              ${GLOBUS_SH_CUT-cut} -c2- `"

    #Convert the subject to the correct form.
    SUBJECT=`echo "/"${SUBJECT} | ${GLOBUS_SH_SED-sed} -e 's|, |/|g'`

    create_request_header >${REQ_HEAD}

    # Finalize the Request file.
    ${GLOBUS_SH_CAT-cat} ${REQ_HEAD} ${REQ_OUTPUT} >${REQUEST_FILE}
    ${GLOBUS_SH_RM-rm} -f ${REQ_HEAD} ${REQ_OUTPUT} ${REQ_CONF}
}

###########################################################
# emit_directions
#   Provide instructions to the end user
###########################################################
emit_directions () {

  if test "${SERVICE}" = "user" ; then
    if printDirections "USER INSTRUCTIONS" ; then
      echo 
      echo "A private key and a certificate request has been generated with the subject:" 
 
      echo 
      echo "${SUBJECT}"
      echo
      echo "If the CN=${COMMON_NAME} is not appropriate, rerun this"
      echo "script with the -force -cn \"Common Name\" options."
      echo
      echo "Your private key is stored in ${KEY_FILE}"
      echo "Your request is stored in ${REQUEST_FILE}"
      echo
      echo "Please e-mail the request to the ${GSI_CA_NAME} ${GSI_CA_EMAIL_ADDR}"
      echo "You may use a command similar to the following:" 
      echo
      echo "  cat ${REQUEST_FILE} | mail ${GSI_CA_EMAIL_ADDR}"
      echo
      echo "Only use the above if this machine can send AND receive e-mail. if not, please"
      echo "mail using some other method."
      echo
      echo "Your certificate will be mailed to you within two working days."
      echo "If you receive no response, contact ${GSI_CA_NAME} at ${GSI_CA_EMAIL_ADDR}"
    fi

  else 
    if printDirections "NONUSER INSTRUCTIONS" ; then
      echo 
      echo "A private ${SERVICE} key and a certificate request has been generated"
      echo "with the subject:" 
      echo 
      echo "${SUBJECT}"
      echo
      echo "----------------------------------------------------------"
      echo
      echo "The private key is stored in ${KEY_FILE}"
      echo "The request is stored in ${REQUEST_FILE}"
      echo
      echo "Please e-mail the request to the ${GSI_CA_NAME} ${GSI_CA_EMAIL_ADDR}"
      echo "You may use a command similar to the following:"
      echo
      echo " cat ${REQUEST_FILE} | mail ${GSI_CA_EMAIL_ADDR}"
      echo
      echo "Only use the above if this machine can send AND receive e-mail. if not, please"
      echo "mail using some other method."
      echo
      echo "Your certificate will be mailed to you within two working days."
      echo "If you receive no response, contact ${GSI_CA_NAME} at ${GSI_CA_EMAIL_ADDR}"
    fi
  fi

}


###########################################################
# MAIN
###########################################################


#SSL related variables
if test -x ${bindir}/openssl; then
    SSL_EXEC=${bindir}/openssl
else
    SSL_EXEC="openssl"
fi

if ! ${SSL_EXEC} version > /dev/null 2> /dev/null; then
    echo "Unable to locate openssl binary in $bindir or PATH" 1>&2
    exit 1
fi

SSL_CONFIG="${secconfdir}/globus-user-ssl.conf"
SSL_USER_CONFIG="${secconfdir}/globus-user-ssl.conf"
SSL_HOST_CONFIG="${secconfdir}/globus-host-ssl.conf"
DIRECTIONS_FILE="${secconfdir}/directions"
security_conf="${secconfdir}/grid-security.conf"

# Info sent with the certificate
SUBJECT=
USERID="`${GLOBUS_SH_WHOAMI-whoami}`"
HOST="`${bindir}/globus-hostname`"
SERVICE=
SERVICE_HOST=
INTERACTIVE=
NO_DES=
TARGET_DIR=
CERT_FILE=
KEY_FILE=
REQUEST_FILE=

CERTREQ="${bindir}/grid-cert-request"

# Parse the command line

read_command_line "$@"

if test -z "$trusted_certs_dir"; then
    if test -n "$GRID_SECURITY_DIR"; then
        echo "Can't find valid CA config files in " \
             "GRID_SECURITY_DIR \"${GRID_SECURITY_DIR}\"." 
        echo "Please make sure that you have installed and " \
             "setup a CA setup package."
    else
        echo "Error reading"
        echo "    /etc/grid-security/globus-user-ssl.conf"
        echo "    /etc/grid-security/globus-host-ssl.conf"
        echo "    ${sysconfdir}/globus-user-ssl.conf"
        echo "    ${sysconfdir}/globus-host-ssl.conf"
        echo ""
        echo "Can't find valid CA config files."
        echo "Not all CAs use this tool to generate certificate requests."
        echo "If your CA is using this tool, make sure that you have installed"
        echo "your CA's setup package."
        echo "If your CA is not using this tool, consult the CA documentation"
        echo "to find the right way to request a certificate."
    fi

    echo ""
    exit 1   
fi

# set temporary files
REQ_HEAD=${TARGET_DIR}/$PROGRAM_NAME.$$.head
REQ_INPUT=${TARGET_DIR}/$PROGRAM_NAME.$$.input
REQ_OUTPUT=${TARGET_DIR}/$PROGRAM_NAME.$$.output
RAND_TEMP=${TARGET_DIR}/$PROGRAM_NAME.$$.random
REQ_CONF="${TARGET_DIR}/$PROGRAM_NAME.$$.conf"

# Source the security.conf file
. ${security_conf}

# Check the target directory
setup_target_dir

# Check for preexisting credential files
check4certs

# Derive the Common Name
if test "${SERVICE}" = "user"; then
    COMMON_NAME="`get_user_CN`"
else
    COMMON_NAME="`get_host_CN`"
    if [ $? -ne 0 ]; then
        exit 1
    fi
fi

# do clean up on abort

trap abort_cleanup 1 2 3 6 9 13 15

# create the certificate request and private key
create_certs

RET=$?

COMMON_NAME="`echo ${SUBJECT} | ${GLOBUS_SH_SED-sed} -e 's|^.*/CN=||'`"

# print directions to screen
emit_directions ${SERVICE}

exit ${RET}