root/source4/lib/ldb/tools/cmdline.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ldb_cmdline_process
  2. handle_controls_reply

   1 /* 
   2    ldb database library - command line handling for ldb tools
   3 
   4    Copyright (C) Andrew Tridgell  2005
   5 
   6      ** NOTE! The following LGPL license applies to the ldb
   7      ** library. This does NOT imply that all of Samba is released
   8      ** under the LGPL
   9    
  10    This library is free software; you can redistribute it and/or
  11    modify it under the terms of the GNU Lesser General Public
  12    License as published by the Free Software Foundation; either
  13    version 3 of the License, or (at your option) any later version.
  14 
  15    This library is distributed in the hope that it will be useful,
  16    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18    Lesser General Public License for more details.
  19 
  20    You should have received a copy of the GNU Lesser General Public
  21    License along with this library; if not, see <http://www.gnu.org/licenses/>.
  22 */
  23 
  24 #include "ldb_includes.h"
  25 #include "ldb.h"
  26 #include "tools/cmdline.h"
  27 
  28 #if (_SAMBA_BUILD_ >= 4)
  29 #include "includes.h"
  30 #include "lib/cmdline/popt_common.h"
  31 #include "lib/ldb-samba/ldif_handlers.h"
  32 #include "auth/gensec/gensec.h"
  33 #include "auth/auth.h"
  34 #include "ldb_wrap.h"
  35 #include "param/param.h"
  36 #endif
  37 
  38 
  39 
  40 /**
  41   process command line options
  42 */
  43 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, 
     /* [<][>][^][v][top][bottom][index][help] */
  44                                         int argc, const char **argv,
  45                                         void (*usage)(void))
  46 {
  47         static struct ldb_cmdline options; /* needs to be static for older compilers */
  48         struct ldb_cmdline *ret=NULL;
  49         poptContext pc;
  50 #if (_SAMBA_BUILD_ >= 4)
  51         int r;
  52 #endif
  53         int num_options = 0;
  54         int opt;
  55         int flags = 0;
  56 
  57         struct poptOption popt_options[] = {
  58                 POPT_AUTOHELP
  59                 { "url",       'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
  60                 { "basedn",    'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
  61                 { "editor",    'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
  62                 { "scope",     's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
  63                 { "verbose",   'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
  64                 { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
  65                 { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
  66                 { "modules-path", 0, POPT_ARG_STRING, &options.modules_path, 0, "modules path", "PATH" },
  67                 { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
  68                 { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
  69                 { "all", 'a',    POPT_ARG_NONE, &options.all_records, 0, "(|(objectClass=*)(distinguishedName=*))", NULL },
  70                 { "nosync", 0,   POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
  71                 { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
  72                 { "input", 'I', POPT_ARG_STRING, &options.input, 0, "Input File", "Input" },
  73                 { "output", 'O', POPT_ARG_STRING, &options.output, 0, "Output File", "Output" },
  74                 { NULL,    'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
  75                 { "controls", 0, POPT_ARG_STRING, NULL, 'c', "controls", NULL },
  76 #if (_SAMBA_BUILD_ >= 4)
  77                 POPT_COMMON_SAMBA
  78                 POPT_COMMON_CREDENTIALS
  79                 POPT_COMMON_CONNECTION
  80                 POPT_COMMON_VERSION
  81 #endif
  82                 { NULL }
  83         };
  84 
  85 #if (_SAMBA_BUILD_ >= 4)
  86         r = ldb_register_samba_handlers(ldb);
  87         if (r != 0) {
  88                 goto failed;
  89         }
  90 
  91 #endif
  92 
  93         ret = talloc_zero(ldb, struct ldb_cmdline);
  94         if (ret == NULL) {
  95                 fprintf(stderr, "Out of memory!\n");
  96                 goto failed;
  97         }
  98 
  99         options = *ret;
 100         
 101         /* pull in URL */
 102         options.url = getenv("LDB_URL");
 103 
 104         /* and editor (used by ldbedit) */
 105         options.editor = getenv("VISUAL");
 106         if (!options.editor) {
 107                 options.editor = getenv("EDITOR");
 108         }
 109         if (!options.editor) {
 110                 options.editor = "vi";
 111         }
 112 
 113         options.scope = LDB_SCOPE_DEFAULT;
 114 
 115         pc = poptGetContext(argv[0], argc, argv, popt_options, 
 116                             POPT_CONTEXT_KEEP_FIRST);
 117 
 118         while((opt = poptGetNextOpt(pc)) != -1) {
 119                 switch (opt) {
 120                 case 's': {
 121                         const char *arg = poptGetOptArg(pc);
 122                         if (strcmp(arg, "base") == 0) {
 123                                 options.scope = LDB_SCOPE_BASE;
 124                         } else if (strcmp(arg, "sub") == 0) {
 125                                 options.scope = LDB_SCOPE_SUBTREE;
 126                         } else if (strcmp(arg, "one") == 0) {
 127                                 options.scope = LDB_SCOPE_ONELEVEL;
 128                         } else {
 129                                 fprintf(stderr, "Invalid scope '%s'\n", arg);
 130                                 goto failed;
 131                         }
 132                         break;
 133                 }
 134 
 135                 case 'v':
 136                         options.verbose++;
 137                         break;
 138 
 139                 case 'o':
 140                         options.options = talloc_realloc(ret, options.options, 
 141                                                          const char *, num_options+3);
 142                         if (options.options == NULL) {
 143                                 fprintf(stderr, "Out of memory!\n");
 144                                 goto failed;
 145                         }
 146                         options.options[num_options] = poptGetOptArg(pc);
 147                         options.options[num_options+1] = NULL;
 148                         num_options++;
 149                         break;
 150 
 151                 case 'c': {
 152                         const char *cs = poptGetOptArg(pc);
 153                         const char *p, *q;
 154                         int cc;
 155 
 156                         for (p = cs, cc = 1; (q = strchr(p, ',')); cc++, p = q + 1) ;
 157 
 158                         options.controls = talloc_array(ret, char *, cc + 1);
 159                         if (options.controls == NULL) {
 160                                 fprintf(stderr, "Out of memory!\n");
 161                                 goto failed;
 162                         }
 163                         for (p = cs, cc = 0; p != NULL; cc++) {
 164                                 const char *t;
 165 
 166                                 t = strchr(p, ',');
 167                                 if (t == NULL) {
 168                                         options.controls[cc] = talloc_strdup(options.controls, p);
 169                                         p = NULL;
 170                                 } else {
 171                                         options.controls[cc] = talloc_strndup(options.controls, p, t-p);
 172                                         p = t + 1;
 173                                 }
 174                         }
 175                         options.controls[cc] = NULL;
 176 
 177                         break;    
 178                 }
 179                 default:
 180                         fprintf(stderr, "Invalid option %s: %s\n", 
 181                                 poptBadOption(pc, 0), poptStrerror(opt));
 182                         if (usage) usage();
 183                         goto failed;
 184                 }
 185         }
 186 
 187         /* setup the remaining options for the main program to use */
 188         options.argv = poptGetArgs(pc);
 189         if (options.argv) {
 190                 options.argv++;
 191                 while (options.argv[options.argc]) options.argc++;
 192         }
 193 
 194         *ret = options;
 195 
 196         /* all utils need some option */
 197         if (ret->url == NULL) {
 198                 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
 199                 if (usage) usage();
 200                 goto failed;
 201         }
 202 
 203         if (strcmp(ret->url, "NONE") == 0) {
 204                 return ret;
 205         }
 206 
 207         if (options.nosync) {
 208                 flags |= LDB_FLG_NOSYNC;
 209         }
 210 
 211 #if (_SAMBA_BUILD_ >= 4)
 212         /* Must be after we have processed command line options */
 213         gensec_init(cmdline_lp_ctx); 
 214         
 215         if (ldb_set_opaque(ldb, "sessionInfo", system_session(ldb, cmdline_lp_ctx))) {
 216                 goto failed;
 217         }
 218         if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
 219                 goto failed;
 220         }
 221         if (ldb_set_opaque(ldb, "loadparm", cmdline_lp_ctx)) {
 222                 goto failed;
 223         }
 224 
 225         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
 226 #endif
 227 
 228         if (options.modules_path != NULL) {
 229                 ldb_set_modules_dir(ldb, options.modules_path);
 230         } else if (getenv("LDB_MODULES_PATH") != NULL) {
 231                 ldb_set_modules_dir(ldb, getenv("LDB_MODULES_PATH"));
 232         }
 233 
 234         /* now connect to the ldb */
 235         if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
 236                 fprintf(stderr, "Failed to connect to %s - %s\n", 
 237                         ret->url, ldb_errstring(ldb));
 238                 goto failed;
 239         }
 240 
 241         return ret;
 242 
 243 failed:
 244         talloc_free(ret);
 245         exit(1);
 246         return NULL;
 247 }
 248 
 249 /* this function check controls reply and determines if more
 250  * processing is needed setting up the request controls correctly
 251  *
 252  * returns:
 253  *      -1 error
 254  *      0 all ok
 255  *      1 all ok, more processing required
 256  */
 257 int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
     /* [<][>][^][v][top][bottom][index][help] */
 258 {
 259         int i, j;
 260         int ret = 0;
 261 
 262         if (reply == NULL || request == NULL) return -1;
 263         
 264         for (i = 0; reply[i]; i++) {
 265                 if (strcmp(LDB_CONTROL_VLV_RESP_OID, reply[i]->oid) == 0) {
 266                         struct ldb_vlv_resp_control *rep_control;
 267 
 268                         rep_control = talloc_get_type(reply[i]->data, struct ldb_vlv_resp_control);
 269                         
 270                         /* check we have a matching control in the request */
 271                         for (j = 0; request[j]; j++) {
 272                                 if (strcmp(LDB_CONTROL_VLV_REQ_OID, request[j]->oid) == 0)
 273                                         break;
 274                         }
 275                         if (! request[j]) {
 276                                 fprintf(stderr, "Warning VLV reply received but no request have been made\n");
 277                                 continue;
 278                         }
 279 
 280                         /* check the result */
 281                         if (rep_control->vlv_result != 0) {
 282                                 fprintf(stderr, "Warning: VLV not performed with error: %d\n", rep_control->vlv_result);
 283                         } else {
 284                                 fprintf(stderr, "VLV Info: target position = %d, content count = %d\n", rep_control->targetPosition, rep_control->contentCount);
 285                         }
 286 
 287                         continue;
 288                 }
 289 
 290                 if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) {
 291                         struct ldb_asq_control *rep_control;
 292 
 293                         rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control);
 294 
 295                         /* check the result */
 296                         if (rep_control->result != 0) {
 297                                 fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result);
 298                         }
 299 
 300                         continue;
 301                 }
 302 
 303                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) {
 304                         struct ldb_paged_control *rep_control, *req_control;
 305 
 306                         rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control);
 307                         if (rep_control->cookie_len == 0) /* we are done */
 308                                 break;
 309 
 310                         /* more processing required */
 311                         /* let's fill in the request control with the new cookie */
 312 
 313                         for (j = 0; request[j]; j++) {
 314                                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0)
 315                                         break;
 316                         }
 317                         /* if there's a reply control we must find a request
 318                          * control matching it */
 319                         if (! request[j]) return -1;
 320 
 321                         req_control = talloc_get_type(request[j]->data, struct ldb_paged_control);
 322 
 323                         if (req_control->cookie)
 324                                 talloc_free(req_control->cookie);
 325                         req_control->cookie = (char *)talloc_memdup(
 326                                 req_control, rep_control->cookie,
 327                                 rep_control->cookie_len);
 328                         req_control->cookie_len = rep_control->cookie_len;
 329 
 330                         ret = 1;
 331 
 332                         continue;
 333                 }
 334 
 335                 if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) {
 336                         struct ldb_sort_resp_control *rep_control;
 337 
 338                         rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control);
 339 
 340                         /* check we have a matching control in the request */
 341                         for (j = 0; request[j]; j++) {
 342                                 if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0)
 343                                         break;
 344                         }
 345                         if (! request[j]) {
 346                                 fprintf(stderr, "Warning Server Sort reply received but no request found\n");
 347                                 continue;
 348                         }
 349 
 350                         /* check the result */
 351                         if (rep_control->result != 0) {
 352                                 fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result);
 353                         }
 354 
 355                         continue;
 356                 }
 357 
 358                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, reply[i]->oid) == 0) {
 359                         struct ldb_dirsync_control *rep_control, *req_control;
 360                         char *cookie;
 361 
 362                         rep_control = talloc_get_type(reply[i]->data, struct ldb_dirsync_control);
 363                         if (rep_control->cookie_len == 0) /* we are done */
 364                                 break;
 365 
 366                         /* more processing required */
 367                         /* let's fill in the request control with the new cookie */
 368 
 369                         for (j = 0; request[j]; j++) {
 370                                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, request[j]->oid) == 0)
 371                                         break;
 372                         }
 373                         /* if there's a reply control we must find a request
 374                          * control matching it */
 375                         if (! request[j]) return -1;
 376 
 377                         req_control = talloc_get_type(request[j]->data, struct ldb_dirsync_control);
 378 
 379                         if (req_control->cookie)
 380                                 talloc_free(req_control->cookie);
 381                         req_control->cookie = (char *)talloc_memdup(
 382                                 req_control, rep_control->cookie,
 383                                 rep_control->cookie_len);
 384                         req_control->cookie_len = rep_control->cookie_len;
 385 
 386                         cookie = ldb_base64_encode(req_control, rep_control->cookie, rep_control->cookie_len);
 387                         printf("# DIRSYNC cookie returned was:\n# %s\n", cookie);
 388 
 389                         continue;
 390                 }
 391 
 392                 /* no controls matched, throw a warning */
 393                 fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid);
 394         }
 395 
 396         return ret;
 397 }
 398 

/* [<][>][^][v][top][bottom][index][help] */