]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/parse_conf.c
kes Add dynamic dll entry point for SHGetFolderPath to Win32 code.
[bacula/bacula] / bacula / src / lib / parse_conf.c
index 41177e10479a36d0b293e535e1089f74386a477c..1e432a640efe04f3d3e880b109b7ee73cc5f9648 100755 (executable)
  *   Version $Id$
  */
 /*
-   Copyright (C) 2000-2005 Kern Sibbald
+   Bacula® - The Network Backup Solution
 
-   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 ammended with additional clauses defined in the
-   file LICENSE in the main source directory.
+   Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
 
-   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.
+   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 plus additions
+   that are listed 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.
+*/
 
 
 #include "bacula.h"
 
-extern int debug_level;
+#if defined(HAVE_WIN32)
+#include "shlobj.h"
+#else
+#define MAX_PATH  1024
+#endif
 
 /* Each daemon has a slightly different set of
  * resources, so it will define the following
@@ -61,22 +78,22 @@ extern int r_last;
 extern RES_TABLE resources[];
 extern RES **res_head;
 
-#ifdef HAVE_WIN32
-// work around visual studio name manling preventing external linkage since res_all
+#if defined(_MSC_VER)
+// work around visual studio name mangling preventing external linkage since res_all
 // is declared as a different type when instantiated.
 extern "C" CURES res_all;
-extern "C" int res_all_size;
 #else
 extern  CURES res_all;
-extern int res_all_size;
 #endif
+extern int res_all_size;
 
 extern brwlock_t res_lock;            /* resource lock */
 
 
 /* Forward referenced subroutines */
 static void scan_types(LEX *lc, MSGS *msg, int dest, char *where, char *cmd);
-
+static const char *get_default_configdir();
+static bool find_config_file(const char *config_file, char *full_path);
 
 /* Common Resource definitions */
 
