nsresult
GnomeKeyring::deleteFoundItems(GList* foundList,
                                 PRBool aExpectOnlyOne = PR_FALSE)
{
  if (foundList == NULL) {
    GK_LOG(("Found not items to delete"));
    return NS_OK;
  }

  PRUint32 i = 0;
  for (GList* l = foundList; l != NULL; l = l->next, i++)
  {
    GnomeKeyringFound* found = static_cast<GnomeKeyringFound*>(l->data);
    GK_LOG(("Found item with id %i\n", found->item_id));

    GnomeKeyringResult result = gnome_keyring_item_delete_sync(keyringName.get(),
                                                               found->item_id);
    if (result != GNOME_KEYRING_RESULT_OK) {
      return NS_ERROR_FAILURE;
    }

    if (i == 1 && aExpectOnlyOne)
      NS_WARNING("Expected only one item to delete, but found more");
  }
  return NS_OK;
}
Esempio n. 2
0
int keyring_erase(struct credential *c)
{
	char  *object = NULL;
	GList *entries;
	GnomeKeyringNetworkPasswordData *password_data;
	GnomeKeyringResult result;

	/*
	 * Sanity check that we actually have something to match
	 * against. The input we get is a restrictive pattern,
	 * so technically a blank credential means "erase everything".
	 * But it is too easy to accidentally send this, since it is equivalent
	 * to empty input. So explicitly disallow it, and require that the
	 * pattern have some actual content to match.
	 */
	if (!c->protocol && !c->host && !c->path && !c->username)
		return EXIT_FAILURE;

	object = keyring_object(c);

	result = gnome_keyring_find_network_password_sync(
				c->username,
				NULL /* domain */,
				c->host,
				object,
				c->protocol,
				NULL /* authtype */,
				c->port,
				&entries);

	free(object);

	if (result == GNOME_KEYRING_RESULT_NO_MATCH)
		return EXIT_SUCCESS;

	if (result == GNOME_KEYRING_RESULT_CANCELLED)
		return EXIT_SUCCESS;

	if (result != GNOME_KEYRING_RESULT_OK)
	{
		error("%s",gnome_keyring_result_to_message(result));
		return EXIT_FAILURE;
	}

	/* pick the first one from the list (delete all matches?) */
	password_data = (GnomeKeyringNetworkPasswordData *) entries->data;

	result = gnome_keyring_item_delete_sync(
		password_data->keyring, password_data->item_id);

	gnome_keyring_network_password_list_free(entries);

	if (result != GNOME_KEYRING_RESULT_OK)
	{
		error("%s",gnome_keyring_result_to_message(result));
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}
Esempio n. 3
0
int gnome_keyring_sf_delete_password(const char * repo_id, const char * type)
{
    guint item_id;
    GnomeKeyringResult result;
    char * tmp = NULL;

    tmp = gnome_keyring_sf_get_password(repo_id, type, &item_id);
    g_free(tmp);
    result = gnome_keyring_item_delete_sync(NULL, item_id);

    return (result == GNOME_KEYRING_RESULT_OK);    
}
void GnomeKeyringPasswordBackend::removeEntry(const PasswordEntry &entry)
{
    initialize();

    GnomeKeyringResult result = gnome_keyring_item_delete_sync(GNOME_KEYRING_DEFAULT, entry.id.toUInt());

    if (result != GNOME_KEYRING_RESULT_OK) {
        qWarning() << "GnomeKeyringPasswordBackend::removeEntry Cannot remove entry from keyring!";
        return;
    }

    int index = m_allEntries.indexOf(entry);

    if (index > -1) {
        m_allEntries.remove(index);
    }
}
static gboolean
ep_keyring_delete_passwords (const gchar *user,
                             const gchar *server,
                             const gchar *protocol,
                             GList *passwords,
                             GError **error)
{
	while (passwords != NULL) {
		GnomeKeyringFound *found = passwords->data;
		GnomeKeyringResult result;

		/* Validate the item before deleting it. */
		if (!ep_keyring_validate (user, server, protocol, found->attributes)) {
			/* XXX We didn't always store protocols in the
			 *     keyring, so for backward-compatibility
			 *     try validating by user and server only. */
			if (!ep_keyring_validate (user, server, NULL, found->attributes)) {
				passwords = g_list_next (passwords);
				continue;
			}
		}

		result = gnome_keyring_item_delete_sync (NULL, found->item_id);
		if (result != GNOME_KEYRING_RESULT_OK) {
			g_set_error (
				error, EP_KEYRING_ERROR, result,
				"Unable to delete password in "
				"keyring (Keyring reports: %s)",
				gnome_keyring_result_to_message (result));
			return FALSE;
		}

		passwords = g_list_next (passwords);
	}

	return TRUE;
}
/**
 * ggn_keyring_item_delete:
 * @user: The username string.
 * @domain: The domain name string.
 *
 * This function will access the gnome-keyring daemon to delete
 * the given gmail account information from the keyring.
 *
 * Returns: Success #gboolean.
 **/
gboolean ggn_keyring_item_delete (const gchar *user,
                                  const gchar *domain) {
  /* declare some helping variables. */
  GList *found;
  GnomeKeyringFound *item;
  GnomeKeyringResult result;
  GnomeKeyringAttributeList *keyattribs;

  /* define some variables for finding the passphrase. */
  keyattribs = ggn_keyring_attribs (user, domain);

  /* search for the password. */
  result = gnome_keyring_find_items_sync (GNOME_KEYRING_ITEM_NETWORK_PASSWORD,
                                          keyattribs,
                                          &found);

  /* did we find a passphrase? */
  if (result == GNOME_KEYRING_RESULT_OK) {
    /* NOTE: read the passphrase from the _first_ found item. */
    item = g_list_nth_data (found, 0);

    /* delete this item. */
    gnome_keyring_item_delete_sync (NULL, item->item_id);
  }

  /* free the variables used for the search. */
  gnome_keyring_found_list_free (found);
  gnome_keyring_attribute_list_free (keyattribs);

  /* return our status boolean. */
  if (result == GNOME_KEYRING_RESULT_OK)
    return TRUE;

  /* otherwise, we failed. */
  return FALSE;
}
nsresult
GnomeKeyring::deleteFoundItems(GList* foundList,
                               bool aExpectOnlyOne = PR_FALSE)
{
  if (foundList == NULL) {
    GK_LOG(("Found not items to delete"));
    return NS_OK;
  }

  bool deleted = PR_FALSE;
  for (GList* l = foundList; l != NULL; l = l->next, deleted = PR_TRUE)
  {
    if (aExpectOnlyOne && deleted)
      NS_WARNING("Expected only one item to delete, but found more");

    GnomeKeyringFound* found = static_cast<GnomeKeyringFound*>(l->data);
    GK_LOG(("Found item with id %i\n", found->item_id));

    GnomeKeyringResult result = gnome_keyring_item_delete_sync(found->keyring,
                                                               found->item_id);
    MGK_GK_CHECK_NS(result);
  }
  return NS_OK;
}
static void
tny_gnome_keyring_password_getter_forget_password (TnyPasswordGetter *self, const gchar *aid)
{
	GList *list=NULL;
	GnomeKeyringResult keyringret;
	gchar *keyring;
	GnomeKeyringNetworkPasswordData *pwd_data;

	gnome_keyring_get_default_keyring_sync (&keyring);

	keyringret = gnome_keyring_find_network_password_sync (
		aid /* user */,
		"Mail", aid /* hostname */,
		"password", aid /* proto */, 
		"PLAIN", 0, &list);

	if (keyringret == GNOME_KEYRING_RESULT_OK && list)
	{
		pwd_data = list->data;
		gnome_keyring_item_delete_sync (keyring, pwd_data->item_id);
		gnome_keyring_network_password_list_free (list);
	}
	return;
}
Esempio n. 9
0
int main(int argc, char **argv) {
  GnomeKeyringResult result;
  GList *results;
  char *host;
  int i, tried, verbose = 0, remove = 0, list = 0;
  int set_domain = 0, set_host = 0, set_server = 0;
  int set_protocol = 0, set_port = 0, set_passfd = 0;
  int dry = 0;

  char *user = default_user();
  char *domain = NULL;
  char *server = NULL;
  char *object = NULL;
  char *protocol = NULL;
  char *authtype = NULL;
  guint32 port = 0;
  int passfd = 0;

  char *use_keyring = GNOME_KEYRING_DEFAULT;

  for (i = 1; i < argc; i++) {
    if (ARG(u) || LARG(user)) {
      S_OPT(user);
    } else if (ARG(h) || LARG(host)) {
      S_OPT_Q(host);
    } else if (ARG(s) || LARG(server)) {
      S_OPT_Q(server);
    } else if (ARG(d) || LARG(domain)) {
      S_OPT_Q(domain);
    } else if (ARG(P) || LARG(protocol)) {
      S_OPT_Q(protocol);
    } else if (ARG(p) || LARG(port)) {
      I_OPT_Q(port);
    } else if (LARG(passfd)) {
      I_OPT_Q(passfd);
    } else if (ARG(v) || LARG(verbose)) {
      verbose = 1;
    } else if (ARG(R) || LARG(remove)) {
      MODE(remove);
    } else if (ARG(l) || LARG(list)) {
      MODE(list);
    } else if (ARG(n) || LARG(dry)) {
      dry = 1;
      verbose++;
    } else if (ARG(k) || LARG(keyring)) {
      S_OPT(use_keyring);
    } else if (LARG(help)) {
      usage();
      return 0;
    } else {
      fprintf(stderr, "Unknown argument: %s\n", argv[i]);
      return 1;
    }
  }

  if (set_host && !(set_domain && set_server)) {
    char *dot = index(host, '.');
    if (dot) {
      dot[0] = '\0';
      dot++;
      if (!set_domain) domain = dot;
      if (!set_server) server = host;
    } else {
      server = host;
      domain = "";
    }
  } else if (set_domain && set_server) {
    host = NULL;
  } else if (!list) {
    fprintf(stderr, "Must set --host or (--server and --domain)\n");
    return 1;
  }

  if ((set_port + set_protocol) == 1) {
    if (set_protocol) {
      set_port_by_protocol(&port, protocol);
    } else if (set_port) {
      set_protocol_by_port(&protocol, port);
    }
  } else if (!(set_port || set_protocol || list)) {
    fprintf(stderr, "Must set at least one of --port or --protocol\n");
    return 1;
  }

  if (set_protocol && !port) {
    fprintf(stderr, "Couldn't determine port for --protocol %s\n", protocol);
    return 1;
  }

  if (verbose && !list) {
#define VALUE(X) printf("%s: %s\n", #X, X ? X : "(null)")
    VALUE(user);
    VALUE(domain);
    VALUE(server);
    VALUE(host);
    VALUE(protocol);
#undef VALUE
    printf("port: %d\n", port);
    if (dry)
      return 0;
  }

  if (!gnome_keyring_is_available()) {
    fprintf(stderr, "No keyring available\n");
    return 1;
  }

  if (list)
    return key_listing(verbose);

  for (tried = 0; tried < 2; tried++) {
    result = gnome_keyring_find_network_password_sync(
      user, domain, server, object, protocol, authtype, port,
      &results
    );
    if (verbose) printf("attempt #%d: ", tried);
    if (result == OK) {
      GList *current;
      GnomeKeyringNetworkPasswordData *passdata;
      char *password;
      for (i = 0, current = results; current; i++, current = current->next) {
        passdata = (GnomeKeyringNetworkPasswordData *)current->data;
        password = passdata->password;
        if (verbose) {
          printf("Result[%d]=%s\n", i, password);
          continue;
        }
        if (remove) {
          result = gnome_keyring_item_delete_sync(
            passdata->keyring,
            passdata->item_id
          );
          if (verbose)
            printf("Remove %s %d -> %s\n", passdata->keyring, passdata->item_id, result == OK ? "OK" : "NOT OK");
          if (!current->next)
            return 0;
          continue;
        }
        printf("%s", password);
        return 0;
      }
      if (password)
        break;
    }
    if (remove) {
      printf("No such password\n");
      return 1;
    }
    if (verbose) printf("nope\n");
    if (!tried) {
      char *password;
      if (set_passfd) {
        password = password_from(passfd);
      } else {
        password = password_prompt(user, server, domain, protocol, port);
      }
      if (password) {
        guint32 item_id;
        gnome_keyring_set_network_password_sync(
          use_keyring,
          user, domain, server, object, protocol, authtype, port,
          password, &item_id
        );
        if (verbose) printf("Stored password? %s\n", item_id ? "yes" : "no");
      }
    }
  }
  return 0;
}