loadparm3: Add lp_wi_scan_global_parametrics()
authorVolker Lendecke <vl@samba.org>
Tue, 18 Aug 2015 11:18:33 +0000 (13:18 +0200)
committerVolker Lendecke <vl@samba.org>
Mon, 24 Aug 2015 14:16:11 +0000 (16:16 +0200)
This routine takes a regex and goes through all parametric parameters
in [global], matching the regex. It can easily be extended to also
look at shares, but right now it will only be used to list all idmap
config domain names.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
Bug: https://bugzilla.samba.org/show_bug.cgi?id=11464

source3/include/proto.h
source3/param/loadparm.c

index 0858289ad9f4ed1df5dda49d483822b44523454d..b8f4a670da0d623b4af54dc99f9d202d425660cf 100644 (file)
@@ -23,6 +23,9 @@
 #ifndef _PROTO_H_
 #define _PROTO_H_
 
+#include <sys/types.h>
+#include <regex.h>
+
 /* The following definitions come from lib/access.c  */
 
 bool client_match(const char *tok, const void *item);
@@ -986,6 +989,12 @@ int lp_smb2_max_credits(void);
 int lp_cups_encrypt(void);
 bool lp_widelinks(int );
 
+int lp_wi_scan_global_parametrics(
+       const char *regex, size_t max_matches,
+       bool (*cb)(const char *string, regmatch_t matches[],
+                  void *private_data),
+       void *private_data);
+
 char *lp_parm_talloc_string(TALLOC_CTX *ctx, int snum, const char *type, const char *option, const char *def);
 const char *lp_parm_const_string(int snum, const char *type, const char *option, const char *def);
 struct loadparm_service;
index 87e63e2f7654ac15902ae95d6708f6f46cd8944a..b24d198a1d926cfb4cf95b74f49ba537cdab5b3a 100644 (file)
@@ -1066,6 +1066,79 @@ static struct parmlist_entry *get_parametrics(int snum, const char *type,
        }
 }
 
+static void discard_whitespace(char *str)
+{
+       size_t len = strlen(str);
+       size_t i = 0;
+
+       while (i < len) {
+               if (isspace(str[i])) {
+                       memmove(&str[i], &str[i+1], len-i);
+                       len -= 1;
+                       continue;
+               }
+               i += 1;
+       }
+}
+
+/**
+ * @brief Go through all global parametric parameters
+ *
+ * @param regex_str    A regular expression to scan param for
+ * @param max_matches   Max number of submatches the regexp expects
+ * @param cb           Function to call on match. Should return true
+ *                      when it wants wi_scan_global_parametrics to stop
+ *                      scanning
+ * @param private_data  Anonymous pointer passed to cb
+ *
+ * @return              0: success, regcomp/regexec return value on error.
+ *                      See "man regexec" for possible errors
+ */
+
+int lp_wi_scan_global_parametrics(
+       const char *regex_str, size_t max_matches,
+       bool (*cb)(const char *string, regmatch_t matches[],
+                  void *private_data),
+       void *private_data)
+{
+       struct parmlist_entry *data;
+       regex_t regex;
+       int ret;
+
+       ret = regcomp(&regex, regex_str, REG_ICASE);
+       if (ret != 0) {
+               return ret;
+       }
+
+       for (data = Globals.param_opt; data != NULL; data = data->next) {
+               size_t keylen = strlen(data->key);
+               char key[keylen+1];
+               regmatch_t matches[max_matches];
+               bool stop;
+
+               memcpy(key, data->key, sizeof(key));
+               discard_whitespace(key);
+
+               ret = regexec(&regex, key, max_matches, matches, 0);
+               if (ret == REG_NOMATCH) {
+                       continue;
+               }
+               if (ret != 0) {
+                       goto fail;
+               }
+
+               stop = cb(key, matches, private_data);
+               if (stop) {
+                       break;
+               }
+       }
+
+       ret = 0;
+fail:
+       regfree(&regex);
+       return ret;
+}
+
 
 #define MISSING_PARAMETER(name) \
     DEBUG(0, ("%s(): value is NULL or empty!\n", #name))