Initial commit for Tizen
[profile/extras/shadow-utils.git] / libmisc / audit_help.c
1 /*
2  * Copyright (c) 2005       , Red Hat, Inc.
3  * Copyright (c) 2005       , Tomasz Kłoczko
4  * Copyright (c) 2008       , Nicolas François
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the copyright holders or contributors may not be used to
16  *    endorse or promote products derived from this software without
17  *    specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
23  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 /*
33  *  Audit helper functions used throughout shadow
34  *
35  */
36
37 #include <config.h>
38
39 #ifdef WITH_AUDIT
40
41 #include <stdlib.h>
42 #include <syslog.h>
43 #include <stdarg.h>
44 #include <libaudit.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include "prototypes.h"
48 int audit_fd;
49
50 void audit_help_open (void)
51 {
52         audit_fd = audit_open ();
53         if (audit_fd < 0) {
54                 /* You get these only when the kernel doesn't have
55                  * audit compiled in. */
56                 if (   (errno == EINVAL)
57                     || (errno == EPROTONOSUPPORT)
58                     || (errno == EAFNOSUPPORT)) {
59                         return;
60                 }
61                 (void) fputs (_("Cannot open audit interface - aborting.\n"),
62                               stderr);
63                 exit (EXIT_FAILURE);
64         }
65 }
66
67 /*
68  * This function will log a message to the audit system using a predefined
69  * message format. Parameter usage is as follows:
70  *
71  * type - type of message: AUDIT_USER_CHAUTHTOK for changing any account 
72  *        attributes.
73  * pgname - program's name
74  * op  -  operation. "adding user", "changing finger info", "deleting group"
75  * name - user's account or group name. If not available use NULL.
76  * id  -  uid or gid that the operation is being performed on. This is used
77  *        only when user is NULL.
78  */
79 void audit_logger (int type, const char *pgname, const char *op,
80                    const char *name, unsigned int id,
81                    shadow_audit_result result)
82 {
83         if (audit_fd < 0) {
84                 return;
85         } else {
86                 audit_log_acct_message (audit_fd, type, NULL, op, name, id,
87                                         NULL, NULL, NULL, (int) result);
88         }
89 }
90
91 void audit_logger_message (const char *message, shadow_audit_result result)
92 {
93         if (audit_fd < 0) {
94                 return;
95         } else {
96                 audit_log_user_message (audit_fd,
97                                         AUDIT_USYS_CONFIG,
98                                         message,
99                                         NULL, /* hostname */
100                                         NULL, /* addr */
101                                         NULL, /* tty */
102                                         (int) result);
103         }
104 }
105
106 #else                           /* WITH_AUDIT */
107 extern int errno;       /* warning: ANSI C forbids an empty source file */
108 #endif                          /* WITH_AUDIT */
109