Hi Landry,

On Sun, Jan 31, 2016 at 09:39:52AM +0100, Landry Breuil wrote:
> Hi,
> 
> i'm tinkering with ldapd and writing regress tests for it, and to
> allow running independent instances (with separate port/control
> socket/etc) i needed to add the possibility to specify an alternative
> datadir, which was so far #defined in the code.
> Patch is pretty simple and works fine, i'm open to suggestions of course
> on a better wording for the manpage and option choose (i went for -r..)
> okays welcome too !

slight tweak,
looks like it is missing a chdir(3) to check failure if an invalid(nonexistent)
datadir was passed to optarg.

I just added these lines in ldapd.c:
159             if (datadir && chdir(datadir))
160                     err(1, "chdir");

% doas ./ldapd -r /home/gsoares/non-existentdoas
ldapd: chdir: No such file or directory
% 

updated diff attached.
Index: ldapd.8
===================================================================
RCS file: /cvs/src/usr.sbin/ldapd/ldapd.8,v
retrieving revision 1.12
diff -u -p -r1.12 ldapd.8
--- ldapd.8     11 Aug 2014 08:21:55 -0000      1.12
+++ ldapd.8     1 Feb 2016 18:51:17 -0000
@@ -57,6 +57,11 @@ Use
 .Ar file
 as the configuration file, instead of the default
 .Pa /etc/ldapd.conf .
+.It Fl r Ar directory
+Store and read database files in
+.Ar directory
+, instead of the default
+.Pa /var/db/ldap .
 .It Fl n
 Configtest mode.
 Only check the configuration file for validity.
Index: ldapd.c
===================================================================
RCS file: /cvs/src/usr.sbin/ldapd/ldapd.c,v
retrieving revision 1.15
diff -u -p -r1.15 ldapd.c
--- ldapd.c     24 Dec 2015 17:47:57 -0000      1.15
+++ ldapd.c     1 Feb 2016 18:51:17 -0000
@@ -48,6 +48,7 @@ static void    ldapd_log_verbose(struct im
 
 struct ldapd_stats      stats;
 pid_t                   ldape_pid;
+char *                  datadir;
 
 void
 usage(void)
@@ -55,7 +56,7 @@ usage(void)
        extern char     *__progname;
 
        fprintf(stderr, "usage: %s [-dnv] [-D macro=value] "
-           "[-f file] [-s file]\n", __progname);
+           "[-f file] [-r directory] [-s file]\n", __progname);
        exit(1);
 }
 
@@ -115,9 +116,10 @@ main(int argc, char *argv[])
        struct event             ev_sigchld;
        struct event             ev_sighup;
 
+       datadir = DATADIR;
        log_init(1);            /* log to stderr until daemonized */
 
-       while ((c = getopt(argc, argv, "dhvD:f:ns:")) != -1) {
+       while ((c = getopt(argc, argv, "dhvD:f:nr:s:")) != -1) {
                switch (c) {
                case 'd':
                        debug = 1;
@@ -137,6 +139,9 @@ main(int argc, char *argv[])
                case 'n':
                        configtest = 1;
                        break;
+               case 'r':
+                       datadir = optarg;
+                       break;
                case 's':
                        csockpath = optarg;
                        break;
@@ -174,6 +179,9 @@ main(int argc, char *argv[])
        if (!skip_chroot && (pw = getpwnam(LDAPD_USER)) == NULL)
                err(1, "%s", LDAPD_USER);
 
+       if (datadir && chdir(datadir))
+               err(1, "chdir");
+
        if (!debug) {
                if (daemon(1, 0) == -1)
                        err(1, "failed to daemonize");
@@ -343,7 +351,7 @@ ldapd_open_request(struct imsgev *iev, s
        /* make sure path is null-terminated */
        oreq->path[PATH_MAX] = '\0';
 
-       if (strncmp(oreq->path, DATADIR, strlen(DATADIR)) != 0) {
+       if (strncmp(oreq->path, datadir, strlen(datadir)) != 0) {
                log_warnx("refusing to open file %s", oreq->path);
                fatal("ldape sent invalid open request");
        }
Index: namespace.c
===================================================================
RCS file: /cvs/src/usr.sbin/ldapd/namespace.c,v
retrieving revision 1.14
diff -u -p -r1.14 namespace.c
--- namespace.c 24 Dec 2015 17:47:57 -0000      1.14
+++ namespace.c 1 Feb 2016 18:51:17 -0000
@@ -38,6 +38,7 @@ static void            namespace_queue_replay(int
 static int              namespace_set_fd(struct namespace *ns,
                            struct btree **bt, int fd, unsigned int flags);
 
+extern char            *datadir;
 int
 namespace_begin_txn(struct namespace *ns, struct btree_txn **data_txn,
     struct btree_txn **indx_txn, int rdonly)
@@ -115,7 +116,7 @@ namespace_open(struct namespace *ns)
        if (ns->sync == 0)
                db_flags |= BT_NOSYNC;
 
-       if (asprintf(&ns->data_path, "%s/%s_data.db", DATADIR, ns->suffix) < 0)
+       if (asprintf(&ns->data_path, "%s/%s_data.db", datadir, ns->suffix) < 0)
                return -1;
        log_info("opening namespace %s", ns->suffix);
        ns->data_db = btree_open(ns->data_path, db_flags | BT_REVERSEKEY, 0644);
@@ -124,7 +125,7 @@ namespace_open(struct namespace *ns)
 
        btree_set_cache_size(ns->data_db, ns->cache_size);
 
-       if (asprintf(&ns->indx_path, "%s/%s_indx.db", DATADIR, ns->suffix) < 0)
+       if (asprintf(&ns->indx_path, "%s/%s_indx.db", datadir, ns->suffix) < 0)
                return -1;
        ns->indx_db = btree_open(ns->indx_path, db_flags, 0644);
        if (ns->indx_db == NULL)

Reply via email to