1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
5    Copyright (C) 2011-2012 Planets Communications B.V.
6    Copyright (C) 2013-2018 Bareos GmbH & Co. KG
7 
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12 
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    Affero General Public License for more details.
17 
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22 */
23 /*
24  * Kern Sibbald, 2000
25  */
26 /**
27  * @file
28  * Define Message Types for BAREOS
29  */
30 #include "bits.h"
31 
32 #undef M_DEBUG
33 #undef M_ABORT
34 #undef M_FATAL
35 #undef M_ERROR
36 #undef M_WARNING
37 #undef M_INFO
38 #undef M_MOUNT
39 #undef M_ERROR_TERM
40 #undef M_TERM
41 #undef M_RESTORED
42 #undef M_SECURITY
43 #undef M_ALERT
44 #undef M_VOLMGMT
45 #undef M_AUDIT
46 
47 /**
48  * Most of these message levels are more or less obvious.
49  * They have evolved somewhat during the development of BAREOS,
50  * and here are some of the details of where I am trying to
51  * head (in the process of changing the code) as of 15 June 2002.
52  *
53  * M_ABORT       BAREOS immediately aborts and tries to produce a traceback
54  *               This is for really serious errors like segmentation fault.
55  * M_ERROR_TERM  BAREOS immediately terminates but no dump. This is for
56  *               "obvious" serious errors like daemon already running or
57  *               cannot open critical file, ... where a dump is not wanted.
58  * M_TERM        BAREOS daemon shutting down because of request (SIGTERM).
59  * M_DEBUG       Debug Messages
60  *
61  * The remaining apply to Jobs rather than the daemon.
62  *
63  * M_FATAL       BAREOS detected a fatal Job error. The Job will be killed,
64  *               but BAREOS continues running.
65  * M_ERROR       BAREOS detected a Job error. The Job will continue running
66  *               but the termination status will be error.
67  * M_WARNING     Job warning message.
68  * M_INFO        Job information message.
69  * M_SAVED       Info on saved file
70  * M_NOTSAVED    Info on not saved file
71  * M_RESTORED    An ls -l of each restored file.
72  * M_SKIPPED     File skipped during backup by option setting
73  * M_SECURITY    For security viloations. This is equivalent to FATAL.
74  * M_ALERT       For Tape Alert messages.
75  * M_VOLMGMT     Volume Management message
76  * M_AUDIT       Auditing message
77  * M_MOUNT       Mount requests
78  */
79 enum {
80    /*
81     * Keep M_ABORT=1 for dlist.h
82     */
83    M_ABORT = 1,
84    M_DEBUG,
85    M_FATAL,
86    M_ERROR,
87    M_WARNING,
88    M_INFO,
89    M_SAVED,
90    M_NOTSAVED,
91    M_SKIPPED,
92    M_MOUNT,
93    M_ERROR_TERM,
94    M_TERM,
95    M_RESTORED,
96    M_SECURITY,
97    M_ALERT,
98    M_VOLMGMT,
99    M_AUDIT
100 };
101 
102 #define M_MAX M_AUDIT                 /* keep this updated ! */
103 #define NR_MSG_TYPES NbytesForBits(M_MAX + 1)
104 
105 /**
106  * Define message destination structure
107  */
108 /* *** FIXME **** where should be extended to handle multiple values */
109 typedef struct s_dest {
110    struct s_dest *next;
111    int dest_code;                     /* Destination (one of the MD_ codes) */
112    int max_len;                       /* Max mail line length */
113    FILE *fd;                          /* File descriptor */
114    char msg_types[NR_MSG_TYPES];      /* Message type mask */
115    char *where;                       /* Filename/program name */
116    char *mail_cmd;                    /* Mail command */
117    char *timestamp_format;            /* Timestamp format to use in logging messages */
118    int syslog_facility;               /* Syslog Facility */
119    POOLMEM *mail_filename;            /* Unique mail filename */
120 } DEST;
121 
122 /**
123  * Message Destination values for dest field of DEST
124  *
125  * MD_SYSLOG          Send msg to syslog
126  * MD_MAIL            Email group of messages
127  * MD_FILE            Write messages to a file
128  * MD_APPEND          Append messages to a file
129  * MD_STDOUT          Print messages
130  * MD_STDERR          Print messages to stderr
131  * MD_DIRECTOR,       Send message to the Director
132  * MD_OPERATOR        Email a single message to the operator
133  * MD_CONSOLE         Send msg to UserAgent or console
134  * MD_MAIL_ON_ERROR   Email messages if job errors
135  * MD_MAIL_ON_SUCCESS Email messages if job succeeds
136  * MD_CATALOG
137  */
138 enum {
139    MD_SYSLOG = 1,
140    MD_MAIL,
141    MD_FILE,
142    MD_APPEND,
143    MD_STDOUT,
144    MD_STDERR,
145    MD_DIRECTOR,
146    MD_OPERATOR,
147    MD_CONSOLE,
148    MD_MAIL_ON_ERROR,
149    MD_MAIL_ON_SUCCESS,
150    MD_CATALOG
151 };
152 
153 /**
154  * Queued message item
155  */
156 struct MessageQeueItem {
157    dlink link;
158    int type;
159    utime_t mtime;
160    char msg[1];
161 };
162 
163 extern "C" {
164    typedef char *(*job_code_callback_t)(JobControlRecord *, const char *);
165 }
166 
167 void Jmsg(JobControlRecord *jcr, int type, utime_t mtime, const char *fmt,...);
168 void Qmsg(JobControlRecord *jcr, int type, utime_t mtime, const char *fmt,...);
169 bool GetTrace(void);
170 const char *get_basename(const char *pathname);
171 void SetLogTimestampFormat(const char *format);
172 
173 typedef bool (*db_log_insert_func)(JobControlRecord *jcr, utime_t mtime, char *msg);
174 extern db_log_insert_func p_db_log_insert;
175 
176 
177 class MessagesResource;
178 
179 extern int debug_level;
180 extern bool dbg_timestamp; /* print timestamp in debug output */
181 extern bool prt_kaboom;    /* Print kaboom output */
182 extern int verbose;
183 extern char my_name[];
184 extern const char *assert_msg; /* Assert error message */
185 extern const char *working_directory;
186 extern utime_t daemon_start_time;
187 
188 extern int console_msg_pending;
189 extern FILE *con_fd;       /* Console file descriptor */
190 extern brwlock_t con_lock; /* Console lock structure */
191 
192 void MyNameIs(int argc, char *argv[], const char *name);
193 void InitMsg(JobControlRecord *jcr, MessagesResource *msg, job_code_callback_t job_code_callback = NULL);
194 void TermMsg(void);
195 void CloseMsg(JobControlRecord *jcr);
196 void AddMsgDest(MessagesResource *msg, int dest, int type,
197                   char *where, char *mail_cmd, char *timestamp_format);
198 void RemMsgDest(MessagesResource *msg, int dest, int type, char *where);
199 void Jmsg(JobControlRecord *jcr, int type, utime_t mtime, const char *fmt, ...);
200 void DispatchMessage(JobControlRecord *jcr, int type, utime_t mtime, char *buf);
201 void InitConsoleMsg(const char *wd);
202 void FreeMsgsRes(MessagesResource *msgs);
203 void DequeueMessages(JobControlRecord *jcr);
204 void SetTrace(int trace_flag);
205 bool GetTrace(void);
206 void SetHangup(int hangup_value);
207 bool GetHangup(void);
208 void SetTimestamp(int timestamp_flag);
209 bool GetTimestamp(void);
210 void SetDbType(const char *name);
211 void RegisterMessageCallback(void msg_callback(int type, char *msg));
212