⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myproxy.c

📁 代理服务器源代码 供大家学习使用,希望大家喜欢
💻 C
📖 第 1 页 / 共 4 页
字号:
	  free(request->renewers);       if (request->credname != NULL)	  free(request->credname);       if (request->creddesc != NULL)	  free(request->creddesc);       if (request->authzcreds != NULL)	  free(request->authzcreds);       if (request->keyretrieve != NULL)	  free(request->keyretrieve);       if (request->trusted_retrievers != NULL)	  free(request->trusted_retrievers);       free(request);    }        if (response != NULL) {       if (response->version != NULL)     	  free(response->version);       if (response->authorization_data != NULL)    	  authorization_data_free(response->authorization_data);       if (response->error_string != NULL)	   free(response->error_string);       if (response->info_creds != NULL) {	   myproxy_creds_free(response->info_creds);       }       if (response->trusted_certs != NULL) {	   myproxy_certs_free(response->trusted_certs);       }       free(response);    }}/*--------- Helper functions ------------*//* * convert_message() * * Searches a buffer and locates varname. Stores contents of varname into line * e.g. convert_message(buf, "VERSION=", &version); * The line argument should be a pointer to NULL or a malloc'ed buffer. * The line buffer will be realloc'ed as required. * The buffer MUST BE NULL TERMINATED. * * flags is a bitwise or of the following values: *     CONVERT_MESSAGE_ALLOW_MULTIPLE      Allow a multiple instances of *                                         varname, in which case the rvalues *                                         are concatenated. * * Returns the number of characters copied into the line (not including the * terminating '\0'). On error returns -1, setting verror. Returns -2 * if string not found */static intconvert_message(const char			*buffer,		const char			*varname, 		const int			flags,		char				**line){    int				foundone = 0;    char			*varname_start;    int				return_value = -1;    int				line_index = 0;    const char			*buffer_p;    assert(buffer != NULL);        assert(varname != NULL);    assert(line != NULL);    if ((flags & ~CONVERT_MESSAGE_KNOWN_FLAGS) != 0)    {	verror_put_string("Illegal flags value (%d)", flags);	goto error;    }    /*     * Our current position in buffer is in buffer_p. Since we're     * done modifying buffer buffer_p can be a const.     */    buffer_p = buffer;        while ((varname_start = strstr(buffer_p, varname)) != NULL)    {	char			*value_start;	int			value_length;		/* Have is this the first varname we've found? */	if (foundone == 1)	{	    /* No. Is that OK? */	    if (flags * CONVERT_MESSAGE_ALLOW_MULTIPLE)	    {		/* Yes. Add carriage return to existing line and concatenate */		*line = realloc(*line, line_index+2);		(*line)[line_index] = '\n';		line_index++;		(*line)[line_index] = '\0';	    }	    else	    {		/* No. That's an error */		verror_put_string("Multiple values found in convert_message()");		goto error;	    }	}		/* Find start of value */	value_start = &varname_start[strlen(varname)];	/* Find length of value (might be zero) */	value_length = strcspn(value_start, "\n");	*line = realloc(*line, line_index+value_length+1);	/* Copy it over */	strncpy((*line)+line_index, value_start, value_length);	line_index += value_length;		/* Make sure line stays NULL-terminated */	(*line)[line_index] = '\0';	/* Indicate we've found a match */        foundone = 1;	/* Advance our buffer position pointer */	buffer_p = &value_start[value_length];    }	    /* Did we find anything */    if (foundone == 0)    {	/* verror_put_string("No value found"); */        return_value = -2; /*string not found*/	goto error;    }    /* Success */    return_value = strlen(*line);      error:    if (return_value == -1 || return_value == -2)    {	/* Don't return anything in line on error */	if (*line) (*line)[0] = '\0';    }    return return_value;}/* * parse_command() * * Parse command_str return the respresentation of the command in * command_value. * * Returns 0 on success, -1 on error setting verror. */static intparse_command(const char			*command_str,	      myproxy_proto_request_type_t	*command_value){    int				value;    int				return_value = -1;        assert(command_str != NULL);    assert(command_value != NULL);        /* XXX Should also handle string commands */    switch (string_to_int(command_str, &value))    {      case STRING_TO_INT_SUCCESS:	return_value = 0;	*command_value = (myproxy_proto_request_type_t) value;	break;	      case STRING_TO_INT_NONNUMERIC:	verror_put_string("Non-numeric characters in command string \"%s\"",			  command_str);	break;	      case STRING_TO_INT_ERROR:	break;    }        return return_value;}/* * encode_command() * * Return a string encoding of the command in command_value. * Returns NULL on error, setting verror. */static const char *encode_command(const myproxy_proto_request_type_t	command_value){    const char *string;        /*     * XXX Should return actual string description.     */    switch(command_value)    {      case MYPROXY_GET_PROXY:	string = "0";	break;	      case MYPROXY_PUT_PROXY:	string = "1";	break;	      case MYPROXY_INFO_PROXY:	string = "2";	break;	      case MYPROXY_DESTROY_PROXY:	string = "3";	break;      case MYPROXY_CHANGE_CRED_PASSPHRASE:	string = "4";	break;      case MYPROXY_STORE_CERT:        string = "5";        break;      case MYPROXY_RETRIEVE_CERT:        string = "6";        break;      default:	/* Should never get here */	string = NULL;	verror_put_string("Internal error: Bad command type(%d)",			  command_value);	break;    }    return string;}/* * parse_string * * Given a string representation of an integer value, fill in the given * integer with its integral value. * * Currently the string is just an ascii representation of the integer. * * Returns 0 on success, -1 on error setting verror. */static intparse_string(const char			*str,	       int			*value){    int				val;    int				return_value = -1;        assert(str != NULL);    assert(value != NULL);        /* XXX Should also handle string commands */    switch (string_to_int(str, &val))    {      case STRING_TO_INT_SUCCESS:	return_value = 0;	*value = val;	break;	      case STRING_TO_INT_NONNUMERIC:	verror_put_string("Non-numeric characters in string \"%s\"",			  str);	break;	      case STRING_TO_INT_ERROR:	break;    }        return return_value;}/* * encode_integer() * * Encode the given integer as a string into the given buffer with * length of buffer_len. * * Returns 0 on success, -1 on error setting verror. */static intencode_integer(int				value,		char				*string,		int				string_len){    /* Buffer large enough to hold string representation of lifetime */    char buffer[20];        assert(string != NULL);    sprintf(buffer, "%d", value);        if (my_strncpy(string, buffer, string_len) == -1)    {	return -1;    }        return 0;}/* * parse_response_type() * * Given a string representation of a response_type, fill in type_value * with the value. * * Currently the string is just an ascii representation of the value. * * Returns 0 on success, -1 on error setting verror. */static intparse_response_type(const char				*type_str,		    myproxy_proto_response_type_t	*type_value){    int				value;    int				return_value = -1;        assert(type_str != NULL);    assert(type_value != NULL);        /* XXX Should also handle string representations */    switch (string_to_int(type_str, &value))    {      case STRING_TO_INT_SUCCESS:	return_value = 0;	*type_value = (myproxy_proto_response_type_t) value;	break;	      case STRING_TO_INT_NONNUMERIC:	verror_put_string("Non-numeric characters in string \"%s\"",			  type_str);	break;	      case STRING_TO_INT_ERROR:	break;    }        return return_value;}/* * encode_response() * * Return a string encoding of the response_type in response_value. * Returns NULL on error. */static const char *encode_response(const myproxy_proto_response_type_t	response_value){    const char *string;        /*     * XXX Should return actual string description.     */    switch(response_value)    {      case MYPROXY_OK_RESPONSE:	string = "0";	break;	      case MYPROXY_ERROR_RESPONSE:	string = "1";	break;      case MYPROXY_AUTHORIZATION_RESPONSE:	string = "2";	break;	      default:	/* Should never get here */	string = NULL;	verror_put_string("Internal error: Bad reponse type (%d)",			  response_value);	break;    }    return string;}/* * string_to_int() * * Convert a string representation of an integer into an integer. * * Returns 1 on success, 0 if string contains non-numeric characters, * -1 on error setting verror. */static intstring_to_int(const char			*string,	      int				*integer){    char			*parse_end = NULL;    int				base = 0 /* Any */;    long int			value;    int				return_value = -1;        assert(string != NULL);    assert(integer != NULL);        /* Check for empty string */    if (strlen(string) == 0)    {	verror_put_string("Zero-length string");	goto error;    }        value = strtol(string, &parse_end, base);        if (value == LONG_MIN)    {	verror_put_string("Underflow error");	goto error;    }        if (value == LONG_MAX)    {	verror_put_string("Overflow error");	goto error;    }        /* Make sure we parsed all the characters in string */    if (*parse_end != '\0')    {	return_value = 0;	goto error;    }        /* Success */    *integer = (int) value;    return_value = 1;      error:    return return_value;}/* Returns pointer to last processed char in the buffer or NULL on error *//* The entries are separated either by '\n' or by '\0' */static char *parse_entry(char *buffer, authorization_data_t *data){   char *str;   char *str_method;   char *p = buffer;   author_method_t method;   assert (data != NULL);   while (*p == '\0')       p++;   str_method = p;   if ((p = strchr(str_method, ':')) == NULL) {      verror_put_string("Parse error");      return NULL;   }   *p = '\0';   method = authorization_get_method(str_method);      str = p + 1;   if ((p = strchr(str, '\n')))       *p = '\0';   data->server_data = malloc(strlen(str) + 1);   if (data->server_data == NULL) {      verror_put_errno(errno);      return NULL;   }   strcpy(data->server_data, str);   data->client_data = NULL;   data->client_data_len = 0;   data->method = method;   return str + strlen(str);}/*   Parse buffer into author_data. The buffer is supposed to be '0'-terminated*/static intparse_auth_data(char *buffer, authorization_data_t ***auth_data){   char *p = buffer;   char *buffer_end;   void *tmp;   authorization_data_t **data = NULL;   int num_data = 0;   authorization_data_t entry;   int return_status = -1;   data = malloc(sizeof(*data));   if (data == NULL) {      verror_put_errno(errno);      return -1;   }   data[0] = NULL;      buffer_end = buffer + strlen(buffer);   do {      p = parse_entry(p, &entry);      if (p == NULL)	 goto end;      if (entry.method == AUTHORIZETYPE_NULL)	 continue;      tmp = realloc(data, (num_data + 1 + 1) * sizeof(*data));      if (tmp == NULL) {	 verror_put_errno(errno);	 goto end;      }      data = tmp;      data[num_data] = malloc(sizeof(entry));      if (data[num_data] == NULL) {	 verror_put_errno(errno);	 goto end;      }      data[num_data]->server_data = entry.server_data;      data[num_data]->client_data = entry.client_data;      data[num_data]->client_data_len = entry.client_data_len;      data[num_data]->method = entry.method;      data[num_data + 1] = NULL;      num_data++;   } while (p < buffer_end);   return_status = 0;   *auth_data = data;end:   if (return_status == -1)      authorization_data_free(data);   return return_status;}intmyproxy_init_credentials(myproxy_socket_attrs_t *attrs,                         const char             *delegfile){  char error_string[1024];  if (attrs == NULL)    return -1;  if (GSI_SOCKET_credentials_init_ext(attrs->gsi_socket,                                     delegfile) == GSI_SOCKET_ERROR) {    GSI_SOCKET_get_error_string(attrs->gsi_socket, error_string,                                sizeof(error_string));    verror_put_string("Error storing  credentials: %s\n", error_string);    return -1;  }  return 0;}/*** Accepts a credential and stores the information in a temp file** delegfile. */intmyproxy_accept_credentials(myproxy_socket_attrs_t *attrs,                           char                   *delegfile,                           int                     delegfile_len){  char error_string[1024];  if (attrs == NULL)    return -1;  if (GSI_SOCKET_credentials_accept_ext(attrs->gsi_socket,                                        delegfile,                                        delegfile_len) == GSI_SOCKET_ERROR)  {    GSI_SOCKET_get_error_string(attrs->gsi_socket, error_string,                                sizeof(error_string));    verror_put_string("Error accepting credentials: %s\n", error_string);    return -1;  }  return 0;}/*** Retrieves a credential from the repository and sends it to the client.  */intmyproxy_get_credentials(myproxy_socket_attrs_t *attrs,                         const char             *delegfile){  char error_string[1024];  if (attrs == NULL)    return -1;  if (GSI_SOCKET_get_creds(attrs->gsi_socket,                                     delegfile) == GSI_SOCKET_ERROR)  {    GSI_SOCKET_get_error_string(attrs->gsi_socket, error_string,                                sizeof(error_string));    verror_put_string("Error getting credentials: %s\n", error_string);    return -1;  }  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -