1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
/*
* e-signature-utils.c
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the program; if not, see <http://www.gnu.org/licenses/>
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "e-signature-utils.h"
#include <errno.h>
#include <camel/camel.h>
#include <glib/gstdio.h>
#include <gconf/gconf-client.h>
#ifndef G_OS_WIN32
#include <sys/wait.h>
#endif
#include "e-util/e-util.h"
static ESignatureList *global_signature_list;
ESignatureList *
e_get_signature_list (void)
{
if (G_UNLIKELY (global_signature_list == NULL)) {
GConfClient *client;
client = gconf_client_get_default ();
global_signature_list = e_signature_list_new (client);
g_object_unref (client);
}
g_return_val_if_fail (global_signature_list != NULL, NULL);
return global_signature_list;
}
ESignature *
e_get_signature_by_name (const gchar *name)
{
ESignatureList *signature_list;
g_return_val_if_fail (name != NULL, NULL);
signature_list = e_get_signature_list ();
return e_signature_list_find_by_name (signature_list, name);
}
ESignature *
e_get_signature_by_uid (const gchar *uid)
{
ESignatureList *signature_list;
g_return_val_if_fail (uid != NULL, NULL);
signature_list = e_get_signature_list ();
return e_signature_list_find_by_uid (signature_list, uid);
}
gchar *
e_create_signature_file (GError **error)
{
const gchar *data_dir;
gchar basename[32];
gchar *filename;
gchar *pathname;
gint32 ii;
data_dir = e_get_user_data_dir ();
pathname = g_build_filename (data_dir, "signatures", NULL);
filename = NULL;
if (g_mkdir_with_parents (pathname, 0700) < 0) {
g_set_error (
error, G_FILE_ERROR,
g_file_error_from_errno (errno),
"%s: %s", pathname, g_strerror (errno));
g_free (pathname);
return NULL;
}
for (ii = 0; ii < G_MAXINT32; ii++) {
g_snprintf (
basename, sizeof (basename),
"signature-%" G_GINT32_FORMAT, ii);
g_free (filename);
filename = g_build_filename (pathname, basename, NULL);
if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
gint fd;
fd = g_creat (filename, 0600);
if (fd >= 0) {
close (fd);
break;
}
/* If we failed once we're probably going
* to continue failing, so just give up. */
g_set_error (
error, G_FILE_ERROR,
g_file_error_from_errno (errno),
"%s: %s", filename, g_strerror (errno));
g_free (filename);
filename = NULL;
break;
}
}
/* If there are actually G_MAXINT32 signature files, the
* most recent signature file we be overwritten. Sorry. */
return filename;
}
gchar *
e_read_signature_file (ESignature *signature,
gboolean convert_to_html,
GError **error)
{
CamelStream *input_stream;
CamelStream *output_stream;
GByteArray *buffer;
const gchar *filename;
gboolean is_html;
gchar *content;
gsize length;
gint fd;
g_return_val_if_fail (E_IS_SIGNATURE (signature), NULL);
filename = e_signature_get_filename (signature);
is_html = e_signature_get_is_html (signature);
fd = g_open (filename, O_RDONLY, 0);
if (fd < 0) {
g_set_error (
error, G_FILE_ERROR,
g_file_error_from_errno (errno),
"%s: %s", filename, g_strerror (errno));
return NULL;
}
input_stream = camel_stream_fs_new_with_fd (fd);
if (!is_html && convert_to_html) {
CamelStream *filtered_stream;
CamelMimeFilter *filter;
gint32 flags;
filtered_stream =
camel_stream_filter_new (input_stream);
g_object_unref (input_stream);
flags =
CAMEL_MIME_FILTER_TOHTML_PRESERVE_8BIT |
CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS |
CAMEL_MIME_FILTER_TOHTML_CONVERT_ADDRESSES |
CAMEL_MIME_FILTER_TOHTML_CONVERT_SPACES;
filter = camel_mime_filter_tohtml_new (flags, 0);
camel_stream_filter_add (
CAMEL_STREAM_FILTER (filtered_stream), filter);
g_object_unref (filter);
input_stream = filtered_stream;
}
buffer = g_byte_array_new ();
output_stream = camel_stream_mem_new ();
camel_stream_mem_set_byte_array (
CAMEL_STREAM_MEM (output_stream), buffer);
camel_stream_write_to_stream (input_stream, output_stream, NULL, NULL);
g_object_unref (output_stream);
g_object_unref (input_stream);
/* Make sure the buffer is nul-terminated. */
length = (gsize) buffer->len;
g_byte_array_append (buffer, (guint8 *) "", 1);
content = (gchar *) g_byte_array_free (buffer, FALSE);
/* Signatures are saved as UTF-8, but we still need to check that
* the signature is valid UTF-8 because the user may be opening
* a signature file that is in his/her locale character set. If
* it's not in UTF-8 then try converting from the current locale. */
if (!g_utf8_validate (content, length, NULL)) {
gchar *utf8;
utf8 = g_locale_to_utf8 (content, length, NULL, NULL, error);
g_free (content);
content = utf8;
}
return content;
}
gchar *
e_run_signature_script (const gchar *filename)
{
/* FIXME Make this cross-platform, prefer GLib functions over
* POSIX, and report errors via GError instead of dumping
* messages to the terminal where users won't see them. */
#ifndef G_OS_WIN32
gint in_fds[2];
pid_t pid;
g_return_val_if_fail (filename != NULL, NULL);
if (pipe (in_fds) == -1) {
g_warning (
"Failed to create pipe to '%s': %s",
filename, g_strerror (errno));
return NULL;
}
pid = fork ();
/* Child Process */
if (pid == 0) {
gint maxfd, ii;
close (in_fds[0]);
if (dup2 (in_fds[1], STDOUT_FILENO) < 0)
_exit (255);
close (in_fds[1]);
setsid ();
maxfd = sysconf (_SC_OPEN_MAX);
for (ii = 3; ii < maxfd; ii++) {
if (ii == STDIN_FILENO)
continue;
if (ii == STDOUT_FILENO)
continue;
if (ii == STDERR_FILENO)
continue;
fcntl (ii, F_SETFD, FD_CLOEXEC);
}
execlp ("/bin/sh", "/bin/sh", "-c", filename, NULL);
g_warning (
"Could not execute '%s': %s",
filename, g_strerror (errno));
_exit (255);
/* Parent Process */
} else if (pid > 0) {
CamelStream *output_stream;
CamelStream *input_stream;
GByteArray *buffer;
gchar *content;
gsize length;
gint result;
gint status;
close (in_fds[1]);
buffer = g_byte_array_new ();
output_stream = camel_stream_mem_new ();
camel_stream_mem_set_byte_array (
CAMEL_STREAM_MEM (output_stream), buffer);
input_stream = camel_stream_fs_new_with_fd (in_fds[0]);
camel_stream_write_to_stream (
input_stream, output_stream, NULL, NULL);
g_object_unref (input_stream);
g_object_unref (output_stream);
/* Make sure the buffer is nul-terminated. */
length = (gsize) buffer->len;
g_byte_array_append (buffer, (guchar *) "", 1);
content = (gchar *) g_byte_array_free (buffer, FALSE);
/* Signature scripts are supposed to generate UTF-8 content,
* but because users are known to never read the manual, we
* try to do our best if the content isn't valid UTF-8 by
* assuming that the content is in the user's locale
* character set. */
if (!g_utf8_validate (content, length, NULL)) {
gchar *utf8;
/* XXX Should pass a GError here. */
utf8 = g_locale_to_utf8 (
content, length, NULL, NULL, NULL);
g_free (content);
content = utf8;
}
/* Wait for the script process to terminate. */
result = waitpid (pid, &status, 0);
if (result == -1 && errno == EINTR) {
/* Child process is hanging... */
kill (pid, SIGTERM);
sleep (1);
result = waitpid (pid, &status, WNOHANG);
if (result == 0) {
/* ...still hanging, set phasers to KILL. */
kill (pid, SIGKILL);
sleep (1);
waitpid (pid, &status, WNOHANG);
}
}
return content;
/* Forking Failed */
} else {
g_warning (
"Failed to create child process '%s': %s",
filename, g_strerror (errno));
close (in_fds[0]);
close (in_fds[1]);
}
#endif
return NULL;
}
|