<commit_before>#include "myproxy_common.h"	/* all needed headers included here */

struct authorization_func {
   char * (*create_server_data) (void);
   char * (*create_client_data) (authorization_data_t *data, 
	                         void *extra_data, 
				 size_t extra_data_len,
				 size_t *client_data_len);
   int (*check_client) (authorization_data_t *client_auth_data,
			struct myproxy_creds *creds, /* o co zada */
			char *client_name);
   author_method_t method;
   char *name; /* arbitrary ASCII string without a colon (':') */
};

static struct authorization_func * _find_func(author_method_t method);

static authorization_data_t * 
_find_data(author_method_t method, authorization_data_t **data);


/*
 * Implementation of password-based authorization
 */
char * 
auth_passwd_create_server_data(void)
{
   return strdup("Put your password for the Myproxy server");
}

char * 
auth_passwd_create_client_data(authorization_data_t *data, 
                               void *extra_data, size_t extra_data_len,
			       size_t *client_data_len)
{ 
   char *tmp;

   tmp = malloc(extra_data_len + 1);
   if (tmp == NULL)
      return NULL;
   memcpy(tmp, extra_data, extra_data_len);
   tmp[extra_data_len] = '\0';
   *client_data_len = extra_data_len + 1;
   return tmp;
}

int auth_passwd_check_client(authorization_data_t *client_auth_data,
                             struct myproxy_creds *creds, char *client_name)
{ 
   char *tmp1;

   if (creds->passphrase != NULL &&
       client_auth_data->client_data_len >= MIN_PASS_PHRASE_LEN &&
       client_auth_data->client_data != NULL && 
       (tmp1 = (char *)crypt(client_auth_data->client_data,
		    &creds->owner_name[strlen(creds->owner_name)-3])) != NULL &&
       strcmp(creds->passphrase, tmp1) == 0) {
      return 1;
   }
   else 
      return 0;
}

struct authorization_func authorization_passwd = {
   auth_passwd_create_server_data,
   auth_passwd_create_client_data,
   auth_passwd_check_client,
   AUTHORIZETYPE_PASSWD,
   "password"
};



/* 
 * Implementation of certificate-based authorization
 */

#define CHALLENGE_SIZE  16

char * auth_cert_create_server_data(void)
{
   unsigned char random[CHALLENGE_SIZE];
   char *challenge; 
   int i;
   
   /* XXX */
   if (RAND_load_file("/dev/urandom", 128) == 0) {
      verror_put_string("RAND_load_file(): %s");
      ssl_error_to_verror();
      return NULL;
   }

   RAND_bytes(random, sizeof(random));

   challenge = malloc(CHALLENGE_SIZE * 2 + 1);
   if (challenge == NULL) {
      verror_put_string("malloc()");
      verror_put_errno(errno);
      return NULL;
   }

   for (i = 0; i < CHALLENGE_SIZE; i++) {
      int     dd = random[i] & 0x0f;
      challenge[2*i+1] = dd<10 ? dd+'0' : dd-10+'a';
      dd = random[i] >> 4;
      challenge[2*i] = dd<10 ? dd+'0' : dd-10+'a';
   }
   challenge[CHALLENGE_SIZE * 2] = '\0';

   return challenge;
}<commit_msg>- don't hard-code in /dev/urandom load; openssl will do the right thing - check return value from RAND_bytes() as the openssl PRNG might fail   if problem with random seed<commit_after>#include "myproxy_common.h"	/* all needed headers included here */

struct authorization_func {
   char * (*create_server_data) (void);
   char * (*create_client_data) (authorization_data_t *data, 
	                         void *extra_data, 
				 size_t extra_data_len,
				 size_t *client_data_len);
   int (*check_client) (authorization_data_t *client_auth_data,
			struct myproxy_creds *creds, /* o co zada */
			char *client_name);
   author_method_t method;
   char *name; /* arbitrary ASCII string without a colon (':') */
};

static struct authorization_func * _find_func(author_method_t method);

static authorization_data_t * 
_find_data(author_method_t method, authorization_data_t **data);


/*
 * Implementation of password-based authorization
 */
char * 
auth_passwd_create_server_data(void)
{
   return strdup("Put your password for the Myproxy server");
}

char * 
auth_passwd_create_client_data(authorization_data_t *data, 
                               void *extra_data, size_t extra_data_len,
			       size_t *client_data_len)
{ 
   char *tmp;

   tmp = malloc(extra_data_len + 1);
   if (tmp == NULL)
      return NULL;
   memcpy(tmp, extra_data, extra_data_len);
   tmp[extra_data_len] = '\0';
   *client_data_len = extra_data_len + 1;
   return tmp;
}

int auth_passwd_check_client(authorization_data_t *client_auth_data,
                             struct myproxy_creds *creds, char *client_name)
{ 
   char *tmp1;

   if (creds->passphrase != NULL &&
       client_auth_data->client_data_len >= MIN_PASS_PHRASE_LEN &&
       client_auth_data->client_data != NULL && 
       (tmp1 = (char *)crypt(client_auth_data->client_data,
		    &creds->owner_name[strlen(creds->owner_name)-3])) != NULL &&
       strcmp(creds->passphrase, tmp1) == 0) {
      return 1;
   }
   else 
      return 0;
}

struct authorization_func authorization_passwd = {
   auth_passwd_create_server_data,
   auth_passwd_create_client_data,
   auth_passwd_check_client,
   AUTHORIZETYPE_PASSWD,
   "password"
};



/* 
 * Implementation of certificate-based authorization
 */

#define CHALLENGE_SIZE  16

char * auth_cert_create_server_data(void)
{
   unsigned char random[CHALLENGE_SIZE];
   char *challenge; 
   int i;
   
#if 0
   /* XXX */
   if (RAND_load_file("/dev/urandom", 128) == 0) {
      verror_put_string("RAND_load_file(\"/dev/urandom\") failed");
      ssl_error_to_verror();
      return NULL;
   }
#endif

   /* RAND_bytes() will fail if the PRNG has not been seeded with
      enough randomness to ensure an unpredictable byte sequence. */
   if (RAND_bytes(random, sizeof(random)) == 0) {
      verror_put_string("RAND_bytes failed");
      ssl_error_to_verror();
      return NULL;
   }

   challenge = malloc(CHALLENGE_SIZE * 2 + 1);
   if (challenge == NULL) {
      verror_put_string("malloc()");
      verror_put_errno(errno);
      return NULL;
   }

   for (i = 0; i < CHALLENGE_SIZE; i++) {
      int     dd = random[i] & 0x0f;
      challenge[2*i+1] = dd<10 ? dd+'0' : dd-10+'a';
      dd = random[i] >> 4;
      challenge[2*i] = dd<10 ? dd+'0' : dd-10+'a';
   }
   challenge[CHALLENGE_SIZE * 2] = '\0';

   return challenge;
}
{'fraction_non_alphanumeric': 0.1156865886877103, 'fraction_numerical': 0.012177535651337926, 'mean_word_length': 3.237610319076714, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6516659', 'n_tokens_mistral': 2304, 'n_tokens_neox': 2135, 'n_words': 458}