Mercurial > geeqie
annotate src/secure_save.c @ 1397:a0bd58a6535f
In various Edit context menus, only display editors that match the file types in the selection.
author | zas_ |
---|---|
date | Sun, 08 Mar 2009 13:23:25 +0000 |
parents | 2abdd6e50120 |
children | a6f9ba6fd751 |
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 |
1284 | 3 * Copyright (C) 2008 - 2009 The Geeqie Team |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
4 * |
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 |
1305 | 12 #include <glib/gprintf.h> |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
13 #include <glib/gstdio.h> |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
14 #include <errno.h> |
311
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
15 #include <utime.h> |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
16 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
17 #include "main.h" |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
18 #include "secure_save.h" |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
19 |
507 | 20 |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
21 /* ABOUT SECURE SAVE */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
22 /* 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
|
23 * 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
|
24 * modified and improved by all ELinks contributors. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
25 * 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
|
26 * 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
|
27 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
28 /* If ssi->secure_save is TRUE: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
29 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
30 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
31 * 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
|
32 * 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
|
33 * 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
|
34 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
35 * 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
|
36 * mkstemp(). XXXXXX is a random string. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
37 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
38 * 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
|
39 * field named fp. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
40 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
41 * 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
|
42 * (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
|
43 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
44 * 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
|
45 * 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
|
46 * 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
|
47 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
48 * 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
|
49 * 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
|
50 * then secsave is disabled for that file. |
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 * If ssi->secure_save is FALSE: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
53 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
54 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
55 * 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
|
56 * 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
|
57 * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
58 * In both cases: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
59 * ~~~~~~~~~~~~~ |
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 * 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
|
62 */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
63 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
64 /* 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
|
65 /* 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
|
66 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
67 SecureSaveErrno secsave_errno = SS_ERR_NONE; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
68 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
69 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
70 /** 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
|
71 * 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
|
72 static SecureSaveInfo * |
309 | 73 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
|
74 { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
75 struct stat st; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
76 SecureSaveInfo *ssi; |
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 secsave_errno = SS_ERR_NONE; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
79 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
80 ssi = g_new0(SecureSaveInfo, 1); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
81 if (!ssi) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
82 secsave_errno = SS_ERR_OUT_OF_MEM; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
83 goto end; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
84 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
85 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
86 ssi->secure_save = TRUE; |
311
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
87 ssi->preserve_perms = TRUE; |
313
a955b7fd626b
Secure save now unlinks temporary file on error by default.
zas_
parents:
311
diff
changeset
|
88 ssi->unlink_on_error = TRUE; |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
89 |
310
4b25b3b30f35
Revert part of the previous patch, let the caller take care
zas_
parents:
309
diff
changeset
|
90 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
|
91 if (!ssi->file_name) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
92 secsave_errno = SS_ERR_OUT_OF_MEM; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
93 goto free_f; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
94 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
95 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
96 /* Check properties of final file. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
97 #ifndef NO_UNIX_SOFTLINKS |
699
9873d695a9c1
Do not use glib posix wrappers since they were introduced in 2.6
zas_
parents:
698
diff
changeset
|
98 if (lstat(ssi->file_name, &st)) { |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
99 #else |
699
9873d695a9c1
Do not use glib posix wrappers since they were introduced in 2.6
zas_
parents:
698
diff
changeset
|
100 if (stat(ssi->file_name, &st)) { |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
101 #endif |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
102 /* We ignore error caused by file inexistence. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
103 if (errno != ENOENT) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
104 /* lstat() error. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
105 ssi->err = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
106 secsave_errno = SS_ERR_STAT; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
107 goto free_file_name; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
108 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
109 } else { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
110 if (!S_ISREG(st.st_mode)) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
111 /* 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
|
112 ssi->secure_save = FALSE; |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
113 } else { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
114 #ifdef HAVE_ACCESS |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
115 /* XXX: access() do not work with setuid programs. */ |
698
2700db14aaa6
Use access() instead of g_access() which is only available in 2.8.
zas_
parents:
671
diff
changeset
|
116 if (access(ssi->file_name, R_OK | W_OK) < 0) { |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
117 ssi->err = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
118 secsave_errno = SS_ERR_ACCESS; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
119 goto free_file_name; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
120 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
121 #else |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
122 FILE *f1; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
123 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
124 /* 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
|
125 * [l]stat() and fopen() */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
126 |
699
9873d695a9c1
Do not use glib posix wrappers since they were introduced in 2.6
zas_
parents:
698
diff
changeset
|
127 f1 = fopen(ssi->file_name, "rb+"); |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
128 if (f1) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
129 fclose(f1); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
130 } else { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
131 ssi->err = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
132 secsave_errno = SS_ERR_OPEN_READ; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
133 goto free_file_name; |
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 #endif |
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 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
138 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
139 if (ssi->secure_save) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
140 /* 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
|
141 * 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
|
142 * then converted to FILE * using fdopen(). |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
143 */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
144 gint fd; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
145 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
|
146 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
147 if (!randname) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
148 secsave_errno = SS_ERR_OUT_OF_MEM; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
149 goto free_file_name; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
150 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
151 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
152 /* 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
|
153 fd = g_mkstemp(randname); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
154 if (fd == -1) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
155 secsave_errno = SS_ERR_MKSTEMP; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
156 g_free(randname); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
157 goto free_file_name; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
158 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
159 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
160 ssi->fp = fdopen(fd, "wb"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
161 if (!ssi->fp) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
162 secsave_errno = SS_ERR_OPEN_WRITE; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
163 ssi->err = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
164 g_free(randname); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
165 goto free_file_name; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
166 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
167 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
168 ssi->tmp_file_name = randname; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
169 } else { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
170 /* No need to create a temporary file here. */ |
699
9873d695a9c1
Do not use glib posix wrappers since they were introduced in 2.6
zas_
parents:
698
diff
changeset
|
171 ssi->fp = fopen(ssi->file_name, "wb"); |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
172 if (!ssi->fp) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
173 secsave_errno = SS_ERR_OPEN_WRITE; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
174 ssi->err = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
175 goto free_file_name; |
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 } |
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 return ssi; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
180 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
181 free_file_name: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
182 g_free(ssi->file_name); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
183 ssi->file_name = NULL; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
184 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
185 free_f: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
186 g_free(ssi); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
187 ssi = NULL; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
188 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
189 end: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
190 return NULL; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
191 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
192 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
193 SecureSaveInfo * |
309 | 194 secure_open(const gchar *file_name) |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
195 { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
196 SecureSaveInfo *ssi; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
197 mode_t saved_mask; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
198 #ifdef CONFIG_OS_WIN32 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
199 /* 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
|
200 const mode_t mask = 0177; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
201 #else |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
202 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
|
203 #endif |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
204 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
205 saved_mask = umask(mask); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
206 ssi = secure_open_umask(file_name); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
207 umask(saved_mask); |
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 return ssi; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
210 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
211 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
212 /** 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
|
213 * errno or -1 on failure. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
214 */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
215 gint |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
216 secure_close(SecureSaveInfo *ssi) |
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 gint ret = -1; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
219 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
220 if (!ssi) return ret; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
221 if (!ssi->fp) goto free; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
222 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
223 if (ssi->err) { /* Keep previous errno. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
224 ret = ssi->err; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
225 fclose(ssi->fp); /* Close file */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
226 goto free; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
227 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
228 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
229 /* 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
|
230 * 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
|
231 * 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
|
232 * 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
|
233 * 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
|
234 #if defined(HAVE_FFLUSH) || defined(HAVE_FSYNC) |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
235 if (ssi->secure_save) { |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
699
diff
changeset
|
236 gint fail = 0; |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
237 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
238 #ifdef HAVE_FFLUSH |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
239 fail = (fflush(ssi->fp) == EOF); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
240 #endif |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
241 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
242 #ifdef HAVE_FSYNC |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
243 if (!fail) fail = fsync(fileno(ssi->fp)); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
244 #endif |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
245 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
246 if (fail) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
247 ret = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
248 secsave_errno = SS_ERR_OTHER; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
249 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
250 fclose(ssi->fp); /* Close file, ignore errors. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
251 goto free; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
252 } |
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 #endif |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
255 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
256 /* Close file. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
257 if (fclose(ssi->fp) == EOF) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
258 ret = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
259 secsave_errno = SS_ERR_OTHER; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
260 goto free; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
261 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
262 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
263 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
|
264 struct stat st; |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
265 |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
266 /* 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
|
267 * 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
|
268 * 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
|
269 * more..). */ |
311
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
270 #ifndef NO_UNIX_SOFTLINKS |
699
9873d695a9c1
Do not use glib posix wrappers since they were introduced in 2.6
zas_
parents:
698
diff
changeset
|
271 if (lstat(ssi->file_name, &st) == 0) |
442 | 272 #else |
699
9873d695a9c1
Do not use glib posix wrappers since they were introduced in 2.6
zas_
parents:
698
diff
changeset
|
273 if (stat(ssi->file_name, &st) == 0) |
311
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
274 #endif |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
275 { |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
276 /* 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
|
277 if (ssi->preserve_perms) |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
278 { |
1180 | 279 if (chown(ssi->tmp_file_name, st.st_uid, st.st_gid) != 0) log_printf("chown('%s', %d, %d) failed", ssi->tmp_file_name, st.st_uid, st.st_gid); |
280 if (chmod(ssi->tmp_file_name, st.st_mode) != 0) log_printf("chmod('%s', %o) failed", ssi->tmp_file_name, st.st_mode); | |
311
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
281 } |
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 if (ssi->preserve_mtime) |
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 struct utimbuf tb; |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
286 |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
287 tb.actime = st.st_atime; |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
288 tb.modtime = st.st_mtime; |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
289 utime(ssi->tmp_file_name, &tb); |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
290 } |
8a6650589829
Preserve permissions of the destination file when using secure save.
zas_
parents:
310
diff
changeset
|
291 } |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
292 DEBUG_3("rename %s -> %s", ssi->tmp_file_name, ssi->file_name); |
699
9873d695a9c1
Do not use glib posix wrappers since they were introduced in 2.6
zas_
parents:
698
diff
changeset
|
293 if (rename(ssi->tmp_file_name, ssi->file_name) == -1) { |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
294 ret = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
295 secsave_errno = SS_ERR_RENAME; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
296 goto free; |
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 } |
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 ret = 0; /* Success. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
301 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
302 free: |
313
a955b7fd626b
Secure save now unlinks temporary file on error by default.
zas_
parents:
311
diff
changeset
|
303 if (ssi->tmp_file_name) |
a955b7fd626b
Secure save now unlinks temporary file on error by default.
zas_
parents:
311
diff
changeset
|
304 { |
a955b7fd626b
Secure save now unlinks temporary file on error by default.
zas_
parents:
311
diff
changeset
|
305 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
|
306 g_free(ssi->tmp_file_name); |
a955b7fd626b
Secure save now unlinks temporary file on error by default.
zas_
parents:
311
diff
changeset
|
307 } |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
308 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
|
309 if (ssi) g_free(ssi); |
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 return ret; |
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 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
314 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
315 /** 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
|
316 * called, it immediatly returns EOF. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
317 */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
318 gint |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
319 secure_fputs(SecureSaveInfo *ssi, const gchar *s) |
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 gint ret; |
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 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
|
324 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
325 ret = fputs(s, ssi->fp); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
326 if (ret == EOF) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
327 secsave_errno = SS_ERR_OTHER; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
328 ssi->err = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
329 } |
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 return ret; |
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 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
334 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
335 /** 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
|
336 * called, it immediatly returns EOF. |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
337 */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
338 gint |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
339 secure_fputc(SecureSaveInfo *ssi, gint c) |
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 gint ret; |
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 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
|
344 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
345 ret = fputc(c, ssi->fp); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
346 if (ret == EOF) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
347 ssi->err = errno; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
348 secsave_errno = SS_ERR_OTHER; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
349 } |
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 return ret; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
352 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
353 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
354 /** 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
|
355 * 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
|
356 */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
357 gint |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
358 secure_fprintf(SecureSaveInfo *ssi, const gchar *format, ...) |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
359 { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
360 va_list ap; |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
361 gint ret; |
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 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
|
364 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
365 va_start(ap, format); |
1305 | 366 ret = g_vfprintf(ssi->fp, format, ap); |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
367 va_end(ap); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
368 |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
369 return ret; |
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 |
536 | 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. | |
374 */ | |
375 size_t | |
1003 | 376 secure_fwrite(gconstpointer ptr, size_t size, size_t nmemb, SecureSaveInfo *ssi) |
536 | 377 { |
378 size_t ret; | |
379 | |
380 if (!ssi || !ssi->fp || ssi->err) return 0; | |
381 | |
382 ret = fwrite(ptr, size, nmemb, ssi->fp); | |
383 if (ret < nmemb) | |
384 { | |
385 ssi->err = errno; | |
386 secsave_errno = SS_ERR_OTHER; | |
387 } | |
388 | |
389 return ret; | |
390 } | |
391 | |
307
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
392 gchar * |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
393 secsave_strerror(SecureSaveErrno secsave_error) |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
394 { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
395 switch (secsave_error) { |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
396 case SS_ERR_OPEN_READ: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
397 return _("Cannot read the file"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
398 case SS_ERR_STAT: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
399 return _("Cannot get file status"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
400 case SS_ERR_ACCESS: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
401 return _("Cannot access the file"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
402 case SS_ERR_MKSTEMP: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
403 return _("Cannot create temp file"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
404 case SS_ERR_RENAME: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
405 return _("Cannot rename the file"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
406 case SS_ERR_DISABLED: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
407 return _("File saving disabled by option"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
408 case SS_ERR_OUT_OF_MEM: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
409 return _("Out of memory"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
410 case SS_ERR_OPEN_WRITE: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
411 return _("Cannot write the file"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
412 case SS_ERR_NONE: /* Impossible. */ |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
413 case SS_ERR_OTHER: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
414 default: |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
415 return _("Secure file saving error"); |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
416 } |
667e49f52168
Move secure save code to its own files: secure_save.{c,h}.
zas_
parents:
diff
changeset
|
417 } |
1055
1646720364cf
Adding a vim modeline to all files - patch by Klaus Ethgen
nadvornik
parents:
1003
diff
changeset
|
418 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ |