<commit_before>#if defined(HAVE_LIBSASL2)

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

int myproxy_sasl_authenticated = 0;

static sasl_conn_t *conn = NULL;

static void
sasl_free_conn(void)
{
  if (conn)
    sasl_dispose(&conn);
}


static int
sasl_my_log(void *context __attribute__((unused)),
            int priority,
            const char *message)
{
  const char *label;

  if (! message)
    return SASL_BADPARAM;

  switch (priority) {
  case SASL_LOG_ERR:
    label = "Error";
    break;
  case SASL_LOG_NOTE:
    label = "Info";
    break;
  default:
    label = "Other";
    break;
  }

  myproxy_log("SASL %s: %s\n", label, message);

  return SASL_OK;
}


static sasl_callback_t callbacks[] = {
  {
    SASL_CB_LOG, &sasl_my_log, NULL
  }, {
    SASL_CB_LIST_END, NULL, NULL
  }
};

static int
send_response_sasl_data(myproxy_socket_attrs_t *attrs, 
			const char *data, int data_len)
{
    myproxy_response_t response = {0};
    authorization_data_t*	auth_data;
    char	buf[SASL_BUFFER_SIZE];
    int		result;
    unsigned    len=0;
    
    result = sasl_encode64(data, data_len, buf, SASL_BUFFER_SIZE, &len);
    buf[len] = '\0';
    if (result != SASL_OK) {
       verror_put_string("Encoding SASL data in base64 failed.\n");
       verror_put_errno(errno);
       return -1;
    }
    myproxy_debug("S: %s", buf);

    memset(&response, 0, sizeof (response));
    response.version = strdup(MYPROXY_VERSION);
    response.response_type = MYPROXY_AUTHORIZATION_RESPONSE;
    response.authorization_data = malloc(sizeof(authorization_data_t*) * 2);
    response.authorization_data[0] = malloc(sizeof(authorization_data_t));
    response.authorization_data[1] = NULL;

    auth_data = response.authorization_data[0];
    auth_data->server_data = strdup(buf);
    auth_data->client_data = NULL;
    auth_data->client_data_len = 0;
    auth_data->method = AUTHORIZETYPE_SASL;
    
    len = myproxy_serialize_response(&response, buf, sizeof(buf));
    if (len < 0) {
	verror_put_string("error in myproxy_serialize_response()");
	return -1;
    }

    if (myproxy_send(attrs, buf, len) < 0) {
        verror_put_string("error in myproxy_send()\n");
	return -1;
    } 

    free(response.version);
    authorization_data_free(response.authorization_data);

    return 0;
}


static int
recv_response_sasl_data(myproxy_socket_attrs_t *attrs, char *data)
{
   char  buf[SASL_BUFFER_SIZE];
   int   result;
   unsigned len;
   author_method_t client_auth_method;

   int   client_data_len = 0;

   len = myproxy_recv(attrs, buf, sizeof(buf));

   if (len <= 0)
       return -1;

   client_auth_method = (*buf);
   if (client_auth_method != AUTHORIZETYPE_SASL) {
      verror_put_string("SASL method not match.\n");
      verror_put_errno(errno);
      return -1;
   }

   client_data_len = len - sizeof(int);


   myproxy_debug("C: %s", buf + sizeof(int));
   result = sasl_decode64(buf + sizeof(int), client_data_len, data, SASL_BUFFER_SIZE, &len);
   if (result != SASL_OK) {
        myproxy_log("Decoding data from base64 failed in recv_response_sasl_data.");
	return -1;
   }
   data[len] = '\0'; 

   return len;
}

