]>
Commit | Line | Data |
---|---|---|
c4fdb7d1 | 1 | /* |
a83ff38a | 2 | * Copyright (c) 2009-2010 Apple Inc. All rights reserved. |
c4fdb7d1 A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | #include <stdio.h> | |
25 | #include <dirent.h> | |
26 | #include <string.h> | |
27 | #include <stdlib.h> | |
28 | #include <unistd.h> | |
29 | #include <stdint.h> | |
30 | #include <errno.h> | |
31 | #include <time.h> | |
32 | #include <sys/time.h> | |
33 | #include <sys/stat.h> | |
34 | #include <asl.h> | |
35 | #include <asl_private.h> | |
36 | #include <asl_core.h> | |
37 | #include <asl_file.h> | |
38 | #include <asl_store.h> | |
39 | ||
40 | extern time_t asl_parse_time(const char *); | |
41 | ||
42 | #define TEMP_NAME "_TMP_.asl" | |
43 | #define STORE_DATA_FLAGS 0x00000000 | |
44 | ||
45 | static const char *store_path = PATH_ASL_STORE; | |
46 | ||
47 | /* | |
48 | * Cache the output file for BB writes. | |
49 | * we write messages in the order in which they were generated, | |
50 | * so we are almost guaranteed to use the cache in most cases. | |
51 | */ | |
52 | static asl_file_t *cache_file = NULL; | |
53 | static uid_t cache_uid = -1; | |
54 | static uid_t cache_gid = -1; | |
55 | static time_t cache_bb = 0; | |
56 | ||
57 | typedef struct name_list_s | |
58 | { | |
59 | char *name; | |
60 | struct name_list_s *next; | |
61 | } name_list_t; | |
62 | ||
63 | static name_list_t * | |
64 | add_to_list(name_list_t *l, const char *name) | |
65 | { | |
66 | name_list_t *e, *x; | |
67 | ||
68 | if (name == NULL) return l; | |
69 | ||
70 | e = (name_list_t *)calloc(1, sizeof(name_list_t)); | |
71 | if (e == NULL) return NULL; | |
72 | ||
73 | e->name = strdup(name); | |
74 | if (e->name == NULL) | |
75 | { | |
76 | free(e); | |
77 | return NULL; | |
78 | } | |
79 | ||
80 | /* list is sorted by name (i.e. primarily by timestamp) */ | |
81 | if (l == NULL) return e; | |
82 | ||
83 | if (strcmp(e->name, l->name) <= 0) | |
84 | { | |
85 | e->next = l; | |
86 | return e; | |
87 | } | |
88 | ||
89 | for (x = l; (x->next != NULL) && (strcmp(e->name, x->next->name) > 0) ; x = x->next); | |
90 | ||
91 | e->next = x->next; | |
92 | x->next = e; | |
93 | return l; | |
94 | } | |
95 | ||
96 | static void | |
97 | free_list(name_list_t *l) | |
98 | { | |
99 | name_list_t *e; | |
100 | ||
101 | while (l != NULL) | |
102 | { | |
103 | e = l; | |
104 | l = l->next; | |
105 | free(e->name); | |
106 | free(e); | |
107 | } | |
108 | ||
109 | free(l); | |
110 | } | |
111 | ||
112 | /* find all messages that have an ASLExpireTime key */ | |
113 | static uint32_t | |
114 | do_ASLExpireTime_search(asl_store_t *s, asl_search_result_t **out) | |
115 | { | |
116 | asl_search_result_t q, *query, *res; | |
117 | asl_msg_t *qm[1]; | |
118 | uint32_t status; | |
119 | uint64_t mid; | |
120 | ||
a83ff38a | 121 | qm[0] = asl_msg_new(ASL_TYPE_QUERY); |
c4fdb7d1 A |
122 | if (qm[0] == NULL) return ASL_STATUS_NO_MEMORY; |
123 | ||
124 | q.count = 1; | |
125 | q.curr = 0; | |
126 | q.msg = qm; | |
127 | query = &q; | |
128 | ||
a83ff38a | 129 | if (asl_msg_set_key_val_op(qm[0], ASL_KEY_EXPIRE_TIME, NULL, ASL_QUERY_OP_TRUE) != 0) |
c4fdb7d1 | 130 | { |
a83ff38a | 131 | asl_msg_release(qm[0]); |
c4fdb7d1 A |
132 | return ASL_STATUS_NO_MEMORY; |
133 | } | |
134 | ||
135 | res = NULL; | |
136 | mid = 0; | |
137 | status = asl_store_match(s, query, out, &mid, 0, 0, 1); | |
138 | ||
a83ff38a | 139 | asl_msg_release(qm[0]); |
c4fdb7d1 A |
140 | return status; |
141 | } | |
142 | ||
143 | /* remove all messages that have an ASLExpireTime key */ | |
144 | static uint32_t | |
145 | do_ASLExpireTime_filter(const char *name) | |
146 | { | |
a83ff38a | 147 | aslmsg msg; |
c4fdb7d1 A |
148 | asl_file_t *in, *out; |
149 | uint32_t status; | |
150 | uint64_t mid; | |
151 | char *inpath, *outpath; | |
152 | struct stat sb; | |
153 | ||
154 | if (name == NULL) return ASL_STATUS_INVALID_ARG; | |
155 | ||
156 | in = NULL; | |
157 | inpath = NULL; | |
158 | asprintf(&inpath, "%s/%s", store_path, name); | |
159 | if (inpath == NULL) return ASL_STATUS_NO_MEMORY; | |
160 | ||
161 | memset(&sb, 0, sizeof(struct stat)); | |
162 | if (stat(inpath, &sb) < 0) | |
163 | { | |
164 | free(inpath); | |
165 | return ASL_STATUS_INVALID_STORE; | |
166 | } | |
167 | ||
168 | status = asl_file_open_read(inpath, &in); | |
169 | if (status != ASL_STATUS_OK) | |
170 | { | |
171 | free(inpath); | |
172 | return ASL_STATUS_OK; | |
173 | } | |
174 | ||
175 | out = NULL; | |
176 | outpath = NULL; | |
177 | asprintf(&outpath, "%s/%s", store_path, TEMP_NAME); | |
178 | if (outpath == NULL) | |
179 | { | |
180 | asl_file_close(in); | |
181 | free(inpath); | |
182 | return ASL_STATUS_NO_MEMORY; | |
183 | } | |
184 | ||
185 | status = asl_file_open_write(outpath, sb.st_mode, sb.st_uid, sb.st_gid, &out); | |
186 | if (status != ASL_STATUS_OK) | |
187 | { | |
188 | asl_file_close(in); | |
189 | free(inpath); | |
190 | free(outpath); | |
191 | return status; | |
192 | } | |
193 | ||
194 | out->flags = ASL_FILE_FLAG_PRESERVE_MSG_ID; | |
195 | ||
196 | msg = NULL; | |
197 | while (asl_file_fetch_next(in, &msg) == ASL_STATUS_OK) | |
198 | { | |
199 | if (msg == NULL) break; | |
200 | ||
201 | mid = 0; | |
202 | ||
203 | if (asl_get(msg, ASL_KEY_EXPIRE_TIME) == NULL) status = asl_file_save(out, msg, &mid); | |
204 | ||
205 | asl_free(msg); | |
206 | msg = NULL; | |
207 | ||
208 | if (status != ASL_STATUS_OK) break; | |
209 | } | |
210 | ||
211 | asl_file_close(in); | |
212 | asl_file_close(out); | |
213 | ||
214 | unlink(inpath); | |
215 | rename(outpath, inpath); | |
216 | ||
217 | free(inpath); | |
218 | free(outpath); | |
219 | ||
220 | return status; | |
221 | } | |
222 | ||
223 | /* qsort compare function for sorting by message ID */ | |
224 | static int | |
225 | sort_compare(const void *a, const void *b) | |
226 | { | |
227 | const char *va, *vb; | |
228 | uint64_t na, nb; | |
229 | ||
a83ff38a A |
230 | va = asl_get(*(aslmsg *)a, ASL_KEY_MSG_ID); |
231 | vb = asl_get(*(aslmsg *)b, ASL_KEY_MSG_ID); | |
c4fdb7d1 A |
232 | |
233 | if (va == NULL) return -1; | |
234 | if (vb == NULL) return 1; | |
235 | ||
236 | na = atoll(va); | |
237 | nb = atoll(vb); | |
238 | ||
239 | if (na < nb) return -1; | |
240 | if (na > nb) return 1; | |
241 | return 0; | |
242 | } | |
243 | ||
244 | /* save a message to an appropriately named BB file */ | |
245 | static uint32_t | |
a83ff38a | 246 | save_bb_msg(aslmsg msg) |
c4fdb7d1 A |
247 | { |
248 | const char *val; | |
249 | uid_t u, ruid; | |
250 | gid_t g, rgid; | |
251 | struct tm ctm; | |
252 | time_t msg_time, bb; | |
253 | char *path, *tstring; | |
254 | asl_file_t *out; | |
255 | uint64_t mid; | |
256 | mode_t m; | |
257 | uint32_t status; | |
258 | ||
259 | if (msg == NULL) return ASL_STATUS_OK; | |
260 | ||
261 | val = asl_get(msg, ASL_KEY_EXPIRE_TIME); | |
262 | if (val == NULL) return ASL_STATUS_INVALID_ARG; | |
263 | msg_time = asl_parse_time(val); | |
264 | ||
265 | val = asl_get(msg, ASL_KEY_READ_UID); | |
266 | ruid = -1; | |
267 | if (val != NULL) ruid = atoi(val); | |
268 | ||
269 | val = asl_get(msg, ASL_KEY_READ_GID); | |
270 | rgid = -1; | |
271 | if (val != NULL) rgid = atoi(val); | |
272 | ||
273 | if (localtime_r((const time_t *)&msg_time, &ctm) == NULL) return ASL_STATUS_FAILED; | |
274 | ||
275 | /* | |
276 | * This supports 12 monthy "Best Before" buckets. | |
277 | * We advance the actual expiry time to day zero of the following month. | |
278 | * mktime() is clever enough to know that you actually mean the last day | |
279 | * of the previous month. What we get back from localtime is the last | |
280 | * day of the month in which the message expires, which we use in the name. | |
281 | */ | |
282 | ctm.tm_sec = 0; | |
283 | ctm.tm_min = 0; | |
284 | ctm.tm_hour = 0; | |
285 | ctm.tm_mday = 0; | |
286 | ctm.tm_mon += 1; | |
287 | ||
288 | bb = mktime(&ctm); | |
289 | ||
290 | u = 0; | |
291 | g = 0; | |
292 | if (ruid != -1) u = ruid; | |
293 | if (rgid != -1) g = rgid; | |
294 | ||
295 | out = NULL; | |
296 | ||
297 | if (cache_file != NULL) | |
298 | { | |
299 | if ((cache_uid == u) && (cache_gid == g) && (cache_bb == bb)) | |
300 | { | |
301 | out = cache_file; | |
302 | } | |
303 | else | |
304 | { | |
305 | asl_file_close(cache_file); | |
306 | cache_file = NULL; | |
307 | cache_uid = -1; | |
308 | cache_gid = -1; | |
309 | cache_bb = 0; | |
310 | } | |
311 | } | |
312 | ||
313 | if (out == NULL) | |
314 | { | |
315 | if (localtime_r((const time_t *)&bb, &ctm) == NULL) return ASL_STATUS_FAILED; | |
316 | ||
317 | tstring = NULL; | |
318 | asprintf(&tstring, "%s/BB.%d.%02d.%02d", store_path, ctm.tm_year + 1900, ctm.tm_mon + 1, ctm.tm_mday); | |
319 | if (tstring == NULL) return ASL_STATUS_NO_MEMORY; | |
320 | ||
321 | path = NULL; | |
322 | m = 0644; | |
323 | ||
324 | if (ruid == -1) | |
325 | { | |
326 | if (rgid == -1) | |
327 | { | |
328 | asprintf(&path, "%s.asl", tstring); | |
329 | } | |
330 | else | |
331 | { | |
332 | m = 0640; | |
333 | asprintf(&path, "%s.G%d.asl", tstring, g); | |
334 | } | |
335 | } | |
336 | else | |
337 | { | |
338 | if (rgid == -1) | |
339 | { | |
340 | m = 0600; | |
341 | asprintf(&path, "%s.U%d.asl", tstring, u); | |
342 | } | |
343 | else | |
344 | { | |
345 | m = 0640; | |
346 | asprintf(&path, "%s.U%d.G%u.asl", tstring, u, g); | |
347 | } | |
348 | } | |
349 | ||
350 | if (path == NULL) return ASL_STATUS_NO_MEMORY; | |
351 | ||
352 | status = asl_file_open_write(path, m, u, g, &out); | |
353 | free(path); | |
354 | if (status != ASL_STATUS_OK) return status; | |
355 | if (out == NULL) return ASL_STATUS_FAILED; | |
356 | ||
357 | out->flags = ASL_FILE_FLAG_PRESERVE_MSG_ID; | |
358 | ||
359 | cache_file = out; | |
360 | cache_uid = u; | |
361 | cache_gid = g; | |
362 | cache_bb = bb; | |
363 | } | |
364 | ||
365 | status = asl_file_save(out, msg, &mid); | |
366 | ||
367 | return status; | |
368 | } | |
369 | ||
370 | static uint32_t | |
371 | finish_conversion() | |
372 | { | |
373 | FILE *sd; | |
374 | uint32_t store_flags; | |
375 | int status; | |
376 | char *path; | |
377 | ||
378 | path = NULL; | |
379 | asprintf(&path, "%s/%s", store_path, FILE_ASL_STORE_DATA); | |
380 | ||
381 | sd = fopen(path, "a"); | |
382 | free(path); | |
383 | if (sd == NULL) return ASL_STATUS_WRITE_FAILED; | |
384 | ||
385 | store_flags = STORE_DATA_FLAGS; | |
386 | status = fwrite(&store_flags, sizeof(uint32_t), 1, sd); | |
387 | fclose(sd); | |
388 | ||
389 | if (status != 1) return ASL_STATUS_WRITE_FAILED; | |
390 | ||
391 | return ASL_STATUS_OK; | |
392 | } | |
393 | ||
394 | /* | |
395 | * Utility to convert a data store with LongTTL files into | |
396 | * a store with Best Before files. | |
397 | * | |
398 | * Returns quickly if the data store has already been converted. | |
399 | * | |
400 | * Older versions of the data store included messages with non-standard time-to-live | |
401 | * records in the daily data files (yyyy.mm.dd.asl). When the files expired, aslmanager | |
402 | * first copied messages with ASLExpireTime keys to a LongTTL file, then deleted the | |
403 | * original data file. | |
404 | * | |
405 | * We now write ASLExpireTime messages to a Best Before file (BB.yyyy.mm.dd.asl) | |
406 | * and aslmanager just deletes these files after the Best Before date has passed. | |
407 | * | |
408 | * If StoreData is bigger than 8 bytes, the store has been converted. Do nothing. | |
409 | * | |
410 | * Convert the store: | |
411 | * Search the store for messages that have an ASLExpireTime. | |
412 | * Sort by ASLMessageID | |
413 | * Remove all BB.* files and all LongTTL.* files | |
414 | * Write the ASLExpireTime messages into a new set of BB files | |
415 | * Re-write each YMD file without messages that have an ASLExpireTime | |
416 | * Add a new 4-byte flags field to StoreData | |
417 | */ | |
418 | ||
419 | uint32_t | |
420 | bb_convert(const char *name) | |
421 | { | |
422 | struct stat sb; | |
423 | asl_store_t *store; | |
424 | uint32_t status; | |
425 | asl_search_result_t *expire_time_records; | |
426 | DIR *dp; | |
427 | struct dirent *dent; | |
428 | int i; | |
429 | name_list_t *list, *e; | |
430 | char *path; | |
431 | ||
432 | if (name != NULL) store_path = name; | |
433 | ||
434 | /* StoreData must exist */ | |
435 | path = NULL; | |
436 | asprintf(&path, "%s/%s", store_path, FILE_ASL_STORE_DATA); | |
437 | if (path == NULL) return ASL_STATUS_NO_MEMORY; | |
438 | ||
439 | memset(&sb, 0, sizeof(struct stat)); | |
440 | i = stat(path, &sb); | |
441 | free(path); | |
442 | if (i != 0) return ASL_STATUS_INVALID_STORE; | |
443 | ||
444 | /* must be a regular file */ | |
a83ff38a | 445 | if (!S_ISREG(sb.st_mode)) return ASL_STATUS_INVALID_STORE; |
c4fdb7d1 A |
446 | |
447 | /* check is the store has already been converted */ | |
448 | if (sb.st_size > sizeof(uint64_t)) return ASL_STATUS_OK; | |
449 | ||
450 | /* find ASLExpireTime messages */ | |
451 | status = asl_store_open_read(store_path, &store); | |
452 | if (status != ASL_STATUS_OK) return status; | |
453 | ||
454 | expire_time_records = NULL; | |
455 | status = do_ASLExpireTime_search(store, &expire_time_records); | |
456 | ||
457 | asl_store_close(store); | |
458 | if (status != ASL_STATUS_OK) return status; | |
459 | ||
460 | /* unlink BB.* and LongTTL.* */ | |
461 | dp = opendir(store_path); | |
462 | if (dp == NULL) return ASL_STATUS_READ_FAILED; | |
463 | ||
464 | while ((dent = readdir(dp)) != NULL) | |
465 | { | |
466 | if ((!strncmp(dent->d_name, "BB.", 3)) || (!strncmp(dent->d_name, "LongTTL.", 8))) | |
467 | { | |
468 | path = NULL; | |
469 | asprintf(&path, "%s/%s", store_path, dent->d_name); | |
470 | if (path == NULL) | |
471 | { | |
472 | closedir(dp); | |
473 | return ASL_STATUS_NO_MEMORY; | |
474 | } | |
475 | ||
476 | unlink(path); | |
477 | free(path); | |
478 | } | |
479 | } | |
480 | ||
481 | closedir(dp); | |
482 | ||
483 | if ((expire_time_records == NULL) || (expire_time_records->count == 0)) return finish_conversion(); | |
484 | ||
485 | /* sort by ASLMessageID */ | |
a83ff38a | 486 | qsort(expire_time_records->msg, expire_time_records->count, sizeof(aslmsg), sort_compare); |
c4fdb7d1 A |
487 | |
488 | /* save the ASLExpireTime messages into a new set of BB files */ | |
489 | for (i = 0; i < expire_time_records->count; i++) | |
490 | { | |
a83ff38a | 491 | status = save_bb_msg((aslmsg)expire_time_records->msg[i]); |
c4fdb7d1 A |
492 | if (status != ASL_STATUS_OK) |
493 | { | |
494 | if (cache_file != NULL) asl_file_close(cache_file); | |
495 | return status; | |
496 | } | |
497 | } | |
498 | ||
499 | if (cache_file != NULL) asl_file_close(cache_file); | |
500 | ||
501 | aslresponse_free(expire_time_records); | |
502 | ||
503 | /* Re-write each YMD file without messages that have an ASLExpireTime */ | |
504 | dp = opendir(store_path); | |
505 | if (dp == NULL) return ASL_STATUS_READ_FAILED; | |
506 | ||
507 | list = NULL; | |
508 | ||
509 | while ((dent = readdir(dp)) != NULL) | |
510 | { | |
511 | if ((dent->d_name[0] < '0') || (dent->d_name[0] > '9')) continue; | |
512 | list = add_to_list(list, dent->d_name); | |
513 | } | |
514 | ||
515 | closedir(dp); | |
516 | ||
c4fdb7d1 A |
517 | for (e = list; e != NULL; e = e->next) |
518 | { | |
519 | status = do_ASLExpireTime_filter(e->name); | |
520 | if (status != ASL_STATUS_OK) | |
521 | { | |
522 | free_list(list); | |
523 | return status; | |
524 | } | |
525 | } | |
526 | ||
527 | free_list(list); | |
528 | ||
529 | return finish_conversion(); | |
530 | } |