@@ -91,6 +108,7 @@ RES_ITEM msgs_items[] = {
    {"syslog",      store_msgs, ITEM(res_msgs), MD_SYSLOG,   0, 0},
    {"mail",        store_msgs, ITEM(res_msgs), MD_MAIL,     0, 0},
    {"mailonerror", store_msgs, ITEM(res_msgs), MD_MAIL_ON_ERROR, 0, 0},
+   {"mailonsuccess", store_msgs, ITEM(res_msgs), MD_MAIL_ON_SUCCESS, 0, 0},
    {"file",        store_msgs, ITEM(res_msgs), MD_FILE,     0, 0},
    {"append",      store_msgs, ITEM(res_msgs), MD_APPEND,   0, 0},
    {"stdout",      store_msgs, ITEM(res_msgs), MD_STDOUT,   0, 0},
@@ -98,7 +116,8 @@ RES_ITEM msgs_items[] = {
    {"director",    store_msgs, ITEM(res_msgs), MD_DIRECTOR, 0, 0},
    {"console",     store_msgs, ITEM(res_msgs), MD_CONSOLE,  0, 0},
    {"operator",    store_msgs, ITEM(res_msgs), MD_OPERATOR, 0, 0},
-   {NULL, NULL,    NULL,       0,              0}
+   {"catalog",     store_msgs, ITEM(res_msgs), MD_CATALOG,  0, 0},
+   {NULL,          NULL,       {0},       0, 0, 0}
 };
 
 struct s_mtypes {
@@ -121,6 +140,7 @@ static struct s_mtypes msg_types[] = {
    {"restored",      M_RESTORED},
    {"security",      M_SECURITY},
    {"alert",         M_ALERT},
+   {"volmgmt",       M_VOLMGMT},
    {"all",           M_MAX+1},
    {NULL,            0}
 };
@@ -136,7 +156,7 @@ struct s_kw {
  *
  *   tape label      label code = token
  */
-struct s_kw tapelabels[] = {
+static s_kw tapelabels[] = {
    {"bacula",        B_BACULA_LABEL},
    {"ansi",          B_ANSI_LABEL},
    {"ibm",           B_IBM_LABEL},
@@ -185,13 +205,16 @@ void init_resource(int type, RES_ITEM *items, int pass)
    res_all.hdr.rcode = type;
    res_all.hdr.refcnt = 1;
 
+   /* Set defaults in each item */
    for (i=0; items[i].name; i++) {
       Dmsg3(900, "Item=%s def=%s defval=%d\n", items[i].name,
             (items[i].flags & ITEM_DEFAULT) ? "yes" : "no",
             items[i].default_value);
       if (items[i].flags & ITEM_DEFAULT && items[i].default_value != 0) {
-         if (items[i].handler == store_yesno) {
+         if (items[i].handler == store_bit) {
             *(int *)(items[i].value) |= items[i].code;
+         } else if (items[i].handler == store_bool) {
+            *(bool *)(items[i].value) = items[i].default_value != 0;
          } else if (items[i].handler == store_pint ||
                     items[i].handler == store_int) {
             *(int *)(items[i].value) = items[i].default_value;
@@ -228,12 +251,14 @@ void store_msgs(LEX *lc, RES_ITEM *item, int index, int pass)
       case MD_STDERR:
       case MD_SYSLOG:              /* syslog */
       case MD_CONSOLE:
+      case MD_CATALOG:
          scan_types(lc, (MSGS *)(item->value), item->code, NULL, NULL);
          break;
       case MD_OPERATOR:            /* send to operator */
       case MD_DIRECTOR:            /* send to Director */
       case MD_MAIL:                /* mail */
       case MD_MAIL_ON_ERROR:       /* mail if Job errors */
+      case MD_MAIL_ON_SUCCESS:     /* mail if Job succeeds */
          if (item->code == MD_OPERATOR) {
             cmd = res_all.res_msgs.operator_cmd;
          } else {
@@ -302,24 +327,25 @@ void store_msgs(LEX *lc, RES_ITEM *item, int index, int pass)
  */
 static void scan_types(LEX *lc, MSGS *msg, int dest_code, char *where, char *cmd)
 {
-   int i, found, quit, is_not;
+   int i; 
+   bool found, is_not;
    int msg_type = 0;
    char *str;
 
-   for (quit=0; !quit;) {
+   for ( ;; ) {
       lex_get_token(lc, T_NAME);            /* expect at least one type */
-      found = FALSE;
+      found = false;
       if (lc->str[0] == '!') {
-         is_not = TRUE;
+         is_not = true;
          str = &lc->str[1];
       } else {
-         is_not = FALSE;
+         is_not = false;
          str = &lc->str[0];
       }
       for (i=0; msg_types[i].name; i++) {
          if (strcasecmp(str, msg_types[i].name) == 0) {
             msg_type = msg_types[i].token;
-            found = TRUE;
+            found = true;
             break;
          }
       }
@@ -332,12 +358,10 @@ static void scan_types(LEX *lc, MSGS *msg, int dest_code, char *where, char *cmd
          for (i=1; i<=M_MAX; i++) {      /* yes set all types */
             add_msg_dest(msg, dest_code, i, where, cmd);
          }
+      } else if (is_not) {
+         rem_msg_dest(msg, dest_code, msg_type, where);
       } else {
-         if (is_not) {
-            rem_msg_dest(msg, dest_code, msg_type, where);
-         } else {
-            add_msg_dest(msg, dest_code, msg_type, where, cmd);
-         }
+         add_msg_dest(msg, dest_code, msg_type, where, cmd);
       }
       if (lc->ch != ',') {
          break;
@@ -422,7 +446,7 @@ void store_password(LEX *lc, RES_ITEM *item, int index, int pass)
 {
    unsigned int i, j;
    struct MD5Context md5c;
-   unsigned char signature[16];
+   unsigned char digest[CRYPTO_DIGEST_MD5_SIZE];
    char sig[100];
 
 
@@ -430,9 +454,9 @@ void store_password(LEX *lc, RES_ITEM *item, int index, int pass)
    if (pass == 1) {
       MD5Init(&md5c);
       MD5Update(&md5c, (unsigned char *) (lc->str), lc->str_len);
-      MD5Final(signature, &md5c);
-      for (i = j = 0; i < sizeof(signature); i++) {
-         sprintf(&sig[j], "%02x", signature[i]);
+      MD5Final(digest, &md5c);
+      for (i = j = 0; i < sizeof(digest); i++) {
+         sprintf(&sig[j], "%02x", digest[i]);
          j += 2;
       }
       *(item->value) = bstrdup(sig);
@@ -568,17 +592,6 @@ void store_defs(LEX *lc, RES_ITEM *item, int index, int pass)
         scan_err3(lc, _("Missing config Resource \"%s\" referenced on line %d : %s\n"),
            lc->str, lc->line_no, lc->line);
      }
-     /* for each item not set, we copy the field from res */
-#ifdef xxx
-     for (int i=0; item->name;; i++, item++) {
-        if (bit_is_set(i, res->item_present)) {
-           Dmsg2(900, "Item %d is present in %s\n", i, res->name);
-        } else {
-           Dmsg2(900, "Item %d is not present in %s\n", i, res->name);
-        }
-     }
-     /* ***FIXME **** add code */
-#endif
    }
    scan_to_eol(lc);
 }
@@ -629,9 +642,10 @@ void store_size(LEX *lc, RES_ITEM *item, int index, int pass)
    case T_UNQUOTED_STRING:
       bstrncpy(bsize, lc->str, sizeof(bsize));  /* save first part */
       /* if terminated by space, scan and get modifier */
-      if (lc->ch == ' ') {
+      while (lc->ch == ' ') {
          token = lex_get_token(lc, T_ALL);
          switch (token) {
+         case T_NUMBER:
          case T_IDENTIFIER:
          case T_UNQUOTED_STRING:
             bstrncat(bsize, lc->str, sizeof(bsize));
@@ -647,7 +661,9 @@ void store_size(LEX *lc, RES_ITEM *item, int index, int pass)
       scan_err1(lc, _("expected a size, got: %s"), lc->str);
       break;
    }
-   scan_to_eol(lc);
+   if (token != T_EOL) {
+      scan_to_eol(lc);
+   }
    set_bit(index, res_all.hdr.item_present);
    Dmsg0(900, "Leave store_size\n");
 }
@@ -668,9 +684,10 @@ void store_time(LEX *lc, RES_ITEM *item, int index, int pass)
    case T_UNQUOTED_STRING:
       bstrncpy(period, lc->str, sizeof(period));  /* get first part */
       /* if terminated by space, scan and get modifier */
-      if (lc->ch == ' ') {
+      while (lc->ch == ' ') {
          token = lex_get_token(lc, T_ALL);
          switch (token) {
+         case T_NUMBER:
          case T_IDENTIFIER:
          case T_UNQUOTED_STRING:
             bstrncat(period, lc->str, sizeof(period));
@@ -694,20 +711,36 @@ void store_time(LEX *lc, RES_ITEM *item, int index, int pass)
 
 
 /* Store a yes/no in a bit field */
-void store_yesno(LEX *lc, RES_ITEM *item, int index, int pass)
+void store_bit(LEX *lc, RES_ITEM *item, int index, int pass)
 {
    lex_get_token(lc, T_NAME);
-   if (strcasecmp(lc->str, "yes") == 0) {
+   if (strcasecmp(lc->str, "yes") == 0 || strcasecmp(lc->str, "true") == 0) {
       *(int *)(item->value) |= item->code;
-   } else if (strcasecmp(lc->str, "no") == 0) {
+   } else if (strcasecmp(lc->str, "no") == 0 || strcasecmp(lc->str, "false") == 0) {
       *(int *)(item->value) &= ~(item->code);
    } else {
-      scan_err1(lc, _("Expect a YES or NO, got: %s"), lc->str);
+      scan_err2(lc, _("Expect %s, got: %s"), "YES, NO, TRUE, or FALSE", lc->str); /* YES and NO must not be translated */
    }
    scan_to_eol(lc);
    set_bit(index, res_all.hdr.item_present);
 }
 
+/* Store a bool in a bit field */
+void store_bool(LEX *lc, RES_ITEM *item, int index, int pass)
+{
+   lex_get_token(lc, T_NAME);
+   if (strcasecmp(lc->str, "yes") == 0 || strcasecmp(lc->str, "true") == 0) {
+      *(bool *)(item->value) = true;
+   } else if (strcasecmp(lc->str, "no") == 0 || strcasecmp(lc->str, "false") == 0) {
+      *(bool *)(item->value) = false;
+   } else {
+      scan_err2(lc, _("Expect %s, got: %s"), "YES, NO, TRUE, or FALSE", lc->str); /* YES and NO must not be translated */
+   }
+   scan_to_eol(lc);
+   set_bit(index, res_all.hdr.item_present);
+}
+
+
 /*
  * Store Tape Label Type (Bacula, ANSI, IBM)
  *
@@ -726,7 +759,7 @@ void store_label(LEX *lc, RES_ITEM *item, int index, int pass)
       }
    }
    if (i != 0) {
-      scan_err1(lc, "Expected a Tape Label keyword, got: %s", lc->str);
+      scan_err1(lc, _("Expected a Tape Label keyword, got: %s"), lc->str);
    }
    scan_to_eol(lc);
    set_bit(index, res_all.hdr.item_present);
@@ -748,7 +781,7 @@ enum parse_state {
  *  scan_error handler is to die on an error.
  */
 int
-parse_config(const char *cf, LEX_ERROR_HANDLER *scan_error)
+parse_config(const char *cf, LEX_ERROR_HANDLER *scan_error, int err_type)
 {
    LEX *lc = NULL;
    int token, i, pass;
@@ -757,6 +790,12 @@ parse_config(const char *cf, LEX_ERROR_HANDLER *scan_error)
    RES_ITEM *items = NULL;
    int level = 0;
 
+   char *full_path = (char *)alloca(MAX_PATH);
+
+   if (find_config_file(cf, full_path)) {
+      cf = full_path;
+   }
+
    /* Make two passes. The first builds the name symbol table,
     * and the second picks up the items.
     */
@@ -773,6 +812,7 @@ parse_config(const char *cf, LEX_ERROR_HANDLER *scan_error)
          } else {
             lex_set_default_error_handler(lc);
          }
+         lex_set_error_handler_error_type(lc, err_type) ;
          bstrncpy(lc->str, cf, sizeof(lc->str));
          lc->fname = lc->str;
          scan_err2(lc, _("Cannot open config file \"%s\": %s\n"),
@@ -780,6 +820,7 @@ parse_config(const char *cf, LEX_ERROR_HANDLER *scan_error)
          free(lc);
          return 0;
       }
+      lex_set_error_handler_error_type(lc, err_type) ;
       while ((token=lex_get_token(lc, T_ALL)) != T_EOF) {
          Dmsg1(900, "parse got token=%s\n", lex_tok_to_str(token));
          switch (state) {
@@ -787,6 +828,9 @@ parse_config(const char *cf, LEX_ERROR_HANDLER *scan_error)
             if (token == T_EOL) {
                break;
             }
+            if (token == T_UNICODE_MARK) {
+               break;
+            }
             if (token != T_IDENTIFIER) {
                scan_err1(lc, _("Expected a Resource name identifier, got: %s"), lc->str);
                return 0;
@@ -801,7 +845,7 @@ parse_config(const char *cf, LEX_ERROR_HANDLER *scan_error)
                }
             if (state == p_none) {
                scan_err1(lc, _("expected resource name, got: %s"), lc->str);
-          return 0;
+               return 0;
             }
             break;
          case p_resource:
@@ -826,7 +870,7 @@ parse_config(const char *cf, LEX_ERROR_HANDLER *scan_error)
                            return 0;
                         }
                      }
-                     Dmsg1(900, "calling handler for %s\n", items[i].name);
+                     Dmsg1(800, "calling handler for %s\n", items[i].name);
                      /* Call item handler */
                      items[i].handler(lc, &items[i], i, pass);
                      i = -1;
@@ -846,6 +890,9 @@ parse_config(const char *cf, LEX_ERROR_HANDLER *scan_error)
                level--;
                state = p_none;
                Dmsg0(900, "T_EOB => define new resource\n");
+               if (res_all.hdr.name == NULL) {
+                  scan_err0(lc, _("Name not specified for resource"));
+               }
                save_resource(res_type, items, pass);  /* save resource */
                break;
 
@@ -879,6 +926,64 @@ parse_config(const char *cf, LEX_ERROR_HANDLER *scan_error)
    return 1;
 }
 
+const char *get_default_configdir()
+{
+#if defined(HAVE_WIN32)
+   HRESULT hr;
+   static char szConfigDir[MAX_PATH + 1] = { 0 };
+
+   if (!p_SHGetFolderPath) {
+      bstrncpy(szConfigDir, DEFAULT_CONFIGDIR, sizeof(szConfigDir));
+      return szConfigDir;
+   }
+
+   if (szConfigDir[0] == '\0') {
+      hr = p_SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 0, szConfigDir);
+
+      if (SUCCEEDED(hr)) {
+         bstrncat(szConfigDir, "\\Bacula", sizeof(szConfigDir));
+      } else {
+         bstrncpy(szConfigDir, DEFAULT_CONFIGDIR, sizeof(szConfigDir));
+      }
+   }
+   return szConfigDir;
+#else
+   return SYSCONFDIR;
+#endif
+}
+
+bool
+find_config_file(const char *config_file, char *full_path)
+{
+   if (first_path_separator(config_file) != NULL) {
+      return false;
+   }
+
+   struct stat st;
+
+   if (stat(config_file, &st) == 0) {
+      return false;
+   }
+
+   const char *config_dir = get_default_configdir();
+   size_t dir_length = strlen(config_dir);
+   size_t file_length = strlen(config_file);
+
+   if ((dir_length + 1 + file_length + 1) > MAX_PATH) {
+      return false;
+   }
+
+   memcpy(full_path, config_dir, dir_length + 1);
+
+   if (!IsPathSeparator(full_path[dir_length - 1])) {
+      full_path[dir_length++] = '/';
+   }
+
+   memcpy(&full_path[dir_length], config_file, file_length + 1);
+
+   return true;
+}
+
 /*********************************************************************
  *
  *      Free configuration resources