📄 myproxy_get_credential.c
字号:
/* free memory allocated */ myproxy_free(socket_attrs, client_request, server_response); if( deletefile ) { ssl_proxy_file_destroy(delegfile); } return retval;}void init_arguments(int argc, char *argv[], myproxy_socket_attrs_t *attrs, myproxy_request_t *request) { extern char *optarg; int arg; while((arg = getopt_long(argc, argv, short_options, long_options, NULL)) != EOF) { switch(arg) { case 's': /* pshost name */ attrs->pshost = strdup(optarg); break; case 'p': /* psport */ attrs->psport = atoi(optarg); break; case 'h': /* print help and exit */ fprintf(stderr, usage); exit(1); break; case 'u': /* print help and exit */ fprintf(stderr, usage); exit(1); break; case 'l': /* username */ request->username = strdup(optarg); break; case 'a': /* special authorization */ request->authzcreds = strdup(optarg); use_empty_passwd = 1; break; case 'n': /* no passphrase */ use_empty_passwd = 1; break; case 'v': myproxy_debug_set_level(1); break; case 'V': /* print version and exit */ fprintf(stderr, version); exit(1); break; case 'd': /* use the certificate subject (DN) as the default username instead of LOGNAME */ dn_as_username = 1; break; case 'k': /* credential name */ request->credname = strdup (optarg); break; case 'S': read_passwd_from_stdin = 1; break; case 'T': request->want_trusted_certs = 1; myproxy_debug("Requesting trusted certificates.\n"); break; case 'c': /* credential file name */ if (certfile) free(certfile); certfile = strdup(optarg); break; case 'y': /* key file name */ if (keyfile) free(keyfile); keyfile = strdup(optarg); break; default: /* print usage and exit */ fprintf(stderr, usage); exit(1); break; } } /* Check to see if myproxy-server specified */ if (attrs->pshost == NULL) { fprintf(stderr, "Unspecified myproxy-server. Set the MYPROXY_SERVER environment variable to\nthe hostname of the myproxy-server or run with '-s server-hostname'.\n"); exit(1); } return;}intstore_credential( char *delegfile, char *certfile, char *keyfile ){ unsigned char *input_buffer = NULL; int retval = -1; assert(delegfile != NULL); assert(certfile != NULL); assert(keyfile != NULL); if (buffer_from_file(delegfile, &input_buffer, NULL) < 0) { fprintf(stderr, "open(%s) failed: %s\n", delegfile, strerror(errno)); goto error; } if (write_cert(certfile, (const char *)input_buffer) < 0) { goto error; } if (write_key(keyfile, (const char *)input_buffer) < 0) { goto error; } retval = 0;error: free(input_buffer); return(retval);}intwrite_cert( char *path, const char *buffer ){ int fd = 0; static char BEGINCERT[] = "-----BEGIN CERTIFICATE-----"; static char ENDCERT[] = "-----END CERTIFICATE-----"; char *certstart, *certend; int retval = -1; int size; assert(path != NULL); assert(buffer != NULL); if( make_path( path ) < 0 ) { verror_print_error(stderr); goto error; } /* Open the output file. */ if ((fd = open(path, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR)) < 0) { if( errno == EEXIST ) { fprintf(stderr, "open(%s) failed: This file already exists.\nmyproxy-retrieve will not overwrite end-entity credentials.\n", path ); goto error; } fprintf(stderr, "Open(%s) failed: %s\n", path, strerror(errno)); goto error; } if ((certstart = strstr(buffer, BEGINCERT)) == NULL) { fprintf(stderr, "CRED doesn't contain '%s'.\n", BEGINCERT); goto error; } if ((certend = strstr(certstart, ENDCERT)) == NULL) { fprintf(stderr, "CRED doesn't contain '%s'.\n", ENDCERT); goto error; } certend += strlen(ENDCERT); size = certend-certstart; if( buffer2file( certstart, size, fd ) != 0 ) { fprintf(stderr, "Could not write cert to: '%s'.\n", path); goto error; } certstart += size; while ((certstart = strstr(certstart, BEGINCERT)) != NULL) { if ((certend = strstr(certstart, ENDCERT)) == NULL) { fprintf(stderr, "Can't find matching '%s' in %s.\n", ENDCERT, certfile); goto error; } certend += strlen(ENDCERT); size = certend-certstart; buffer2file( certstart, size, fd ); certstart += size; } retval = 0;error: if( fd ) { close( fd ); } return( retval );}intwrite_key( char *path, const char *buffer ){ int fd = 0; static char BEGINKEY[] = "-----BEGIN RSA PRIVATE KEY-----"; static char ENDKEY[] = "-----END RSA PRIVATE KEY-----"; char *keystart, *keyend; int retval = -1; int size; if( make_path( path ) < 0 ) { verror_print_error(stderr); goto error; } /* Open the output file. */ if ((fd = open(path, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR)) < 0) { if( errno == EEXIST ) { fprintf(stderr, "open(%s) failed: This file already exists.\nmyproxy-retrieve will not overwrite end-entity credentials.\n", path ); goto error; } fprintf(stderr, "open(%s) failed: %s\n", path, strerror(errno)); goto error; } /* Write the key. */ if ((keystart = strstr(buffer, BEGINKEY)) == NULL) { fprintf(stderr, "CREDKEY doesn't contain '%s'.\n", BEGINKEY); goto error; } if ((keyend = strstr(keystart, ENDKEY)) == NULL) { fprintf(stderr, "CREDKEY doesn't contain '%s'.\n", ENDKEY); goto error; } keyend += strlen(ENDKEY); size = keyend-keystart; if( buffer2file( keystart, size, fd ) != 0 ) { fprintf(stderr, "Could not write key to: '%s'.\n", path); goto error; } retval = 0;error: if( fd ) { close( fd ); } return( retval );}intbuffer2file( char *buffer, int size, int fd ){ int rval; char *certstart; certstart = buffer; while (size) { if ((rval = write(fd, certstart, size)) < 0) { perror("write"); return( -1 ); } size -= rval; certstart += rval; } if (write(fd, "\n", 1) < 0) { perror("write"); return(-1); } return( 0 );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -