]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/tools/bsmtp.c
ebl Update SQLite for Long term statistics
[bacula/bacula] / bacula / src / tools / bsmtp.c
index 75cbdcdbfbd7813e9aba08b21a5bb4653664f506..0188cedab3e7c14fe45138b9096510c81ddb9478 100644 (file)
@@ -1,32 +1,74 @@
 /*
-  Copyright (C) 2001-2006 Kern Sibbald
-
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License
-   version 2 as amended with additional clauses defined in the
-   file LICENSE in the main source directory.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
-   the file LICENSE for additional details.
-
- */
+   Bacula® - The Network Backup Solution
+
+   Copyright (C) 2001-2007 Free Software Foundation Europe e.V.
+
+   The main author of Bacula is Kern Sibbald, with contributions from
+   many others, a complete list can be found in the file AUTHORS.
+   This program is Free Software; you can redistribute it and/or
+   modify it under the terms of version two of the GNU General Public
+   License as published by the Free Software Foundation and included
+   in the file LICENSE.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
+
+   Bacula® is a registered trademark of John Walker.
+   The licensor of Bacula is the Free Software Foundation Europe
+   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
+   Switzerland, email:ftf@fsfeurope.org.
+*/
 /*
    Derived from a SMTPclient:
 
+  ======== Original copyrights ==========  
+
        SMTPclient -- simple SMTP client
 
-       Copyright (C) 1997 Ralf S. Engelschall, All Rights Reserved.
-       rse@engelschall.com
-       www.engelschall.com
+       Copyright (c) 1997 Ralf S. Engelschall, All rights reserved.
+
+       This program is free software; it may be redistributed and/or modified
+       only under the terms of either the Artistic License or the GNU General
+       Public License, which may be found in the SMTP source distribution.
+       Look at the file COPYING.
+
+       This program is distributed in the hope that it will be useful, but
+       WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+       GNU General Public License for more details.
+
+       ======================================================================
+
+       smtpclient_main.c -- program source
+
+       Based on smtp.c as of August 11, 1995 from
+           W.Z. Venema,
+           Eindhoven University of Technology,
+           Department of Mathematics and Computer Science,
+           Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands.
+
+   =========
+
 
    Kern Sibbald, July 2001
 
+     Note, the original W.Z. Venema smtp.c had no license and no
+     copyright.  See:
+        http://archives.neohapsis.com/archives/postfix/2000-05/1520.html
+
    Version $Id$
 
  */
 
+
 #include "bacula.h"
 #include "jcr.h"
 #define MY_NAME "bsmtp"
@@ -54,14 +96,39 @@ static const char *mailhost = NULL;
 static char *reply_addr = NULL;
 static int mailport = 25;
 static char my_hostname[MAXSTRING];
