<commit_before>#include <gnome-keyring.h>
#define OK GNOME_KEYRING_RESULT_OK

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>

/* for password prompt */
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

static char *
default_user(void)
{
  int i;
  char *vars[] = { "USER", "LOGNAME", NULL };
  char *ret = NULL;
  for (i = 0; vars[i] && !ret; i++)
    ret = getenv(vars[i]);
  return ret;
}

static void
detailed_key_listing(gchar *keyring, guint32 id)
{
  GnomeKeyringAttributeList *attlist;
  GnomeKeyringResult result =
    gnome_keyring_item_get_attributes_sync(keyring, id, &attlist);
  if (result == GNOME_KEYRING_RESULT_OK) {
    GnomeKeyringAttribute att, *atts;
    int i;
    atts = (GnomeKeyringAttribute *)attlist->data;
    for (i = 0; i < attlist->len; i++) {
      att = atts[i];
      if (att.type == GNOME_KEYRING_ATTRIBUTE_TYPE_STRING) {
        printf("  %s = '%s'\n", att.name, att.value.string);
      } else if (att.type == GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32) {
        printf("  %s = %u\n", att.name, att.value.integer);
      } else {
        printf("Found %d\n", i);
      }
    }
    gnome_keyring_attribute_list_free(attlist);
  }
}

static int
key_listing(int verbose)
{
  GList *keyrings, *current;
  if (gnome_keyring_list_keyring_names_sync(&keyrings) != OK) {
    fprintf(stderr, "Couldn't get list of keyring names\n");
    return 1;
  }

  for (current = keyrings; current; current = current->next) {
    GList *ids, *cid;
    gchar *keyring = (gchar *)current->data;
    printf("Keyring %s\n", keyring);

    if (gnome_keyring_list_item_ids_sync(keyring, &ids) != OK) {
      fprintf(stderr, "Couldn't list IDs on keyring %s\n", keyring);
      continue;
    }

    for (cid = ids; cid; cid = cid->next) {
      GnomeKeyringItemInfo *info;
      GnomeKeyringResult result =
        gnome_keyring_item_get_info_full_sync(keyring, GPOINTER_TO_INT(cid->data), GNOME_KEYRING_ITEM_INFO_SECRET, &info);
      if (result == GNOME_KEYRING_RESULT_OK) {
        printf(" %s\n", gnome_keyring_item_info_get_display_name(info));
        if (verbose)
          detailed_key_listing(keyring, GPOINTER_TO_INT(cid->data));

        gnome_keyring_item_info_free(info);
      } else {
#define CHECKIT(X) case GNOME_KEYRING_RESULT_ ## X: printf(#X "\n"); break
        switch (result) {
          CHECKIT(OK); // warning if not included
          CHECKIT(DENIED);
          CHECKIT(NO_KEYRING_DAEMON);
          CHECKIT(ALREADY_UNLOCKED);
          CHECKIT(NO_SUCH_KEYRING);
          CHECKIT(BAD_ARGUMENTS);
          CHECKIT(IO_ERROR);
          CHECKIT(CANCELLED);
          CHECKIT(KEYRING_ALREADY_EXISTS);
          CHECKIT(NO_MATCH);
        }
      }
    }
  }

  return 0;
}

#define MAXPASS 65536
static void
password_cleaner(char *password)
{
  int i;
  for (i = 0; password[i] && i < MAXPASS; i++)
    if (password[i] == '\r' || password[i] == '\n')
      break;
  password[i] = '\0';
}

static char *
password_prompt(const char *user, const char *server, const char *domain, const char *protocol, guint32 port)
{
  int status;
  int pid;
  int fd[2];
  char *password;
  password = (char *)malloc(MAXPASS * sizeof(char));
  if (!password) return password;
  password[0] = '\0';

  pipe(fd);

  pid = fork();
  if (pid < 0) return NULL;

  if (pid) { /* parent */
    int ret;
    close(fd[1]);
    ret = read(fd[0], password, MAXPASS);
    close(fd[0]);
    waitpid(-1, &status, 0);
    if (ret < 0 || !*password) password[0] = '\0';
    password_cleaner(password);
  } else {
    char *argv[] = { "dmenu", "--secret", "-p", "password", NULL };
    close(fd[0]);
    dup2(fd[1], 1);
    close(0);
    execvp("dmenu", argv);
    exit(1);
  }
  return password;
}

static char *
password_from(int fd)
{
  char *password;
  int ret;

  password = (char *)malloc(MAXPASS * sizeof(char));
  if (!password) return password;
  ret = read(fd, password, MAXPASS);
  if (ret < 0) password[0] = '\0';
  password_cleaner(password);
  return password;
}

static void
set_port_by_protocol(guint32 *port, const char *protocol)
{
  struct servent *srv;
  srv = getservbyname(protocol, NULL);
  if (srv) *port = ntohs(srv->s_port);
}

static void
set_protocol_by_port(char **protocol, int port)
{
  struct servent *srv;
  srv = getservbyport(htons(port), NULL);
  if (srv) *protocol = srv->s_name;
}

