Exemple #1
0
CK_RV
get_myproxy_creds(char *server, char *username, char *password,
                  char **creds)
{
    myproxy_socket_attrs_t *socket_attrs = NULL;
    myproxy_request_t      *client_request = NULL;
    myproxy_response_t     *server_response = NULL;
    char *request_buffer = NULL;
    char creds_file[MAXPATHLEN];
    int ret, requestlen;

    verror_clear();

    socket_attrs = malloc(sizeof(*socket_attrs));
    if (socket_attrs == NULL) {
	ret = CKR_DEVICE_MEMORY;
	goto end;
    }
    memset(socket_attrs, 0, sizeof(*socket_attrs));

    client_request = malloc(sizeof(*client_request));
    if (client_request == NULL) {
	ret = CKR_DEVICE_MEMORY;
	goto end;
    }
    memset(client_request, 0, sizeof(*client_request));

    server_response = malloc(sizeof(*server_response));
    if (server_response == NULL) {
	ret = CKR_DEVICE_MEMORY;
	goto end;
    }
    memset(server_response, 0, sizeof(*server_response));

    socket_attrs->psport = MYPROXY_SERVER_PORT;
    socket_attrs->pshost = strdup(server);
    if (socket_attrs->pshost == NULL) {
	ret = CKR_DEVICE_MEMORY;
	goto end;
    }

    ret = myproxy_init_client(socket_attrs);
    if (ret < 0) {
	gpkcs11_log("Error contacting MyProxy server %s: %s\n",
		    socket_attrs->pshost, verror_get_string());
	ret = CKR_GENERAL_ERROR;
	goto end;
    }

    GSI_SOCKET_allow_anonymous(socket_attrs->gsi_socket, 1);
    ret = myproxy_authenticate_init(socket_attrs, NULL);
    if (ret < 0) {
	gpkcs11_log("Error authenticating MyProxy server %s: %s\n",
		    socket_attrs->pshost, verror_get_string());
	ret = CKR_GENERAL_ERROR;
	goto end;
    }

    client_request->version = strdup(MYPROXY_VERSION);
    client_request->command_type = MYPROXY_RETRIEVE_CERT;
    strncpy(client_request->passphrase, password, sizeof(client_request->passphrase));
    client_request->username = strdup(username);

    requestlen = myproxy_serialize_request_ex(client_request, &request_buffer);
    if (requestlen < 0) {
	gpkcs11_log("Error preparing MyProxy request: %s\n",
		    verror_get_string());
	ret = CKR_GENERAL_ERROR;
	goto end;
    }

    ret = myproxy_send(socket_attrs, request_buffer, requestlen);
    free(request_buffer);
    if (ret < 0) {
	gpkcs11_log("Error sending MyProxy request: %s\n",
		    verror_get_string());
	ret = CKR_GENERAL_ERROR;
	goto end;
    }

    ret = myproxy_recv_response_ex(socket_attrs, server_response,
				   client_request);
    if (ret != 0) {
	gpkcs11_log("Error receiving MyProxy response: %s\n",
		    verror_get_string());
	ret = CKR_GENERAL_ERROR;
	goto end;
    }

    ret = myproxy_accept_credentials(socket_attrs, creds_file,
				     sizeof(creds_file));
    if (ret < 0) {
	gpkcs11_log("Error receiving credentials: %s\n",
		    verror_get_string());
	ret = CKR_GENERAL_ERROR;
	goto end;
    }

    *creds = strdup(creds_file);
    if (*creds == NULL) {
	ret = CKR_DEVICE_MEMORY;
	goto end;
    }

    ret = 0;

end:
    if (socket_attrs && socket_attrs->socket_fd)
	close(socket_attrs->socket_fd);
    myproxy_free(socket_attrs, client_request, server_response);
    verror_clear();

    return ret;
}
int main(int argc, char *argv[])
{
    SSL_CREDENTIALS *creds;
    myproxy_creds_t my_creds = {0};
    char proxyfile[64] = "";
    int rval=1;

    /* check library version */
    if (myproxy_check_version()) {
	fprintf(stderr, "MyProxy library version mismatch.\n"
		"Expecting %s.  Found %s.\n",
		MYPROXY_VERSION_DATE, myproxy_version(0,0,0));
	exit(1);
    }

    myproxy_log_use_stream (stderr);

    creds = ssl_credentials_new();
    init_arguments (argc, argv, &my_creds);

    if (certfile == NULL) {
	fprintf (stderr, "Specify certificate file with -c option\n");
	fprintf(stderr, "%s", usage);
	goto cleanup;
    }

    if (keyfile == NULL) {
	fprintf (stderr, "Specify key file with -y option\n");
	fprintf(stderr, "%s", usage);
	goto cleanup;
    }

    sprintf(proxyfile, "%s.%u.%u", MYPROXY_DEFAULT_PROXY,
	    (unsigned)getuid(), (unsigned)getpid());
    /* Remove proxyfile if it already exists. */
    ssl_proxy_file_destroy(proxyfile);
    verror_clear();

    if (makeproxy(certfile, keyfile, proxyfile) < 0) {
	fprintf(stderr, "Failed to create temporary credentials file.\n");
	goto cleanup;
    }
		
    if (my_creds.username == NULL) { /* set default username */
	if (dn_as_username) {
	    if (ssl_get_base_subject_file(proxyfile,
					  &my_creds.username)) {
		fprintf(stderr,
			"Cannot get subject name from your certificate\n");
		goto cleanup;
	    }
	} else {
	    char *username = NULL;
	    if (!(username = getenv("LOGNAME"))) {
		fprintf(stderr, "Please specify a username.\n");
		goto cleanup;
	    }
	    my_creds.username = strdup(username);
	}
    }

    if (ssl_get_base_subject_file(proxyfile,
				  &my_creds.owner_name)) {
	fprintf(stderr,
		"Cannot get subject name from certificate.\n");
	goto cleanup;
    }
    my_creds.location = strdup(proxyfile);

    if (myproxy_creds_store(&my_creds) < 0) {
	myproxy_log_verror();
	fprintf (stderr, "Unable to store credentials. %s\n",
		 verror_get_string()); 
    goto cleanup;
    } else {
	fprintf (stdout, "Credential stored successfully\n");
    }

    rval = 0;
 cleanup:
    if (proxyfile[0]) ssl_proxy_file_destroy(proxyfile);
    return rval;
}