ldifutil.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2022 The OpenLDAP Foundation.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted only as authorized by the OpenLDAP
  9. * Public License.
  10. *
  11. * A copy of this license is available in the file LICENSE in the
  12. * top-level directory of the distribution or, alternatively, at
  13. * <http://www.OpenLDAP.org/license.html>.
  14. */
  15. /* Portions Copyright (c) 1990 Regents of the University of Michigan.
  16. * All rights reserved.
  17. */
  18. /*
  19. * This file contains public API to help with parsing LDIF
  20. */
  21. #include "portable.h"
  22. #include <stdio.h>
  23. #include <ac/stdlib.h>
  24. #include <ac/ctype.h>
  25. #include <ac/string.h>
  26. #include <ac/unistd.h>
  27. #include <ac/socket.h>
  28. #include <ac/time.h>
  29. #include "ldap-int.h"
  30. #include "ldif.h"
  31. #define M_SEP 0x7f
  32. /* strings found in LDIF entries */
  33. static struct berval BV_VERSION = BER_BVC("version");
  34. static struct berval BV_DN = BER_BVC("dn");
  35. static struct berval BV_CONTROL = BER_BVC("control");
  36. static struct berval BV_CHANGETYPE = BER_BVC("changetype");
  37. static struct berval BV_ADDCT = BER_BVC("add");
  38. static struct berval BV_MODIFYCT = BER_BVC("modify");
  39. static struct berval BV_DELETECT = BER_BVC("delete");
  40. static struct berval BV_MODRDNCT = BER_BVC("modrdn");
  41. static struct berval BV_MODDNCT = BER_BVC("moddn");
  42. static struct berval BV_RENAMECT = BER_BVC("rename");
  43. static struct berval BV_MODOPADD = BER_BVC("add");
  44. static struct berval BV_MODOPREPLACE = BER_BVC("replace");
  45. static struct berval BV_MODOPDELETE = BER_BVC("delete");
  46. static struct berval BV_MODOPINCREMENT = BER_BVC("increment");
  47. static struct berval BV_NEWRDN = BER_BVC("newrdn");
  48. static struct berval BV_DELETEOLDRDN = BER_BVC("deleteoldrdn");
  49. static struct berval BV_NEWSUP = BER_BVC("newsuperior");
  50. #define BV_CASEMATCH(a, b) \
  51. ((a)->bv_len == (b)->bv_len && 0 == strcasecmp((a)->bv_val, (b)->bv_val))
  52. static int parse_ldif_control LDAP_P(( struct berval *bval, LDAPControl ***ppctrls ));
  53. void
  54. ldap_ldif_record_done( LDIFRecord *lr )
  55. {
  56. int i;
  57. /* the LDAPControl stuff does not allow the use of memory contexts */
  58. if (lr->lr_ctrls != NULL) {
  59. ldap_controls_free( lr->lr_ctrls );
  60. }
  61. if ( lr->lr_lm != NULL ) {
  62. ber_memfree_x( lr->lr_lm, lr->lr_ctx );
  63. }
  64. if ( lr->lr_mops != NULL ) {
  65. ber_memfree_x( lr->lr_mops, lr->lr_ctx );
  66. }
  67. for (i=lr->lr_lines-1; i>=0; i--)
  68. if ( lr->lr_freeval[i] ) ber_memfree_x( lr->lr_vals[i].bv_val, lr->lr_ctx );
  69. ber_memfree_x( lr->lr_btype, lr->lr_ctx );
  70. memset( lr, 0, sizeof(LDIFRecord) );
  71. }
  72. /*
  73. * ldap_parse_ldif_record_x() will convert an LDIF record read with ldif_read_record()
  74. * into an array of LDAPMod* and an array of LDAPControl*, suitable for passing
  75. * directly to any other LDAP API function that takes LDAPMod** and LDAPControl**
  76. * arguments, such as ldap_modify_s().
  77. *
  78. * rbuf - the ldif record buffer returned from ldif_read_record - rbuf.bv_val must be
  79. * writable - will use ldif_getline to read from it
  80. * linenum - the ldif line number returned from ldif_read_record
  81. * - used for logging errors (e.g. error at line N)
  82. * lr - holds the data to return
  83. * errstr - a string used for logging (usually the program name e.g. "ldapmodify"
  84. * flags - 0 or some combination of LDIF_DEFAULT_ADD LDIF_ENTRIES_ONLY LDIF_NO_CONTROLS
  85. * ctx is the memory allocation context - if NULL, use the standard memory allocator
  86. */
  87. int
  88. ldap_parse_ldif_record_x(
  89. struct berval *rbuf,
  90. unsigned long linenum,
  91. LDIFRecord *lr,
  92. const char *errstr,
  93. unsigned int flags,
  94. void *ctx )
  95. {
  96. char *line, *dn;
  97. int rc, modop;
  98. int expect_modop, expect_sep;
  99. int ldapadd, new_entry, delete_entry, got_all, no_dn;
  100. LDAPMod **pmods;
  101. int version;
  102. LDAPControl **pctrls;
  103. int i, j, k, idn, nmods;
  104. struct berval **bvl, bv;
  105. assert( lr != NULL );
  106. assert( rbuf != NULL );
  107. memset( lr, 0, sizeof(LDIFRecord) );
  108. lr->lr_ctx = ctx; /* save memory context for later */
  109. ldapadd = flags & LDIF_DEFAULT_ADD;
  110. no_dn = flags & LDIF_NO_DN;
  111. expect_modop = flags & LDIF_MODS_ONLY;
  112. new_entry = ldapadd;
  113. rc = got_all = delete_entry = modop = 0;
  114. expect_sep = 0;
  115. version = 0;
  116. pmods = NULL;
  117. pctrls = NULL;
  118. dn = NULL;
  119. lr->lr_lines = ldif_countlines( rbuf->bv_val );
  120. lr->lr_btype = ber_memcalloc_x( 1, (lr->lr_lines+1)*2*sizeof(struct berval)+lr->lr_lines, ctx );
  121. if ( !lr->lr_btype )
  122. return LDAP_NO_MEMORY;
  123. lr->lr_vals = lr->lr_btype+lr->lr_lines+1;
  124. lr->lr_freeval = (char *)(lr->lr_vals+lr->lr_lines+1);
  125. i = -1;
  126. while ( rc == 0 && ( line = ldif_getline( &rbuf->bv_val )) != NULL ) {
  127. int freev;
  128. if ( *line == '\n' || *line == '\0' ) {
  129. break;
  130. }
  131. ++i;
  132. if ( line[0] == '-' && !line[1] ) {
  133. BER_BVZERO( lr->lr_btype+i );
  134. lr->lr_freeval[i] = 0;
  135. continue;
  136. }
  137. if ( ( rc = ldif_parse_line2( line, lr->lr_btype+i, lr->lr_vals+i, &freev ) ) < 0 ) {
  138. fprintf( stderr, _("%s: invalid format (line %lu) entry: \"%s\"\n"),
  139. errstr, linenum+i, dn == NULL ? "" : dn );
  140. rc = LDAP_PARAM_ERROR;
  141. goto leave;
  142. }
  143. lr->lr_freeval[i] = freev;
  144. if ( dn == NULL && !no_dn ) {
  145. if ( linenum+i == 1 && BV_CASEMATCH( lr->lr_btype+i, &BV_VERSION )) {
  146. /* lutil_atoi() introduces a dependence of libldap
  147. * on liblutil; we only allow version 1 by now (ITS#6654)
  148. */
  149. #if 0
  150. int v;
  151. if( lr->lr_vals[i].bv_len == 0 || lutil_atoi( &v, lr->lr_vals[i].bv_val) != 0 || v != 1 )
  152. #endif
  153. static const struct berval version1 = { 1, "1" };
  154. if ( lr->lr_vals[i].bv_len != version1.bv_len || strncmp( lr->lr_vals[i].bv_val, version1.bv_val, version1.bv_len ) != 0 )
  155. {
  156. fprintf( stderr,
  157. _("%s: invalid version %s, line %lu (ignored)\n"),
  158. errstr, lr->lr_vals[i].bv_val, linenum );
  159. }
  160. version++;
  161. } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_DN )) {
  162. lr->lr_dn = lr->lr_vals[i];
  163. dn = lr->lr_dn.bv_val; /* primarily for logging */
  164. idn = i;
  165. }
  166. /* skip all lines until we see "dn:" */
  167. }
  168. }
  169. /* check to make sure there was a dn: line */
  170. if ( !dn && !no_dn ) {
  171. rc = 0;
  172. goto leave;
  173. }
  174. lr->lr_lines = i+1;
  175. if( lr->lr_lines == 0 ) {
  176. rc = 0;
  177. goto leave;
  178. }
  179. if( version && lr->lr_lines == 1 ) {
  180. rc = 0;
  181. goto leave;
  182. }
  183. if ( no_dn ) {
  184. i = 0;
  185. } else {
  186. i = idn+1;
  187. /* Check for "control" tag after dn and before changetype. */
  188. if ( BV_CASEMATCH( lr->lr_btype+i, &BV_CONTROL )) {
  189. /* Parse and add it to the list of controls */
  190. if ( !( flags & LDIF_NO_CONTROLS ) ) {
  191. rc = parse_ldif_control( lr->lr_vals+i, &pctrls );
  192. if (rc != 0) {
  193. fprintf( stderr,
  194. _("%s: Error processing %s line, line %lu: %s\n"),
  195. errstr, BV_CONTROL.bv_val, linenum+i, ldap_err2string(rc) );
  196. }
  197. }
  198. i++;
  199. if ( i>= lr->lr_lines ) {
  200. short_input:
  201. fprintf( stderr,
  202. _("%s: Expecting more input after %s line, line %lu\n"),
  203. errstr, lr->lr_btype[i-1].bv_val, linenum+i );
  204. rc = LDAP_PARAM_ERROR;
  205. goto leave;
  206. }
  207. }
  208. }
  209. /* Check for changetype */
  210. if ( BV_CASEMATCH( lr->lr_btype+i, &BV_CHANGETYPE )) {
  211. #ifdef LIBERAL_CHANGETYPE_MODOP
  212. /* trim trailing spaces (and log warning ...) */
  213. int icnt;
  214. for ( icnt = lr->lr_vals[i].bv_len; --icnt > 0; ) {
  215. if ( !isspace( (unsigned char) lr->lr_vals[i].bv_val[icnt] ) ) {
  216. break;
  217. }
  218. }
  219. if ( ++icnt != lr->lr_vals[i].bv_len ) {
  220. fprintf( stderr, _("%s: illegal trailing space after"
  221. " \"%s: %s\" trimmed (line %lu, entry \"%s\")\n"),
  222. errstr, BV_CHANGETYPE.bv_val, lr->lr_vals[i].bv_val, linenum+i, dn );
  223. lr->lr_vals[i].bv_val[icnt] = '\0';
  224. }
  225. #endif /* LIBERAL_CHANGETYPE_MODOP */
  226. /* if LDIF_ENTRIES_ONLY, then either the changetype must be add, or
  227. there must be no changetype, and the flag LDIF_DEFAULT_ADD must be set */
  228. if ( flags & LDIF_ENTRIES_ONLY ) {
  229. if ( !( BV_CASEMATCH( lr->lr_vals+i, &BV_ADDCT )) ) {
  230. ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
  231. _("%s: skipping LDIF record beginning at line %lu: "
  232. "changetype '%.*s' found but entries only was requested\n"),
  233. errstr, linenum,
  234. (int)lr->lr_vals[i].bv_len,
  235. (const char *)lr->lr_vals[i].bv_val );
  236. goto leave;
  237. }
  238. }
  239. if ( BV_CASEMATCH( lr->lr_vals+i, &BV_MODIFYCT )) {
  240. new_entry = 0;
  241. expect_modop = 1;
  242. } else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_ADDCT )) {
  243. new_entry = 1;
  244. modop = LDAP_MOD_ADD;
  245. } else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_MODRDNCT )
  246. || BV_CASEMATCH( lr->lr_vals+i, &BV_MODDNCT )
  247. || BV_CASEMATCH( lr->lr_vals+i, &BV_RENAMECT ))
  248. {
  249. i++;
  250. if ( i >= lr->lr_lines )
  251. goto short_input;
  252. if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_NEWRDN )) {
  253. fprintf( stderr, _("%s: expecting \"%s:\" but saw"
  254. " \"%s:\" (line %lu, entry \"%s\")\n"),
  255. errstr, BV_NEWRDN.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn );
  256. rc = LDAP_PARAM_ERROR;
  257. goto leave;
  258. }
  259. lr->lrop_newrdn = lr->lr_vals[i];
  260. i++;
  261. if ( i >= lr->lr_lines )
  262. goto short_input;
  263. if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_DELETEOLDRDN )) {
  264. fprintf( stderr, _("%s: expecting \"%s:\" but saw"
  265. " \"%s:\" (line %lu, entry \"%s\")\n"),
  266. errstr, BV_DELETEOLDRDN.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn );
  267. rc = LDAP_PARAM_ERROR;
  268. goto leave;
  269. }
  270. lr->lrop_delold = ( lr->lr_vals[i].bv_val[0] == '0' ) ? 0 : 1;
  271. i++;
  272. if ( i < lr->lr_lines ) {
  273. if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_NEWSUP )) {
  274. fprintf( stderr, _("%s: expecting \"%s:\" but saw"
  275. " \"%s:\" (line %lu, entry \"%s\")\n"),
  276. errstr, BV_NEWSUP.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn );
  277. rc = LDAP_PARAM_ERROR;
  278. goto leave;
  279. }
  280. lr->lrop_newsup = lr->lr_vals[i];
  281. i++;
  282. }
  283. got_all = 1;
  284. } else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_DELETECT )) {
  285. got_all = delete_entry = 1;
  286. } else {
  287. fprintf( stderr,
  288. _("%s: unknown %s \"%s\" (line %lu, entry \"%s\")\n"),
  289. errstr, BV_CHANGETYPE.bv_val, lr->lr_vals[i].bv_val, linenum+i, dn );
  290. rc = LDAP_PARAM_ERROR;
  291. goto leave;
  292. }
  293. i++;
  294. } else if ( ldapadd ) { /* missing changetype => add */
  295. new_entry = 1;
  296. modop = LDAP_MOD_ADD;
  297. } else {
  298. /* if LDIF_ENTRIES_ONLY, then either the changetype must be add, or
  299. there must be no changetype, and the flag LDIF_DEFAULT_ADD must be set */
  300. if ( flags & LDIF_ENTRIES_ONLY ) {
  301. ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
  302. _("%s: skipping LDIF record beginning at line %lu: "
  303. "no changetype found but entries only was requested and "
  304. "the default setting for missing changetype is modify\n"),
  305. errstr, linenum );
  306. goto leave;
  307. }
  308. expect_modop = 1; /* missing changetype => modify */
  309. }
  310. if ( got_all ) {
  311. if ( i < lr->lr_lines ) {
  312. fprintf( stderr,
  313. _("%s: extra lines at end (line %lu, entry \"%s\")\n"),
  314. errstr, linenum+i, dn );
  315. rc = LDAP_PARAM_ERROR;
  316. goto leave;
  317. }
  318. goto doit;
  319. }
  320. nmods = lr->lr_lines - i;
  321. idn = i;
  322. if ( new_entry ) {
  323. int fv;
  324. /* Make sure all attributes with multiple values are contiguous */
  325. for (; i<lr->lr_lines; i++) {
  326. for (j=i+1; j<lr->lr_lines; j++) {
  327. if ( !lr->lr_btype[j].bv_val ) {
  328. fprintf( stderr,
  329. _("%s: missing attributeDescription (line %lu, entry \"%s\")\n"),
  330. errstr, linenum+j, dn );
  331. rc = LDAP_PARAM_ERROR;
  332. goto leave;
  333. }
  334. if ( BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+j )) {
  335. nmods--;
  336. /* out of order, move intervening attributes down */
  337. if ( j != i+1 ) {
  338. bv = lr->lr_vals[j];
  339. fv = lr->lr_freeval[j];
  340. for (k=j; k>i; k--) {
  341. lr->lr_btype[k] = lr->lr_btype[k-1];
  342. lr->lr_vals[k] = lr->lr_vals[k-1];
  343. lr->lr_freeval[k] = lr->lr_freeval[k-1];
  344. }
  345. k++;
  346. lr->lr_btype[k] = lr->lr_btype[i];
  347. lr->lr_vals[k] = bv;
  348. lr->lr_freeval[k] = fv;
  349. }
  350. i++;
  351. }
  352. }
  353. }
  354. /* Allocate space for array of mods, array of pointers to mods,
  355. * and array of pointers to values, allowing for NULL terminators
  356. * for the pointer arrays...
  357. */
  358. lr->lr_lm = ber_memalloc_x( nmods * sizeof(LDAPMod) +
  359. (nmods+1) * sizeof(LDAPMod*) +
  360. (lr->lr_lines + nmods - idn) * sizeof(struct berval *), ctx );
  361. if ( lr->lr_lm == NULL ) {
  362. rc = LDAP_NO_MEMORY;
  363. goto leave;
  364. }
  365. pmods = (LDAPMod **)(lr->lr_lm+nmods);
  366. bvl = (struct berval **)(pmods+nmods+1);
  367. j = 0;
  368. k = -1;
  369. BER_BVZERO(&bv);
  370. for (i=idn; i<lr->lr_lines; i++) {
  371. if ( BV_CASEMATCH( lr->lr_btype+i, &BV_DN )) {
  372. fprintf( stderr, _("%s: attributeDescription \"%s\":"
  373. " (possible missing newline"
  374. " after line %lu, entry \"%s\"?)\n"),
  375. errstr, lr->lr_btype[i].bv_val, linenum+i - 1, dn );
  376. }
  377. if ( !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
  378. bvl[k++] = NULL;
  379. bv = lr->lr_btype[i];
  380. lr->lr_lm[j].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
  381. lr->lr_lm[j].mod_type = bv.bv_val;
  382. lr->lr_lm[j].mod_bvalues = bvl+k;
  383. pmods[j] = lr->lr_lm+j;
  384. j++;
  385. }
  386. bvl[k++] = lr->lr_vals+i;
  387. }
  388. bvl[k] = NULL;
  389. pmods[j] = NULL;
  390. goto doit;
  391. }
  392. lr->lr_mops = ber_memalloc_x( lr->lr_lines+1, ctx );
  393. if ( lr->lr_mops == NULL ) {
  394. rc = LDAP_NO_MEMORY;
  395. goto leave;
  396. }
  397. lr->lr_mops[lr->lr_lines] = M_SEP;
  398. if ( i > 0 )
  399. lr->lr_mops[i-1] = M_SEP;
  400. for ( ; i<lr->lr_lines; i++ ) {
  401. if ( expect_modop ) {
  402. #ifdef LIBERAL_CHANGETYPE_MODOP
  403. /* trim trailing spaces (and log warning ...) */
  404. int icnt;
  405. for ( icnt = lr->lr_vals[i].bv_len; --icnt > 0; ) {
  406. if ( !isspace( (unsigned char) lr->lr_vals[i].bv_val[icnt] ) ) break;
  407. }
  408. if ( ++icnt != lr->lr_vals[i].bv_len ) {
  409. fprintf( stderr, _("%s: illegal trailing space after"
  410. " \"%s: %s\" trimmed (line %lu, entry \"%s\")\n"),
  411. errstr, type, lr->lr_vals[i].bv_val, linenum+i, dn );
  412. lr->lr_vals[i].bv_val[icnt] = '\0';
  413. }
  414. #endif /* LIBERAL_CHANGETYPE_MODOP */
  415. expect_modop = 0;
  416. expect_sep = 1;
  417. if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPADD )) {
  418. modop = LDAP_MOD_ADD;
  419. lr->lr_mops[i] = M_SEP;
  420. nmods--;
  421. } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPREPLACE )) {
  422. /* defer handling these since they might have no values.
  423. * Use the BVALUES flag to signal that these were
  424. * deferred. If values are provided later, this
  425. * flag will be switched off.
  426. */
  427. modop = LDAP_MOD_REPLACE;
  428. lr->lr_mops[i] = modop | LDAP_MOD_BVALUES;
  429. lr->lr_btype[i] = lr->lr_vals[i];
  430. } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPDELETE )) {
  431. modop = LDAP_MOD_DELETE;
  432. lr->lr_mops[i] = modop | LDAP_MOD_BVALUES;
  433. lr->lr_btype[i] = lr->lr_vals[i];
  434. } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPINCREMENT )) {
  435. modop = LDAP_MOD_INCREMENT;
  436. lr->lr_mops[i] = M_SEP;
  437. nmods--;
  438. } else { /* no modify op: invalid LDIF */
  439. fprintf( stderr, _("%s: modify operation type is missing at"
  440. " line %lu, entry \"%s\"\n"),
  441. errstr, linenum+i, dn );
  442. rc = LDAP_PARAM_ERROR;
  443. goto leave;
  444. }
  445. bv = lr->lr_vals[i];
  446. } else if ( expect_sep && BER_BVISEMPTY( lr->lr_btype+i )) {
  447. lr->lr_mops[i] = M_SEP;
  448. expect_sep = 0;
  449. expect_modop = 1;
  450. nmods--;
  451. } else {
  452. if ( !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
  453. fprintf( stderr, _("%s: wrong attributeType at"
  454. " line %lu, entry \"%s\"\n"),
  455. errstr, linenum+i, dn );
  456. rc = LDAP_PARAM_ERROR;
  457. goto leave;
  458. }
  459. lr->lr_mops[i] = modop;
  460. /* If prev op was deferred and matches this type,
  461. * clear the flag
  462. */
  463. if ( (lr->lr_mops[i-1] & LDAP_MOD_BVALUES)
  464. && BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+i-1 ))
  465. {
  466. lr->lr_mops[i-1] = M_SEP;
  467. nmods--;
  468. }
  469. }
  470. }
  471. /* Allocate space for array of mods, array of pointers to mods,
  472. * and array of pointers to values, allowing for NULL terminators
  473. * for the pointer arrays...
  474. */
  475. lr->lr_lm = ber_memalloc_x( nmods * sizeof(LDAPMod) +
  476. (nmods+1) * sizeof(LDAPMod*) +
  477. (lr->lr_lines + nmods - idn) * sizeof(struct berval *), ctx );
  478. if ( lr->lr_lm == NULL ) {
  479. rc = LDAP_NO_MEMORY;
  480. goto leave;
  481. }
  482. pmods = (LDAPMod **)(lr->lr_lm+nmods);
  483. bvl = (struct berval **)(pmods+nmods+1);
  484. j = 0;
  485. k = -1;
  486. BER_BVZERO(&bv);
  487. if ( idn > 0 )
  488. lr->lr_mops[idn-1] = M_SEP;
  489. for (i=idn; i<lr->lr_lines; i++) {
  490. if ( lr->lr_mops[i] == M_SEP )
  491. continue;
  492. if ( lr->lr_mops[i] != lr->lr_mops[i-1] || !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
  493. bvl[k++] = NULL;
  494. bv = lr->lr_btype[i];
  495. lr->lr_lm[j].mod_op = lr->lr_mops[i] | LDAP_MOD_BVALUES;
  496. lr->lr_lm[j].mod_type = bv.bv_val;
  497. if ( lr->lr_mops[i] & LDAP_MOD_BVALUES ) {
  498. lr->lr_lm[j].mod_bvalues = NULL;
  499. } else {
  500. lr->lr_lm[j].mod_bvalues = bvl+k;
  501. }
  502. pmods[j] = lr->lr_lm+j;
  503. j++;
  504. }
  505. bvl[k++] = lr->lr_vals+i;
  506. }
  507. bvl[k] = NULL;
  508. pmods[j] = NULL;
  509. doit:
  510. /* first, set the common fields */
  511. lr->lr_ctrls = pctrls;
  512. /* next, set the op */
  513. if ( delete_entry ) {
  514. lr->lr_op = LDAP_REQ_DELETE;
  515. } else if ( lr->lrop_newrdn.bv_val != NULL ) {
  516. lr->lr_op = LDAP_REQ_MODDN;
  517. } else {
  518. /* for now, either add or modify */
  519. lr->lrop_mods = pmods;
  520. if ( new_entry ) {
  521. lr->lr_op = LDAP_REQ_ADD;
  522. } else {
  523. lr->lr_op = LDAP_REQ_MODIFY;
  524. }
  525. }
  526. leave:
  527. if ( rc != LDAP_SUCCESS ) {
  528. ldap_ldif_record_done( lr );
  529. }
  530. return( rc );
  531. }
  532. /* Same as ldap_parse_ldif_record_x()
  533. * public API does not expose memory context
  534. */
  535. int
  536. ldap_parse_ldif_record(
  537. struct berval *rbuf,
  538. unsigned long linenum,
  539. LDIFRecord *lr,
  540. const char *errstr,
  541. unsigned int flags )
  542. {
  543. return ldap_parse_ldif_record_x( rbuf, linenum, lr, errstr, flags, NULL );
  544. }
  545. /* Parse an LDIF control line of the form
  546. control: oid [true/false] [: value] or
  547. control: oid [true/false] [:: base64-value] or
  548. control: oid [true/false] [:< url]
  549. The control is added to the list of controls in *ppctrls.
  550. */
  551. static int
  552. parse_ldif_control(
  553. struct berval *bval,
  554. LDAPControl ***ppctrls)
  555. {
  556. char *oid = NULL;
  557. int criticality = 0; /* Default is false if not present */
  558. int i, rc=0;
  559. char *s, *oidStart;
  560. LDAPControl *newctrl = NULL;
  561. LDAPControl **pctrls = NULL;
  562. struct berval type, bv = BER_BVNULL;
  563. int freeval = 0;
  564. if (ppctrls) pctrls = *ppctrls;
  565. /* OID should come first. Validate and extract it. */
  566. s = bval->bv_val;
  567. if (*s == 0) return ( LDAP_PARAM_ERROR );
  568. oidStart = s;
  569. while (isdigit((unsigned char)*s) || *s == '.') {
  570. s++; /* OID should be digits or . */
  571. }
  572. if (s == oidStart) {
  573. return ( LDAP_PARAM_ERROR ); /* OID was not present */
  574. }
  575. if (*s) { /* End of OID should be space or NULL */
  576. if (!isspace((unsigned char)*s)) {
  577. return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
  578. }
  579. *s++ = 0; /* Replace space with null to terminate */
  580. }
  581. oid = ber_strdup(oidStart);
  582. if (oid == NULL) return ( LDAP_NO_MEMORY );
  583. /* Optional Criticality field is next. */
  584. while (*s && isspace((unsigned char)*s)) {
  585. s++; /* Skip white space before criticality */
  586. }
  587. if (strncasecmp(s, "true", 4) == 0) {
  588. criticality = 1;
  589. s += 4;
  590. }
  591. else if (strncasecmp(s, "false", 5) == 0) {
  592. criticality = 0;
  593. s += 5;
  594. }
  595. /* Optional value field is next */
  596. while (*s && isspace((unsigned char)*s)) {
  597. s++; /* Skip white space before value */
  598. }
  599. if (*s) {
  600. if (*s != ':') { /* If value is present, must start with : */
  601. rc = LDAP_PARAM_ERROR;
  602. goto cleanup;
  603. }
  604. /* Back up so value is in the form
  605. a: value
  606. a:: base64-value
  607. a:< url
  608. Then we can use ldif_parse_line2 to extract and decode the value
  609. */
  610. s--;
  611. *s = 'a';
  612. rc = ldif_parse_line2(s, &type, &bv, &freeval);
  613. if (rc < 0) {
  614. rc = LDAP_PARAM_ERROR;
  615. goto cleanup;
  616. }
  617. }
  618. /* Create a new LDAPControl structure. */
  619. newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
  620. if ( newctrl == NULL ) {
  621. rc = LDAP_NO_MEMORY;
  622. goto cleanup;
  623. }
  624. newctrl->ldctl_oid = oid;
  625. oid = NULL;
  626. newctrl->ldctl_iscritical = criticality;
  627. if ( freeval )
  628. newctrl->ldctl_value = bv;
  629. else
  630. ber_dupbv( &newctrl->ldctl_value, &bv );
  631. /* Add the new control to the passed-in list of controls. */
  632. i = 0;
  633. if (pctrls) {
  634. while ( pctrls[i] ) { /* Count the # of controls passed in */
  635. i++;
  636. }
  637. }
  638. /* Allocate 1 more slot for the new control and 1 for the NULL. */
  639. pctrls = (LDAPControl **) ber_memrealloc(pctrls,
  640. (i+2)*(sizeof(LDAPControl *)));
  641. if (pctrls == NULL) {
  642. rc = LDAP_NO_MEMORY;
  643. goto cleanup;
  644. }
  645. pctrls[i] = newctrl;
  646. newctrl = NULL;
  647. pctrls[i+1] = NULL;
  648. *ppctrls = pctrls;
  649. cleanup:
  650. if (newctrl) {
  651. if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
  652. if (newctrl->ldctl_value.bv_val) {
  653. ber_memfree(newctrl->ldctl_value.bv_val);
  654. }
  655. ber_memfree(newctrl);
  656. }
  657. if (oid) ber_memfree(oid);
  658. return( rc );
  659. }