3 * Copyright (C) 2008 The Geeqie Team
5 * based on the code developped for ELinks by Laurent Monin
7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
12 #include <glib/gstdio.h>
17 #include "secure_save.h"
20 /* ABOUT SECURE SAVE */
21 /* This code was borrowed from the ELinks project (http://elinks.cz)
22 * It was originally written by me (Laurent Monin aka Zas) and heavily
23 * modified and improved by all ELinks contributors.
24 * This code was released under the GPLv2 licence.
25 * It was modified to be included in geeqie on 2008/04/05 */
27 /* If ssi->secure_save is TRUE:
28 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30 * A call to secure_open("/home/me/.confdir/filename", mask) will open a file
31 * named "filename.tmp_XXXXXX" in /home/me/.confdir/ and return a pointer to a
32 * structure SecureSaveInfo on success or NULL on error.
34 * filename.tmp_XXXXXX can't conflict with any file since it's created using
35 * mkstemp(). XXXXXX is a random string.
37 * Subsequent write operations are done using returned SecureSaveInfo FILE *
40 * If an error is encountered, SecureSaveInfo int field named err is set
41 * (automatically if using secure_fp*() functions or by programmer)
43 * When secure_close() is called, "filename.tmp_XXXXXX" is flushed and closed,
44 * and if SecureSaveInfo err field has a value of zero, "filename.tmp_XXXXXX"
45 * is renamed to "filename". If this succeeded, then secure_close() returns 0.
47 * WARNING: since rename() is used, any symlink called "filename" may be
48 * replaced by a regular file. If destination file isn't a regular file,
49 * then secsave is disabled for that file.
51 * If ssi->secure_save is FALSE:
52 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54 * No temporary file is created, "filename" is truncated, all operations are
55 * done on it, no rename nor flush occur, symlinks are preserved.
60 * Access rights are affected by secure_open() mask parameter.
63 /* FIXME: locking system on files about to be rewritten ? */
64 /* FIXME: Low risk race conditions about ssi->file_name. */
66 SecureSaveErrno secsave_errno = SS_ERR_NONE;
69 /** Open a file for writing in a secure way. @returns a pointer to a
70 * structure secure_save_info on success, or NULL on failure. */
71 static SecureSaveInfo *
72 secure_open_umask(const gchar *file_name)
77 secsave_errno = SS_ERR_NONE;
79 ssi = g_new0(SecureSaveInfo, 1);
81 secsave_errno = SS_ERR_OUT_OF_MEM;
85 ssi->secure_save = TRUE;
86 ssi->preserve_perms = TRUE;
87 ssi->unlink_on_error = TRUE;
89 ssi->file_name = g_strdup(file_name);
90 if (!ssi->file_name) {
91 secsave_errno = SS_ERR_OUT_OF_MEM;
95 /* Check properties of final file. */
96 #ifndef NO_UNIX_SOFTLINKS
97 if (lstat(ssi->file_name, &st)) {
99 if (stat(ssi->file_name, &st)) {
101 /* We ignore error caused by file inexistence. */
102 if (errno != ENOENT) {
105 secsave_errno = SS_ERR_STAT;
109 if (!S_ISREG(st.st_mode)) {
110 /* Not a regular file, secure_save is disabled. */
111 ssi->secure_save = FALSE;
114 /* XXX: access() do not work with setuid programs. */
115 if (access(ssi->file_name, R_OK | W_OK) < 0) {
117 secsave_errno = SS_ERR_ACCESS;
123 /* We still have a race condition here between
124 * [l]stat() and fopen() */
126 f1 = fopen(ssi->file_name, "rb+");
131 secsave_errno = SS_ERR_OPEN_READ;
138 if (ssi->secure_save) {
139 /* We use a random name for temporary file, mkstemp() opens
140 * the file and return a file descriptor named fd, which is
141 * then converted to FILE * using fdopen().
144 gchar *randname = g_strconcat(ssi->file_name, ".tmp_XXXXXX", NULL);
147 secsave_errno = SS_ERR_OUT_OF_MEM;
151 /* No need to use safe_mkstemp() here. --Zas */
152 fd = g_mkstemp(randname);
154 secsave_errno = SS_ERR_MKSTEMP;
159 ssi->fp = fdopen(fd, "wb");
161 secsave_errno = SS_ERR_OPEN_WRITE;
167 ssi->tmp_file_name = randname;
169 /* No need to create a temporary file here. */
170 ssi->fp = fopen(ssi->file_name, "wb");
172 secsave_errno = SS_ERR_OPEN_WRITE;
181 g_free(ssi->file_name);
182 ssi->file_name = NULL;
193 secure_open(const gchar *file_name)
197 #ifdef CONFIG_OS_WIN32
198 /* There is neither S_IRWXG nor S_IRWXO under crossmingw32-gcc */
199 const mode_t mask = 0177;
201 const mode_t mask = S_IXUSR | S_IRWXG | S_IRWXO;
204 saved_mask = umask(mask);
205 ssi = secure_open_umask(file_name);
211 /** Close a file opened with secure_open(). Rreturns 0 on success,
212 * errno or -1 on failure.
215 secure_close(SecureSaveInfo *ssi)
219 if (!ssi) return ret;
220 if (!ssi->fp) goto free;
222 if (ssi->err) { /* Keep previous errno. */
224 fclose(ssi->fp); /* Close file */
228 /* Ensure data is effectively written to disk, we first flush libc buffers
229 * using fflush(), then fsync() to flush kernel buffers, and finally call
230 * fclose() (which call fflush() again, but the first one is needed since
231 * it doesn't make much sense to flush kernel buffers and then libc buffers,
232 * while closing file releases file descriptor we need to call fsync(). */
233 #if defined(HAVE_FFLUSH) || defined(HAVE_FSYNC)
234 if (ssi->secure_save) {
238 fail = (fflush(ssi->fp) == EOF);
242 if (!fail) fail = fsync(fileno(ssi->fp));
247 secsave_errno = SS_ERR_OTHER;
249 fclose(ssi->fp); /* Close file, ignore errors. */
256 if (fclose(ssi->fp) == EOF) {
258 secsave_errno = SS_ERR_OTHER;
262 if (ssi->secure_save && ssi->file_name && ssi->tmp_file_name) {
265 /* FIXME: Race condition on ssi->file_name. The file
266 * named ssi->file_name may have changed since
267 * secure_open() call (where we stat() file and
269 #ifndef NO_UNIX_SOFTLINKS
270 if (lstat(ssi->file_name, &st) == 0)
272 if (stat(ssi->file_name, &st) == 0)
275 /* set the dest file attributes to that of source (ignoring errors) */
276 if (ssi->preserve_perms)
278 chown(ssi->tmp_file_name, st.st_uid, st.st_gid);
279 chmod(ssi->tmp_file_name, st.st_mode);
282 if (ssi->preserve_mtime)
286 tb.actime = st.st_atime;
287 tb.modtime = st.st_mtime;
288 utime(ssi->tmp_file_name, &tb);
291 DEBUG_3("rename %s -> %s", ssi->tmp_file_name, ssi->file_name);
292 if (rename(ssi->tmp_file_name, ssi->file_name) == -1) {
294 secsave_errno = SS_ERR_RENAME;
299 ret = 0; /* Success. */
302 if (ssi->tmp_file_name)
304 if (ret && ssi->unlink_on_error) unlink(ssi->tmp_file_name);
305 g_free(ssi->tmp_file_name);
307 if (ssi->file_name) g_free(ssi->file_name);
308 if (ssi) g_free(ssi);
314 /** fputs() wrapper, set ssi->err to errno on error. If ssi->err is set when
315 * called, it immediatly returns EOF.
318 secure_fputs(SecureSaveInfo *ssi, const gchar *s)
322 if (!ssi || !ssi->fp || ssi->err) return EOF;
324 ret = fputs(s, ssi->fp);
326 secsave_errno = SS_ERR_OTHER;
334 /** fputc() wrapper, set ssi->err to errno on error. If ssi->err is set when
335 * called, it immediatly returns EOF.
338 secure_fputc(SecureSaveInfo *ssi, gint c)
342 if (!ssi || !ssi->fp || ssi->err) return EOF;
344 ret = fputc(c, ssi->fp);
347 secsave_errno = SS_ERR_OTHER;
353 /** fprintf() wrapper, set ssi->err to errno on error and return a negative
354 * value. If ssi->err is set when called, it immediatly returns -1.
357 secure_fprintf(SecureSaveInfo *ssi, const gchar *format, ...)
362 if (!ssi || !ssi->fp || ssi->err) return -1;
364 va_start(ap, format);
365 ret = vfprintf(ssi->fp, format, ap);
366 if (ret < 0) ssi->err = errno;
372 /** fwrite() wrapper, set ssi->err to errno on error and return a value less than
373 * the number of elements to write. If ssi->err is set when called, it immediatly returns 0.
376 secure_fwrite(const gpointer ptr, size_t size, size_t nmemb, SecureSaveInfo *ssi)
380 if (!ssi || !ssi->fp || ssi->err) return 0;
382 ret = fwrite(ptr, size, nmemb, ssi->fp);
386 secsave_errno = SS_ERR_OTHER;
393 secsave_strerror(SecureSaveErrno secsave_error)
395 switch (secsave_error) {
396 case SS_ERR_OPEN_READ:
397 return _("Cannot read the file");
399 return _("Cannot get file status");
401 return _("Cannot access the file");
403 return _("Cannot create temp file");
405 return _("Cannot rename the file");
406 case SS_ERR_DISABLED:
407 return _("File saving disabled by option");
408 case SS_ERR_OUT_OF_MEM:
409 return _("Out of memory");
410 case SS_ERR_OPEN_WRITE:
411 return _("Cannot write the file");
412 case SS_ERR_NONE: /* Impossible. */
415 return _("Secure file saving error");