+static bool content_utf8 = false;
+
+/* 
+ * Take input that may have names and other stuff and strip
+ *  it down to the mail box address ... i.e. what is enclosed
+ *  in < >.  Otherwise add < >.
+ */
+static char *cleanup_addr(char *addr, char *buf, int buf_len)
+{
+   char *p, *q;
 
+   if ((p = strchr(addr, '<')) == NULL) {
+      snprintf(buf, buf_len, "<%s>", addr);
+   } else {
+      /* Copy <addr> */
+      for (q=buf; *p && *p!='>'; ) {
+         *q++ = *p++;
+      }
+      if (*p) {
+         *q++ = *p;
+      }
+      *q = 0;
+  }
+  Dmsg2(100, "cleanup in=%s out=%s\n", addr, buf);
+  return buf;    
+}
 
 /*
  *  examine message from server
  */
 static void get_response(void)
 {
-    char buf[MAXSTRING];
+    char buf[1000];
 
     Dmsg0(50, "Calling fgets on read socket rfp.\n");
     buf[3] = 0;
@@ -97,11 +164,13 @@ static void chat(const char *fmt, ...)
 
     va_start(ap, fmt);
     vfprintf(sfp, fmt, ap);
+    va_end(ap);
     if (debug_level >= 10) {
        fprintf(stdout, "%s --> ", my_hostname);
+       va_start(ap, fmt);
        vfprintf(stdout, fmt, ap);
+       va_end(ap);
     }
-    va_end(ap);
 
     fflush(sfp);
     if (debug_level >= 10) {
@@ -116,8 +185,10 @@ static void usage()
    fprintf(stderr,
 _("\n"
 "Usage: %s [-f from] [-h mailhost] [-s subject] [-c copy] [recipient ...]\n"
+"       -8          set charset utf-8\n"
 "       -c          set the Cc: field\n"
-"       -dnn        set debug level to nn\n"
+"       -d <nn>     set debug level to <nn>\n"
+"       -dt         print timestamp in debug output\n"
 "       -f          set the From: field\n"
 "       -h          use mailhost:port as the SMTP server\n"
 "       -s          set the Subject: field\n"
@@ -129,6 +200,57 @@ _("\n"
    exit(1);
 }
 
+/*
+ * Return the offset west from localtime to UTC in minutes
+  * Same as timezone.tz_minuteswest
+  *   Unix tz_offset coded by:  Attila Fülöp
+  */
+
+static long tz_offset(time_t lnow, struct tm &tm)
+{
+#if defined(HAVE_WIN32)
+#if defined(HAVE_MINGW)
+__MINGW_IMPORT long     _dstbias;
+#endif
+
+   /* Win32 code */
+   long offset;
+   _tzset();
+   offset = _timezone;
+   if (tm.tm_isdst) {
+      offset += _dstbias;
+   }
+   return offset /= 60;
+#else
+
+   /* Unix/Linux code */
+   struct tm tm_utc;
+   time_t now = lnow;
+
+   (void)gmtime_r(&now, &tm_utc);
+   tm_utc.tm_isdst = tm.tm_isdst;
+   return (long)difftime(mktime(&tm_utc), now) / 60;
+#endif
+}
+
+static void get_date_string(char *buf, int buf_len)
+{
+   time_t now = time(NULL);
+   struct tm tm;
+   char tzbuf[MAXSTRING];
+   long my_timezone;
+
+   /* Add RFC822 date */
+   (void)localtime_r(&now, &tm);
+
+   my_timezone = tz_offset(now, tm);
+   strftime(buf, buf_len, "%a, %d %b %Y %H:%M:%S", &tm);
+   sprintf(tzbuf, " %+2.2ld%2.2u", -my_timezone / 60, abs(my_timezone) % 60);
+   strcat(buf, tzbuf);              /* add +0100 */
+   strftime(tzbuf, sizeof(tzbuf), " (%Z)", &tm);
+   strcat(buf, tzbuf);              /* add (CEST) */
+}
+
 
 /*********************************************************************
  *
@@ -136,7 +258,7 @@ _("\n"
  */
 int main (int argc, char *argv[])
 {
-    char buf[MAXSTRING];
+    char buf[1000];
     struct sockaddr_in sin;
     struct hostent *hp;
     int i, ch;
@@ -148,8 +270,6 @@ int main (int argc, char *argv[])
     struct passwd *pwd;
 #endif
     char *cp, *p;
-    time_t now = time(NULL);
-    struct tm tm;
     
    setlocale(LC_ALL, "en_US");
    bindtextdomain("bacula", LOCALEDIR);
@@ -158,17 +278,24 @@ int main (int argc, char *argv[])
    my_name_is(argc, argv, "bsmtp");
    maxlines = 0;
 
-   while ((ch = getopt(argc, argv, "c:d:f:h:r:s:l:?")) != -1) {
+   while ((ch = getopt(argc, argv, "8c:d:f:h:r:s:l:?")) != -1) {
       switch (ch) {
+      case '8':
+         content_utf8 = true;
+         break;
       case 'c':
          Dmsg1(20, "cc=%s\n", optarg);
          cc_addr = optarg;
          break;
 
       case 'd':                    /* set debug level */
-         debug_level = atoi(optarg);
-         if (debug_level <= 0) {
-            debug_level = 1;
+         if (*optarg == 't') {
+            dbg_timestamp = true;
+         } else {
+            debug_level = atoi(optarg);
+            if (debug_level <= 0) {
+               debug_level = 1;
+            }
          }
          Dmsg1(20, "Debug level = %d\n", debug_level);
          break;
@@ -216,6 +343,7 @@ int main (int argc, char *argv[])
       exit(1);
    }
 
+
    /*
     *  Determine SMTP server
     */
@@ -230,6 +358,7 @@ int main (int argc, char *argv[])
 #if defined(HAVE_WIN32)
    WSADATA  wsaData;
 
+   _setmode(0, _O_BINARY);
    WSAStartup(MAKEWORD(2,2), &wsaData);
 #endif
 
@@ -314,7 +443,7 @@ hp:
    Dmsg0(20, "Connected\n");
 
 #if defined(HAVE_WIN32)
-   int fdSocket = _open_osfhandle(s, _O_RDWR);
+   int fdSocket = _open_osfhandle(s, _O_RDWR | _O_BINARY);
    if (fdSocket == -1) {
       Pmsg1(0, _("Fatal _open_osfhandle error: ERR=%s\n"), strerror(errno));
       exit(1);
@@ -346,19 +475,21 @@ hp:
 #endif
 
    /*
-    *  Send SMTP headers
+    *  Send SMTP headers.  Note, if any of the strings have a <
+    *   in them already, we do not enclose the string in < >, otherwise
+    *   we do.
     */
    get_response(); /* banner */
    chat("helo %s\r\n", my_hostname);
-   chat("mail from:<%s>\r\n", from_addr);
-
+   chat("mail from:%s\r\n", cleanup_addr(from_addr, buf, sizeof(buf)));
+   
    for (i = 0; i < argc; i++) {
       Dmsg1(20, "rcpt to: %s\n", argv[i]);
-      chat("rcpt to:<%s>\r\n", argv[i]);
+      chat("rcpt to:%s\r\n", cleanup_addr(argv[i], buf, sizeof(buf)));
    }
 
    if (cc_addr) {
-      chat("rcpt to:<%s>\r\n", cc_addr);
+      chat("rcpt to:%s\r\n", cleanup_addr(cc_addr, buf, sizeof(buf)));
    }
    Dmsg0(20, "Data\n");
    chat("data\r\n");
@@ -416,25 +547,12 @@ hp:
       Dmsg1(10, "Cc: %s\r\n", cc_addr);
    }
 
-   /* Add RFC822 date */
-   (void)localtime_r(&now, &tm);
-#if defined(HAVE_WIN32)
-#if defined(HAVE_MINGW)
-__MINGW_IMPORT long    _dstbias;
-#endif
-   long tzoffset = 0;
-
-   _tzset();
-
-   tzoffset = _timezone;
-   tzoffset += _dstbias;
-   tzoffset /= 60;
+   if (content_utf8) {
+      fprintf(sfp, "Content-Type: text/plain; charset=UTF-8\r\n");
+      Dmsg0(10, "Content-Type: text/plain; charset=UTF-8\r\n");
+   }
 
-   size_t length = strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S", &tm);
-   sprintf(&buf[length], " %+2.2ld%2.2u", -tzoffset / 60, abs(tzoffset) % 60);
-#else
-   strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S %z", &tm);
-#endif
+   get_date_string(buf, sizeof(buf));
    fprintf(sfp, "Date: %s\r\n", buf);
    Dmsg1(10, "Date: %s\r\n", buf);
 
@@ -449,11 +567,13 @@ __MINGW_IMPORT long       _dstbias;
          Dmsg1(20, "skip line because of maxlines limit: %lu\n", maxlines);
          break;
       }
-      buf[strlen(buf)-1] = 0;
-      if (strcmp(buf, ".") == 0) { /* quote lone dots */
-         fprintf(sfp, "..\r\n");
+      buf[sizeof(buf)-1] = '\0';
+      buf[strlen(buf)-1] = '\0';
+      if (buf[0] == '.' && buf[1] == '\0') { /* quote lone dots */
+         fputs("..\r\n", sfp);
       } else {                     /* pass body through unchanged */
-         fprintf(sfp, "%s\r\n", buf);
+         fputs(buf, sfp);
+         fputs("\r\n", sfp);
       }
    }