1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-11-04 08:17:17 -05:00
elinks/src/formhist/formhist.c

461 lines
9.2 KiB
C
Raw Normal View History

/* Implementation of a login manager for HTML forms */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include "elinks.h"
#include "bfu/dialog.h"
#include "config/home.h"
#include "document/forms.h"
#include "formhist/dialogs.h"
#include "formhist/formhist.h"
#include "intl/libintl.h"
#include "main/module.h"
#include "main/object.h"
#include "session/session.h"
#include "terminal/window.h"
#include "util/base64.h"
#include "util/file.h"
#include "util/lists.h"
#include "util/secsave.h"
#include "util/string.h"
#include "viewer/text/form.h"
#define FORMS_HISTORY_FILENAME "formhist"
/* TODO: Remember multiple login for the same form.
* TODO: Password manager GUI (here?) (in dialogs.c, of course --pasky). */
bug 764: Initialize the right member of union option_value INIT_OPTION used to initialize union option_value at compile time by casting the default value to LIST_OF(struct option) *, which is the type of the first member. On sparc64 and other big-endian systems where sizeof(int) < sizeof(struct list_head *), this tended to leave option->value.number as zero, thus messing up OPT_INT and OPT_BOOL at least. OPT_LONG however tended to work right. This would be easy to fix with C99 designated initializers, but doc/hacking.txt says ELinks must be kept C89 compatible. Another solution would be to make register_options() read the value from option->value.tree (the first member), cast it back to the right type, and write it to the appropriate member; but that would still require somewhat dubious conversions between integers, data pointers, and function pointers. So here's a rather more invasive solution. Add struct option_init, which is somewhat similar to struct option but has non-overlapping members for different types of values, to ensure nothing is lost in compile-time conversions. Move unsigned char *path from struct option_info to struct option_init, and replace struct option_info with a union that contains struct option_init and struct option. Now, this union can be initialized with no portability problems, and register_options() then moves the values from struct option_init to their final places in struct option. In my x86 ELinks build with plenty of options configured in, this change bloated the text section by 340 bytes but compressed the data section by 2784 bytes, presumably because union option_info is a pointer smaller than struct option_info was. (cherry picked from elinks-0.12 commit e5f6592ee20780a61f70feeb1f9e17631b9c5835) Conflicts: src/protocol/fsp/fsp.c: All options had been removed in 0.13.GIT. src/protocol/smb/smb2.c: Ditto.
2009-08-15 15:39:07 -04:00
static union option_info forms_history_options[] = {
INIT_OPT_BOOL("document.browse.forms", N_("Show form history dialog"),
2022-01-15 14:10:37 -05:00
"show_formhist", OPT_ZERO, 0,
N_("Ask if a login form should be saved to file or not. "
"This option only disables the dialog, already saved login "
"forms are unaffected.")),
NULL_OPTION_INFO,
};
2007-07-26 15:39:08 -04:00
INIT_LIST_OF(struct formhist_data, saved_forms);
static struct formhist_data *
new_formhist_item(char *url)
{
struct formhist_data *form;
int url_len = strlen(url);
form = mem_calloc(1, sizeof(*form) + url_len);
if (!form) return NULL;
memcpy(form->url, url, url_len);
2022-01-16 13:09:27 -05:00
form->submit = (LIST_OF(struct submitted_value) *)mem_alloc(sizeof(*form->submit));
if (!form->submit) { mem_free(form); return NULL; }
object_nolock(form, "formhist");
init_list(*form->submit);
form->box_item = add_listbox_leaf(&formhist_browser, NULL, form);
if (!form->box_item) {
mem_free(form->submit);
mem_free(form);
return NULL;
}
return form;
}
static void
done_formhist_item(struct formhist_data *form)
{
done_listbox_item(&formhist_browser, form->box_item);
done_submitted_value_list(form->submit);
mem_free(form->submit);
mem_free(form);
}
void
delete_formhist_item(struct formhist_data *form)
{
del_from_list(form);
done_formhist_item(form);
}
static int loaded = 0;
int
load_formhist_from_file(void)
{
struct formhist_data *form;
char tmp[MAX_STR_LEN];
char *file;
FILE *f;
if (loaded) return 1;
if (!elinks_home) return 0;
file = straconcat(elinks_home, FORMS_HISTORY_FILENAME,
(char *) NULL);
if (!file) return 0;
f = fopen(file, "rb");
mem_free(file);
if (!f) return 0;
while (fgets(tmp, MAX_STR_LEN, f)) {
char *p;
int dontsave = 0;
if (tmp[0] == '\n' && !tmp[1]) continue;
p = strchr((const char *)tmp, '\t');
if (p) {
*p = '\0';
++p;
if (!strcmp(tmp, "dontsave"))
dontsave = 1;
} else {
/* Compat. with older file formats. Remove it at some
* time. --Zas */
if (!strncmp(tmp, "dontsave,", 9)) {
dontsave = 1;
p = tmp + 9;
} else {
p = tmp;
}
}
/* URL */
p[strlen(p) - 1] = '\0';
form = new_formhist_item(p);
if (!form) continue;
if (dontsave) form->dontsave = 1;
/* Fields type, name, value */
while (fgets(tmp, MAX_STR_LEN, f)) {
struct submitted_value *sv;
char *type, *name, *value;
char *enc_value;
enum form_type ftype;
int ret;
if (tmp[0] == '\n' && !tmp[1]) break;
/* Type */
type = tmp;
p = strchr((const char *)type, '\t');
if (!p) goto fail;
*p = '\0';
/* Name */
name = ++p;
p = strchr((const char *)name, '\t');
if (!p) {
/* Compatibility with previous file formats.
* REMOVE AT SOME TIME --Zas */
value = name;
name = type;
if (*name == '*') {
name++;
type = "password";
} else {
type = "text";
}
goto cont;
}
*p = '\0';
/* Value */
value = ++p;
cont:
p = strchr((const char *)value, '\n');
if (!p) goto fail;
*p = '\0';
ret = str2form_type(type);
if (ret == -1) goto fail;
ftype = ret;
if (form->dontsave) continue;
enc_value = *value ? base64_decode(value)
: stracpy(value);
if (!enc_value) goto fail;
sv = init_submitted_value(name, enc_value,
ftype, NULL, 0);
mem_free(enc_value);
if (!sv) goto fail;
add_to_list(*form->submit, sv);
}
add_to_list(saved_forms, form);
}
fclose(f);
loaded = 1;
return 1;
fail:
done_formhist_item(form);
return 0;
}
int
save_formhist_to_file(void)
{
struct secure_save_info *ssi;
char *file;
struct formhist_data *form;
int r;
if (!elinks_home || get_cmd_opt_bool("anonymous"))
return 0;
file = straconcat(elinks_home, FORMS_HISTORY_FILENAME,
(char *) NULL);
if (!file) return 0;
ssi = secure_open(file);
mem_free(file);
if (!ssi) return 0;
/* Write the list to password file ($ELINKS_HOME/formhist) */
foreach (form, saved_forms) {
struct submitted_value *sv;
if (form->dontsave) {
secure_fprintf(ssi, "dontsave\t%s\n\n", form->url);
continue;
}
secure_fprintf(ssi, "%s\n", form->url);
foreach (sv, *form->submit) {
char *encvalue;
if (sv->value && *sv->value) {
/* Obfuscate the value. If we do
* $ cat ~/.elinks/formhist
* we don't want someone behind our back to read our
* password (androids don't count). */
encvalue = base64_encode(sv->value);
} else {
encvalue = stracpy("");
}
if (!encvalue) return 0;
/* Format is : type[TAB]name[TAB]value[CR] */
secure_fprintf(ssi, "%s\t%s\t%s\n", form_type2str(sv->type),
sv->name, encvalue);
mem_free(encvalue);
}
secure_fputc(ssi, '\n');
}
r = secure_close(ssi);
if (r == 0) loaded = 1;
return r;
}
/* Check whether the form (chain of @submit submitted_values at @url document)
* is already present in the form history. */
static int
form_exists(struct formhist_data *form1)
{
struct formhist_data *form;
if (!load_formhist_from_file()) return 0;
foreach (form, saved_forms) {
int count = 0;
int exact = 0;
struct submitted_value *sv;
if (strcmp(form->url, form1->url)) continue;
if (form->dontsave) return 1;
/* Iterate through submitted entries. */
foreach (sv, *form1->submit) {
struct submitted_value *sv2;
char *value = NULL;
count++;
foreach (sv2, *form->submit) {
if (sv->type != sv2->type) continue;
if (!strcmp(sv->name, sv2->name)) {
exact++;
value = sv2->value;
break;
}
}
/* If we found a value for that name, check if value
* has changed or not. */
if (value && strcmp(sv->value, value)) return 0;
}
/* Check if submitted values have changed or not. */
if (count && exact && count == exact) return 1;
}
return 0;
}
static int
forget_forms_with_url(char *url)
{
struct formhist_data *form, *next;
int count = 0;
foreachsafe (form, next, saved_forms) {
if (strcmp(form->url, url)) continue;
delete_formhist_item(form);
count++;
}
return count;
}
/* Appends form data @form_ (url and submitted_value(s)) to the password file. */
static void
remember_form(void *form_)
{
struct formhist_data *form = form_;
forget_forms_with_url(form->url);
add_to_list(saved_forms, form);
save_formhist_to_file();
}
static void
dont_remember_form(void *form_)
{
struct formhist_data *form = form_;
done_formhist_item(form);
}
static void
never_for_this_site(void *form_)
{
struct formhist_data *form = form_;
form->dontsave = 1;
remember_form(form);
}
char *
get_form_history_value(char *url, char *name)
{
struct formhist_data *form;
if (!url || !*url || !name || !*name) return NULL;
if (!load_formhist_from_file()) return NULL;
foreach (form, saved_forms) {
if (form->dontsave) continue;
if (!strcmp(form->url, url)) {
struct submitted_value *sv;
foreach (sv, *form->submit)
if (!strcmp(sv->name, name))
return sv->value;
}
}
return NULL;
}
void
2007-07-26 15:39:08 -04:00
memorize_form(struct session *ses, LIST_OF(struct submitted_value) *submit,
struct form *forminfo)
{
Here is a framework that detects cases where a PO file assigns the same accelerator key to multiple buttons in a dialog box or to multiple items in a menu. ELinks already has some support for this but it requires the translator to run ELinks and manually scan through all menus and dialogs. The attached changes make it possible to quickly detect and list any conflicts, including ones that can only occur on operating systems or configurations that the translator is not currently using. The changes have no immediate effect on the elinks executable or the MO files. PO files become larger, however. The scheme works like this: - Like before, accelerator keys in translatable strings are tagged with the tilde (~) character. - Whenever a C source file defines an accelerator key, it must assign one or more named "contexts" to it. The translations in the PO files inherit these contexts. If multiple strings use the same accelerator (case insensitive) in the same context, that's a conflict and can be detected automatically. - The contexts are defined with "gettext_accelerator_context" comments in source files. These comments delimit regions where all translatable strings containing tildes are given the same contexts. There must be one special comment at the top of the region; it lists the contexts assigned to that region. The region automatically ends at the end of the function (found with regexp /^\}/), but it can also be closed explicitly with another special comment. The comments are formatted like this: /* [gettext_accelerator_context(foo, bar, baz)] begins a region that uses the contexts "foo", "bar", and "baz". The comma is the delimiter; whitespace is optional. [gettext_accelerator_context()] ends the region. */ The scripts don't currently check whether this syntax occurs inside or outside comments. - The names of contexts consist of C identifiers delimited with periods. I typically used the name of a function that sets up a dialog, or the name of an array where the items of a menu are listed. There is a special feature for static functions: if the name begins with a period, then the period will be replaced with the name of the source file and a colon. - If a menu is programmatically generated from multiple parts, of which some are never used together, so that it is safe to use the same accelerators in them, then it is necessary to define multiple contexts for the same menu. link_menu() in src/viewer/text/link.c is the most complex example of this. - During make update-po: - A Perl script (po/gather-accelerator-contexts.pl) reads po/elinks.pot, scans the source files listed in it for "gettext_accelerator_context" comments, and rewrites po/elinks.pot with "accelerator_context" comments that indicate the contexts of each msgid: the union of all contexts of all of its uses in the source files. It also removes any "gettext_accelerator_context" comments that xgettext --add-comments has copied to elinks.pot. - If po/gather-accelerator-contexts.pl does not find any contexts for some use of an msgid that seems to contain an accelerator (because it contains a tilde), it warns. If the tilde refers to e.g. "~/.elinks" and does not actually mark an accelerator, the warning can be silenced by specifying the special context "IGNORE", which the script otherwise ignores. - msgmerge copies the "accelerator_context" comments from po/elinks.pot to po/*.po. Translators do not edit those comments. - During make check-po: - Another Perl script (po/check-accelerator-contexts.pl) reads po/*.po and keeps track of which accelerators have been bound in each context. It warns about any conflicts it finds. This script does not access the C source files; thus it does not matter if the line numbers in "#:" lines are out of date. This implementation is not perfect and I am not proposing to add it to the main source tree at this time. Specifically: - It introduces compile-time dependencies on Perl and Locale::PO. There should be a configure-time or compile-time check so that the new features are skipped if the prerequisites are missing. - When the scripts include msgstr strings in warnings, they should transcode them from the charset of the PO file to the one specified by the user's locale. - It is not adequately documented (well, except perhaps here). - po/check-accelerator-contexts.pl reports the same conflict multiple times if it occurs in multiple contexts. - The warning messages should include line numbers, so that users of Emacs could conveniently edit the conflicting part of the PO file. This is not feasible with the current version of Locale::PO. - Locale::PO does not understand #~ lines and spews warnings about them. There is an ugly hack to hide these warnings. - Jonas Fonseca suggested the script could propose accelerators that are still available. This has not been implemented. There are three files attached: - po/gather-accelerator-contexts.pl: Augments elinks.pot with context information. - po/check-accelerator-contexts.pl: Checks conflicts. - accelerator-contexts.diff: Makes po/Makefile run the scripts, and adds special comments to source files.
2005-12-04 18:38:29 -05:00
/* [gettext_accelerator_context(memorize_form)] */
struct formhist_data *form;
struct submitted_value *sv;
int save = 0;
/* XXX: For now, we only save these types of form fields. */
foreach (sv, *submit) {
if (sv->type == FC_PASSWORD && sv->value && *sv->value) {
save = 1;
break;
}
}
if (!save) return;
/* Create a temporary form. */
form = new_formhist_item(forminfo->action);
if (!form) return;
foreach (sv, *submit) {
if ((sv->type == FC_TEXT) || (sv->type == FC_PASSWORD)) {
struct submitted_value *sv2;
sv2 = init_submitted_value(sv->name, sv->value,
sv->type, NULL, 0);
if (!sv2) goto fail;
add_to_list(*form->submit, sv2);
}
}
if (form_exists(form)) goto fail;
msg_box(ses->tab->term, NULL, 0,
N_("Form history"), ALIGN_CENTER,
N_("Should this login be remembered?\n\n"
"Please note that the password will be stored "
"obscured (but unencrypted) in a file on your disk.\n\n"
"If you are using a valuable password, answer NO."),
form, 3,
MSG_BOX_BUTTON(N_("~Yes"), remember_form, B_ENTER),
MSG_BOX_BUTTON(N_("~No"), dont_remember_form, B_ESC),
MSG_BOX_BUTTON(N_("Ne~ver for this site"), never_for_this_site, 0));
return;
fail:
done_formhist_item(form);
}
static void
done_form_history(struct module *module)
{
struct formhist_data *form, *next;
foreachsafe (form, next, saved_forms) {
delete_formhist_item(form);
}
}
struct module forms_history_module = struct_module(
/* name: */ N_("Form History"),
/* options: */ forms_history_options,
/* events: */ NULL,
/* submodules: */ NULL,
/* data: */ NULL,
/* init: */ NULL,
/* done: */ done_form_history
);