Exemplo n.º 1
0
void sgpgme_create_secret_key(PrefsAccount *account, gboolean ask_create)
{
	AlertValue val = G_ALERTDEFAULT;
	gchar *key_parms = NULL;
	gchar *name = NULL;
	gchar *email = NULL;
	gchar *passphrase = NULL, *passphrase_second = NULL;
	gint prev_bad = 0;
	gchar *tmp = NULL;
	gpgme_error_t err = 0;
	gpgme_ctx_t ctx;
	GtkWidget *window = NULL;
	gpgme_genkey_result_t key;

	if (account == NULL)
		account = account_get_default();

	if (account->address == NULL) {
		alertpanel_error(_("You have to save the account's information with \"OK\" "
				   "before being able to generate a key pair.\n"));
		return;
	}
	if (ask_create) {
		val = alertpanel(_("No PGP key found"),
				_("Claws Mail did not find a secret PGP key, "
				  "which means that you won't be able to sign "
				  "emails or receive encrypted emails.\n"
				  "Do you want to create a new key pair now?"),
				  GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
		if (val == G_ALERTDEFAULT) {
			prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
			prefs_gpg_save_config();
			return;
		}
	}

	if (account->name) {
		name = g_strdup(account->name);
	} else {
		name = g_strdup(account->address);
	}
	email = g_strdup(account->address);
	tmp = g_strdup_printf("%s <%s>", account->name?account->name:account->address, account->address);
again:
	passphrase = passphrase_mbox(tmp, NULL, prev_bad, 1);
	if (passphrase == NULL) {
		g_free(tmp);
		g_free(email);
		g_free(name);		
		return;
	}
	passphrase_second = passphrase_mbox(tmp, NULL, 0, 2);
	if (passphrase_second == NULL) {
		g_free(tmp);
		g_free(email);
		g_free(passphrase);		
		g_free(name);		
		return;
	}
	if (strcmp(passphrase, passphrase_second)) {
		g_free(passphrase);
		g_free(passphrase_second);
		prev_bad = 1;
		goto again;
	}
	
	key_parms = g_strdup_printf("<GnupgKeyParms format=\"internal\">\n"
					"Key-Type: RSA\n"
					"Key-Length: 2048\n"
					"Subkey-Type: RSA\n"
					"Subkey-Length: 2048\n"
					"Name-Real: %s\n"
					"Name-Email: %s\n"
					"Expire-Date: 0\n"
					"%s%s%s"
					"</GnupgKeyParms>\n",
					name, email, 
					strlen(passphrase)?"Passphrase: ":"",
					passphrase,
					strlen(passphrase)?"\n":"");
#ifndef G_PLATFORM_WIN32
	if (mlock(passphrase, strlen(passphrase)) == -1)
		debug_print("couldn't lock passphrase\n");
	if (mlock(passphrase_second, strlen(passphrase_second)) == -1)
		debug_print("couldn't lock passphrase2\n");
#endif
	g_free(tmp);
	g_free(email);
	g_free(name);
	g_free(passphrase_second);
	g_free(passphrase);
	
	err = gpgme_new (&ctx);
	if (err) {
		alertpanel_error(_("Couldn't generate a new key pair: %s"),
				 gpgme_strerror(err));
		g_free(key_parms);
		return;
	}
	

	window = label_window_create(_("Generating your new key pair... Please move the mouse "
			      "around to help generate entropy..."));

	err = gpgme_op_genkey(ctx, key_parms, NULL, NULL);
	g_free(key_parms);

	label_window_destroy(window);

	if (err) {
		alertpanel_error(_("Couldn't generate a new key pair: %s"), gpgme_strerror(err));
		gpgme_release(ctx);
		return;
	}
	key = gpgme_op_genkey_result(ctx);
	if (key == NULL) {
		alertpanel_error(_("Couldn't generate a new key pair: unknown error"));
		gpgme_release(ctx);
		return;
	} else {
		gchar *buf = g_strdup_printf(_("Your new key pair has been generated. "
				    "Its fingerprint is:\n%s\n\nDo you want to export it "
				    "to a keyserver?"),
				    key->fpr ? key->fpr:"null");
		AlertValue val = alertpanel(_("Key generated"), buf,
				  GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
		g_free(buf);
		if (val == G_ALERTALTERNATE) {
#ifndef G_OS_WIN32
			gchar *cmd = g_strdup_printf("gpg --no-tty --send-keys %s", key->fpr);
			int res = 0;
			pid_t pid = 0;
			pid = fork();
			if (pid == -1) {
				res = -1;
			} else if (pid == 0) {
				/* son */
				res = system(cmd);
				res = WEXITSTATUS(res);
				_exit(res);
			} else {
				int status = 0;
				time_t start_wait = time(NULL);
				res = -1;
				do {
					if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
						usleep(200000);
					} else {
						res = WEXITSTATUS(status);
						break;
					}
					if (time(NULL) - start_wait > 5) {
						debug_print("SIGTERM'ing gpg\n");
						kill(pid, SIGTERM);
					}
					if (time(NULL) - start_wait > 6) {
						debug_print("SIGKILL'ing gpg\n");
						kill(pid, SIGKILL);
						break;
					}
				} while(1);
			}
			if (res == 0) {
				alertpanel_notice(_("Key exported."));
			} else {
				alertpanel_error(_("Couldn't export key."));
			}
			g_free(cmd);
#else
			alertpanel_error(_("Key export isn't implemented in Windows."));
#endif
		}
	}
	prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
	prefs_gpg_save_config();
	gpgme_release(ctx);
}
Exemplo n.º 2
0
void exec_autoforward_cb(GObject *obj, FolderItem *item, const gchar *file, guint num)
{
  PrefsCommon *prefs_common;
  PrefsAccount *ac;

  gchar *rcpath;
  GSList* to_list=NULL;

  gchar *to;
  GError *errval;
  gboolean forward_all;

  gsize gz=0;
  gchar **folders;
  gboolean bmatch;
  int nindex;
  
  if (g_enable!=TRUE){
    return;
  }
  if (item->stype != F_NORMAL && item->stype != F_INBOX){
    return;
  }

  prefs_common = prefs_common_get();
  if (prefs_common->online_mode != TRUE){
    return;
  }

  ac = (PrefsAccount*)account_get_default();
  g_return_if_fail(ac != NULL);

  /* check item->path for filter */
  g_print("[DEBUG] %s:%s name:%s\n", G_STRLOC, G_STRFUNC, item->name);
  g_print("[DEBUG] %s:%s path:%s\n", G_STRLOC, G_STRFUNC,  item->path);

#if 0
  MsgInfo *msginfo = folder_item_get_msginfo(item, num);
  debug_print("[DEBUG] flags:%08x UNREAD:%08x NEW:%08x MARKED:%08x ", msginfo->flags, MSG_UNREAD, MSG_NEW, MSG_MARKED);
  debug_print("[DEBUG] perm_flags:%08x \n", msginfo->flags.perm_flags);
  debug_print("[DEBUG] tmp_flags:%08x \n", msginfo->flags.tmp_flags);
  if ( g_unreadonly_flg != FALSE){
    debug_print("[DEBUG] unreadonly flag:%s\n", g_unreadonly_flg ? "true" : "false");
    if (MSG_IS_UNREAD(msginfo->flags)){
      debug_print("[DEBUG] MSG_IS_UNREAD:true");
    } else {
      return;
    }
  }
#endif    

  rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "autoforwardrc", NULL);

  g_keyfile = g_key_file_new();
  g_key_file_load_from_file(g_keyfile, rcpath, G_KEY_FILE_KEEP_COMMENTS, NULL);
  
  to=g_key_file_get_string (g_keyfile, "forward", "to", NULL);
  debug_print("[DEBUG] to:%s", to);
  to_list = address_list_append(to_list, to);


  forward_all = g_key_file_get_boolean (g_keyfile, "forward", "all", &errval);
  if (forward_all != TRUE){
    if (errval) {
      switch (errval->code){
      case G_KEY_FILE_ERROR_INVALID_VALUE:
      case G_KEY_FILE_ERROR_KEY_NOT_FOUND:
        forward_all=TRUE;
        break;
      default:
        break;
      }
    }
  }

  bmatch = FALSE;
  if (forward_all != TRUE){
    folders = g_key_file_get_string_list(g_keyfile, "forward", "folder", &gz, NULL);
    if (gz != 0) {
      /* match or not */
      nindex = 0;
      for (nindex = 0; nindex < gz; nindex++){
        if (g_strcmp0(folders[nindex], item->path) == 0){
          bmatch = TRUE;
          debug_print("[DEBUG] %s %s => match\n", folders[nindex], item->path);
        }
      }
    } else {
      bmatch = FALSE;
    }
  }else{
    bmatch = TRUE;
  }
  g_free(rcpath);
  g_return_if_fail(to_list != NULL);

  g_print("[DEBUG] item->path:%s\n", item->path);
  g_print("[DEBUG] bmatch:%d\n", bmatch);
    
  g_return_if_fail(bmatch == TRUE);

  syl_plugin_send_message_set_forward_flags(ac->address);
  syl_plugin_send_message(file, ac, to_list);
}