static void usage() {
  printf("Usage: password-manager [options]\n");
  printf("\n");
  printf("Password attributes:\n");
  printf("  -u/--user USERNAME\n");
  printf("  -h/--host HOSTNAME | -s/--server SERVER | -d/--domain DOMAIN\n");
  printf("     HOST = SERVER.DOMAIN\n");
  printf("  -P/--protocol PROTOCOL | -p/--port PORTNUMBER \n");<commit_msg>password-manager: special-cases "rdp" => 3389<commit_after>#include <gnome-keyring.h>
#define OK GNOME_KEYRING_RESULT_OK

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>

/* for password prompt */
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

static char *
default_user(void)
{
  int i;
  char *vars[] = { "USER", "LOGNAME", NULL };
  char *ret = NULL;
  for (i = 0; vars[i] && !ret; i++)
    ret = getenv(vars[i]);
  return ret;
}

static void
detailed_key_listing(gchar *keyring, guint32 id)
{
  GnomeKeyringAttributeList *attlist;
  GnomeKeyringResult result =
    gnome_keyring_item_get_attributes_sync(keyring, id, &attlist);
  if (result == GNOME_KEYRING_RESULT_OK) {
    GnomeKeyringAttribute att, *atts;
    int i;
    atts = (GnomeKeyringAttribute *)attlist->data;
    for (i = 0; i < attlist->len; i++) {
      att = atts[i];
      if (att.type == GNOME_KEYRING_ATTRIBUTE_TYPE_STRING) {
        printf("  %s = '%s'\n", att.name, att.value.string);
      } else if (att.type == GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32) {
        printf("  %s = %u\n", att.name, att.value.integer);
      } else {
        printf("Found %d\n", i);
      }
    }
    gnome_keyring_attribute_list_free(attlist);
  }
}

static int
key_listing(int verbose)
{
  GList *keyrings, *current;
  if (gnome_keyring_list_keyring_names_sync(&keyrings) != OK) {
    fprintf(stderr, "Couldn't get list of keyring names\n");
    return 1;
  }

  for (current = keyrings; current; current = current->next) {
    GList *ids, *cid;
    gchar *keyring = (gchar *)current->data;
    printf("Keyring %s\n", keyring);

    if (gnome_keyring_list_item_ids_sync(keyring, &ids) != OK) {
      fprintf(stderr, "Couldn't list IDs on keyring %s\n", keyring);
      continue;
    }

    for (cid = ids; cid; cid = cid->next) {
      GnomeKeyringItemInfo *info;
      GnomeKeyringResult result =
        gnome_keyring_item_get_info_full_sync(keyring, GPOINTER_TO_INT(cid->data), GNOME_KEYRING_ITEM_INFO_SECRET, &info);
      if (result == GNOME_KEYRING_RESULT_OK) {
        printf(" %s\n", gnome_keyring_item_info_get_display_name(info));
        if (verbose)
          detailed_key_listing(keyring, GPOINTER_TO_INT(cid->data));

        gnome_keyring_item_info_free(info);
      } else {
#define CHECKIT(X) case GNOME_KEYRING_RESULT_ ## X: printf(#X "\n"); break
        switch (result) {
          CHECKIT(OK); // warning if not included
          CHECKIT(DENIED);
          CHECKIT(NO_KEYRING_DAEMON);
          CHECKIT(ALREADY_UNLOCKED);
          CHECKIT(NO_SUCH_KEYRING);
          CHECKIT(BAD_ARGUMENTS);
          CHECKIT(IO_ERROR);
          CHECKIT(CANCELLED);
          CHECKIT(KEYRING_ALREADY_EXISTS);
          CHECKIT(NO_MATCH);
        }
      }
    }
  }

  return 0;
}

#define MAXPASS <PASSWORD>6
static void
password_cleaner(char *password)
{
  int i;
  for (i = 0; password[i] && i < MAXPASS; i++)
    if (password[i] == '\r' || password[i] == '\n')
      break;
  password[i] = '\0';
}

static char *
password_prompt(const char *user, const char *server, const char *domain, const char *protocol, guint32 port)
{
  int status;
  int pid;
  int fd[2];
  char *password;
  password = (char *)malloc(MAXPASS * sizeof(char));
  if (!password) return password;
  password[0] = '\0';

  pipe(fd);

  pid = fork();
  if (pid < 0) return NULL;

  if (pid) { /* parent */
    int ret;
    close(fd[1]);
    ret = read(fd[0], password, MAXPASS);
    close(fd[0]);
    waitpid(-1, &status, 0);
    if (ret < 0 || !*password) password[0] = '\0';
    password_cleaner(password);
  } else {
    char *argv[] = { "dmenu", "--secret", "-p", "password", NULL };
    close(fd[0]);
    dup2(fd[1], 1);
    close(0);
    execvp("dmenu", argv);
    exit(1);
  }
  return password;
}

static char *
password_from(int fd)
{
  char *password;
  int ret;

  password = (char *)malloc(MAXPASS * sizeof(char));
  if (!password) return password;
  ret = read(fd, password, MAXPASS);
  if (ret < 0) password[0] = '\0';
  password_cleaner(password);
  return password;
}

static void
set_port_by_protocol(guint32 *port, const char *protocol)
{
  struct servent *srv;
  if (!strcmp("rdp", protocol)) {
    *port = 3389;
    return;
  }
  srv = getservbyname(protocol, NULL);
  if (srv) *port = ntohs(srv->s_port);
}

static void
set_protocol_by_port(char **protocol, int port)
{
  struct servent *srv;
  srv = getservbyport(htons(port), NULL);
  if (srv) *protocol = srv->s_name;
}

static void usage() {
  printf("Usage: password-manager [options]\n");
  printf("\n");
  printf("Password attributes:\n");
  printf("  -u/--user USERNAME\n");
  printf("  -h/--host HOSTNAME | -s/--server SERVER | -d/--domain DOMAIN\n");
  printf("     HOST = SERVER.DOMAIN\n");
  printf("  -P/--protocol PROTOCOL | -p/--port PORTNUMBER \n");
{'fraction_non_alphanumeric': 0.1564204783885858, 'fraction_numerical': 0.008812421317666807, 'mean_word_length': 3.252007136485281, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 39, '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': '1044249', 'n_tokens_mistral': 3672, 'n_tokens_neox': 3564, 'n_words': 792}