File: grid_proxy_destroy.c

package info (click to toggle)
globus-proxy-utils 3.9-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,660 kB
  • ctags: 131
  • sloc: sh: 8,562; ansic: 2,608; xml: 831; makefile: 224
file content (367 lines) | stat: -rw-r--r-- 10,435 bytes parent folder | download
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
/*
 * 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.
 */

#ifndef GLOBUS_DONT_DOCUMENT_INTERNAL
/**
 * @file grid_proxy_destroy.h
 * Globus GSI Proxy Utils
 * @author Sam Lang, Sam Meder
 *
 * $RCSfile: grid_proxy_destroy.c,v $
 * $Revision: 1.17.12.1 $
 * $Date: 2010/03/18 17:11:09 $
 */
#endif

#include "globus_common.h"
#include "globus_error.h"
#include "globus_gsi_cert_utils.h"
#include "globus_gsi_system_config.h"
#include "globus_gsi_proxy.h"
#include "globus_gsi_credential.h"
#include "globus_gsi_system_config.h"
#ifdef WIN32
#include "openssl/applink.c"
#endif

int                                     debug = 0;

#define SHORT_USAGE_FORMAT \
"\nSyntax: %s [-help][-dryrun][-default][-all][--] [file1...]\n"

static char *  LONG_USAGE = \
"\n" \
"    Options\n" \
"    -help, -usage             Displays usage\n" \
"    -version                  Displays version\n" \
"    -debug                    Display debugging information\n" \
"    -dryrun                   Prints what files would have been destroyed\n" \
"    -default                  Destroys file at default proxy location\n" \
"    -all                      Destroys any user (default) and delegated\n"
"                              proxies that are found\n" \
"    --                        End processing of options\n" \
"    file1 file2 ...           Destroys files listed\n" \
"\n";


#   define args_show_version() \
    { \
        char buf[64]; \
        sprintf( buf, \
                 "%s-%s", \
                 PACKAGE, \
                 VERSION); \
        fprintf(stderr, "%s\n", buf); \
        globus_module_deactivate_all(); \
        exit(0); \
    }

#   define args_show_short_help() \
    { \
        fprintf(stderr, \
                SHORT_USAGE_FORMAT \
                "\nUse -help to display full usage.\n", \
                program); \
        globus_module_deactivate_all(); \
    }

#   define args_show_full_usage() \
    { \
        fprintf(stderr, SHORT_USAGE_FORMAT \
                "%s", \
                program ? program : "(null)", \
                LONG_USAGE); \
        globus_module_deactivate_all(); \
        exit(0); \
    }

#   define args_error_message(errmsg) \
    { \
        fprintf(stderr, "\nERROR: %s\n", errmsg ? errmsg : "(null)"); \
        args_show_short_help(); \
        globus_module_deactivate_all(); \
        exit(1); \
    }

#   define args_error(argval, errmsg) \
    { \
        char buf[1024]; \
        sprintf(buf, "option %s : %s", argval, errmsg); \
        args_error_message(buf); \
    }

void
globus_i_gsi_proxy_utils_print_error(
    globus_result_t                     result,
    int                                 debug,
    const char *                        filename,
    int                                 line);

#define GLOBUS_I_GSI_PROXY_UTILS_PRINT_ERROR \
    globus_i_gsi_proxy_utils_print_error(result, debug, __FILE__, __LINE__)

static int
globus_i_gsi_proxy_utils_clear_and_remove(
    char *                              filename,
    int                                 flag); 

