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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
|
/*
* Copyright (c) 2013, Red Hat Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* * The names of contributors to this software may not be
* used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* Author: Stef Walter <stefw@redhat.com>
*/
#include "config.h"
#include "buffer.h"
#include "debug.h"
#include "dict.h"
#include "message.h"
#include "save.h"
#include <sys/stat.h>
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct _p11_save_file {
char *bare;
char *extension;
char *temp;
int fd;
int flags;
};
struct _p11_save_dir {
p11_dict *cache;
char *path;
int flags;
};
static char * make_unique_name (const char *bare,
const char *extension,
int (*check) (void *, char *),
void *data);
bool
p11_save_write_and_finish (p11_save_file *file,
const void *data,
ssize_t length)
{
bool ret;
if (!file)
return false;
ret = p11_save_write (file, data, length);
if (!p11_save_finish_file (file, NULL, ret))
ret = false;
return ret;
}
p11_save_file *
p11_save_open_file (const char *path,
const char *extension,
int flags)
{
p11_save_file *file;
char *temp;
int fd;
return_val_if_fail (path != NULL, NULL);
if (extension == NULL)
extension = "";
if (asprintf (&temp, "%s%s.XXXXXX", path, extension) < 0)
return_val_if_reached (NULL);
fd = mkstemp (temp);
if (fd < 0) {
p11_message ("couldn't create file: %s%s: %s",
path, extension, strerror (errno));
free (temp);
return NULL;
}
file = calloc (1, sizeof (p11_save_file));
return_val_if_fail (file != NULL, NULL);
file->temp = temp;
file->bare = strdup (path);
return_val_if_fail (file->bare != NULL, NULL);
file->extension = strdup (extension);
return_val_if_fail (file->extension != NULL, NULL);
file->flags = flags;
file->fd = fd;
return file;
}
bool
p11_save_write (p11_save_file *file,
const void *data,
ssize_t length)
{
const unsigned char *buf = data;
ssize_t written = 0;
ssize_t res;
if (!file)
return false;
/* Automatically calculate length */
if (length < 0) {
if (!data)
return true;
length = strlen (data);
}
while (written < length) {
res = write (file->fd, buf + written, length - written);
if (res <= 0) {
if (errno == EAGAIN && errno == EINTR)
continue;
p11_message ("couldn't write to file: %s: %s",
file->temp, strerror (errno));
return false;
} else {
written += res;
}
}
return true;
}
static void
filo_free (p11_save_file *file)
{
free (file->temp);
free (file->bare);
free (file->extension);
free (file);
}
#ifdef OS_UNIX
static int
on_unique_try_link (void *data,
char *path)
{
p11_save_file *file = data;
if (link (file->temp, path) < 0) {
if (errno == EEXIST)
return 0; /* Continue trying other names */
p11_message ("couldn't complete writing of file: %s: %s",
path, strerror (errno));
return -1;
}
return 1; /* All done */
}
#else /* OS_WIN32 */
static int
on_unique_try_rename (void *data,
char *path)
{
p11_save_file *file = data;
if (rename (file->temp, path) < 0) {
if (errno == EEXIST)
return 0; /* Continue trying other names */
p11_message ("couldn't complete writing of file: %s: %s",
path, strerror (errno));
return -1;
}
return 1; /* All done */
}
#endif /* OS_WIN32 */
bool
p11_save_finish_file (p11_save_file *file,
char **path_out,
bool commit)
{
bool ret = true;
char *path;
if (!file)
return false;
if (!commit) {
close (file->fd);
unlink (file->temp);
filo_free (file);
return true;
}
if (asprintf (&path, "%s%s", file->bare, file->extension) < 0)
return_val_if_reached (false);
if (close (file->fd) < 0) {
p11_message ("couldn't write file: %s: %s",
file->temp, strerror (errno));
ret = false;
#ifdef OS_UNIX
/* Set the mode of the file, readable by everyone, but not writable */
} else if (chmod (file->temp, S_IRUSR | S_IRGRP | S_IROTH) < 0) {
p11_message ("couldn't set file permissions: %s: %s",
file->temp, strerror (errno));
close (file->fd);
ret = false;
/* Atomically rename the tempfile over the filename */
} else if (file->flags & P11_SAVE_OVERWRITE) {
if (rename (file->temp, path) < 0) {
p11_message ("couldn't complete writing file: %s: %s",
path, strerror (errno));
ret = false;
} else {
unlink (file->temp);
}
/* Create a unique name if requested unique file name */
} else if (file->flags & P11_SAVE_UNIQUE) {
free (path);
path = make_unique_name (file->bare, file->extension,
on_unique_try_link, file);
if (!path)
ret = false;
unlink (file->temp);
/* When not overwriting, link will fail if filename exists. */
} else {
if (link (file->temp, path) < 0) {
p11_message ("couldn't complete writing of file: %s: %s",
path, strerror (errno));
ret = false;
}
unlink (file->temp);
#else /* OS_WIN32 */
/* Windows does not do atomic renames, so delete original file first */
} else {
/* Create a unique name if requested unique file name */
if (file->flags & P11_SAVE_UNIQUE) {
free (path);
path = make_unique_name (file->bare, file->extension,
on_unique_try_rename, file);
if (!path)
ret = false;
} else {
if ((file->flags & P11_SAVE_OVERWRITE) &&
unlink (path) < 0 && errno != ENOENT) {
p11_message ("couldn't remove original file: %s: %s",
path, strerror (errno));
ret = false;
}
if (ret == true &&
rename (file->temp, file->path) < 0) {
p11_message ("couldn't complete writing file: %s: %s",
file->path, strerror (errno));
ret = false;
}
}
unlink (file->temp);
#endif /* OS_WIN32 */
}
if (ret && path_out) {
*path_out = path;
path = NULL;
}
free (path);
filo_free (file);
return ret;
}
p11_save_dir *
p11_save_open_directory (const char *path,
int flags)
{
#ifdef OS_UNIX
struct stat sb;
#endif
p11_save_dir *dir;
return_val_if_fail (path != NULL, NULL);
#ifdef OS_UNIX
/* We update the permissions when we finish writing */
if (mkdir (path, S_IRWXU) < 0) {
#else /* OS_WIN32 */
if (mkdir (path) < 0) {
#endif
/* Some random error, report it */
if (errno != EEXIST) {
p11_message ("couldn't create directory: %s: %s", path, strerror (errno));
/* The directory exists and we're not overwriting */
} else if (!(flags & P11_SAVE_OVERWRITE)) {
p11_message ("directory already exists: %s", path);
return NULL;
}
#ifdef OS_UNIX
/*
* If the directory exists on unix, we may have restricted
* the directory permissions to read-only. We have to change
* them back to writable in order for things to work.
*/
if (stat (path, &sb) >= 0) {
if ((sb.st_mode & S_IRWXU) != S_IRWXU &&
chmod (path, S_IRWXU | sb.st_mode) < 0) {
p11_message ("couldn't make directory writable: %s: %s",
path, strerror (errno));
return NULL;
}
}
#endif /* OS_UNIX */
}
dir = calloc (1, sizeof (p11_save_dir));
return_val_if_fail (dir != NULL, NULL);
dir->path = strdup (path);
return_val_if_fail (dir->path != NULL, NULL);
dir->cache = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, free, NULL);
return_val_if_fail (dir->cache != NULL, NULL);
dir->flags = flags;
return dir;
}
static char *
make_unique_name (const char *bare,
const char *extension,
int (*check) (void *, char *),
void *data)
{
char unique[16];
p11_buffer buf;
int ret;
int i;
assert (bare != NULL);
assert (check != NULL);
p11_buffer_init_null (&buf, 0);
for (i = 0; true; i++) {
p11_buffer_reset (&buf, 64);
switch (i) {
/*
* For the first iteration, just build the filename as
* provided by the caller.
*/
case 0:
p11_buffer_add (&buf, bare, -1);
break;
/*
* On later iterations we try to add a numeric .N suffix
* before the extension, so the resulting file might look
* like filename.1.ext.
*
* As a special case if the extension is already '.0' then
* just just keep incerementing that.
*/
case 1:
if (extension && strcmp (extension, ".0") == 0)
extension = NULL;
/* fall through */
default:
p11_buffer_add (&buf, bare, -1);
snprintf (unique, sizeof (unique), ".%d", i);
p11_buffer_add (&buf, unique, -1);
break;
}
if (extension)
p11_buffer_add (&buf, extension, -1);
return_val_if_fail (p11_buffer_ok (&buf), NULL);
ret = check (data, buf.data);
if (ret < 0)
return NULL;
else if (ret > 0)
return p11_buffer_steal (&buf, NULL);
}
assert_not_reached ();
}
static int
on_unique_check_dir (void *data,
char *name)
{
p11_save_dir *dir = data;
if (!p11_dict_get (dir->cache, name))
return 1;
return 0; /* Keep looking */
}
p11_save_file *
p11_save_open_file_in (p11_save_dir *dir,
const char *basename,
const char *extension)
{
p11_save_file *file = NULL;
char *name;
char *path;
return_val_if_fail (dir != NULL, NULL);
return_val_if_fail (basename != NULL, NULL);
name = make_unique_name (basename, extension, on_unique_check_dir, dir);
return_val_if_fail (name != NULL, NULL);
if (asprintf (&path, "%s/%s", dir->path, name) < 0)
return_val_if_reached (NULL);
file = p11_save_open_file (path, NULL, dir->flags);
if (file) {
if (!p11_dict_set (dir->cache, name, name))
return_val_if_reached (NULL);
name = NULL;
}
free (name);
free (path);
return file;
}
#ifdef OS_UNIX
bool
p11_save_symlink_in (p11_save_dir *dir,
const char *linkname,
const char *extension,
const char *destination)
{
char *name;
char *path;
bool ret;
return_val_if_fail (dir != NULL, false);
return_val_if_fail (linkname != NULL, false);
return_val_if_fail (destination != NULL, false);
name = make_unique_name (linkname, extension, on_unique_check_dir, dir);
return_val_if_fail (name != NULL, false);
if (asprintf (&path, "%s/%s", dir->path, name) < 0)
return_val_if_reached (false);
unlink (path);
if (symlink (destination, path) < 0) {
p11_message ("couldn't create symlink: %s: %s",
path, strerror (errno));
ret = false;
} else {
if (!p11_dict_set (dir->cache, name, name))
return_val_if_reached (false);
name = NULL;
ret = true;
}
free (path);
free (name);
return ret;
}
#endif /* OS_UNIX */
static bool
cleanup_directory (const char *directory,
p11_dict *cache)
{
struct dirent *dp;
p11_dict *remove;
p11_dictiter iter;
char *path;
DIR *dir;
int skip;
bool ret;
/* First we load all the modules */
dir = opendir (directory);
if (!dir) {
p11_message ("couldn't list directory: %s: %s",
directory, strerror (errno));
return false;
}
remove = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, free, NULL);
/* We're within a global mutex, so readdir is safe */
while ((dp = readdir (dir)) != NULL) {
if (p11_dict_get (cache, dp->d_name))
continue;
if (asprintf (&path, "%s/%s", directory, dp->d_name) < 0)
return_val_if_reached (false);
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
if(dp->d_type != DT_UNKNOWN) {
skip = (dp->d_type == DT_DIR);
} else
#endif
{
struct stat st;
skip = (stat (path, &st) < 0) || S_ISDIR (st.st_mode);
}
if (!skip) {
if (!p11_dict_set (remove, path, path))
return_val_if_reached (false);
} else {
free (path);
}
}
closedir (dir);
ret = true;
/* Remove all the files still in the cache */
p11_dict_iterate (remove, &iter);
while (p11_dict_next (&iter, (void **)&path, NULL)) {
if (unlink (path) < 0 && errno != ENOENT) {
p11_message ("couldn't remove file: %s: %s",
path, strerror (errno));
ret = false;
break;
}
}
p11_dict_free (remove);
return ret;
}
bool
p11_save_finish_directory (p11_save_dir *dir,
bool commit)
{
bool ret = true;
if (!dir)
return false;
if (commit) {
if (dir->flags & P11_SAVE_OVERWRITE)
ret = cleanup_directory (dir->path, dir->cache);
#ifdef OS_UNIX
/* Try to set the mode of the directory to readable */
if (ret && chmod (dir->path, S_IRUSR | S_IXUSR | S_IRGRP |
S_IXGRP | S_IROTH | S_IXOTH) < 0) {
p11_message ("couldn't set directory permissions: %s: %s",
dir->path, strerror (errno));
ret = false;
}
#endif /* OS_UNIX */
}
p11_dict_free (dir->cache);
free (dir->path);
free (dir);
return ret;
}
|