int
auth_sasl_negotiate_server(myproxy_socket_attrs_t *attrs,
			   myproxy_request_t *client_request)
{
   char  client_buffer[SASL_BUFFER_SIZE];
   int   client_data_len = 0;
<commit_msg>use strnlen() to decide how much data we need to b64decode<commit_after>#if defined(HAVE_LIBSASL2)

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

int myproxy_sasl_authenticated = 0;

static sasl_conn_t *conn = NULL;

static void
sasl_free_conn(void)
{
  if (conn)
    sasl_dispose(&conn);
}


static int
sasl_my_log(void *context __attribute__((unused)),
            int priority,
            const char *message)
{
  const char *label;

  if (! message)
    return SASL_BADPARAM;

  switch (priority) {
  case SASL_LOG_ERR:
    label = "Error";
    break;
  case SASL_LOG_NOTE:
    label = "Info";
    break;
  default:
    label = "Other";
    break;
  }

  myproxy_log("SASL %s: %s\n", label, message);

  return SASL_OK;
}


static sasl_callback_t callbacks[] = {
  {
    SASL_CB_LOG, &sasl_my_log, NULL
  }, {
    SASL_CB_LIST_END, NULL, NULL
  }
};

static int
send_response_sasl_data(myproxy_socket_attrs_t *attrs, 
			const char *data, int data_len)
{
    myproxy_response_t response = {0};
    authorization_data_t*	auth_data;
    char	buf[SASL_BUFFER_SIZE];
    int		result;
    unsigned    len=0;
    
    result = sasl_encode64(data, data_len, buf, SASL_BUFFER_SIZE, &len);
    buf[len] = '\0';
    if (result != SASL_OK) {
       verror_put_string("Encoding SASL data in base64 failed.\n");
       verror_put_errno(errno);
       return -1;
    }
    myproxy_debug("S: %s", buf);

    memset(&response, 0, sizeof (response));
    response.version = strdup(MYPROXY_VERSION);
    response.response_type = MYPROXY_AUTHORIZATION_RESPONSE;
    response.authorization_data = malloc(sizeof(authorization_data_t*) * 2);
    response.authorization_data[0] = malloc(sizeof(authorization_data_t));
    response.authorization_data[1] = NULL;

    auth_data = response.authorization_data[0];
    auth_data->server_data = strdup(buf);
    auth_data->client_data = NULL;
    auth_data->client_data_len = 0;
    auth_data->method = AUTHORIZETYPE_SASL;
    
    len = myproxy_serialize_response(&response, buf, sizeof(buf));
    if (len < 0) {
	verror_put_string("error in myproxy_serialize_response()");
	return -1;
    }

    if (myproxy_send(attrs, buf, len) < 0) {
        verror_put_string("error in myproxy_send()\n");
	return -1;
    } 

    free(response.version);
    authorization_data_free(response.authorization_data);

    return 0;
}


static int
recv_response_sasl_data(myproxy_socket_attrs_t *attrs, char *data)
{
   char  buf[SASL_BUFFER_SIZE];
   int   result;
   unsigned len;
   author_method_t client_auth_method;
   char *b64data;

   int   client_data_len = 0;

   len = myproxy_recv(attrs, buf, sizeof(buf));

   if (len <= 0)
       return -1;

   client_auth_method = (*buf);
   if (client_auth_method != AUTHORIZETYPE_SASL) {
      verror_put_string("SASL method not match.\n");
      verror_put_errno(errno);
      return -1;
   }

   client_data_len = len - sizeof(int);

   b64data = buf + sizeof(int);
   myproxy_debug("C: %s", b64data);
   result = sasl_decode64(b64data, strnlen(b64data, client_data_len),
                          data, SASL_BUFFER_SIZE, &len);
   if (result != SASL_OK) {
        myproxy_log("Decoding data from base64 failed in recv_response_sasl_data.");
	return -1;
   }
   data[len] = '\0'; 

   return len;
}

int
auth_sasl_negotiate_server(myproxy_socket_attrs_t *attrs,
			   myproxy_request_t *client_request)
{
   char  client_buffer[SASL_BUFFER_SIZE];
   int   client_data_len = 0;

{'fraction_non_alphanumeric': 0.11401704378489567, 'fraction_numerical': 0.011166617690273288, 'mean_word_length': 3.501984126984127, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '6848409', 'n_tokens_mistral': 2626, 'n_tokens_neox': 2510, 'n_words': 500}