Exemplo n.º 1
0
gboolean
grg_save_attachment (gchar * path, GtkWidget * parent)
{
	GtkWidget *wait;
	GList *tmp = ((struct grg_entry *) current->data)->attach;
	GRG_TMPFILE tmpf;
	guchar *mem;
	gint fd;
	glong memDim;

	while (tmp
	       && ((struct grg_attachment *) tmp->data)->ID !=
	       current_attach_ID)
		tmp = tmp->next;

	if (!tmp)
		return FALSE;

	tmpf = ((struct grg_attachment *) tmp->data)->pointer;

	wait = grg_wait_msg (_("saving"), parent);

	if (grg_tmpfile_read (gctx, tmpf, &mem, &memDim) < 0)
	{
		gtk_widget_destroy (wait);
		grg_msg (_("Cannot decode tempfile."), GTK_MESSAGE_ERROR,
			 parent);
		return FALSE;
	}

	fd = open (path, O_WRONLY | O_CREAT | O_EXCL,
		   S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);

	if (fd < 3)
	{
		gtk_widget_destroy (wait);
		grg_msg (_("Cannot create file, or file already existent."),
			 GTK_MESSAGE_ERROR, parent);
		GRGFREE (mem, memDim);
		close (fd);
		return FALSE;
	}

	write (fd, mem, memDim);
	GRGFREE (mem, memDim);
	close (fd);
	gtk_widget_destroy (wait);
	return TRUE;
}
Exemplo n.º 2
0
void
grg_attachment_free (gpointer att, gpointer user_data)
{
	GRGAFREE (((struct grg_attachment *) att)->filename);
	GRGAFREE (((struct grg_attachment *) att)->comment);
	grg_tmpfile_close (gctx, ((struct grg_attachment *) att)->pointer);
	GRGFREE (att, sizeof (struct grg_attachment));
}
Exemplo n.º 3
0
gint
grg_attach_content (void *cont, glong fdim, gchar * fname, gchar * comment)
{
	GList *ceal = ((struct grg_entry *) current->data)->attach;
	struct grg_attachment *newatt;
	gint ID;

	if (!ceal)
		ID = 0;
	else
		ID = ((struct grg_attachment *) (g_list_last (ceal))->data)->
			ID + 1;

	newatt = (struct grg_attachment *)
		grg_malloc (sizeof (struct grg_attachment));
	newatt->ID = ID;
	newatt->filedim = fdim;
	newatt->filename = g_strdup (fname);
	newatt->comment = g_strdup (comment);
	newatt->pointer = grg_tmpfile_gen (gctx);

	if (grg_tmpfile_write (gctx, newatt->pointer, cont, newatt->filedim) <
	    0)
	{
		GRGFREE (newatt, sizeof (struct grg_attachment));
		return -1;
	}

	((struct grg_entry *) current->data)->attach =
		g_list_append (ceal, newatt);

#ifdef ATTACH_LIMIT
	total_size += fdim;
#endif

	return ID;
}
Exemplo n.º 4
0
gint
grg_attach_file (gchar * path, GtkWidget * parent)
{
	GtkWidget *wait;
	GList *ceal = ((struct grg_entry *) current->data)->attach;	/*Current Entry Attachment List*/
	struct grg_attachment *newatt;
	gint fd, ID;
	glong fdim;
	gchar *comment;
	void *data;
	struct stat info;

	if (!ceal)
		ID = 0;
	else
		ID = ((struct grg_attachment *) (g_list_last (ceal))->data)->
			ID + 1;

	fd = grg_safe_open (path);

	if (fd < 3)
	{
		grg_msg (_("Cannot open file to attach."), GTK_MESSAGE_ERROR,
			 parent);
		return -1;
	}

	if (fd == GRG_OPEN_FILE_IRREGULAR)
	{
		grg_msg (_("Only regular files can be attached."),
			 GTK_MESSAGE_ERROR, parent);
		return -1;
	}

	fstat (fd, &info);
	fdim = info.st_size;

#ifdef ATTACH_LIMIT
	if (total_size + fdim > ATT_SIZE_MAX)
	{
		grg_msg (_
			 ("Sorry, currently you can attach files only up to 2 Mb"),
			 GTK_MESSAGE_ERROR, parent);
		close (fd);
		return -1;
	}
#endif

	comment =
		grg_input_dialog (_("Enter comment"),
				  _
				  ("Please enter a comment for this\nfile (max. 32 chars)"),
				  "", FALSE, parent);

	if (!comment)
	{
		close (fd);
		return -1;
	}

	if (STR_EQ (comment, ""))
		comment = g_strdup (_("none"));

	newatt = (struct grg_attachment *)
		grg_malloc (sizeof (struct grg_attachment));
	newatt->ID = ID;
	newatt->filedim = fdim;
	newatt->filename = g_path_get_basename (path);
	newatt->comment = comment;
	newatt->pointer = grg_tmpfile_gen (gctx);

	wait = grg_wait_msg (_("attaching"), parent);

	data = mmap (NULL, newatt->filedim, PROT_READ, MAP_PRIVATE, fd, 0);

	if (grg_tmpfile_write (gctx, newatt->pointer, data, newatt->filedim) <
	    0)
	{
		gtk_widget_destroy (wait);
		grg_msg (_("Cannot encode tempfile."), GTK_MESSAGE_ERROR,
			 parent);
		munmap (data, newatt->filedim);
		close (fd);
		GRGFREE (newatt, sizeof (struct grg_attachment));
		return -1;
	}

	munmap (data, newatt->filedim);
	close (fd);

	gtk_widget_destroy (wait);

	((struct grg_entry *) current->data)->attach =
		g_list_append (ceal, newatt);

#ifdef ATTACH_LIMIT
	total_size += fdim;
#endif

	return ID;
}
Exemplo n.º 5
0
static void
dump_content (gchar * fname, gint ennum, gchar * enpage)
{
	GRG_KEY key;
	glong len;
	gint err, fd;
	gchar *txt;

#ifndef HAVE_TCGETATTR
	fprintf (stderr, "%s: %s\n", _("Warning"),
		 _
		 ("it isn't possible to hide password typing; be extremely careful!"));
#endif

	fd = grg_safe_open (fname);

	if (fd < 3)
		report_err (_("The selected file doesn't exists"), 0, 1,
			    NULL);

	if (fd == GRG_OPEN_FILE_IRREGULAR)
		report_err (_("You've selected a directory or a symlink"), 0,
			    1, NULL);

	err = grg_validate_file_direct (gctx, fd);

	switch (err)
	{
	case GRG_OK:
		break;

	case GRG_MEM_ALLOCATION_ERR:
		report_err ("error: malloc failed. Probably this indicates a memory "
		   "problem, such as resource exhaustion. Attempting "
		   "to exit cleanly...",
			    0, 1, NULL);

	case GRG_ARGUMENT_ERR:
		report_err (_
			    ("Gringotts internal error. Cannot finish operation."),
			    0, 1, NULL);

	case GRG_READ_MAGIC_ERR:
	case GRG_READ_UNSUPPORTED_VERSION:
		report_err (_
			    ("This file doesn't seem to be a valid Gringotts one!"),
			    0, 1, NULL);

	case GRG_READ_FILE_ERR:
		report_err (_("Uh-oh! I can't read from the file!"), 0, 1,
			    NULL);

	case GRG_READ_CRC_ERR:
	case GRG_READ_COMP_ERR:
		report_err (_("The file appears to be corrupted!"),
			    0, 1, NULL);
#ifdef GRG_READ_TOO_BIG_ERR
	case GRG_READ_TOO_BIG_ERR:
		report_err (_("File is too big"), 0, 1, NULL);
#endif
	default:
		if (err < 0)
			report_err (_
				    ("Gringotts internal error. Cannot finish operation."),
				    0, 1, NULL);
	}

	while (TRUE)
	{
		key = grg_get_cmdline_key ();

		if (!key)
		{
			report_err (_("You must enter a valid password!"), 0,
				    0, NULL);
			continue;
		}

        {
            guchar *unsigned_txt;
		    err = grg_decrypt_file_direct (gctx, key, fd, &unsigned_txt, &len);
            txt = (gchar*)unsigned_txt;
        }

		grg_key_free (gctx, key);

		switch (err)
		{
		case GRG_OK:
			break;

		case GRG_MEM_ALLOCATION_ERR:
			report_err ("error: malloc failed. Probably this indicates a memory "
			   "problem, such as resource exhaustion. Attempting "
			   "to exit cleanly...",
					0, 1, NULL);

		case GRG_ARGUMENT_ERR:
			report_err (_("Gringotts internal error. Cannot finish operation."),
					0, 1, NULL);

		case GRG_READ_PWD_ERR:
			report_err (_("Wrong password! Re-enter it"), 0, 0,
				    NULL);
			continue;

		case GRG_READ_ENC_INIT_ERR:
			report_err (_
				    ("Problem with libmcrypt, probably a faulty installation"),
				    0, 1, NULL);

		/*just to be sure... */
		default:
			if (err < 0)
				report_err (_("Gringotts internal error. Cannot finish operation."),
					    0, 1, NULL);
		}

		if (!g_utf8_validate (txt, len, NULL))
		{
			GRGFREE (txt, len);
			report_err (_
				    ("Saved data contain invalid UTF-8 chars"),
				    0, 1, NULL);
		}

		break;
	}

	close (fd);

	grg_entries_load_from_string (txt, NULL, FALSE);
	GRGFREE (txt, len);

	grg_entries_print (ennum, enpage);
	grg_entries_free ();
}