Mercurial > geeqie
annotate src/secure_save.c @ 342:07490120df2d
Rename dupe_custom_threshold option to duplicates_similarity_threshold.
author | zas_ |
---|---|
date | Sat, 12 Apr 2008 22:10:38 +0000 |
parents | a955b7fd626b |
children | 4b2d7f9af171 |
rev | line source |
---|---|
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
1 /* |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
2 * Geeqie |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
3 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
4 * Author: Vladimir Nadvornik |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
5 * based on the code developped for ELinks by Laurent Monin |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
6 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
7 * This software is released under the GNU General Public License (GNU GPL). |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
8 * Please read the included file COPYING for more information. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
9 * This software comes with no warranty of any kind, use at your own risk! |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
10 */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
11 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
12 #include <glib/gstdio.h> |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
13 #include <errno.h> |
311
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
14 #include <utime.h> |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
15 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
16 #include "main.h" |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
17 #include "secure_save.h" |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
18 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
19 /* ABOUT SECURE SAVE */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
20 /* This code was borrowed from the ELinks project (http://elinks.cz) |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
21 * It was originally written by me (Laurent Monin aka Zas) and heavily |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
22 * modified and improved by all ELinks contributors. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
23 * This code was released under the GPLv2 licence. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
24 * It was modified to be included in geeqie on 2008/04/05 */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
25 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
26 /* If ssi->secure_save is TRUE: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
27 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
28 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
29 * A call to secure_open("/home/me/.confdir/filename", mask) will open a file |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
30 * named "filename.tmp_XXXXXX" in /home/me/.confdir/ and return a pointer to a |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
31 * structure SecureSaveInfo on success or NULL on error. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
32 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
33 * filename.tmp_XXXXXX can't conflict with any file since it's created using |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
34 * mkstemp(). XXXXXX is a random string. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
35 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
36 * Subsequent write operations are done using returned SecureSaveInfo FILE * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
37 * field named fp. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
38 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
39 * If an error is encountered, SecureSaveInfo int field named err is set |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
40 * (automatically if using secure_fp*() functions or by programmer) |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
41 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
42 * When secure_close() is called, "filename.tmp_XXXXXX" is flushed and closed, |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
43 * and if SecureSaveInfo err field has a value of zero, "filename.tmp_XXXXXX" |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
44 * is renamed to "filename". If this succeeded, then secure_close() returns 0. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
45 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
46 * WARNING: since rename() is used, any symlink called "filename" may be |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
47 * replaced by a regular file. If destination file isn't a regular file, |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
48 * then secsave is disabled for that file. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
49 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
50 * If ssi->secure_save is FALSE: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
51 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
52 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
53 * No temporary file is created, "filename" is truncated, all operations are |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
54 * done on it, no rename nor flush occur, symlinks are preserved. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
55 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
56 * In both cases: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
57 * ~~~~~~~~~~~~~ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
58 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
59 * Access rights are affected by secure_open() mask parameter. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
60 */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
61 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
62 /* FIXME: locking system on files about to be rewritten ? */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
63 /* FIXME: Low risk race conditions about ssi->file_name. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
64 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
65 SecureSaveErrno secsave_errno = SS_ERR_NONE; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
66 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
67 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
68 /** Open a file for writing in a secure way. @returns a pointer to a |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
69 * structure secure_save_info on success, or NULL on failure. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
70 static SecureSaveInfo * |
309 | 71 secure_open_umask(const gchar *file_name) |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
72 { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
73 struct stat st; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
74 SecureSaveInfo *ssi; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
75 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
76 secsave_errno = SS_ERR_NONE; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
77 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
78 ssi = g_new0(SecureSaveInfo, 1); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
79 if (!ssi) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
80 secsave_errno = SS_ERR_OUT_OF_MEM; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
81 goto end; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
82 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
83 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
84 ssi->secure_save = TRUE; |
311
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
85 ssi->preserve_perms = TRUE; |
313
a955b7fd626b
Secure save now unlinks temporary file on error by default.
zas_
parents:
311
diff
changeset
|
86 ssi->unlink_on_error = TRUE; |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
87 |
310
4b25b3b30f35
Revert part of the previous patch, let the caller take care
zas_
parents:
309
diff
changeset
|
88 ssi->file_name = g_strdup(file_name); |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
89 if (!ssi->file_name) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
90 secsave_errno = SS_ERR_OUT_OF_MEM; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
91 goto free_f; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
92 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
93 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
94 /* Check properties of final file. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
95 #ifndef NO_UNIX_SOFTLINKS |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
96 if (g_lstat(ssi->file_name, &st)) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
97 #else |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
98 if (g_stat(ssi->file_name, &st)) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
99 #endif |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
100 /* We ignore error caused by file inexistence. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
101 if (errno != ENOENT) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
102 /* lstat() error. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
103 ssi->err = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
104 secsave_errno = SS_ERR_STAT; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
105 goto free_file_name; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
106 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
107 } else { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
108 if (!S_ISREG(st.st_mode)) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
109 /* Not a regular file, secure_save is disabled. */ |
313
a955b7fd626b
Secure save now unlinks temporary file on error by default.
zas_
parents:
311
diff
changeset
|
110 ssi->secure_save = FALSE; |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
111 } else { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
112 #ifdef HAVE_ACCESS |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
113 /* XXX: access() do not work with setuid programs. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
114 if (g_access(ssi->file_name, R_OK | W_OK) < 0) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
115 ssi->err = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
116 secsave_errno = SS_ERR_ACCESS; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
117 goto free_file_name; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
118 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
119 #else |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
120 FILE *f1; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
121 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
122 /* We still have a race condition here between |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
123 * [l]stat() and fopen() */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
124 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
125 f1 = g_fopen(ssi->file_name, "rb+"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
126 if (f1) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
127 fclose(f1); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
128 } else { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
129 ssi->err = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
130 secsave_errno = SS_ERR_OPEN_READ; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
131 goto free_file_name; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
132 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
133 #endif |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
134 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
135 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
136 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
137 if (ssi->secure_save) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
138 /* We use a random name for temporary file, mkstemp() opens |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
139 * the file and return a file descriptor named fd, which is |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
140 * then converted to FILE * using fdopen(). |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
141 */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
142 gint fd; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
143 gchar *randname = g_strconcat(ssi->file_name, ".tmp_XXXXXX", NULL); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
144 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
145 if (!randname) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
146 secsave_errno = SS_ERR_OUT_OF_MEM; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
147 goto free_file_name; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
148 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
149 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
150 /* No need to use safe_mkstemp() here. --Zas */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
151 fd = g_mkstemp(randname); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
152 if (fd == -1) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
153 secsave_errno = SS_ERR_MKSTEMP; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
154 g_free(randname); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
155 goto free_file_name; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
156 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
157 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
158 ssi->fp = fdopen(fd, "wb"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
159 if (!ssi->fp) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
160 secsave_errno = SS_ERR_OPEN_WRITE; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
161 ssi->err = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
162 g_free(randname); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
163 goto free_file_name; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
164 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
165 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
166 ssi->tmp_file_name = randname; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
167 } else { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
168 /* No need to create a temporary file here. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
169 ssi->fp = g_fopen(ssi->file_name, "wb"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
170 if (!ssi->fp) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
171 secsave_errno = SS_ERR_OPEN_WRITE; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
172 ssi->err = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
173 goto free_file_name; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
174 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
175 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
176 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
177 return ssi; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
178 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
179 free_file_name: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
180 g_free(ssi->file_name); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
181 ssi->file_name = NULL; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
182 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
183 free_f: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
184 g_free(ssi); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
185 ssi = NULL; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
186 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
187 end: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
188 return NULL; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
189 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
190 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
191 SecureSaveInfo * |
309 | 192 secure_open(const gchar *file_name) |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
193 { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
194 SecureSaveInfo *ssi; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
195 mode_t saved_mask; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
196 #ifdef CONFIG_OS_WIN32 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
197 /* There is neither S_IRWXG nor S_IRWXO under crossmingw32-gcc */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
198 const mode_t mask = 0177; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
199 #else |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
200 const mode_t mask = S_IXUSR | S_IRWXG | S_IRWXO; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
201 #endif |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
202 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
203 saved_mask = umask(mask); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
204 ssi = secure_open_umask(file_name); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
205 umask(saved_mask); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
206 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
207 return ssi; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
208 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
209 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
210 /** Close a file opened with secure_open(). Rreturns 0 on success, |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
211 * errno or -1 on failure. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
212 */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
213 gint |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
214 secure_close(SecureSaveInfo *ssi) |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
215 { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
216 gint ret = -1; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
217 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
218 if (!ssi) return ret; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
219 if (!ssi->fp) goto free; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
220 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
221 if (ssi->err) { /* Keep previous errno. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
222 ret = ssi->err; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
223 fclose(ssi->fp); /* Close file */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
224 goto free; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
225 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
226 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
227 /* Ensure data is effectively written to disk, we first flush libc buffers |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
228 * using fflush(), then fsync() to flush kernel buffers, and finally call |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
229 * fclose() (which call fflush() again, but the first one is needed since |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
230 * it doesn't make much sense to flush kernel buffers and then libc buffers, |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
231 * while closing file releases file descriptor we need to call fsync(). */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
232 #if defined(HAVE_FFLUSH) || defined(HAVE_FSYNC) |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
233 if (ssi->secure_save) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
234 int fail = 0; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
235 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
236 #ifdef HAVE_FFLUSH |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
237 fail = (fflush(ssi->fp) == EOF); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
238 #endif |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
239 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
240 #ifdef HAVE_FSYNC |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
241 if (!fail) fail = fsync(fileno(ssi->fp)); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
242 #endif |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
243 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
244 if (fail) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
245 ret = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
246 secsave_errno = SS_ERR_OTHER; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
247 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
248 fclose(ssi->fp); /* Close file, ignore errors. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
249 goto free; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
250 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
251 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
252 #endif |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
253 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
254 /* Close file. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
255 if (fclose(ssi->fp) == EOF) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
256 ret = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
257 secsave_errno = SS_ERR_OTHER; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
258 goto free; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
259 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
260 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
261 if (ssi->secure_save && ssi->file_name && ssi->tmp_file_name) { |
311
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
262 struct stat st; |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
263 |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
264 /* FIXME: Race condition on ssi->file_name. The file |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
265 * named ssi->file_name may have changed since |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
266 * secure_open() call (where we stat() file and |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
267 * more..). */ |
311
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
268 #ifndef NO_UNIX_SOFTLINKS |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
269 if (g_lstat(ssi->file_name, &st) == 0) |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
270 #else |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
271 if (g_stat(ssi->file_name, &st) == 0) |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
272 #endif |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
273 { |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
274 /* set the dest file attributes to that of source (ignoring errors) */ |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
275 if (ssi->preserve_perms) |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
276 { |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
277 chown(ssi->tmp_file_name, st.st_uid, st.st_gid); |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
278 chmod(ssi->tmp_file_name, st.st_mode); |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
279 } |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
280 |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
281 if (ssi->preserve_mtime) |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
282 { |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
283 struct utimbuf tb; |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
284 |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
285 tb.actime = st.st_atime; |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
286 tb.modtime = st.st_mtime; |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
287 utime(ssi->tmp_file_name, &tb); |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
288 } |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
289 } |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
290 if (debug > 2) g_printf("rename %s -> %s", ssi->tmp_file_name, ssi->file_name); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
291 if (g_rename(ssi->tmp_file_name, ssi->file_name) == -1) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
292 ret = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
293 secsave_errno = SS_ERR_RENAME; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
294 goto free; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
295 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
296 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
297 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
298 ret = 0; /* Success. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
299 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
300 free: |
313
a955b7fd626b
Secure save now unlinks temporary file on error by default.
zas_
parents:
311
diff
changeset
|
301 if (ssi->tmp_file_name) |
a955b7fd626b
Secure save now unlinks temporary file on error by default.
zas_
parents:
311
diff
changeset
|
302 { |
a955b7fd626b
Secure save now unlinks temporary file on error by default.
zas_
parents:
311
diff
changeset
|
303 if (ret && ssi->unlink_on_error) unlink(ssi->tmp_file_name); |
a955b7fd626b
Secure save now unlinks temporary file on error by default.
zas_
parents:
311
diff
changeset
|
304 g_free(ssi->tmp_file_name); |
a955b7fd626b
Secure save now unlinks temporary file on error by default.
zas_
parents:
311
diff
changeset
|
305 } |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
306 if (ssi->file_name) g_free(ssi->file_name); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
307 if (ssi) g_free(ssi); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
308 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
309 return ret; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
310 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
311 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
312 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
313 /** fputs() wrapper, set ssi->err to errno on error. If ssi->err is set when |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
314 * called, it immediatly returns EOF. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
315 */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
316 gint |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
317 secure_fputs(SecureSaveInfo *ssi, const gchar *s) |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
318 { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
319 gint ret; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
320 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
321 if (!ssi || !ssi->fp || ssi->err) return EOF; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
322 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
323 ret = fputs(s, ssi->fp); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
324 if (ret == EOF) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
325 secsave_errno = SS_ERR_OTHER; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
326 ssi->err = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
327 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
328 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
329 return ret; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
330 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
331 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
332 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
333 /** fputc() wrapper, set ssi->err to errno on error. If ssi->err is set when |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
334 * called, it immediatly returns EOF. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
335 */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
336 gint |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
337 secure_fputc(SecureSaveInfo *ssi, gint c) |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
338 { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
339 gint ret; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
340 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
341 if (!ssi || !ssi->fp || ssi->err) return EOF; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
342 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
343 ret = fputc(c, ssi->fp); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
344 if (ret == EOF) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
345 ssi->err = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
346 secsave_errno = SS_ERR_OTHER; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
347 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
348 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
349 return ret; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
350 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
351 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
352 /** fprintf() wrapper, set ssi->err to errno on error and return a negative |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
353 * value. If ssi->err is set when called, it immediatly returns -1. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
354 */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
355 gint |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
356 secure_fprintf(SecureSaveInfo *ssi, const gchar *format, ...) |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
357 { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
358 va_list ap; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
359 gint ret; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
360 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
361 if (!ssi || !ssi->fp || ssi->err) return -1; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
362 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
363 va_start(ap, format); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
364 ret = vfprintf(ssi->fp, format, ap); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
365 if (ret < 0) ssi->err = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
366 va_end(ap); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
367 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
368 return ret; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
369 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
370 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
371 gchar * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
372 secsave_strerror(SecureSaveErrno secsave_error) |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
373 { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
374 switch (secsave_error) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
375 case SS_ERR_OPEN_READ: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
376 return _("Cannot read the file"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
377 case SS_ERR_STAT: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
378 return _("Cannot get file status"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
379 case SS_ERR_ACCESS: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
380 return _("Cannot access the file"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
381 case SS_ERR_MKSTEMP: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
382 return _("Cannot create temp file"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
383 case SS_ERR_RENAME: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
384 return _("Cannot rename the file"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
385 case SS_ERR_DISABLED: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
386 return _("File saving disabled by option"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
387 case SS_ERR_OUT_OF_MEM: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
388 return _("Out of memory"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
389 case SS_ERR_OPEN_WRITE: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
390 return _("Cannot write the file"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
391 case SS_ERR_NONE: /* Impossible. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
392 case SS_ERR_OTHER: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
393 default: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
394 return _("Secure file saving error"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
395 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
396 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
397 |