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>
16 #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(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;
86 ssi->file_name = g_strdup(file_name);
87 if (!ssi->file_name) {
88 secsave_errno = SS_ERR_OUT_OF_MEM;
92 /* Check properties of final file. */
93 #ifndef NO_UNIX_SOFTLINKS
94 if (g_lstat(ssi->file_name, &st)) {
96 if (g_stat(ssi->file_name, &st)) {
98 /* We ignore error caused by file inexistence. */
99 if (errno != ENOENT) {
102 secsave_errno = SS_ERR_STAT;
106 if (!S_ISREG(st.st_mode)) {
107 /* Not a regular file, secure_save is disabled. */
108 ssi->secure_save = 0;
111 /* XXX: access() do not work with setuid programs. */
112 if (g_access(ssi->file_name, R_OK | W_OK) < 0) {
114 secsave_errno = SS_ERR_ACCESS;
120 /* We still have a race condition here between
121 * [l]stat() and fopen() */
123 f1 = g_fopen(ssi->file_name, "rb+");
128 secsave_errno = SS_ERR_OPEN_READ;
135 if (ssi->secure_save) {
136 /* We use a random name for temporary file, mkstemp() opens
137 * the file and return a file descriptor named fd, which is
138 * then converted to FILE * using fdopen().
141 gchar *randname = g_strconcat(ssi->file_name, ".tmp_XXXXXX", NULL);
144 secsave_errno = SS_ERR_OUT_OF_MEM;
148 /* No need to use safe_mkstemp() here. --Zas */
149 fd = g_mkstemp(randname);
151 secsave_errno = SS_ERR_MKSTEMP;
156 ssi->fp = fdopen(fd, "wb");
158 secsave_errno = SS_ERR_OPEN_WRITE;
164 ssi->tmp_file_name = randname;
166 /* No need to create a temporary file here. */
167 ssi->fp = g_fopen(ssi->file_name, "wb");
169 secsave_errno = SS_ERR_OPEN_WRITE;
178 g_free(ssi->file_name);
179 ssi->file_name = NULL;
190 secure_open(gchar *file_name)
194 #ifdef CONFIG_OS_WIN32
195 /* There is neither S_IRWXG nor S_IRWXO under crossmingw32-gcc */
196 const mode_t mask = 0177;
198 const mode_t mask = S_IXUSR | S_IRWXG | S_IRWXO;
201 saved_mask = umask(mask);
202 ssi = secure_open_umask(file_name);
208 /** Close a file opened with secure_open(). Rreturns 0 on success,
209 * errno or -1 on failure.
212 secure_close(SecureSaveInfo *ssi)
216 if (!ssi) return ret;
217 if (!ssi->fp) goto free;
219 if (ssi->err) { /* Keep previous errno. */
221 fclose(ssi->fp); /* Close file */
225 /* Ensure data is effectively written to disk, we first flush libc buffers
226 * using fflush(), then fsync() to flush kernel buffers, and finally call
227 * fclose() (which call fflush() again, but the first one is needed since
228 * it doesn't make much sense to flush kernel buffers and then libc buffers,
229 * while closing file releases file descriptor we need to call fsync(). */
230 #if defined(HAVE_FFLUSH) || defined(HAVE_FSYNC)
231 if (ssi->secure_save) {
235 fail = (fflush(ssi->fp) == EOF);
239 if (!fail) fail = fsync(fileno(ssi->fp));
244 secsave_errno = SS_ERR_OTHER;
246 fclose(ssi->fp); /* Close file, ignore errors. */
253 if (fclose(ssi->fp) == EOF) {
255 secsave_errno = SS_ERR_OTHER;
259 if (ssi->secure_save && ssi->file_name && ssi->tmp_file_name) {
260 /* FIXME: Race condition on ssi->file_name. The file
261 * named ssi->file_name may have changed since
262 * secure_open() call (where we stat() file and
264 if (debug > 2) g_printf("rename %s -> %s", ssi->tmp_file_name, ssi->file_name);
265 if (g_rename(ssi->tmp_file_name, ssi->file_name) == -1) {
267 secsave_errno = SS_ERR_RENAME;
272 ret = 0; /* Success. */
275 if (ssi->tmp_file_name) g_free(ssi->tmp_file_name);
276 if (ssi->file_name) g_free(ssi->file_name);
277 if (ssi) g_free(ssi);
283 /** fputs() wrapper, set ssi->err to errno on error. If ssi->err is set when
284 * called, it immediatly returns EOF.
287 secure_fputs(SecureSaveInfo *ssi, const gchar *s)
291 if (!ssi || !ssi->fp || ssi->err) return EOF;
293 ret = fputs(s, ssi->fp);
295 secsave_errno = SS_ERR_OTHER;
303 /** fputc() wrapper, set ssi->err to errno on error. If ssi->err is set when
304 * called, it immediatly returns EOF.
307 secure_fputc(SecureSaveInfo *ssi, gint c)
311 if (!ssi || !ssi->fp || ssi->err) return EOF;
313 ret = fputc(c, ssi->fp);
316 secsave_errno = SS_ERR_OTHER;
322 /** fprintf() wrapper, set ssi->err to errno on error and return a negative
323 * value. If ssi->err is set when called, it immediatly returns -1.
326 secure_fprintf(SecureSaveInfo *ssi, const gchar *format, ...)
331 if (!ssi || !ssi->fp || ssi->err) return -1;
333 va_start(ap, format);
334 ret = vfprintf(ssi->fp, format, ap);
335 if (ret < 0) ssi->err = errno;
342 secsave_strerror(SecureSaveErrno secsave_error)
344 switch (secsave_error) {
345 case SS_ERR_OPEN_READ:
346 return _("Cannot read the file");
348 return _("Cannot get file status");
350 return _("Cannot access the file");
352 return _("Cannot create temp file");
354 return _("Cannot rename the file");
355 case SS_ERR_DISABLED:
356 return _("File saving disabled by option");
357 case SS_ERR_OUT_OF_MEM:
358 return _("Out of memory");
359 case SS_ERR_OPEN_WRITE:
360 return _("Cannot write the file");
361 case SS_ERR_NONE: /* Impossible. */
364 return _("Secure file saving error");