84
84
#include <fcntl.h>
85
85
#include <signal.h>
86
86
87
+ #include "globus_gsi_credential.h"
88
+ #include "globus_gsi_proxy.h"
89
+
87
90
#include "blahpd.h"
88
91
#include "config.h"
89
92
#include "job_registry.h"
@@ -2591,14 +2594,167 @@ set_cmd_list_option(char **command, classad_context cad, const char *attribute,
2591
2594
if (to_append ) free (to_append );
2592
2595
return (result );
2593
2596
}
2597
+
2598
+ const char * grid_proxy_errmsg = NULL ;
2599
+
2600
+ int activate_globus ()
2601
+ {
2602
+ static int active = 0 ;
2603
+
2604
+ if (active ) {
2605
+ return 0 ;
2606
+ }
2607
+
2608
+ if ( globus_thread_set_model ( "pthread" ) ) {
2609
+ grid_proxy_errmsg = "failed to activate Globus" ;
2610
+ return -1 ;
2611
+ }
2612
+
2613
+ if ( globus_module_activate (GLOBUS_GSI_CREDENTIAL_MODULE ) ) {
2614
+ grid_proxy_errmsg = "failed to activate Globus" ;
2615
+ return -1 ;
2616
+ }
2617
+
2618
+ if ( globus_module_activate (GLOBUS_GSI_PROXY_MODULE ) ) {
2619
+ grid_proxy_errmsg = "failed to activate Globus" ;
2620
+ return -1 ;
2621
+ }
2622
+
2623
+ active = 1 ;
2624
+ return 0 ;
2625
+ }
2626
+
2627
+ /* Returns lifetime left on proxy, in seconds.
2628
+ * 0 means proxy is expired.
2629
+ * -1 means an error occurred.
2630
+ */
2631
+ int grid_proxy_info (const char * proxy_filename )
2632
+ {
2633
+ globus_gsi_cred_handle_t handle = NULL ;
2634
+ time_t time_left = -1 ;
2635
+
2636
+ if ( activate_globus () < 0 ) {
2637
+ return -1 ;
2638
+ }
2639
+
2640
+ if (globus_gsi_cred_handle_init (& handle , NULL )) {
2641
+ grid_proxy_errmsg = "failed to initialize Globus data structures" ;
2642
+ goto cleanup ;
2643
+ }
2644
+
2645
+ // We should have a proxy file, now, try to read it
2646
+ if (globus_gsi_cred_read_proxy (handle , proxy_filename )) {
2647
+ grid_proxy_errmsg = "unable to read proxy file" ;
2648
+ goto cleanup ;
2649
+ }
2650
+
2651
+ if (globus_gsi_cred_get_lifetime (handle , & time_left )) {
2652
+ grid_proxy_errmsg = "unable to extract expiration time" ;
2653
+ goto cleanup ;
2654
+ }
2655
+
2656
+ if ( time_left < 0 ) {
2657
+ time_left = 0 ;
2658
+ }
2659
+
2660
+ cleanup :
2661
+ if (handle ) {
2662
+ globus_gsi_cred_handle_destroy (handle );
2663
+ }
2664
+
2665
+ return time_left ;
2666
+ }
2667
+
2668
+ /* Writes new proxy derived from existing one. Argument lifetime is the
2669
+ * number of seconds until expiration for the new proxy. A 0 lifetime
2670
+ * means the same expiration time as the source proxy.
2671
+ * Returns 0 on success and -1 on error.
2672
+ */
2673
+ int grid_proxy_init (const char * src_filename , char * dst_filename ,
2674
+ int lifetime )
2675
+ {
2676
+ globus_gsi_cred_handle_t src_handle = NULL ;
2677
+ globus_gsi_cred_handle_t dst_handle = NULL ;
2678
+ globus_gsi_proxy_handle_t dst_proxy_handle = NULL ;
2679
+ int rc = -1 ;
2680
+ time_t src_time_left = -1 ;
2681
+ globus_gsi_cert_utils_cert_type_t cert_type = GLOBUS_GSI_CERT_UTILS_TYPE_LIMITED_PROXY ;
2682
+
2683
+ if ( activate_globus () < 0 ) {
2684
+ return -1 ;
2685
+ }
2686
+
2687
+ if (globus_gsi_cred_handle_init (& src_handle , NULL )) {
2688
+ grid_proxy_errmsg = "failed to initialize Globus data structures" ;
2689
+ goto cleanup ;
2690
+ }
2691
+
2692
+ // We should have a proxy file, now, try to read it
2693
+ if (globus_gsi_cred_read_proxy (src_handle , src_filename )) {
2694
+ grid_proxy_errmsg = "unable to read proxy file" ;
2695
+ goto cleanup ;
2696
+ }
2697
+
2698
+ if (globus_gsi_cred_get_lifetime (src_handle , & src_time_left )) {
2699
+ grid_proxy_errmsg = "unable to extract expiration time" ;
2700
+ goto cleanup ;
2701
+ }
2702
+ if ( src_time_left < 0 ) {
2703
+ src_time_left = 0 ;
2704
+ }
2705
+
2706
+ if (globus_gsi_proxy_handle_init ( & dst_proxy_handle , NULL )) {
2707
+ grid_proxy_errmsg = "failed to initialize Globus data structures" ;
2708
+ goto cleanup ;
2709
+ }
2710
+
2711
+ // lifetime == desired dst lifetime
2712
+ // src_time_left == time left on src
2713
+ if ( lifetime == 0 || lifetime > src_time_left ) {
2714
+ lifetime = src_time_left ;
2715
+ }
2716
+ if (globus_gsi_proxy_handle_set_time_valid ( dst_proxy_handle , lifetime /60 )) {
2717
+ grid_proxy_errmsg = "unable to set proxy expiration time" ;
2718
+ goto cleanup ;
2719
+ }
2720
+
2721
+ if (globus_gsi_proxy_handle_set_type ( dst_proxy_handle , cert_type )) {
2722
+ grid_proxy_errmsg = "unable to set proxy type" ;
2723
+ goto cleanup ;
2724
+ }
2725
+
2726
+ if (globus_gsi_proxy_create_signed ( dst_proxy_handle , src_handle , & dst_handle )) {
2727
+ grid_proxy_errmsg = "unable to generate proxy" ;
2728
+ goto cleanup ;
2729
+ }
2730
+
2731
+ if (globus_gsi_cred_write_proxy ( dst_handle , dst_filename )) {
2732
+ grid_proxy_errmsg = "unable to write proxy file" ;
2733
+ goto cleanup ;
2734
+ }
2735
+
2736
+ rc = 0 ;
2737
+
2738
+ cleanup :
2739
+ if (src_handle ) {
2740
+ globus_gsi_cred_handle_destroy (src_handle );
2741
+ }
2742
+ if (dst_handle ) {
2743
+ globus_gsi_cred_handle_destroy (dst_handle );
2744
+ }
2745
+ if ( dst_handle ) {
2746
+ globus_gsi_proxy_handle_destroy ( dst_proxy_handle );
2747
+ }
2748
+
2749
+ return rc ;
2750
+ }
2594
2751
2595
2752
static char *
2596
2753
limit_proxy (char * proxy_name , char * limited_proxy_name , char * * error_message )
2597
2754
{
2598
2755
int seconds_left , hours_left , minutes_left ;
2599
2756
char * limcommand ;
2600
2757
int res ;
2601
- char * globuslocation ;
2602
2758
char * limit_command_output ;
2603
2759
int tmpfd ;
2604
2760
exec_cmd_t exe_command = EXEC_CMD_DEFAULT ;
@@ -2639,31 +2795,15 @@ limit_proxy(char* proxy_name, char *limited_proxy_name, char **error_message)
2639
2795
if (error_message ) * error_message = errmsg ; else if (errmsg ) free (errmsg );
2640
2796
return NULL ;
2641
2797
}
2642
-
2643
- globuslocation = (getenv ("GLOBUS_LOCATION" ) ? getenv ("GLOBUS_LOCATION" ) : "/opt/globus" );
2644
- exe_command .command = make_message ("%s/bin/grid-proxy-info -timeleft -file %s" ,
2645
- globuslocation , proxy_name );
2646
- if (exe_command .command == NULL )
2798
+ else
2647
2799
{
2648
- fprintf (stderr , "blahpd: out of memory\n" );
2649
- exit (1 );
2800
+ close (tmpfd );
2650
2801
}
2651
- res = execute_cmd (& exe_command );
2652
- free (exe_command .command );
2653
2802
2654
- if (res != 0 )
2655
- {
2656
- perror ("blahpd error invoking grid-proxy-info" );
2657
- char * errmsg = make_message ("blahpd error invoking grid-proxy-info; "
2658
- "exit code %d from grid-proxy-info" );
2659
- if (limited_proxy_made_up_name != NULL ) free (limited_proxy_made_up_name );
2660
- if (error_message && errmsg ) * error_message = errmsg ; else if (errmsg ) free (errmsg );
2661
- return (NULL );
2662
- }
2663
- else
2664
- {
2665
- seconds_left = atoi (exe_command .output );
2666
- cleanup_cmd (& exe_command );
2803
+ seconds_left = grid_proxy_info ( proxy_name );
2804
+ if ( seconds_left < 0 ) {
2805
+ perror ("blahpd error reading proxy lifetime" );
2806
+ return NULL ;
2667
2807
}
2668
2808
2669
2809
limit_command_output = make_message ("%s_XXXXXX" , limited_proxy_name );
@@ -2686,18 +2826,9 @@ limit_proxy(char* proxy_name, char *limited_proxy_name, char **error_message)
2686
2826
2687
2827
get_lock_on_limited_proxy = config_test_boolean (config_get ("blah_get_lock_on_limited_proxies" ,blah_config_handle ));
2688
2828
2689
- if (seconds_left <= 0 )
2690
- {
2829
+ if (seconds_left <= 0 ) {
2691
2830
/* Something's wrong with the current proxy - use defaults */
2692
- exe_command .command = make_message ("%s/bin/grid-proxy-init -limited -cert %s -key %s -out %s" ,
2693
- globuslocation , proxy_name , proxy_name , limit_command_output );
2694
- }
2695
- else
2696
- {
2697
- hours_left = (int )(seconds_left /3600 );
2698
- minutes_left = (int )((seconds_left %3600 )/60 ) + 1 ;
2699
- exe_command .command = make_message ("%s/bin/grid-proxy-init -limited -valid %d:%d -cert %s -key %s -out %s" ,
2700
- globuslocation , hours_left , minutes_left , proxy_name , proxy_name , limit_command_output );
2831
+ seconds_left = 12 * 60 * 60 ;
2701
2832
}
2702
2833
2703
2834
if ((limit_command_output == limited_proxy_name ) &&
@@ -2728,8 +2859,7 @@ limit_proxy(char* proxy_name, char *limited_proxy_name, char **error_message)
2728
2859
}
2729
2860
}
2730
2861
2731
- res = execute_cmd (& exe_command );
2732
- free (exe_command .command );
2862
+ res = grid_proxy_init ( proxy_name , limit_command_output , seconds_left );
2733
2863
2734
2864
if ((limit_command_output == limited_proxy_name ) &&
2735
2865
get_lock_on_limited_proxy )
@@ -2746,29 +2876,6 @@ limit_proxy(char* proxy_name, char *limited_proxy_name, char **error_message)
2746
2876
return (NULL );
2747
2877
}
2748
2878
2749
- /* If exitcode != 0 there may be a problem due to a warning by grid-proxy-init but */
2750
- /* the call may have been successful. We just check the temporary proxy */
2751
- if (exe_command .exit_code != 0 )
2752
- {
2753
- int orig_exit_code = exe_command .exit_code ;
2754
- cleanup_cmd (& exe_command );
2755
- exe_command .command = make_message ("%s/bin/grid-proxy-info -f %s" , globuslocation , limit_command_output );
2756
- res = execute_cmd (& exe_command );
2757
- free (exe_command .command );
2758
- if (res != 0 || exe_command .exit_code != 0 )
2759
- {
2760
- char * errmsg = make_message ("Failed to create limited proxy %s (grid-proxy-init "
2761
- "exit_code = %d; grid-proxy-info exit code %d)" , limit_command_output , orig_exit_code , res != 0 ? res : exe_command .exit_code );
2762
- if (limit_command_output != limited_proxy_name )
2763
- free (limit_command_output );
2764
- if (limited_proxy_made_up_name != NULL ) free (limited_proxy_made_up_name );
2765
- if (error_message && errmsg ) * error_message = errmsg ; else if (errmsg ) free (errmsg );
2766
- return (NULL );
2767
- }
2768
- }
2769
-
2770
- cleanup_cmd (& exe_command );
2771
-
2772
2879
if (limit_command_output != limited_proxy_name )
2773
2880
{
2774
2881
if (get_lock_on_limited_proxy )
0 commit comments