int main(
    int                                 argc, 
    char **                             argv)
{
    int                                 all_flag      = 0;
    int                                 default_flag  = 0;
    int                                 dryrun_flag   = 0;
    int                                 filename_flag = 0;
    int                                 i;
    char *                              argp;
    char *                              program;
    char *                              default_file;
    char *                              default_full_file = NULL;
    char *                              dummy_dir_string;
    globus_result_t                     result = GLOBUS_SUCCESS;

    if(globus_module_activate(GLOBUS_GSI_PROXY_MODULE) != (int)GLOBUS_SUCCESS)
    {
        globus_libc_fprintf(
            stderr,
            "\nERROR: Couldn't load module: GLOBUS_GSI_PROXY_MODULE.\n"
            "Make sure Globus is installed correctly.\n\n");
        exit(1);
    }

    if (strrchr(argv[0],'/'))
    {
        program = strrchr(argv[0],'/') + 1;
    }
    else
    {
        program = argv[0];
    }

    for (i = 1; i < argc; i++)
    {
        argp = argv[i];

        /* '--' indicates end of options */
        if (strcmp(argp,"--") == 0)
        {
            i++;
            break;
        }

        /* If no leading dash assume it's start of filenames */
        if (strncmp(argp, "-", 1) != 0)
        {
            break;
        }
        else if (strcmp(argp, "-all") == 0)
        {
            all_flag++;
        }
        else if (strcmp(argp, "-default") == 0)
        {
            default_flag++;
        }
        else if (strcmp(argp, "-dryrun") == 0)
        {
            dryrun_flag++;
        }
        else if (strncmp(argp, "--", 2) == 0)
        {
            args_error(argp, "double-dashed options not allowed");
        }
        else if((strcmp(argp, "-help") == 0) ||
                (strcmp(argp, "-usage") == 0) )
        {
            args_show_full_usage();
        }
        else if (strcmp(argp, "-version") == 0)
        {
            args_show_version();
        }
        else if (strcmp(argp, "-debug") == 0)
        {
            debug = 1;
        }            
        else 
        {
            args_error(argp, "unknown option");
        }
    }

    /* remove the files listed on the command line first */

    if(i < argc)
    {
        filename_flag = 1;
    }
    
    for (; i < argc; i++)
    {
        globus_i_gsi_proxy_utils_clear_and_remove(argv[i], dryrun_flag);
    }

    if(!filename_flag || default_flag || all_flag)
    {
        result = GLOBUS_GSI_SYSCONFIG_GET_PROXY_FILENAME(&default_full_file,
                                                         GLOBUS_PROXY_FILE_INPUT);
        if(result != GLOBUS_SUCCESS)
        {
            globus_libc_fprintf(
                stderr,
                "\nERROR: Proxy file doesn't exist or has bad permissions\n");
            GLOBUS_I_GSI_PROXY_UTILS_PRINT_ERROR;
        }
    }

    if (default_flag)
    {
        globus_i_gsi_proxy_utils_clear_and_remove(default_full_file, 
                                                  dryrun_flag);
    }

    if (all_flag)
    {
        result = GLOBUS_GSI_SYSCONFIG_SPLIT_DIR_AND_FILENAME(default_full_file,
                                                             &dummy_dir_string,
                                                             &default_file);
        if(result != GLOBUS_SUCCESS)
        {
            globus_libc_fprintf(
                stderr,
                "\nERROR: Failed to determine the secure "
                "tmp directory proxies are stored in\n");
            GLOBUS_I_GSI_PROXY_UTILS_PRINT_ERROR;
        }
	if (dryrun_flag)
        {
	    fprintf(stderr, "Would remove all proxies found in %s directory that are owned by the current user \n", 
			dummy_dir_string ? dummy_dir_string : "(null)");
	}
	else 
	{
            result = GLOBUS_GSI_SYSCONFIG_REMOVE_ALL_OWNED_FILES(
                default_file);
	    if(result != GLOBUS_SUCCESS)
	    {
		free(default_full_file);
		globus_libc_fprintf(
		    stderr,
		    "\nERROR: Couldn't remove the all the files "
		    "owned by you in secure tmp directory.\n");
		GLOBUS_I_GSI_PROXY_UTILS_PRINT_ERROR;
	    }
	}
    }
        
    /* 
     * no options, remove the default file, which is the ENV
     * or the /tmp/x509up_u<uid> file
     */

    if (!default_flag && !all_flag && !filename_flag)
    {
        globus_i_gsi_proxy_utils_clear_and_remove(default_full_file,
                                                  dryrun_flag);
    }

 done:

    if(default_full_file)
    { 
        free(default_full_file);
    }

    globus_module_deactivate(GLOBUS_GSI_PROXY_MODULE);

    return 0;
}


static int
globus_i_gsi_proxy_utils_clear_and_remove(
    char *                              filename,
    int                                 flag) 
{
    int                                 f;
    int                                 rec;
    int                                 left;
    long                                size;
    char                                msg[65] 
        = "Destroyed by globus_proxy_destroy\r\n";

    if (flag)
        fprintf(stderr, "Would remove %s\n", filename ? filename : "(null)");
    else
    {
        #ifdef WIN32
        _chmod(filename, S_IREAD|S_IWRITE);
        #endif
        
        f = open(filename, O_RDWR);
        if (f) 
        {
            size = lseek(f, 0L, SEEK_END);
            lseek(f, 0L, SEEK_SET);
            if (size > 0) 
            {
                rec = size / 64;
                left = size - rec * 64;
                while (rec)
                {
                    write(f, msg, 64);
                    rec--;
                }
                if (left) 
                    write(f, msg, left);
            }
            close(f);
        }
        remove(filename);
    }
    return 0;
}

void
globus_i_gsi_proxy_utils_print_error(
    globus_result_t                     result,
    int                                 debug,
    const char *                        filename,
    int                                 line)
{
    globus_object_t *                   error_obj;
    char *                              error_string = NULL;

    error_obj = globus_error_get(result);
    error_string = globus_error_print_chain(error_obj);

    if(debug)
    {
        globus_libc_fprintf(stderr, "       %s:%d: %s", filename, line, error_string);
    }
    else 
    {
        globus_libc_fprintf(stderr, "       %s\nUse -debug for further information.\n", error_string);
    }
    if(error_string)
    {
       globus_libc_free(error_string);
    }
    globus_object_free(error_obj);
    globus_module_deactivate_all();
    exit(1);
}