libreport  2.7.2
A tool to inform users about various problems on the running system
dump_dir.h
1 /*
2  On-disk storage of problem data
3 
4  Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com)
5  Copyright (C) 2009 RedHat inc.
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License along
18  with this program; if not, write to the Free Software Foundation, Inc.,
19  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21 #ifndef LIBREPORT_DUMP_DIR_H_
22 #define LIBREPORT_DUMP_DIR_H_
23 
24 /* For const_string_vector_const_ptr_t */
25 #include "libreport_types.h"
26 
27 #include <stdint.h>
28 
29 /* For DIR */
30 #include <sys/types.h>
31 #include <dirent.h>
32 
33 /* For 'struct stat' */
34 #include <sys/stat.h>
35 
36 /* Fore GList */
37 #include <glib.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /* Utility function */
44 int create_symlink_lockfile(const char *filename, const char *pid_str);
45 int create_symlink_lockfile_at(int dir_fd, const char *filename, const char *pid_str);
46 
47 /* Opens filename for reading relatively to a directory represented by dir_fd.
48  * The function fails if the file is symbolic link, directory or hard link.
49  */
50 int secure_openat_read(int dir_fd, const char *filename);
51 
52 /******************************************************************************/
53 /* Global variables */
54 /******************************************************************************/
55 
56 /* UID of super-user (default 0)
57  *
58  * This variable is used by the dd* functions when they access security
59  * sensitive elements. The functions will ONLY TRUST the contents of those
60  * elements that ARE OWNED by super-user.
61  */
62 extern uid_t dd_g_super_user_uid;
63 
64 /* GID of a dump diretory created via dd_create() with uid != -1
65  *
66  * The default value is -1 which means that the dd* functions must ignore this
67  * variable.
68  *
69  * Initialize this variable only if you don't want to use the default group
70  * ('abrt').
71  */
72 extern gid_t dd_g_fs_group_gid;
73 
74 /******************************************************************************/
75 /* Dump Directory */
76 /******************************************************************************/
77 
78 enum dump_dir_flags {
79  DD_FAIL_QUIETLY_ENOENT = (1 << 0),
80  DD_FAIL_QUIETLY_EACCES = (1 << 1),
81  /* Open symlinks. dd_* funcs don't open symlinks by default */
82  DD_OPEN_FOLLOW = (1 << 2),
83  DD_OPEN_READONLY = (1 << 3),
84  DD_LOAD_TEXT_RETURN_NULL_ON_FAILURE = (1 << 4),
85  DD_DONT_WAIT_FOR_LOCK = (1 << 5),
86  /* Create the new dump directory with parent directories (mkdir -p)*/
87  DD_CREATE_PARENTS = (1 << 6),
88  /* Initializes internal data, opens file descriptors and returns the
89  * structure. This flag is useful for testing whether a directory
90  * exists and to perform stat operations.
91  */
92  DD_OPEN_FD_ONLY = (1 << 7),
93 };
94 
95 struct dump_dir {
96  char *dd_dirname;
97  DIR *next_dir;
98  int locked;
99  uid_t dd_uid;
100  gid_t dd_gid;
101  /* mode of saved files */
102  mode_t mode;
103  time_t dd_time;
104  char *dd_type;
105 
106  /* In case of recursive locking the first caller owns the lock and is
107  * responsible for unlocking. The consecutive dd_lock() callers acquire the
108  * lock but are not able to unlock the dump directory.
109  */
110  int owns_lock;
111  int dd_fd;
112  /* Never use this member directly, it is intialized on demand in
113  * dd_get_meta_data_dir_fd()
114  */
115  int dd_md_fd;
116 };
117 
118 void dd_close(struct dump_dir *dd);
119 
120 /* Opens the given path
121  */
122 struct dump_dir *dd_opendir(const char *dir, int flags);
123 
124 /* Re-opens a dump_dir opened with DD_OPEN_FD_ONLY.
125  *
126  * The passed dump_dir must not be used any more and the return value must be
127  * used instead.
128  *
129  * The passed flags must not contain DD_OPEN_FD_ONLY.
130  *
131  * The passed dump_dir must not be already locked.
132  */
133 struct dump_dir *dd_fdopendir(struct dump_dir *dd, int flags);
134 
135 /* Creates a new directory with internal files
136  *
137  * The functions creates a new directory which remains owned by the user of the
138  * process until dd_reset_ownership() is called.
139  *
140  * The function logs error messages in case of errors.
141  *
142  * @param dir Full file system path of the new directory
143  * @param uid Desired file system owner of the new directory or -1 if the owner
144  * should stay untouched even after calling dd_reset_ownership().
145  * @param mode File system mode of the new directory.
146  * @param flags See 'enum dump_dir_flags'
147  * @return Initialized struct dump_dir of NULL
148  */
149 struct dump_dir *dd_create_skeleton(const char *dir, uid_t uid, mode_t mode, int flags);
150 
151 int dd_reset_ownership(struct dump_dir *dd);
152 
153 /* Pass uid = (uid_t)-1L to disable chown'ing of newly created files
154  * (IOW: if you aren't running under root):
155  */
156 struct dump_dir *dd_create(const char *dir, uid_t uid, mode_t mode);
157 
158 /* Creates the basic files except 'type' and sets the dump dir owner to passed
159  * 'uid'.
160  *
161  * The file 'type' is required and must be added with dd_save_text().
162  *
163  * If you want to have owner different than the problem 'uid', than pass -1 and
164  * add the file 'uid' with dd_save_text()
165  *
166  * List of created files:
167  * - time
168  * - last_occurrence
169  * - uid
170  * - kernel
171  * - architecture
172  * - hostname
173  * - os_info
174  * - os_release
175  *
176  * If any of these files has a counterpart in a chroot directory (os_info,
177  * os_relase), creates an element with the prefix "root_"
178  */
179 void dd_create_basic_files(struct dump_dir *dd, uid_t uid, const char *chroot_dir);
180 int dd_exist(const struct dump_dir *dd, const char *path);
181 void dd_sanitize_mode_and_owner(struct dump_dir *dd);
182 
183 /* Initializes an iterator going through all dump directory items.
184  *
185  * @returns NULL if the iterator cannot be initialized; otherwise returns
186  * the result of opendir(). Do not use the return value after the iteration is
187  * finished or after calling dd_clear_next_file().
188  */
189 DIR *dd_init_next_file(struct dump_dir *dd);
190 
191 /* Iterates over all dump directory item names
192  *
193  * Initialize the iterator by calling dd_init_next_file(). When iteration is
194  * finished, calls dd_clear_next_file().
195  *
196  * @returns 1 if the next item was read; otherwise return 0.
197  */
198 int dd_get_next_file(struct dump_dir *dd, char **short_name, char **full_name);
199 
200 /* Destroys the next file iterator and cleans dump directory internal structures
201  *
202  * Calling dd_get_next_file() after this function returns will return 0. This
203  * function also invalidates the return value of dd_init_next_file().
204  */
205 void dd_clear_next_file(struct dump_dir *dd);
206 
207 char *load_text_file(const char *path, unsigned flags);
208 
209 char* dd_load_text_ext(const struct dump_dir *dd, const char *name, unsigned flags);
210 char* dd_load_text(const struct dump_dir *dd, const char *name);
211 int dd_load_int32(const struct dump_dir *dd, const char *name, int32_t *value);
212 int dd_load_uint32(const struct dump_dir *dd, const char *name, uint32_t *value);
213 int dd_load_int64(const struct dump_dir *dd, const char *name, int64_t *value);
214 int dd_load_uint64(const struct dump_dir *dd, const char *name, uint64_t *value);
215 
216 /* Returns value of environment variable with given name.
217  *
218  * @param dd Dump directory
219  * @param name Variables's name
220  * @param value Return value.
221  * @return 0 no success, or negative value if an error occurred (-ENOENT if the
222  * given dd does not support environment variables).
223  */
224 int dd_get_env_variable(struct dump_dir *dd, const char *name, char **value);
225 
226 void dd_save_text(struct dump_dir *dd, const char *name, const char *data);
227 void dd_save_binary(struct dump_dir *dd, const char *name, const char *data, unsigned size);
228 int dd_copy_file(struct dump_dir *dd, const char *name, const char *source_path);
229 int dd_copy_file_unpack(struct dump_dir *dd, const char *name, const char *source_path);
230 
231 /* Creates/overwrites an element with data read from a file descriptor
232  *
233  * @param dd Dump directory
234  * @param name The name of the element
235  * @param fd The file descriptor
236  * @param flags libreport_copyfd_flags
237  * @param maxsize Limit for number of written Bytes. (0 for unlimited).
238  * @return Number of read Bytes. If the return value is greater than the maxsize
239  * the file descriptor content was truncated to the maxsize. The return value
240  * is not size of the file descriptor.
241  */
242 off_t dd_copy_fd(struct dump_dir *dd, const char *name, int fd, int copy_flags, off_t maxsize);
243 
244 /* Stats dump dir elements
245  *
246  * @param dd Dump Directory
247  * @param name The name of the element
248  * @param statbuf See 'man 2 stat'
249  * @return -EINVAL if name is invalid element name, -EMEDIUMTYPE if name is not
250  * regular file, -errno on errors and 0 on success.
251  */
252 int dd_item_stat(struct dump_dir *dd, const char *name, struct stat *statbuf);
253 
254 /* Returns value less than 0 if any error occured; otherwise returns size of an
255  * item in Bytes. If an item does not exist returns 0 instead of an error
256  * value.
257  */
258 long dd_get_item_size(struct dump_dir *dd, const char *name);
259 
260 /* Returns the number of items in the dump directory (does not count meta-data).
261  *
262  * @return Negative number on errors (-errno). Otherwise number of dump
263  * directory items.
264  */
265 int dd_get_items_count(struct dump_dir *dd);
266 
267 /* Deletes an item from dump directory
268  * On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
269  * For more about errno see unlink documentation
270  */
271 int dd_delete_item(struct dump_dir *dd, const char *name);
272 /* Returns 0 if directory is deleted or not found */
273 int dd_delete(struct dump_dir *dd);
274 int dd_rename(struct dump_dir *dd, const char *new_path);
275 /* Changes owner of dump dir
276  * Uses two different strategies selected at build time by
277  * DUMP_DIR_OWNED_BY_USER configuration:
278  * <= 0 : owner = abrt user's uid, group = new_uid's gid
279  * > 0 : owner = new_uid, group = abrt group's gid
280  *
281  * On success, zero is returned. On error, -1 is returned.
282  */
283 int dd_chown(struct dump_dir *dd, uid_t new_uid);
284 
285 /* Returns the number of Bytes consumed by the dump directory.
286  *
287  * @param flags For the future needs (count also meta-data, ...).
288  * @return Negative number on errors (-errno). Otherwise size in Bytes.
289  */
290 off_t dd_compute_size(struct dump_dir *dd, int flags);
291 
292 /* Sets a new owner (does NOT chown the directory)
293  *
294  * Does not validate the passed uid.
295  * The given dump_dir must be opened for writing.
296  */
297 int dd_set_owner(struct dump_dir *dd, uid_t owner);
298 
299 /* Makes the dump directory owned by nobody.
300  *
301  * The directory will be accessible for all users.
302  * The given dump_dir must be opened for writing.
303  */
304 int dd_set_no_owner(struct dump_dir *dd);
305 
306 /* Gets the owner
307  *
308  * If meta-data misses owner, returns fs owner.
309  * Can be used with DD_OPEN_FD_ONLY.
310  */
311 uid_t dd_get_owner(struct dump_dir *dd);
312 
313 /* Returns UNIX time stamp of the first occurrence of the problem.
314  *
315  * @param dd Examined dump directory
316  * @returns On success, the value of time of the first occurrence in seconds
317  * since the Epoch is returned. On error, ((time_t) -1) is returned, and errno
318  * is set appropriately (ENODATA).
319  */
320 time_t dd_get_first_occurrence(struct dump_dir *dd);
321 
322 /* Returns UNIX time stamp of the last occurrence of the problem.
323  *
324  * @param dd Examined dump directory
325  * @returns The returned value is never lower than the value returned by
326  * dd_get_first_occurrence(). On success, the value of time of the first
327  * occurrence in seconds since the Epoch is returned.On error, ((time_t) -1) is
328  * returned, and errno is set appropriately (ENODATA).
329  */
330 time_t dd_get_last_occurrence(struct dump_dir *dd);
331 
332 /* reported_to handling */
334  char *label;
335  char *url;
336  char *msg;
337  char *bthash;
338  time_t timestamp;
339  /* ^^^ if you add more fields, don't forget to update free_report_result() */
340 };
341 typedef struct report_result report_result_t;
342 
343 /* Appends a new unique line to the list of report results
344  *
345  * If the reported_to data already contains the given line, the line will not
346  * be added again.
347  *
348  * @param reported_to The data
349  * @param line The appended line
350  * @return 1 if the line was added at the end of the reported_to; otherwise 0.
351  */
352 #define add_reported_to_data libreport_add_reported_to_data
353 int add_reported_to_data(char **reported_to, const char *line);
354 
355 /* Appends a new unique entry to the list of report results
356  *
357  * result->label must be non-empty string which does not contain ':' character.
358  *
359  * The function converts the result to a valid reported_to line and calls
360  * add_reported_to_data().
361  *
362  * @param reported_to The data
363  * @param result The appended entry
364  * @return -EINVAL if result->label is invalid; otherwise return value of
365  * add_reported_to_data
366  */
367 #define add_reported_to_entry_data libreport_add_reported_to_entry_data
368 int add_reported_to_entry_data(char **reported_to, struct report_result *result);
369 
370 /* This is a wrapper of add_reported_to_data which accepts 'struct dump_dir *'
371  * in the first argument instead of 'char **'. The added line is stored in
372  * 'reported_to' dump directory file.
373  */
374 #define add_reported_to libreport_add_reported_to
375 void add_reported_to(struct dump_dir *dd, const char *line);
376 
377 /* This is a wrapper of add_reported_to_entry_data which accepts 'struct
378  * dump_dir *' in the first argument instead of 'char **'. The added entry is
379  * stored in 'reported_to' dump directory file.
380  */
381 #define add_reported_to_entry libreport_add_reported_to_entry
382 void add_reported_to_entry(struct dump_dir *dd, struct report_result *result);
383 
384 #define free_report_result libreport_free_report_result
385 void free_report_result(struct report_result *result);
386 #define find_in_reported_to_data libreport_find_in_reported_to_data
387 report_result_t *find_in_reported_to_data(const char *reported_to, const char *report_label);
388 #define find_in_reported_to libreport_find_in_reported_to
389 report_result_t *find_in_reported_to(struct dump_dir *dd, const char *report_label);
390 #define read_entire_reported_to_data libreport_read_entire_reported_to_data
391 GList *read_entire_reported_to_data(const char* reported_to);
392 #define read_entire_reported_to libreport_read_entire_reported_to
393 GList *read_entire_reported_to(struct dump_dir *dd);
394 
395 
396 void delete_dump_dir(const char *dirname);
397 /* Checks dump dir accessibility for particular uid.
398  *
399  * If the directory doesn't exist the directory is not accessible and errno is
400  * set to ENOTDIR.
401  *
402  * Returns non zero if dump dir is accessible otherwise return 0 value.
403  */
404 int dump_dir_accessible_by_uid(const char *dirname, uid_t uid);
405 /* Returns the same information as dump_dir_accessible_by_uid
406  *
407  * The passed dump_dir can be opened with DD_OPEN_FD_ONLY
408  */
409 int dd_accessible_by_uid(struct dump_dir *dd, uid_t uid);
410 
411 enum {
412  DD_STAT_ACCESSIBLE_BY_UID = 1,
413  DD_STAT_OWNED_BY_UID = DD_STAT_ACCESSIBLE_BY_UID << 1,
414  DD_STAT_NO_OWNER = DD_STAT_OWNED_BY_UID << 1,
415 };
416 
417 /* Gets information about a dump directory for particular uid.
418  *
419  * If the directory doesn't exist the directory is not accessible and errno is
420  * set to ENOTDIR.
421  *
422  * Returns negative number if error occurred otherwise returns 0 or positive number.
423  */
424 int dump_dir_stat_for_uid(const char *dirname, uid_t uid);
425 /* Returns the same information as dump_dir_stat_for_uid
426  *
427  * The passed dump_dir can be opened with DD_OPEN_FD_ONLY
428  */
429 int dd_stat_for_uid(struct dump_dir *dd, uid_t uid);
430 
431 /* creates not_reportable file in the problem directory and saves the
432  reason to it, which prevents libreport from reporting the problem
433  On success, zero is returned.
434  On error, -1 is returned and an error message is logged.
435  - this could probably happen only if the dump dir is not locked
436 */
437 int dd_mark_as_notreportable(struct dump_dir *dd, const char *reason);
438 
439 typedef int (*save_data_call_back)(struct dump_dir *, void *args);
440 
441 /* Saves data in a new dump directory
442  *
443  * Creates a new dump directory in "problem dump location", adds the basic
444  * information to the new directory, calls given callback to allow callees to
445  * customize the dump dir contents (save problem data) and commits the dump
446  * directory (makes the directory visible for a problem daemon).
447  */
448 struct dump_dir *create_dump_dir(const char *base_dir_name, const char *type,
449  uid_t uid, save_data_call_back save_data, void *args);
450 
451 /* Creates a new archive from the dump directory contents
452  *
453  * The dd argument must be opened for reading.
454  *
455  * The archive_name must not exist. The file will be created with 0600 mode.
456  *
457  * The archive type is deduced from archive_name suffix. The supported archive
458  * suffixes are the following:
459  * - '.tag.gz' (note: the implementation uses child gzip process)
460  *
461  * The archive will include only the files that are not in the exclude_elements
462  * list. See get_global_always_excluded_elements().
463  *
464  * The argument "flags" is currently unused.
465  *
466  * @return 0 on success; otherwise non-0 value. -ENOSYS if archive type is not
467  * supported. -EEXIST if the archive file already exists. -ECHILD if child
468  * process fails. Other negative values can be converted to errno values by
469  * turning them positive.
470  */
471 int dd_create_archive(struct dump_dir *dd, const char *archive_name,
472  const_string_vector_const_ptr_t exclude_elements, int flags);
473 
474 #ifdef __cplusplus
475 }
476 #endif
477 
478 #endif