]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bsmtp.c
Add Exclude to new FileSet
[bacula/bacula] / bacula / src / tools / bsmtp.c
1 /*
2    Copyright (C) 2000-2004 Kern Sibbald and John Walker
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of
7    the License, or (at your option) any later version.
8
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 GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public
15    License along with this program; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
17    MA 02111-1307, USA.
18
19  */
20
21 /*
22    Derived from a SMTPclient:
23
24        SMTPclient -- simple SMTP client
25
26        Copyright (C) 1997 Ralf S. Engelschall, All Rights Reserved.
27        rse@engelschall.com
28        www.engelschall.com
29
30    Kern Sibbald, July 2001
31   
32    Version $Id$
33     
34  */
35
36 #ifdef APCUPSD
37
38 #include "apc.h"
39 #undef main
40 #define my_name_is(x)
41 #define bstrdup(x) strdup(x)
42 UPSINFO myUPS;
43 UPSINFO *core_ups = &myUPS;
44 #define MY_NAME "smtp"
45
46 #else
47
48 #include "bacula.h"
49 #include "jcr.h"
50 #define MY_NAME "bsmtp"
51
52 #endif
53
54
55 #ifndef MAXSTRING
56 #define MAXSTRING 254
57 #endif
58
59 static FILE *sfp;
60 static FILE *rfp;
61
62 static char *from_addr = NULL;
63 static char *cc_addr = NULL;
64 static char *subject = NULL;
65 static char *err_addr = NULL;
66 static char *mailhost = NULL;
67 static char *reply_addr = NULL;
68 static int mailport = 25;
69 static char my_hostname[MAXSTRING];
70
71
72 /*
73  *  examine message from server 
74  */
75 static void get_response(void)
76 {
77     char buf[MAXSTRING];
78
79     Dmsg0(50, "Calling fgets on read socket rfp.\n");
80     while (fgets(buf, sizeof(buf), rfp)) {
81         buf[strlen(buf)-1] = 0;
82         Dmsg2(10, "%s --> %s\n", mailhost, buf);
83         if (!isdigit((int)buf[0]) || buf[0] > '3') {
84             Pmsg2(0, "Fatal malformed reply from %s: %s\n", mailhost, buf);
85             exit(1);
86         }
87         if (buf[3] != '-') {
88             break;
89         }
90     }
91     return;
92 }
93
94 /*
95  *  say something to server and check the response
96  */
97 static void chat(char *fmt, ...)
98 {
99     va_list ap;
100
101     va_start(ap, fmt);
102     vfprintf(sfp, fmt, ap);
103     if (debug_level >= 10) {
104        fprintf(stdout, "%s --> ", my_hostname); 
105        vfprintf(stdout, fmt, ap);
106     }
107     va_end(ap);
108   
109     fflush(sfp);
110     if (debug_level >= 10) {
111        fflush(stdout);
112     }
113     get_response();
114 }
115
116
117 static void usage()
118 {
119    fprintf(stderr,
120 "\n"
121 "Usage: %s [-f from] [-h mailhost] [-s subject] [-c copy] [recepient ...]\n"
122 "       -c          set the Cc: field\n"
123 "       -dnn        set debug level to nn\n"
124 "       -f          set the From: field\n"
125 "       -h          use mailhost:port as the SMTP server\n"
126 "       -s          set the Subject: field\n"
127 "       -?          print this message.\n"  
128 "\n", MY_NAME);
129
130    exit(1);
131 }
132
133
134 /*********************************************************************
135  *
136  *  Program to send email
137  */
138 int main (int argc, char *argv[])
139 {
140     char buf[MAXSTRING];
141     struct sockaddr_in sin;
142     struct hostent *hp;
143     int s, r, i, ch;
144     struct passwd *pwd;
145     char *cp, *p;
146     time_t now = time(NULL);
147     struct tm tm;
148
149    my_name_is(argc, argv, "bsmtp");
150
151    while ((ch = getopt(argc, argv, "c:d:f:h:r:s:?")) != -1) {
152       switch (ch) {
153       case 'c':                    
154          Dmsg1(20, "cc=%s\n", optarg);
155          cc_addr = optarg;
156          break;
157
158       case 'd':                    /* set debug level */
159          debug_level = atoi(optarg);
160          if (debug_level <= 0) {
161             debug_level = 1; 
162          }
163          Dmsg1(20, "Debug level = %d\n", debug_level);
164          break;
165
166       case 'f':                    /* from */
167          from_addr = optarg;
168          break;
169
170       case 'h':                    /* smtp host */
171          Dmsg1(20, "host=%s\n", optarg);
172          p = strchr(optarg, ':');
173          if (p) {
174             *p++ = 0;
175             mailport = atoi(p);
176          }
177          mailhost = optarg;
178          break;
179
180       case 's':                    /* subject */
181          Dmsg1(20, "subject=%s\n", optarg);
182          subject = optarg;
183          break;
184
185       case 'r':                    /* reply address */
186          reply_addr = optarg;
187          break;
188
189       case '?':
190       default:
191          usage();
192
193       }  
194    }
195    argc -= optind;
196    argv += optind;
197
198    if (argc < 1) {
199       Pmsg0(0, "Fatal error: no recipient given.\n");
200       usage();
201       exit(1);
202    }
203
204    /*
205     *  Determine SMTP server
206     */
207    if (mailhost == NULL) {
208       if ((cp = getenv("SMTPSERVER")) != NULL) {
209          mailhost = cp;
210       } else {
211          mailhost = "localhost";
212       }
213    }
214
215    /*
216     *  Find out my own host name for HELO; 
217     *  if possible, get the fully qualified domain name
218     */
219    if (gethostname(my_hostname, sizeof(my_hostname) - 1) < 0) {
220       Pmsg1(0, "Fatal gethostname error: ERR=%s\n", strerror(errno));
221       exit(1);
222    }
223    if ((hp = gethostbyname(my_hostname)) == NULL) {
224       Pmsg2(0, "Fatal gethostbyname for myself failed \"%s\": ERR=%s\n", my_hostname,
225          strerror(errno));
226       exit(1);
227    }
228    strcpy(my_hostname, hp->h_name);
229    Dmsg1(20, "My hostname is: %s\n", my_hostname);
230
231    /*
232     *  Determine from address.
233     */
234    if (from_addr == NULL) {
235       if ((pwd = getpwuid(getuid())) == 0) {
236          sprintf(buf, "userid-%d@%s", (int)getuid(), my_hostname);
237       } else {
238          sprintf(buf, "%s@%s", pwd->pw_name, my_hostname);
239       }
240       from_addr = bstrdup(buf);
241    }
242    Dmsg1(20, "From addr=%s\n", from_addr);
243
244    /*
245     *  Connect to smtp daemon on mailhost.
246     */
247 hp:
248    if ((hp = gethostbyname(mailhost)) == NULL) {
249       Pmsg2(0, "Error unknown mail host \"%s\": ERR=%s\n", mailhost,
250          strerror(errno));
251       if (strcasecmp(mailhost, "localhost") != 0) {
252          Pmsg0(0, "Retrying connection using \"localhost\".\n");
253          mailhost = "localhost";
254          goto hp;
255       }
256       exit(1);
257    }
258
259    if (hp->h_addrtype != AF_INET) {
260       Pmsg1(0, "Fatal error: Unknown address family for smtp host: %d\n", hp->h_addrtype);
261       exit(1);
262    }
263    memset((char *)&sin, 0, sizeof(sin));
264    memcpy((char *)&sin.sin_addr, hp->h_addr, hp->h_length);
265    sin.sin_family = hp->h_addrtype;
266    sin.sin_port = htons(mailport);
267    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
268       Pmsg1(0, "Fatal socket error: ERR=%s\n", strerror(errno));
269       exit(1);
270    }
271    if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
272       Pmsg2(0, "Fatal connect error to %s: ERR=%s\n", mailhost, strerror(errno));
273       exit(1);
274    }
275    Dmsg0(20, "Connected\n");
276    if ((r = dup(s)) < 0) {
277       Pmsg1(0, "Fatal dup error: ERR=%s\n", strerror(errno));
278       exit(1);
279    }
280    if ((sfp = fdopen(s, "w")) == 0) {
281       Pmsg1(0, "Fatal fdopen error: ERR=%s\n", strerror(errno));
282       exit(1);
283    }
284    if ((rfp = fdopen(r, "r")) == 0) {
285       Pmsg1(0, "Fatal fdopen error: ERR=%s\n", strerror(errno));
286       exit(1);
287    }
288
289    /* 
290     *  Send SMTP headers
291     */
292    get_response(); /* banner */
293    chat("helo %s\r\n", my_hostname);
294    chat("mail from:<%s>\r\n", from_addr);
295
296    for (i = 0; i < argc; i++) {
297       Dmsg1(20, "rcpt to: %s\n", argv[i]);
298       chat("rcpt to:<%s>\r\n", argv[i]);
299    }
300
301    if (cc_addr) {
302       chat("rcpt to:<%s>\r\n", cc_addr);
303    }
304    Dmsg0(20, "Data\n");
305    chat("data\r\n");
306
307    /* 
308     *  Send message header
309     */
310    fprintf(sfp, "From: %s\r\n", from_addr);
311    if (subject) {
312       fprintf(sfp, "Subject: %s\r\n", subject);
313    }
314    if (reply_addr) {
315       fprintf(sfp, "Reply-To: %s\r\n", reply_addr);
316    }
317    if (err_addr) {
318       fprintf(sfp, "Errors-To: %s\r\n", err_addr);
319    }
320    if ((pwd = getpwuid(getuid())) == 0) {
321       fprintf(sfp, "Sender: userid-%d@%s\r\n", (int)getuid(), my_hostname);
322    } else {
323       fprintf(sfp, "Sender: %s@%s\r\n", pwd->pw_name, my_hostname);
324    }
325
326    fprintf(sfp, "To: %s", argv[0]);
327    for (i = 1; i < argc; i++) {
328       fprintf(sfp, ",%s", argv[i]);
329    }
330
331    fprintf(sfp, "\r\n");
332    if (cc_addr) {
333       fprintf(sfp, "Cc: %s\r\n", cc_addr);
334    }
335
336    /* Add RFC822 date */
337    localtime_r(&now, &tm);
338    strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S %z", &tm);
339    fprintf(sfp, "Date: %s\r\n", buf);
340
341    fprintf(sfp, "\r\n");
342
343    /* 
344     *  Send message body 
345     */
346    while (fgets(buf, sizeof(buf), stdin)) {
347       buf[strlen(buf)-1] = 0;
348       if (strcmp(buf, ".") == 0) { /* quote lone dots */
349          fprintf(sfp, "..\r\n");
350       } else {                     /* pass body through unchanged */
351          fprintf(sfp, "%s\r\n", buf);
352       }
353    }
354
355    /* 
356     *  Send SMTP quit command
357     */
358    chat(".\r\n");
359    chat("quit\r\n");
360
361    /* 
362     *  Go away gracefully ...
363     */
364    exit(0);
365 }