ldifutil.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2024 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. int icnt;
  212. for ( icnt = lr->lr_vals[i].bv_len; --icnt > 0; ) {
  213. if ( !isspace( (unsigned char) lr->lr_vals[i].bv_val[icnt] ) ) {
  214. break;
  215. }
  216. }
  217. if ( ++icnt != lr->lr_vals[i].bv_len ) {
  218. #ifdef LIBERAL_CHANGETYPE_MODOP
  219. /* trim trailing spaces (and log warning ...) */
  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. lr->lr_vals[i].bv_len = icnt;
  225. #else /* !LIBERAL_CHANGETYPE_MODOP */
  226. fprintf( stderr, _("%s: illegal trailing space after"
  227. " \"%s: %s\" (line %lu, entry \"%s\")\n"),
  228. errstr, BV_CHANGETYPE.bv_val, lr->lr_vals[i].bv_val, linenum+i, dn );
  229. rc = LDAP_PARAM_ERROR;
  230. goto leave;
  231. #endif /* !LIBERAL_CHANGETYPE_MODOP */
  232. }
  233. /* if LDIF_ENTRIES_ONLY, then either the changetype must be add, or
  234. there must be no changetype, and the flag LDIF_DEFAULT_ADD must be set */
  235. if ( flags & LDIF_ENTRIES_ONLY ) {
  236. if ( !( BV_CASEMATCH( lr->lr_vals+i, &BV_ADDCT )) ) {
  237. ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
  238. _("%s: skipping LDIF record beginning at line %lu: "
  239. "changetype '%.*s' found but entries only was requested\n"),
  240. errstr, linenum,
  241. (int)lr->lr_vals[i].bv_len,
  242. (const char *)lr->lr_vals[i].bv_val );
  243. goto leave;
  244. }
  245. }
  246. if ( BV_CASEMATCH( lr->lr_vals+i, &BV_MODIFYCT )) {
  247. new_entry = 0;
  248. expect_modop = 1;
  249. } else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_ADDCT )) {
  250. new_entry = 1;
  251. modop = LDAP_MOD_ADD;
  252. } else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_MODRDNCT )
  253. || BV_CASEMATCH( lr->lr_vals+i, &BV_MODDNCT )
  254. || BV_CASEMATCH( lr->lr_vals+i, &BV_RENAMECT ))
  255. {
  256. i++;
  257. if ( i >= lr->lr_lines )
  258. goto short_input;
  259. if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_NEWRDN )) {
  260. fprintf( stderr, _("%s: expecting \"%s:\" but saw"
  261. " \"%s:\" (line %lu, entry \"%s\")\n"),
  262. errstr, BV_NEWRDN.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn );
  263. rc = LDAP_PARAM_ERROR;
  264. goto leave;
  265. }
  266. lr->lrop_newrdn = lr->lr_vals[i];
  267. i++;
  268. if ( i >= lr->lr_lines )
  269. goto short_input;
  270. if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_DELETEOLDRDN )) {
  271. fprintf( stderr, _("%s: expecting \"%s:\" but saw"
  272. " \"%s:\" (line %lu, entry \"%s\")\n"),
  273. errstr, BV_DELETEOLDRDN.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn );
  274. rc = LDAP_PARAM_ERROR;
  275. goto leave;
  276. }
  277. lr->lrop_delold = ( lr->lr_vals[i].bv_val[0] == '0' ) ? 0 : 1;
  278. i++;
  279. if ( i < lr->lr_lines ) {
  280. if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_NEWSUP )) {
  281. fprintf( stderr, _("%s: expecting \"%s:\" but saw"
  282. " \"%s:\" (line %lu, entry \"%s\")\n"),
  283. errstr, BV_NEWSUP.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn );
  284. rc = LDAP_PARAM_ERROR;
  285. goto leave;
  286. }
  287. lr->lrop_newsup = lr->lr_vals[i];
  288. i++;
  289. }
  290. got_all = 1;
  291. } else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_DELETECT )) {
  292. got_all = delete_entry = 1;
  293. } else {
  294. fprintf( stderr,
  295. _("%s: unknown %s \"%s\" (line %lu, entry \"%s\")\n"),
  296. errstr, BV_CHANGETYPE.bv_val, lr->lr_vals[i].bv_val, linenum+i, dn );
  297. rc = LDAP_PARAM_ERROR;
  298. goto leave;
  299. }
  300. i++;
  301. } else if ( ldapadd ) { /* missing changetype => add */
  302. new_entry = 1;
  303. modop = LDAP_MOD_ADD;
  304. } else {
  305. /* if LDIF_ENTRIES_ONLY, then either the changetype must be add, or
  306. there must be no changetype, and the flag LDIF_DEFAULT_ADD must be set */
  307. if ( flags & LDIF_ENTRIES_ONLY ) {
  308. ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
  309. _("%s: skipping LDIF record beginning at line %lu: "
  310. "no changetype found but entries only was requested and "
  311. "the default setting for missing changetype is modify\n"),
  312. errstr, linenum );
  313. goto leave;
  314. }
  315. expect_modop = 1; /* missing changetype => modify */
  316. }
  317. if ( got_all ) {
  318. if ( i < lr->lr_lines ) {
  319. fprintf( stderr,
  320. _("%s: extra lines at end (line %lu, entry \"%s\")\n"),
  321. errstr, linenum+i, dn );
  322. rc = LDAP_PARAM_ERROR;
  323. goto leave;
  324. }
  325. goto doit;
  326. }
  327. nmods = lr->lr_lines - i;
  328. idn = i;
  329. if ( new_entry ) {
  330. int fv;
  331. /* Make sure all attributes with multiple values are contiguous */
  332. for (; i<lr->lr_lines; i++) {
  333. for (j=i+1; j<lr->lr_lines; j++) {
  334. if ( !lr->lr_btype[j].bv_val ) {
  335. fprintf( stderr,
  336. _("%s: missing attributeDescription (line %lu, entry \"%s\")\n"),
  337. errstr, linenum+j, dn );
  338. rc = LDAP_PARAM_ERROR;
  339. goto leave;
  340. }
  341. if ( BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+j )) {
  342. nmods--;
  343. /* out of order, move intervening attributes down */
  344. if ( j != i+1 ) {
  345. bv = lr->lr_vals[j];
  346. fv = lr->lr_freeval[j];
  347. for (k=j; k>i; k--) {
  348. lr->lr_btype[k] = lr->lr_btype[k-1];
  349. lr->lr_vals[k] = lr->lr_vals[k-1];
  350. lr->lr_freeval[k] = lr->lr_freeval[k-1];
  351. }
  352. k++;
  353. lr->lr_btype[k] = lr->lr_btype[i];
  354. lr->lr_vals[k] = bv;
  355. lr->lr_freeval[k] = fv;
  356. }
  357. i++;
  358. }
  359. }
  360. }
  361. /* Allocate space for array of mods, array of pointers to mods,
  362. * and array of pointers to values, allowing for NULL terminators
  363. * for the pointer arrays...
  364. */
  365. lr->lr_lm = ber_memalloc_x( nmods * sizeof(LDAPMod) +
  366. (nmods+1) * sizeof(LDAPMod*) +
  367. (lr->lr_lines + nmods - idn) * sizeof(struct berval *), ctx );
  368. if ( lr->lr_lm == NULL ) {
  369. rc = LDAP_NO_MEMORY;
  370. goto leave;
  371. }
  372. pmods = (LDAPMod **)(lr->lr_lm+nmods);
  373. bvl = (struct berval **)(pmods+nmods+1);
  374. j = 0;
  375. k = -1;
  376. BER_BVZERO(&bv);
  377. for (i=idn; i<lr->lr_lines; i++) {
  378. if ( BV_CASEMATCH( lr->lr_btype+i, &BV_DN )) {
  379. fprintf( stderr, _("%s: attributeDescription \"%s\":"
  380. " (possible missing newline"
  381. " after line %lu, entry \"%s\"?)\n"),
  382. errstr, lr->lr_btype[i].bv_val, linenum+i - 1, dn );
  383. }
  384. if ( !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
  385. bvl[k++] = NULL;
  386. bv = lr->lr_btype[i];
  387. lr->lr_lm[j].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
  388. lr->lr_lm[j].mod_type = bv.bv_val;
  389. lr->lr_lm[j].mod_bvalues = bvl+k;
  390. pmods[j] = lr->lr_lm+j;
  391. j++;
  392. }
  393. bvl[k++] = lr->lr_vals+i;
  394. }
  395. bvl[k] = NULL;
  396. pmods[j] = NULL;
  397. goto doit;
  398. }
  399. lr->lr_mops = ber_memalloc_x( lr->lr_lines+1, ctx );
  400. if ( lr->lr_mops == NULL ) {
  401. rc = LDAP_NO_MEMORY;
  402. goto leave;
  403. }
  404. lr->lr_mops[lr->lr_lines] = M_SEP;
  405. if ( i > 0 )
  406. lr->lr_mops[i-1] = M_SEP;
  407. for ( ; i<lr->lr_lines; i++ ) {
  408. if ( expect_modop ) {
  409. /* trim trailing spaces (and log warning ...) */
  410. int icnt;
  411. for ( icnt = lr->lr_vals[i].bv_len; --icnt > 0; ) {
  412. if ( !isspace( (unsigned char) lr->lr_vals[i].bv_val[icnt] ) ) break;
  413. }
  414. if ( ++icnt != lr->lr_vals[i].bv_len ) {
  415. #ifdef LIBERAL_CHANGETYPE_MODOP
  416. fprintf( stderr, _("%s: illegal trailing space after"
  417. " \"%s: %s\" trimmed (line %lu, entry \"%s\")\n"),
  418. errstr, lr->lr_btype[i].bv_val, lr->lr_vals[i].bv_val,
  419. linenum+i, dn );
  420. lr->lr_vals[i].bv_val[icnt] = '\0';
  421. lr->lr_vals[i].bv_len = icnt;
  422. #else /* !LIBERAL_CHANGETYPE_MODOP */
  423. fprintf( stderr, _("%s: illegal trailing space after"
  424. " \"%s: %s\" (line %lu, entry \"%s\")\n"),
  425. errstr, lr->lr_btype[i].bv_val, lr->lr_vals[i].bv_val,
  426. linenum+i, dn );
  427. rc = LDAP_PARAM_ERROR;
  428. goto leave;
  429. #endif /* !LIBERAL_CHANGETYPE_MODOP */
  430. }
  431. expect_modop = 0;
  432. expect_sep = 1;
  433. if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPADD )) {
  434. modop = LDAP_MOD_ADD;
  435. lr->lr_mops[i] = M_SEP;
  436. nmods--;
  437. } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPREPLACE )) {
  438. /* defer handling these since they might have no values.
  439. * Use the BVALUES flag to signal that these were
  440. * deferred. If values are provided later, this
  441. * flag will be switched off.
  442. */
  443. modop = LDAP_MOD_REPLACE;
  444. lr->lr_mops[i] = modop | LDAP_MOD_BVALUES;
  445. lr->lr_btype[i] = lr->lr_vals[i];
  446. } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPDELETE )) {
  447. modop = LDAP_MOD_DELETE;
  448. lr->lr_mops[i] = modop | LDAP_MOD_BVALUES;
  449. lr->lr_btype[i] = lr->lr_vals[i];
  450. } else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPINCREMENT )) {
  451. modop = LDAP_MOD_INCREMENT;
  452. lr->lr_mops[i] = M_SEP;
  453. nmods--;
  454. } else { /* no modify op: invalid LDIF */
  455. fprintf( stderr, _("%s: modify operation type is missing at"
  456. " line %lu, entry \"%s\"\n"),
  457. errstr, linenum+i, dn );
  458. rc = LDAP_PARAM_ERROR;
  459. goto leave;
  460. }
  461. bv = lr->lr_vals[i];
  462. } else if ( expect_sep && BER_BVISEMPTY( lr->lr_btype+i )) {
  463. lr->lr_mops[i] = M_SEP;
  464. expect_sep = 0;
  465. expect_modop = 1;
  466. nmods--;
  467. } else {
  468. if ( !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
  469. fprintf( stderr, _("%s: wrong attributeType at"
  470. " line %lu, entry \"%s\"\n"),
  471. errstr, linenum+i, dn );
  472. rc = LDAP_PARAM_ERROR;
  473. goto leave;
  474. }
  475. lr->lr_mops[i] = modop;
  476. /* If prev op was deferred and matches this type,
  477. * clear the flag
  478. */
  479. if ( (lr->lr_mops[i-1] & LDAP_MOD_BVALUES)
  480. && BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+i-1 ))
  481. {
  482. lr->lr_mops[i-1] = M_SEP;
  483. nmods--;
  484. }
  485. }
  486. }
  487. /* Allocate space for array of mods, array of pointers to mods,
  488. * and array of pointers to values, allowing for NULL terminators
  489. * for the pointer arrays...
  490. */
  491. lr->lr_lm = ber_memalloc_x( nmods * sizeof(LDAPMod) +
  492. (nmods+1) * sizeof(LDAPMod*) +
  493. (lr->lr_lines + nmods - idn) * sizeof(struct berval *), ctx );
  494. if ( lr->lr_lm == NULL ) {
  495. rc = LDAP_NO_MEMORY;
  496. goto leave;
  497. }
  498. pmods = (LDAPMod **)(lr->lr_lm+nmods);
  499. bvl = (struct berval **)(pmods+nmods+1);
  500. j = 0;
  501. k = -1;
  502. BER_BVZERO(&bv);
  503. if ( idn > 0 )
  504. lr->lr_mops[idn-1] = M_SEP;
  505. for (i=idn; i<lr->lr_lines; i++) {
  506. if ( lr->lr_mops[i] == M_SEP )
  507. continue;
  508. if ( lr->lr_mops[i] != lr->lr_mops[i-1] || !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
  509. bvl[k++] = NULL;
  510. bv = lr->lr_btype[i];
  511. lr->lr_lm[j].mod_op = lr->lr_mops[i] | LDAP_MOD_BVALUES;
  512. lr->lr_lm[j].mod_type = bv.bv_val;
  513. if ( lr->lr_mops[i] & LDAP_MOD_BVALUES ) {
  514. lr->lr_lm[j].mod_bvalues = NULL;
  515. } else {
  516. lr->lr_lm[j].mod_bvalues = bvl+k;
  517. }
  518. pmods[j] = lr->lr_lm+j;
  519. j++;
  520. }
  521. bvl[k++] = lr->lr_vals+i;
  522. }
  523. bvl[k] = NULL;
  524. pmods[j] = NULL;
  525. doit:
  526. /* first, set the common fields */
  527. lr->lr_ctrls = pctrls;
  528. /* next, set the op */
  529. if ( delete_entry ) {
  530. lr->lr_op = LDAP_REQ_DELETE;
  531. } else if ( lr->lrop_newrdn.bv_val != NULL ) {
  532. lr->lr_op = LDAP_REQ_MODDN;
  533. } else {
  534. /* for now, either add or modify */
  535. lr->lrop_mods = pmods;
  536. if ( new_entry ) {
  537. lr->lr_op = LDAP_REQ_ADD;
  538. } else {
  539. lr->lr_op = LDAP_REQ_MODIFY;
  540. }
  541. }
  542. leave:
  543. if ( rc != LDAP_SUCCESS ) {
  544. ldap_ldif_record_done( lr );
  545. }
  546. return( rc );
  547. }
  548. /* Same as ldap_parse_ldif_record_x()
  549. * public API does not expose memory context
  550. */
  551. int
  552. ldap_parse_ldif_record(
  553. struct berval *rbuf,
  554. unsigned long linenum,
  555. LDIFRecord *lr,
  556. const char *errstr,
  557. unsigned int flags )
  558. {
  559. return ldap_parse_ldif_record_x( rbuf, linenum, lr, errstr, flags, NULL );
  560. }
  561. /* Parse an LDIF control line of the form
  562. control: oid [true/false] [: value] or
  563. control: oid [true/false] [:: base64-value] or
  564. control: oid [true/false] [:< url]
  565. The control is added to the list of controls in *ppctrls.
  566. */
  567. static int
  568. parse_ldif_control(
  569. struct berval *bval,
  570. LDAPControl ***ppctrls)
  571. {
  572. char *oid = NULL;
  573. int criticality = 0; /* Default is false if not present */
  574. int i, rc=0;
  575. char *s, *oidStart;
  576. LDAPControl *newctrl = NULL;
  577. LDAPControl **pctrls = NULL;
  578. struct berval type, bv = BER_BVNULL;
  579. int freeval = 0;
  580. if (ppctrls) pctrls = *ppctrls;
  581. /* OID should come first. Validate and extract it. */
  582. s = bval->bv_val;
  583. if (*s == 0) return ( LDAP_PARAM_ERROR );
  584. oidStart = s;
  585. while (isdigit((unsigned char)*s) || *s == '.') {
  586. s++; /* OID should be digits or . */
  587. }
  588. if (s == oidStart) {
  589. return ( LDAP_PARAM_ERROR ); /* OID was not present */
  590. }
  591. if (*s) { /* End of OID should be space or NULL */
  592. if (!isspace((unsigned char)*s)) {
  593. return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
  594. }
  595. *s++ = 0; /* Replace space with null to terminate */
  596. }
  597. oid = ber_strdup(oidStart);
  598. if (oid == NULL) return ( LDAP_NO_MEMORY );
  599. /* Optional Criticality field is next. */
  600. while (*s && isspace((unsigned char)*s)) {
  601. s++; /* Skip white space before criticality */
  602. }
  603. if (strncasecmp(s, "true", 4) == 0) {
  604. criticality = 1;
  605. s += 4;
  606. }
  607. else if (strncasecmp(s, "false", 5) == 0) {
  608. criticality = 0;
  609. s += 5;
  610. }
  611. /* Optional value field is next */
  612. while (*s && isspace((unsigned char)*s)) {
  613. s++; /* Skip white space before value */
  614. }
  615. if (*s) {
  616. if (*s != ':') { /* If value is present, must start with : */
  617. rc = LDAP_PARAM_ERROR;
  618. goto cleanup;
  619. }
  620. /* Back up so value is in the form
  621. a: value
  622. a:: base64-value
  623. a:< url
  624. Then we can use ldif_parse_line2 to extract and decode the value
  625. */
  626. s--;
  627. *s = 'a';
  628. rc = ldif_parse_line2(s, &type, &bv, &freeval);
  629. if (rc < 0) {
  630. rc = LDAP_PARAM_ERROR;
  631. goto cleanup;
  632. }
  633. }
  634. /* Create a new LDAPControl structure. */
  635. newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
  636. if ( newctrl == NULL ) {
  637. rc = LDAP_NO_MEMORY;
  638. goto cleanup;
  639. }
  640. newctrl->ldctl_oid = oid;
  641. oid = NULL;
  642. newctrl->ldctl_iscritical = criticality;
  643. if ( freeval )
  644. newctrl->ldctl_value = bv;
  645. else
  646. ber_dupbv( &newctrl->ldctl_value, &bv );
  647. /* Add the new control to the passed-in list of controls. */
  648. i = 0;
  649. if (pctrls) {
  650. while ( pctrls[i] ) { /* Count the # of controls passed in */
  651. i++;
  652. }
  653. }
  654. /* Allocate 1 more slot for the new control and 1 for the NULL. */
  655. pctrls = (LDAPControl **) ber_memrealloc(pctrls,
  656. (i+2)*(sizeof(LDAPControl *)));
  657. if (pctrls == NULL) {
  658. rc = LDAP_NO_MEMORY;
  659. goto cleanup;
  660. }
  661. pctrls[i] = newctrl;
  662. newctrl = NULL;
  663. pctrls[i+1] = NULL;
  664. *ppctrls = pctrls;
  665. cleanup:
  666. if (newctrl) {
  667. if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
  668. if (newctrl->ldctl_value.bv_val) {
  669. ber_memfree(newctrl->ldctl_value.bv_val);
  670. }
  671. ber_memfree(newctrl);
  672. }
  673. if (oid) ber_memfree(oid);
  674. return( rc );
  675. }