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"
19 /* ABOUT SECURE SAVE */
20 /* This code was borrowed from the ELinks project (http://elinks.cz)
21 * It was originally written by me (Laurent Monin aka Zas) and heavily
22 * modified and improved by all ELinks contributors.
23 * This code was released under the GPLv2 licence.
24 * It was modified to be included in geeqie on 2008/04/05 */
26 /* If ssi->secure_save is TRUE:
27 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 * A call to secure_open("/home/me/.confdir/filename", mask) will open a file
30 * named "filename.tmp_XXXXXX" in /home/me/.confdir/ and return a pointer to a
31 * structure SecureSaveInfo on success or NULL on error.
33 * filename.tmp_XXXXXX can't conflict with any file since it's created using
34 * mkstemp(). XXXXXX is a random string.
36 * Subsequent write operations are done using returned SecureSaveInfo FILE *
39 * If an error is encountered, SecureSaveInfo int field named err is set
40 * (automatically if using secure_fp*() functions or by programmer)
42 * When secure_close() is called, "filename.tmp_XXXXXX" is flushed and closed,
43 * and if SecureSaveInfo err field has a value of zero, "filename.tmp_XXXXXX"
44 * is renamed to "filename". If this succeeded, then secure_close() returns 0.
46 * WARNING: since rename() is used, any symlink called "filename" may be
47 * replaced by a regular file. If destination file isn't a regular file,
48 * then secsave is disabled for that file.
50 * If ssi->secure_save is FALSE:
51 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53 * No temporary file is created, "filename" is truncated, all operations are
54 * done on it, no rename nor flush occur, symlinks are preserved.
59 * Access rights are affected by secure_open() mask parameter.
62 /* FIXME: locking system on files about to be rewritten ? */
63 /* FIXME: Low risk race conditions about ssi->file_name. */
65 SecureSaveErrno secsave_errno = SS_ERR_NONE;
68 /** Open a file for writing in a secure way. @returns a pointer to a
69 * structure secure_save_info on success, or NULL on failure. */
70 static SecureSaveInfo *
71 secure_open_umask(const gchar *file_name)
76 secsave_errno = SS_ERR_NONE;
78 ssi = g_new0(SecureSaveInfo, 1);
80 secsave_errno = SS_ERR_OUT_OF_MEM;
84 ssi->secure_save = TRUE;
85 ssi->preserve_perms = TRUE;
86 ssi->unlink_on_error = TRUE;
88 ssi->file_name = g_strdup(file_name);
89 if (!ssi->file_name) {
90 secsave_errno = SS_ERR_OUT_OF_MEM;
94 /* Check properties of final file. */
95 #ifndef NO_UNIX_SOFTLINKS
96 if (g_lstat(ssi->file_name, &st)) {
98 if (g_stat(ssi->file_name, &st)) {
100 /* We ignore error caused by file inexistence. */
101 if (errno != ENOENT) {
104 secsave_errno = SS_ERR_STAT;
108 if (!S_ISREG(st.st_mode)) {
109 /* Not a regular file, secure_save is disabled. */
110 ssi->secure_save = FALSE;
113 /* XXX: access() do not work with setuid programs. */
114 if (g_access(ssi->file_name, R_OK | W_OK) < 0) {
116 secsave_errno = SS_ERR_ACCESS;
122 /* We still have a race condition here between
123 * [l]stat() and fopen() */
125 f1 = g_fopen(ssi->file_name, "rb+");
130 secsave_errno = SS_ERR_OPEN_READ;
137 if (ssi->secure_save) {
138 /* We use a random name for temporary file, mkstemp() opens
139 * the file and return a file descriptor named fd, which is
140 * then converted to FILE * using fdopen().
143 gchar *randname = g_strconcat(ssi->file_name, ".tmp_XXXXXX", NULL);
146 secsave_errno = SS_ERR_OUT_OF_MEM;
150 /* No need to use safe_mkstemp() here. --Zas */
151 fd = g_mkstemp(randname);
153 secsave_errno = SS_ERR_MKSTEMP;
158 ssi->fp = fdopen(fd, "wb");
160 secsave_errno = SS_ERR_OPEN_WRITE;
166 ssi->tmp_file_name = randname;
168 /* No need to create a temporary file here. */
169 ssi->fp = g_fopen(ssi->file_name, "wb");
171 secsave_errno = SS_ERR_OPEN_WRITE;
180 g_free(ssi->file_name);
181 ssi->file_name = NULL;
192 secure_open(const gchar *file_name)
196 #ifdef CONFIG_OS_WIN32
197 /* There is neither S_IRWXG nor S_IRWXO under crossmingw32-gcc */
198 const mode_t mask = 0177;
200 const mode_t mask = S_IXUSR | S_IRWXG | S_IRWXO;
203 saved_mask = umask(mask);
204 ssi = secure_open_umask(file_name);
210 /** Close a file opened with secure_open(). Rreturns 0 on success,
211 * errno or -1 on failure.
214 secure_close(SecureSaveInfo *ssi)
218 if (!ssi) return ret;
219 if (!ssi->fp) goto free;
221 if (ssi->err) { /* Keep previous errno. */
223 fclose(ssi->fp); /* Close file */
227 /* Ensure data is effectively written to disk, we first flush libc buffers
228 * using fflush(), then fsync() to flush kernel buffers, and finally call
229 * fclose() (which call fflush() again, but the first one is needed since
230 * it doesn't make much sense to flush kernel buffers and then libc buffers,
231 * while closing file releases file descriptor we need to call fsync(). */
232 #if defined(HAVE_FFLUSH) || defined(HAVE_FSYNC)
233 if (ssi->secure_save) {
237 fail = (fflush(ssi->fp) == EOF);
241 if (!fail) fail = fsync(fileno(ssi->fp));
246 secsave_errno = SS_ERR_OTHER;
248 fclose(ssi->fp); /* Close file, ignore errors. */
255 if (fclose(ssi->fp) == EOF) {
257 secsave_errno = SS_ERR_OTHER;
261 if (ssi->secure_save && ssi->file_name && ssi->tmp_file_name) {
264 /* FIXME: Race condition on ssi->file_name. The file
265 * named ssi->file_name may have changed since
266 * secure_open() call (where we stat() file and
268 #ifndef NO_UNIX_SOFTLINKS
269 if (g_lstat(ssi->file_name, &st) == 0)
271 if (g_stat(ssi->file_name, &st) == 0)
274 /* set the dest file attributes to that of source (ignoring errors) */
275 if (ssi->preserve_perms)
277 chown(ssi->tmp_file_name, st.st_uid, st.st_gid);
278 chmod(ssi->tmp_file_name, st.st_mode);
281 if (ssi->preserve_mtime)
285 tb.actime = st.st_atime;
286 tb.modtime = st.st_mtime;
287 utime(ssi->tmp_file_name, &tb);
290 DEBUG_3("rename %s -> %s", ssi->tmp_file_name, ssi->file_name);
291 if (g_rename(ssi->tmp_file_name, ssi->file_name) == -1) {
293 secsave_errno = SS_ERR_RENAME;
298 ret = 0; /* Success. */
301 if (ssi->tmp_file_name)
303 if (ret && ssi->unlink_on_error) unlink(ssi->tmp_file_name);
304 g_free(ssi->tmp_file_name);
306 if (ssi->file_name) g_free(ssi->file_name);
307 if (ssi) g_free(ssi);
313 /** fputs() wrapper, set ssi->err to errno on error. If ssi->err is set when
314 * called, it immediatly returns EOF.
317 secure_fputs(SecureSaveInfo *ssi, const gchar *s)
321 if (!ssi || !ssi->fp || ssi->err) return EOF;
323 ret = fputs(s, ssi->fp);
325 secsave_errno = SS_ERR_OTHER;
333 /** fputc() wrapper, set ssi->err to errno on error. If ssi->err is set when
334 * called, it immediatly returns EOF.
337 secure_fputc(SecureSaveInfo *ssi, gint c)
341 if (!ssi || !ssi->fp || ssi->err) return EOF;
343 ret = fputc(c, ssi->fp);
346 secsave_errno = SS_ERR_OTHER;
352 /** fprintf() wrapper, set ssi->err to errno on error and return a negative
353 * value. If ssi->err is set when called, it immediatly returns -1.
356 secure_fprintf(SecureSaveInfo *ssi, const gchar *format, ...)
361 if (!ssi || !ssi->fp || ssi->err) return -1;
363 va_start(ap, format);
364 ret = vfprintf(ssi->fp, format, ap);
365 if (ret < 0) ssi->err = errno;
372 secsave_strerror(SecureSaveErrno secsave_error)
374 switch (secsave_error) {
375 case SS_ERR_OPEN_READ:
376 return _("Cannot read the file");
378 return _("Cannot get file status");
380 return _("Cannot access the file");
382 return _("Cannot create temp file");
384 return _("Cannot rename the file");
385 case SS_ERR_DISABLED:
386 return _("File saving disabled by option");
387 case SS_ERR_OUT_OF_MEM:
388 return _("Out of memory");
389 case SS_ERR_OPEN_WRITE:
390 return _("Cannot write the file");
391 case SS_ERR_NONE: /* Impossible. */
394 return _("Secure file saving error");