4 * Author: Vladimir Nadvornik
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;
87 ssi->file_name = g_strdup(file_name);
88 if (!ssi->file_name) {
89 secsave_errno = SS_ERR_OUT_OF_MEM;
93 /* Check properties of final file. */
94 #ifndef NO_UNIX_SOFTLINKS
95 if (g_lstat(ssi->file_name, &st)) {
97 if (g_stat(ssi->file_name, &st)) {
99 /* We ignore error caused by file inexistence. */
100 if (errno != ENOENT) {
103 secsave_errno = SS_ERR_STAT;
107 if (!S_ISREG(st.st_mode)) {
108 /* Not a regular file, secure_save is disabled. */
109 ssi->secure_save = 0;
112 /* XXX: access() do not work with setuid programs. */
113 if (g_access(ssi->file_name, R_OK | W_OK) < 0) {
115 secsave_errno = SS_ERR_ACCESS;
121 /* We still have a race condition here between
122 * [l]stat() and fopen() */
124 f1 = g_fopen(ssi->file_name, "rb+");
129 secsave_errno = SS_ERR_OPEN_READ;
136 if (ssi->secure_save) {
137 /* We use a random name for temporary file, mkstemp() opens
138 * the file and return a file descriptor named fd, which is
139 * then converted to FILE * using fdopen().
142 gchar *randname = g_strconcat(ssi->file_name, ".tmp_XXXXXX", NULL);
145 secsave_errno = SS_ERR_OUT_OF_MEM;
149 /* No need to use safe_mkstemp() here. --Zas */
150 fd = g_mkstemp(randname);
152 secsave_errno = SS_ERR_MKSTEMP;
157 ssi->fp = fdopen(fd, "wb");
159 secsave_errno = SS_ERR_OPEN_WRITE;
165 ssi->tmp_file_name = randname;
167 /* No need to create a temporary file here. */
168 ssi->fp = g_fopen(ssi->file_name, "wb");
170 secsave_errno = SS_ERR_OPEN_WRITE;
179 g_free(ssi->file_name);
180 ssi->file_name = NULL;
191 secure_open(const gchar *file_name)
195 #ifdef CONFIG_OS_WIN32
196 /* There is neither S_IRWXG nor S_IRWXO under crossmingw32-gcc */
197 const mode_t mask = 0177;
199 const mode_t mask = S_IXUSR | S_IRWXG | S_IRWXO;
202 saved_mask = umask(mask);
203 ssi = secure_open_umask(file_name);
209 /** Close a file opened with secure_open(). Rreturns 0 on success,
210 * errno or -1 on failure.
213 secure_close(SecureSaveInfo *ssi)
217 if (!ssi) return ret;
218 if (!ssi->fp) goto free;
220 if (ssi->err) { /* Keep previous errno. */
222 fclose(ssi->fp); /* Close file */
226 /* Ensure data is effectively written to disk, we first flush libc buffers
227 * using fflush(), then fsync() to flush kernel buffers, and finally call
228 * fclose() (which call fflush() again, but the first one is needed since
229 * it doesn't make much sense to flush kernel buffers and then libc buffers,
230 * while closing file releases file descriptor we need to call fsync(). */
231 #if defined(HAVE_FFLUSH) || defined(HAVE_FSYNC)
232 if (ssi->secure_save) {
236 fail = (fflush(ssi->fp) == EOF);
240 if (!fail) fail = fsync(fileno(ssi->fp));
245 secsave_errno = SS_ERR_OTHER;
247 fclose(ssi->fp); /* Close file, ignore errors. */
254 if (fclose(ssi->fp) == EOF) {
256 secsave_errno = SS_ERR_OTHER;
260 if (ssi->secure_save && ssi->file_name && ssi->tmp_file_name) {
263 /* FIXME: Race condition on ssi->file_name. The file
264 * named ssi->file_name may have changed since
265 * secure_open() call (where we stat() file and
267 #ifndef NO_UNIX_SOFTLINKS
268 if (g_lstat(ssi->file_name, &st) == 0)
270 if (g_stat(ssi->file_name, &st) == 0)
273 /* set the dest file attributes to that of source (ignoring errors) */
274 if (ssi->preserve_perms)
276 chown(ssi->tmp_file_name, st.st_uid, st.st_gid);
277 chmod(ssi->tmp_file_name, st.st_mode);
280 if (ssi->preserve_mtime)
284 tb.actime = st.st_atime;
285 tb.modtime = st.st_mtime;
286 utime(ssi->tmp_file_name, &tb);
289 if (debug > 2) g_printf("rename %s -> %s", ssi->tmp_file_name, ssi->file_name);
290 if (g_rename(ssi->tmp_file_name, ssi->file_name) == -1) {
292 secsave_errno = SS_ERR_RENAME;
297 ret = 0; /* Success. */
300 if (ssi->tmp_file_name) g_free(ssi->tmp_file_name);
301 if (ssi->file_name) g_free(ssi->file_name);
302 if (ssi) g_free(ssi);
308 /** fputs() wrapper, set ssi->err to errno on error. If ssi->err is set when
309 * called, it immediatly returns EOF.
312 secure_fputs(SecureSaveInfo *ssi, const gchar *s)
316 if (!ssi || !ssi->fp || ssi->err) return EOF;
318 ret = fputs(s, ssi->fp);
320 secsave_errno = SS_ERR_OTHER;
328 /** fputc() wrapper, set ssi->err to errno on error. If ssi->err is set when
329 * called, it immediatly returns EOF.
332 secure_fputc(SecureSaveInfo *ssi, gint c)
336 if (!ssi || !ssi->fp || ssi->err) return EOF;
338 ret = fputc(c, ssi->fp);
341 secsave_errno = SS_ERR_OTHER;
347 /** fprintf() wrapper, set ssi->err to errno on error and return a negative
348 * value. If ssi->err is set when called, it immediatly returns -1.
351 secure_fprintf(SecureSaveInfo *ssi, const gchar *format, ...)
356 if (!ssi || !ssi->fp || ssi->err) return -1;
358 va_start(ap, format);
359 ret = vfprintf(ssi->fp, format, ap);
360 if (ret < 0) ssi->err = errno;
367 secsave_strerror(SecureSaveErrno secsave_error)
369 switch (secsave_error) {
370 case SS_ERR_OPEN_READ:
371 return _("Cannot read the file");
373 return _("Cannot get file status");
375 return _("Cannot access the file");
377 return _("Cannot create temp file");
379 return _("Cannot rename the file");
380 case SS_ERR_DISABLED:
381 return _("File saving disabled by option");
382 case SS_ERR_OUT_OF_MEM:
383 return _("Out of memory");
384 case SS_ERR_OPEN_WRITE:
385 return _("Cannot write the file");
386 case SS_ERR_NONE: /* Impossible. */
389 return _("Secure file saving error");