2 Copyright (C) 2001-2006 Kern Sibbald
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as amended with additional clauses defined in the
7 file LICENSE in the main source directory.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 the file LICENSE for additional details.
16 Derived from a SMTPclient:
18 SMTPclient -- simple SMTP client
20 Copyright (C) 1997 Ralf S. Engelschall, All Rights Reserved.
24 Kern Sibbald, July 2001
35 #define bstrdup(x) strdup(x)
37 UPSINFO *core_ups = &myUPS;
38 #define MY_NAME "smtp"
44 #define MY_NAME "bsmtp"
49 int generate_daemon_event(JCR *jcr, const char *event)
59 static char *from_addr = NULL;
60 static char *cc_addr = NULL;
61 static char *subject = NULL;
62 static char *err_addr = NULL;
63 static const char *mailhost = NULL;
64 static char *reply_addr = NULL;
65 static int mailport = 25;
66 static char my_hostname[MAXSTRING];
70 * examine message from server
72 static void get_response(void)
76 Dmsg0(50, "Calling fgets on read socket rfp.\n");
78 while (fgets(buf, sizeof(buf), rfp)) {
79 int len = strlen(buf);
83 Dmsg2(10, "%s --> %s\n", mailhost, buf);
84 if (!isdigit((int)buf[0]) || buf[0] > '3') {
85 Pmsg2(0, _("Fatal malformed reply from %s: %s\n"), mailhost, buf);
96 * say something to server and check the response
98 static void chat(const char *fmt, ...)
103 vfprintf(sfp, fmt, ap);
104 if (debug_level >= 10) {
105 fprintf(stdout, "%s --> ", my_hostname);
106 vfprintf(stdout, fmt, ap);
111 if (debug_level >= 10) {
122 "Usage: %s [-f from] [-h mailhost] [-s subject] [-c copy] [recipient ...]\n"
123 " -c set the Cc: field\n"
124 " -dnn set debug level to nn\n"
125 " -f set the From: field\n"
126 " -h use mailhost:port as the SMTP server\n"
127 " -s set the Subject: field\n"
128 " -? print this message.\n"
135 /*********************************************************************
137 * Program to send email
139 int main (int argc, char *argv[])
142 struct sockaddr_in sin;
147 time_t now = time(NULL);
150 setlocale(LC_ALL, "en_US");
151 bindtextdomain("bacula", LOCALEDIR);
152 textdomain("bacula");
154 my_name_is(argc, argv, "bsmtp");
156 while ((ch = getopt(argc, argv, "c:d:f:h:r:s:?")) != -1) {
159 Dmsg1(20, "cc=%s\n", optarg);
163 case 'd': /* set debug level */
164 debug_level = atoi(optarg);
165 if (debug_level <= 0) {
168 Dmsg1(20, "Debug level = %d\n", debug_level);
175 case 'h': /* smtp host */
176 Dmsg1(20, "host=%s\n", optarg);
177 p = strchr(optarg, ':');
185 case 's': /* subject */
186 Dmsg1(20, "subject=%s\n", optarg);
190 case 'r': /* reply address */
204 Pmsg0(0, _("Fatal error: no recipient given.\n"));
210 * Determine SMTP server
212 if (mailhost == NULL) {
213 if ((cp = getenv("SMTPSERVER")) != NULL) {
216 mailhost = "localhost";
221 * Find out my own host name for HELO;
222 * if possible, get the fully qualified domain name
224 if (gethostname(my_hostname, sizeof(my_hostname) - 1) < 0) {
225 Pmsg1(0, _("Fatal gethostname error: ERR=%s\n"), strerror(errno));
228 if ((hp = gethostbyname(my_hostname)) == NULL) {
229 Pmsg2(0, _("Fatal gethostbyname for myself failed \"%s\": ERR=%s\n"), my_hostname,
233 strcpy(my_hostname, hp->h_name);
234 Dmsg1(20, "My hostname is: %s\n", my_hostname);
237 * Determine from address.
239 if (from_addr == NULL) {
240 if ((pwd = getpwuid(getuid())) == 0) {
241 sprintf(buf, "userid-%d@%s", (int)getuid(), my_hostname);
243 sprintf(buf, "%s@%s", pwd->pw_name, my_hostname);
245 from_addr = bstrdup(buf);
247 Dmsg1(20, "From addr=%s\n", from_addr);
250 * Connect to smtp daemon on mailhost.
253 if ((hp = gethostbyname(mailhost)) == NULL) {
254 Pmsg2(0, _("Error unknown mail host \"%s\": ERR=%s\n"), mailhost,
256 if (strcasecmp(mailhost, "localhost") != 0) {
257 Pmsg0(0, _("Retrying connection using \"localhost\".\n"));
258 mailhost = "localhost";
264 if (hp->h_addrtype != AF_INET) {
265 Pmsg1(0, _("Fatal error: Unknown address family for smtp host: %d\n"), hp->h_addrtype);
268 memset((char *)&sin, 0, sizeof(sin));
269 memcpy((char *)&sin.sin_addr, hp->h_addr, hp->h_length);
270 sin.sin_family = hp->h_addrtype;
271 sin.sin_port = htons(mailport);
272 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
273 Pmsg1(0, _("Fatal socket error: ERR=%s\n"), strerror(errno));
276 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
277 Pmsg2(0, _("Fatal connect error to %s: ERR=%s\n"), mailhost, strerror(errno));
280 Dmsg0(20, "Connected\n");
281 if ((r = dup(s)) < 0) {
282 Pmsg1(0, _("Fatal dup error: ERR=%s\n"), strerror(errno));
285 if ((sfp = fdopen(s, "w")) == 0) {
286 Pmsg1(0, _("Fatal fdopen error: ERR=%s\n"), strerror(errno));
289 if ((rfp = fdopen(r, "r")) == 0) {
290 Pmsg1(0, _("Fatal fdopen error: ERR=%s\n"), strerror(errno));
297 get_response(); /* banner */
298 chat("helo %s\r\n", my_hostname);
299 chat("mail from:<%s>\r\n", from_addr);
301 for (i = 0; i < argc; i++) {
302 Dmsg1(20, "rcpt to: %s\n", argv[i]);
303 chat("rcpt to:<%s>\r\n", argv[i]);
307 chat("rcpt to:<%s>\r\n", cc_addr);
313 * Send message header
315 fprintf(sfp, "From: %s\r\n", from_addr);
316 Dmsg1(10, "From: %s\r\n", from_addr);
318 fprintf(sfp, "Subject: %s\r\n", subject);
319 Dmsg1(10, "Subject: %s\r\n", subject);
322 fprintf(sfp, "Reply-To: %s\r\n", reply_addr);
323 Dmsg1(10, "Reply-To: %s\r\n", reply_addr);
326 fprintf(sfp, "Errors-To: %s\r\n", err_addr);
327 Dmsg1(10, "Errors-To: %s\r\n", err_addr);
329 if ((pwd = getpwuid(getuid())) == 0) {
330 fprintf(sfp, "Sender: userid-%d@%s\r\n", (int)getuid(), my_hostname);
331 Dmsg2(10, "Sender: userid-%d@%s\r\n", (int)getuid(), my_hostname);
333 fprintf(sfp, "Sender: %s@%s\r\n", pwd->pw_name, my_hostname);
334 Dmsg2(10, "Sender: %s@%s\r\n", pwd->pw_name, my_hostname);
337 fprintf(sfp, "To: %s", argv[0]);
338 Dmsg1(10, "To: %s", argv[0]);
339 for (i = 1; i < argc; i++) {
340 fprintf(sfp, ",%s", argv[i]);
341 Dmsg1(10, ",%s", argv[i]);
344 fprintf(sfp, "\r\n");
347 fprintf(sfp, "Cc: %s\r\n", cc_addr);
348 Dmsg1(10, "Cc: %s\r\n", cc_addr);
351 /* Add RFC822 date */
352 localtime_r(&now, &tm);
353 strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S %z", &tm);
354 fprintf(sfp, "Date: %s\r\n", buf);
355 Dmsg1(10, "Date: %s\r\n", buf);
357 fprintf(sfp, "\r\n");
362 while (fgets(buf, sizeof(buf), stdin)) {
363 buf[strlen(buf)-1] = 0;
364 if (strcmp(buf, ".") == 0) { /* quote lone dots */
365 fprintf(sfp, "..\r\n");
366 } else { /* pass body through unchanged */
367 fprintf(sfp, "%s\r\n", buf);
372 * Send SMTP quit command
378 * Go away gracefully ...