notify_email plugin: Replace all sprintf's with ssnprintf's.
[collectd.git] / src / notify_email.c
1 /**
2  * collectd - src/notify_email.c
3  * Copyright (C) 2008  Oleg King
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Oleg King <king2 at kaluga.ru>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26
27 #include <auth-client.h>
28 #include <libesmtp.h>
29
30 #define MAXSTRING               256
31
32 static const char *config_keys[] =
33 {
34   "SMTPHost",
35   "SMTPPort",
36   "SMTPUser",
37   "SMTPPassword",
38   "SMTPFrom",
39   "SMTPSubject",
40   "EmailTo"
41 };
42 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
43
44 static char **emails;
45 static int emails_len = 0;
46
47 static smtp_session_t session;
48 static smtp_message_t message;
49 static auth_context_t authctx = NULL;
50
51 static int smtp_port = 25;
52 static char *smtp_host = NULL;
53 static char *smtp_user = NULL;
54 static char *smtp_password = NULL;
55 static char *smtp_from = NULL;
56 static char *smtp_subject = NULL;
57
58 #define DEFAULT_SMTP_HOST       "localhost"
59 #define DEFAULT_SMTP_FROM       "root@localhost"
60 #define DEFAULT_SMTP_SUBJECT    "Collectd notify: %s@%s"
61
62
63 /* Callback to get username and password */
64 static int authinteract (auth_client_request_t request, char **result, int fields, void *arg)
65 {               
66   int i;
67   for (i = 0; i < fields; i++)
68   {
69     if (request[i].flags & AUTH_USER)
70       result[i] = smtp_user;
71     else if (request[i].flags & AUTH_PASS)
72       result[i] = smtp_password;
73     else
74       return 0;
75   }
76   return 1;
77 }
78
79 /* Callback to print the recipient status */
80 static void print_recipient_status (smtp_recipient_t recipient, const char *mailbox, void *arg)
81 {
82   const smtp_status_t *status;
83
84   status = smtp_recipient_status (recipient);
85   if (status->text[strlen(status->text) - 2] == '\r')
86     status->text[strlen(status->text) - 2] = 0;
87   INFO ("notify_email: notify sent to %s: %d %s", mailbox, status->code, status->text);
88 }
89
90 /* Callback to monitor SMTP activity */
91 static void monitor_cb (const char *buf, int buflen, int writing, void *arg)
92 {
93   char log_str[MAXSTRING];
94
95   sstrncpy (log_str, buf, sizeof (log_str));
96   if (buflen > 2)
97     log_str[buflen - 2] = 0; /* replace \n with \0 */
98
99   if (writing == SMTP_CB_HEADERS) {
100     DEBUG ("SMTP --- H: %s", log_str);
101     return;
102   }
103   DEBUG (writing ? "SMTP >>> C: %s" : "SMTP <<< S: %s", log_str);
104 }
105
106 static int notify_email_init()
107 {
108   char server[MAXSTRING];
109
110   auth_client_init();
111   if ( !(session = smtp_create_session()) ) {
112     ERROR ("notify_email plugin: cannot create SMTP session");
113     return (-1);
114   }
115
116   smtp_set_monitorcb (session, monitor_cb, NULL, 1);
117   smtp_set_hostname (session, hostname_g);
118   ssnprintf(server, sizeof (server), "%s:%i",
119       (smtp_host == NULL) ? DEFAULT_SMTP_HOST : smtp_host,
120       smtp_port);
121   smtp_set_server (session, server);
122
123   if (smtp_user && smtp_password) {
124     authctx = auth_create_context ();
125     auth_set_mechanism_flags (authctx, AUTH_PLUGIN_PLAIN, 0);
126     auth_set_interact_cb (authctx, authinteract, NULL);
127   }
128
129   if ( !smtp_auth_set_context (session, authctx)) {
130     ERROR ("notify_email plugin: cannot set SMTP auth context");
131     return (-1);   
132   }
133
134   return (0);
135 }
136
137
138 static int notify_email_shutdown()
139 {
140   smtp_destroy_session (session);
141   auth_destroy_context (authctx);
142   auth_client_exit();
143   return (0);
144 }
145
146
147 static int notify_email_config (const char *key, const char *value)
148 {
149   if (strcasecmp (key, "EmailTo") == 0)
150   {
151     char **tmp;
152
153     tmp = (char **) realloc ((void *) emails, (emails_len + 1) * sizeof (char *));
154     if (tmp == NULL) {
155       ERROR ("notify_email: realloc failed.");
156       return (-1);
157     }
158
159     emails = tmp;
160     emails[emails_len] = strdup (value);
161     if (emails[emails_len] == NULL) {
162       ERROR ("notify_email: strdup failed.");
163       return (-1);
164     }
165     emails_len++;
166   }
167   else if (0 == strcasecmp (key, "SMTPHost")) {
168     sfree (smtp_host);
169     smtp_host = strdup (value);
170   }
171   else if (0 == strcasecmp (key, "SMTPPort")) {
172     int port_tmp = atoi (value);
173     if (port_tmp < 1 || port_tmp > 65535)
174     {
175       WARNING ("notify_email plugin: Invalid SMTP port: %i", port_tmp);
176       return (1);
177     }
178     smtp_port = port_tmp;
179   }
180   else if (0 == strcasecmp (key, "SMTPUser")) {
181     sfree (smtp_user);
182     smtp_user = strdup (value);
183   }
184   else if (0 == strcasecmp (key, "SMTPPassword")) {
185     sfree (smtp_password);
186     smtp_password = strdup (value);
187   }
188   else if (0 == strcasecmp (key, "SMTPFrom")) {
189     sfree (smtp_from);
190     smtp_from = strdup (value);
191   }
192   else if (0 == strcasecmp (key, "SMTPSubject")) {
193     sfree (smtp_subject);
194     smtp_subject = strdup (value);
195   }
196   else {
197     return -1;
198   }
199   return 0;
200 } /* int notify_email_config (const char *, const char *) */
201
202
203 static int notify_email_notification (const notification_t *n)
204 {
205   smtp_recipient_t recipient;
206
207   struct tm timestamp_tm;
208   char timestamp_str[64];
209
210   char severity[32];
211   char subject[MAXSTRING];
212
213   char buf[4096] = "";
214   int  buf_len = sizeof (buf);
215   int i;
216
217   ssnprintf (severity, sizeof (severity), "%s",
218       (n->severity == NOTIF_FAILURE) ? "FAILURE"
219       : ((n->severity == NOTIF_WARNING) ? "WARNING"
220         : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")));
221
222   ssnprintf (subject, sizeof (subject),
223       (smtp_subject == NULL) ? DEFAULT_SMTP_SUBJECT : smtp_subject,
224       severity, n->host);
225
226   localtime_r (&n->time, &timestamp_tm);
227   strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S", &timestamp_tm);
228   timestamp_str[sizeof (timestamp_str) - 1] = '\0';
229
230   /* Let's make RFC822 message text with \r\n EOLs */
231   ssnprintf (buf, buf_len,
232       "MIME-Version: 1.0\r\n"
233       "Content-Type: text/plain;\r\n"
234       "Content-Transfer-Encoding: 8bit\r\n"
235       "Subject: %s\r\n"
236       "\r\n"
237       "%s - %s@%s\r\n"
238       "\r\n"
239       "Message: %s",
240       subject,
241       timestamp_str,
242       severity,
243       n->host,
244       n->message);
245
246   if ( !(message = smtp_add_message (session))) {
247     ERROR ("notify_email plugin: cannot set SMTP message");
248     return (-1);   
249   }
250   smtp_set_reverse_path (message, smtp_from);
251   smtp_set_header (message, "To", NULL, NULL);
252   smtp_set_message_str (message, buf);
253
254   for (i = 0; i < emails_len; i++)
255     recipient = smtp_add_recipient (message, emails[i]);
256
257   /* Initiate a connection to the SMTP server and transfer the message. */
258   if (!smtp_start_session (session)) {
259     char buf[MAXSTRING];
260     ERROR ("SMTP server problem: %s", smtp_strerror (smtp_errno (), buf, sizeof buf));
261     return (-1);
262   } else {
263     const smtp_status_t *status;
264     /* Report on the success or otherwise of the mail transfer. */
265     status = smtp_message_transfer_status (message);
266     DEBUG ("SMTP server report: %d %s", status->code, (status->text != NULL) ? status->text : "\n");
267     smtp_enumerate_recipients (message, print_recipient_status, NULL);
268   }
269
270   return (0);
271 } /* int notify_email_notification */
272
273 void module_register (void)
274 {
275   plugin_register_init ("notify_email", notify_email_init);
276   plugin_register_shutdown ("notify_email", notify_email_shutdown);
277   plugin_register_config ("notify_email", notify_email_config,
278       config_keys, config_keys_num);
279   plugin_register_notification ("notify_email", notify_email_notification);
280 } /* void module_register (void) */