ldif.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /* ldif.c - routines for dealing with LDIF files */
  2. /* $OpenLDAP$ */
  3. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  4. *
  5. * Copyright 1998-2022 The OpenLDAP Foundation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted only as authorized by the OpenLDAP
  10. * Public License.
  11. *
  12. * A copy of this license is available in the file LICENSE in the
  13. * top-level directory of the distribution or, alternatively, at
  14. * <http://www.OpenLDAP.org/license.html>.
  15. */
  16. /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
  17. * All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms are permitted
  20. * provided that this notice is preserved and that due credit is given
  21. * to the University of Michigan at Ann Arbor. The name of the
  22. * University may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission. This
  24. * software is provided ``as is'' without express or implied warranty.
  25. */
  26. /* This work was originally developed by the University of Michigan
  27. * and distributed as part of U-MICH LDAP.
  28. */
  29. #include "portable.h"
  30. #include <stdio.h>
  31. #include <ac/stdlib.h>
  32. #include <ac/ctype.h>
  33. #include <ac/string.h>
  34. #include <ac/socket.h>
  35. #include <ac/time.h>
  36. int ldif_debug = 0;
  37. #include "ldap-int.h"
  38. #include "ldif.h"
  39. #define CONTINUED_LINE_MARKER '\r'
  40. #ifdef CSRIMALLOC
  41. #define ber_memalloc malloc
  42. #define ber_memcalloc calloc
  43. #define ber_memrealloc realloc
  44. #define ber_strdup strdup
  45. #endif
  46. static const char nib2b64[0x40] =
  47. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  48. /*
  49. * ldif_parse_line - takes a line of the form "type:[:] value" and splits it
  50. * into components "type" and "value". if a double colon separates type from
  51. * value, then value is encoded in base 64, and parse_line un-decodes it
  52. * (in place) before returning. The type and value are stored in malloc'd
  53. * memory which must be freed by the caller.
  54. *
  55. * ldif_parse_line2 - operates in-place on input buffer, returning type
  56. * in-place. Will return value in-place if possible, (must malloc for
  57. * fetched URLs). If freeval is NULL, all return data will be malloc'd
  58. * and the input line will be unmodified. Otherwise freeval is set to
  59. * True if the value was malloc'd.
  60. */
  61. int
  62. ldif_parse_line(
  63. LDAP_CONST char *line,
  64. char **typep,
  65. char **valuep,
  66. ber_len_t *vlenp
  67. )
  68. {
  69. struct berval type, value;
  70. int rc = ldif_parse_line2( (char *)line, &type, &value, NULL );
  71. *typep = type.bv_val;
  72. *valuep = value.bv_val;
  73. *vlenp = value.bv_len;
  74. return rc;
  75. }
  76. int
  77. ldif_parse_line2(
  78. char *line,
  79. struct berval *type,
  80. struct berval *value,
  81. int *freeval
  82. )
  83. {
  84. char *s, *p, *d;
  85. int b64, url;
  86. BER_BVZERO( type );
  87. BER_BVZERO( value );
  88. /* skip any leading space */
  89. while ( isspace( (unsigned char) *line ) ) {
  90. line++;
  91. }
  92. if ( freeval ) {
  93. *freeval = 0;
  94. } else {
  95. line = ber_strdup( line );
  96. if( line == NULL ) {
  97. ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
  98. _("ldif_parse_line: line malloc failed\n"));
  99. return( -1 );
  100. }
  101. }
  102. type->bv_val = line;
  103. s = strchr( type->bv_val, ':' );
  104. if ( s == NULL ) {
  105. ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
  106. _("ldif_parse_line: missing ':' after %s\n"),
  107. type->bv_val );
  108. if ( !freeval ) ber_memfree( line );
  109. return( -1 );
  110. }
  111. /* trim any space between type and : */
  112. for ( p = &s[-1]; p > type->bv_val && isspace( * (unsigned char *) p ); p-- ) {
  113. *p = '\0';
  114. }
  115. *s++ = '\0';
  116. type->bv_len = s - type->bv_val - 1;
  117. url = 0;
  118. b64 = 0;
  119. if ( *s == '<' ) {
  120. s++;
  121. url = 1;
  122. } else if ( *s == ':' ) {
  123. /* base 64 encoded value */
  124. s++;
  125. b64 = 1;
  126. }
  127. /* skip space between : and value */
  128. while ( isspace( (unsigned char) *s ) ) {
  129. s++;
  130. }
  131. /* check for continued line markers that should be deleted */
  132. for ( p = s, d = s; *p; p++ ) {
  133. if ( *p != CONTINUED_LINE_MARKER )
  134. *d++ = *p;
  135. }
  136. *d = '\0';
  137. if ( b64 ) {
  138. char *byte = s;
  139. if ( *s == '\0' ) {
  140. /* no value is present, error out */
  141. ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
  142. _("ldif_parse_line: %s missing base64 value\n"),
  143. type->bv_val );
  144. if ( !freeval ) ber_memfree( line );
  145. return( -1 );
  146. }
  147. value->bv_val = s;
  148. value->bv_len = d - s;
  149. if ( ldap_int_decode_b64_inplace( value ) != LDAP_SUCCESS ) {
  150. ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
  151. _("ldif_parse_line: %s base64 decode failed\n"),
  152. type->bv_val );
  153. if ( !freeval ) ber_memfree( line );
  154. return( -1 );
  155. }
  156. } else if ( url ) {
  157. if ( *s == '\0' ) {
  158. /* no value is present, error out */
  159. ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
  160. _("ldif_parse_line: %s missing URL value\n"),
  161. type->bv_val );
  162. if ( !freeval ) ber_memfree( line );
  163. return( -1 );
  164. }
  165. if( ldif_fetch_url( s, &value->bv_val, &value->bv_len ) ) {
  166. ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
  167. _("ldif_parse_line: %s: URL \"%s\" fetch failed\n"),
  168. type->bv_val, s );
  169. if ( !freeval ) ber_memfree( line );
  170. return( -1 );
  171. }
  172. if ( freeval ) *freeval = 1;
  173. } else {
  174. value->bv_val = s;
  175. value->bv_len = (int) (d - s);
  176. }
  177. if ( !freeval ) {
  178. struct berval bv = *type;
  179. ber_dupbv( type, &bv );
  180. if( BER_BVISNULL( type )) {
  181. ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
  182. _("ldif_parse_line: type malloc failed\n"));
  183. if( url ) ber_memfree( value->bv_val );
  184. ber_memfree( line );
  185. return( -1 );
  186. }
  187. if( !url ) {
  188. bv = *value;
  189. ber_dupbv( value, &bv );
  190. if( BER_BVISNULL( value )) {
  191. ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
  192. _("ldif_parse_line: value malloc failed\n"));
  193. ber_memfree( type->bv_val );
  194. ber_memfree( line );
  195. return( -1 );
  196. }
  197. }
  198. ber_memfree( line );
  199. }
  200. return( 0 );
  201. }
  202. /*
  203. * ldif_getline - return the next "line" (minus newline) of input from a
  204. * string buffer of lines separated by newlines, terminated by \n\n
  205. * or \0. this routine handles continued lines, bundling them into
  206. * a single big line before returning. if a line begins with a white
  207. * space character, it is a continuation of the previous line. the white
  208. * space character (nb: only one char), and preceding newline are changed
  209. * into CONTINUED_LINE_MARKER chars, to be deleted later by the
  210. * ldif_parse_line() routine above.
  211. *
  212. * ldif_getline will skip over any line which starts '#'.
  213. *
  214. * ldif_getline takes a pointer to a pointer to the buffer on the first call,
  215. * which it updates and must be supplied on subsequent calls.
  216. */
  217. int
  218. ldif_countlines( LDAP_CONST char *buf )
  219. {
  220. char *nl;
  221. int ret = 0;
  222. if ( !buf ) return ret;
  223. for ( nl = strchr(buf, '\n'); nl; nl = strchr(nl, '\n') ) {
  224. nl++;
  225. if ( *nl != ' ' ) ret++;
  226. }
  227. return ret;
  228. }
  229. char *
  230. ldif_getline( char **next )
  231. {
  232. char *line;
  233. do {
  234. if ( *next == NULL || **next == '\n' || **next == '\0' ) {
  235. return( NULL );
  236. }
  237. line = *next;
  238. while ( (*next = strchr( *next, '\n' )) != NULL ) {
  239. #if CONTINUED_LINE_MARKER != '\r'
  240. if ( (*next)[-1] == '\r' ) {
  241. (*next)[-1] = CONTINUED_LINE_MARKER;
  242. }
  243. #endif
  244. if ( (*next)[1] != ' ' ) {
  245. if ( (*next)[1] == '\r' && (*next)[2] == '\n' ) {
  246. *(*next)++ = '\0';
  247. }
  248. *(*next)++ = '\0';
  249. break;
  250. }
  251. **next = CONTINUED_LINE_MARKER;
  252. (*next)[1] = CONTINUED_LINE_MARKER;
  253. (*next)++;
  254. }
  255. } while( *line == '#' );
  256. return( line );
  257. }
  258. /*
  259. * name and OID of attributeTypes that must be base64 encoded in any case
  260. */
  261. typedef struct must_b64_encode_s {
  262. struct berval name;
  263. struct berval oid;
  264. } must_b64_encode_s;
  265. static must_b64_encode_s default_must_b64_encode[] = {
  266. { BER_BVC( "userPassword" ), BER_BVC( "2.5.4.35" ) },
  267. { BER_BVNULL, BER_BVNULL }
  268. };
  269. static must_b64_encode_s *must_b64_encode = default_must_b64_encode;
  270. /*
  271. * register name and OID of attributeTypes that must always be base64
  272. * encoded
  273. *
  274. * NOTE: this routine mallocs memory in a static struct which must
  275. * be explicitly freed when no longer required
  276. */
  277. int
  278. ldif_must_b64_encode_register( LDAP_CONST char *name, LDAP_CONST char *oid )
  279. {
  280. int i;
  281. ber_len_t len;
  282. assert( must_b64_encode != NULL );
  283. assert( name != NULL );
  284. assert( oid != NULL );
  285. len = strlen( name );
  286. for ( i = 0; !BER_BVISNULL( &must_b64_encode[i].name ); i++ ) {
  287. if ( len != must_b64_encode[i].name.bv_len ) {
  288. continue;
  289. }
  290. if ( strcasecmp( name, must_b64_encode[i].name.bv_val ) == 0 ) {
  291. break;
  292. }
  293. }
  294. if ( !BER_BVISNULL( &must_b64_encode[i].name ) ) {
  295. return 1;
  296. }
  297. for ( i = 0; !BER_BVISNULL( &must_b64_encode[i].name ); i++ )
  298. /* just count */ ;
  299. if ( must_b64_encode == default_must_b64_encode ) {
  300. must_b64_encode = ber_memalloc( sizeof( must_b64_encode_s ) * ( i + 2 ) );
  301. if ( must_b64_encode == NULL ) {
  302. return 1;
  303. }
  304. for ( i = 0; !BER_BVISNULL( &default_must_b64_encode[i].name ); i++ ) {
  305. ber_dupbv( &must_b64_encode[i].name, &default_must_b64_encode[i].name );
  306. ber_dupbv( &must_b64_encode[i].oid, &default_must_b64_encode[i].oid );
  307. }
  308. } else {
  309. must_b64_encode_s *tmp;
  310. tmp = ber_memrealloc( must_b64_encode,
  311. sizeof( must_b64_encode_s ) * ( i + 2 ) );
  312. if ( tmp == NULL ) {
  313. return 1;
  314. }
  315. must_b64_encode = tmp;
  316. }
  317. ber_str2bv( name, len, 1, &must_b64_encode[i].name );
  318. ber_str2bv( oid, 0, 1, &must_b64_encode[i].oid );
  319. BER_BVZERO( &must_b64_encode[i + 1].name );
  320. return 0;
  321. }
  322. void
  323. ldif_must_b64_encode_release( void )
  324. {
  325. int i;
  326. assert( must_b64_encode != NULL );
  327. if ( must_b64_encode == default_must_b64_encode ) {
  328. return;
  329. }
  330. for ( i = 0; !BER_BVISNULL( &must_b64_encode[i].name ); i++ ) {
  331. ber_memfree( must_b64_encode[i].name.bv_val );
  332. ber_memfree( must_b64_encode[i].oid.bv_val );
  333. }
  334. ber_memfree( must_b64_encode );
  335. must_b64_encode = default_must_b64_encode;
  336. }
  337. /*
  338. * returns 1 iff the string corresponds to the name or the OID of any
  339. * of the attributeTypes listed in must_b64_encode
  340. */
  341. static int
  342. ldif_must_b64_encode( LDAP_CONST char *s )
  343. {
  344. int i;
  345. struct berval bv;
  346. assert( must_b64_encode != NULL );
  347. assert( s != NULL );
  348. ber_str2bv( s, 0, 0, &bv );
  349. for ( i = 0; !BER_BVISNULL( &must_b64_encode[i].name ); i++ ) {
  350. if ( ber_bvstrcasecmp( &must_b64_encode[i].name, &bv ) == 0
  351. || ber_bvcmp( &must_b64_encode[i].oid, &bv ) == 0 )
  352. {
  353. return 1;
  354. }
  355. }
  356. return 0;
  357. }
  358. /* NOTE: only preserved for binary compatibility */
  359. void
  360. ldif_sput(
  361. char **out,
  362. int type,
  363. LDAP_CONST char *name,
  364. LDAP_CONST char *val,
  365. ber_len_t vlen )
  366. {
  367. ldif_sput_wrap( out, type, name, val, vlen, 0 );
  368. }
  369. void
  370. ldif_sput_wrap(
  371. char **out,
  372. int type,
  373. LDAP_CONST char *name,
  374. LDAP_CONST char *val,
  375. ber_len_t vlen,
  376. ber_len_t wrap )
  377. {
  378. const unsigned char *byte, *stop;
  379. unsigned char buf[3];
  380. unsigned long bits;
  381. char *save;
  382. int pad;
  383. int namelen = 0;
  384. ber_len_t savelen;
  385. ber_len_t len=0;
  386. ber_len_t i;
  387. if ( !wrap )
  388. wrap = LDIF_LINE_WIDTH;
  389. /* prefix */
  390. switch( type ) {
  391. case LDIF_PUT_COMMENT:
  392. *(*out)++ = '#';
  393. len++;
  394. if( vlen ) {
  395. *(*out)++ = ' ';
  396. len++;
  397. }
  398. break;
  399. case LDIF_PUT_SEP:
  400. *(*out)++ = '\n';
  401. return;
  402. }
  403. /* name (attribute type) */
  404. if( name != NULL ) {
  405. /* put the name + ":" */
  406. namelen = strlen(name);
  407. strcpy(*out, name);
  408. *out += namelen;
  409. len += namelen;
  410. if( type != LDIF_PUT_COMMENT ) {
  411. *(*out)++ = ':';
  412. len++;
  413. }
  414. }
  415. #ifdef LDAP_DEBUG
  416. else {
  417. assert( type == LDIF_PUT_COMMENT );
  418. }
  419. #endif
  420. if( vlen == 0 ) {
  421. *(*out)++ = '\n';
  422. return;
  423. }
  424. switch( type ) {
  425. case LDIF_PUT_NOVALUE:
  426. *(*out)++ = '\n';
  427. return;
  428. case LDIF_PUT_URL: /* url value */
  429. *(*out)++ = '<';
  430. len++;
  431. break;
  432. case LDIF_PUT_B64: /* base64 value */
  433. *(*out)++ = ':';
  434. len++;
  435. break;
  436. }
  437. switch( type ) {
  438. case LDIF_PUT_TEXT:
  439. case LDIF_PUT_URL:
  440. case LDIF_PUT_B64:
  441. *(*out)++ = ' ';
  442. len++;
  443. /* fall-thru */
  444. case LDIF_PUT_COMMENT:
  445. /* pre-encoded names */
  446. for ( i=0; i < vlen; i++ ) {
  447. if ( len > wrap ) {
  448. *(*out)++ = '\n';
  449. *(*out)++ = ' ';
  450. len = 1;
  451. }
  452. *(*out)++ = val[i];
  453. len++;
  454. }
  455. *(*out)++ = '\n';
  456. return;
  457. }
  458. save = *out;
  459. savelen = len;
  460. *(*out)++ = ' ';
  461. len++;
  462. stop = (const unsigned char *) (val + vlen);
  463. if ( type == LDIF_PUT_VALUE
  464. && isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<'
  465. && isgraph( (unsigned char) val[vlen-1] )
  466. #ifndef LDAP_BINARY_DEBUG
  467. && strstr( name, ";binary" ) == NULL
  468. #endif
  469. #ifndef LDAP_PASSWD_DEBUG
  470. && !ldif_must_b64_encode( name )
  471. #endif
  472. ) {
  473. int b64 = 0;
  474. for ( byte = (const unsigned char *) val; byte < stop;
  475. byte++, len++ )
  476. {
  477. if ( !isascii( *byte ) || !isprint( *byte ) ) {
  478. b64 = 1;
  479. break;
  480. }
  481. if ( len >= wrap ) {
  482. *(*out)++ = '\n';
  483. *(*out)++ = ' ';
  484. len = 1;
  485. }
  486. *(*out)++ = *byte;
  487. }
  488. if( !b64 ) {
  489. *(*out)++ = '\n';
  490. return;
  491. }
  492. }
  493. *out = save;
  494. *(*out)++ = ':';
  495. *(*out)++ = ' ';
  496. len = savelen + 2;
  497. /* convert to base 64 (3 bytes => 4 base 64 digits) */
  498. for ( byte = (const unsigned char *) val;
  499. byte < stop - 2;
  500. byte += 3 )
  501. {
  502. bits = (byte[0] & 0xff) << 16;
  503. bits |= (byte[1] & 0xff) << 8;
  504. bits |= (byte[2] & 0xff);
  505. for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
  506. if ( len >= wrap ) {
  507. *(*out)++ = '\n';
  508. *(*out)++ = ' ';
  509. len = 1;
  510. }
  511. /* get b64 digit from high order 6 bits */
  512. *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
  513. }
  514. }
  515. /* add padding if necessary */
  516. if ( byte < stop ) {
  517. for ( i = 0; byte + i < stop; i++ ) {
  518. buf[i] = byte[i];
  519. }
  520. for ( pad = 0; i < 3; i++, pad++ ) {
  521. buf[i] = '\0';
  522. }
  523. byte = buf;
  524. bits = (byte[0] & 0xff) << 16;
  525. bits |= (byte[1] & 0xff) << 8;
  526. bits |= (byte[2] & 0xff);
  527. for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
  528. if ( len >= wrap ) {
  529. *(*out)++ = '\n';
  530. *(*out)++ = ' ';
  531. len = 1;
  532. }
  533. if( i + pad < 4 ) {
  534. /* get b64 digit from low order 6 bits */
  535. *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
  536. } else {
  537. *(*out)++ = '=';
  538. }
  539. }
  540. }
  541. *(*out)++ = '\n';
  542. }
  543. /*
  544. * ldif_type_and_value return BER malloc'd, zero-terminated LDIF line
  545. */
  546. /* NOTE: only preserved for binary compatibility */
  547. char *
  548. ldif_put(
  549. int type,
  550. LDAP_CONST char *name,
  551. LDAP_CONST char *val,
  552. ber_len_t vlen )
  553. {
  554. return ldif_put_wrap( type, name, val, vlen, 0 );
  555. }
  556. char *
  557. ldif_put_wrap(
  558. int type,
  559. LDAP_CONST char *name,
  560. LDAP_CONST char *val,
  561. ber_len_t vlen,
  562. ber_len_t wrap )
  563. {
  564. char *buf, *p;
  565. ber_len_t nlen;
  566. nlen = ( name != NULL ) ? strlen( name ) : 0;
  567. buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED_WRAP( nlen, vlen, wrap ) + 1 );
  568. if ( buf == NULL ) {
  569. ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
  570. _("ldif_type_and_value: malloc failed!"));
  571. return NULL;
  572. }
  573. p = buf;
  574. ldif_sput_wrap( &p, type, name, val, vlen, wrap );
  575. *p = '\0';
  576. return( buf );
  577. }
  578. int ldif_is_not_printable(
  579. LDAP_CONST char *val,
  580. ber_len_t vlen )
  581. {
  582. if( vlen == 0 || val == NULL ) {
  583. return -1;
  584. }
  585. if( isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<' &&
  586. isgraph( (unsigned char) val[vlen-1] ) )
  587. {
  588. ber_len_t i;
  589. for ( i = 0; val[i]; i++ ) {
  590. if ( !isascii( val[i] ) || !isprint( (unsigned char) val[i] ) ) {
  591. return 1;
  592. }
  593. }
  594. return 0;
  595. }
  596. return 1;
  597. }
  598. LDIFFP *
  599. ldif_open(
  600. LDAP_CONST char *file,
  601. LDAP_CONST char *mode
  602. )
  603. {
  604. FILE *fp = fopen( file, mode );
  605. LDIFFP *lfp = NULL;
  606. if ( fp ) {
  607. lfp = ber_memalloc( sizeof( LDIFFP ));
  608. if ( lfp == NULL ) {
  609. fclose( fp );
  610. return NULL;
  611. }
  612. lfp->fp = fp;
  613. lfp->prev = NULL;
  614. }
  615. return lfp;
  616. }
  617. LDIFFP *
  618. ldif_open_mem(
  619. char *ldif,
  620. size_t size,
  621. LDAP_CONST char *mode
  622. )
  623. {
  624. #ifdef HAVE_FMEMOPEN
  625. FILE *fp = fmemopen( ldif, size, mode );
  626. LDIFFP *lfp = NULL;
  627. if ( fp ) {
  628. lfp = ber_memalloc( sizeof( LDIFFP ));
  629. lfp->fp = fp;
  630. lfp->prev = NULL;
  631. }
  632. return lfp;
  633. #else /* !HAVE_FMEMOPEN */
  634. return NULL;
  635. #endif /* !HAVE_FMEMOPEN */
  636. }
  637. void
  638. ldif_close(
  639. LDIFFP *lfp
  640. )
  641. {
  642. LDIFFP *prev;
  643. while ( lfp ) {
  644. fclose( lfp->fp );
  645. prev = lfp->prev;
  646. ber_memfree( lfp );
  647. lfp = prev;
  648. }
  649. }
  650. #define LDIF_MAXLINE 4096
  651. /*
  652. * ldif_read_record - read an ldif record. Return 1 for success, 0 for EOF,
  653. * -1 for error.
  654. */
  655. int
  656. ldif_read_record(
  657. LDIFFP *lfp,
  658. unsigned long *lno, /* ptr to line number counter */
  659. char **bufp, /* ptr to malloced output buffer */
  660. int *buflenp ) /* ptr to length of *bufp */
  661. {
  662. char line[LDIF_MAXLINE], *nbufp;
  663. ber_len_t lcur = 0, len;
  664. int last_ch = '\n', found_entry = 0, stop, top_comment = 0;
  665. for ( stop = 0; !stop; last_ch = line[len-1] ) {
  666. /* If we're at the end of this file, see if we should pop
  667. * back to a previous file. (return from an include)
  668. */
  669. while ( feof( lfp->fp )) {
  670. pop:
  671. if ( lfp->prev ) {
  672. LDIFFP *tmp = lfp->prev;
  673. fclose( lfp->fp );
  674. *lfp = *tmp;
  675. ber_memfree( tmp );
  676. } else {
  677. stop = 1;
  678. break;
  679. }
  680. }
  681. if ( !stop ) {
  682. if ( fgets( line, sizeof( line ), lfp->fp ) == NULL ) {
  683. if ( !found_entry && !ferror( lfp->fp ) ) {
  684. /* ITS#9811 Reached the end looking for an entry, try again */
  685. goto pop;
  686. }
  687. stop = 1;
  688. len = 0;
  689. } else {
  690. len = strlen( line );
  691. }
  692. }
  693. if ( stop ) {
  694. /* Add \n in case the file does not end with newline */
  695. if (last_ch != '\n') {
  696. len = 1;
  697. line[0] = '\n';
  698. line[1] = '\0';
  699. goto last;
  700. }
  701. break;
  702. }
  703. /* Squash \r\n to \n */
  704. if ( len > 1 && line[len-2] == '\r' ) {
  705. len--;
  706. line[len] = '\0';
  707. line[len-1] = '\n';
  708. }
  709. if ( last_ch == '\n' ) {
  710. (*lno)++;
  711. if ( line[0] == '\n' ) {
  712. if ( !found_entry ) {
  713. lcur = 0;
  714. top_comment = 0;
  715. continue;
  716. }
  717. break;
  718. }
  719. if ( !found_entry ) {
  720. if ( line[0] == '#' ) {
  721. top_comment = 1;
  722. } else if ( ! ( top_comment && line[0] == ' ' ) ) {
  723. /* Found a new entry */
  724. found_entry = 1;
  725. if ( isdigit( (unsigned char) line[0] ) ) {
  726. /* skip index */
  727. continue;
  728. }
  729. if ( !strncasecmp( line, "include:",
  730. STRLENOF("include:"))) {
  731. FILE *fp2;
  732. char *ptr;
  733. found_entry = 0;
  734. if ( line[len-1] == '\n' ) {
  735. len--;
  736. line[len] = '\0';
  737. }
  738. ptr = line + STRLENOF("include:");
  739. while (isspace((unsigned char) *ptr)) ptr++;
  740. fp2 = ldif_open_url( ptr );
  741. if ( fp2 ) {
  742. LDIFFP *lnew = ber_memalloc( sizeof( LDIFFP ));
  743. if ( lnew == NULL ) {
  744. fclose( fp2 );
  745. return 0;
  746. }
  747. lnew->prev = lfp->prev;
  748. lnew->fp = lfp->fp;
  749. lfp->prev = lnew;
  750. lfp->fp = fp2;
  751. line[len] = '\n';
  752. len++;
  753. continue;
  754. } else {
  755. /* We failed to open the file, this should
  756. * be reported as an error somehow.
  757. */
  758. ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
  759. _("ldif_read_record: include %s failed\n"), ptr );
  760. return -1;
  761. }
  762. }
  763. }
  764. }
  765. }
  766. last:
  767. if ( *buflenp - lcur <= len ) {
  768. *buflenp += len + LDIF_MAXLINE;
  769. nbufp = ber_memrealloc( *bufp, *buflenp );
  770. if( nbufp == NULL ) {
  771. return 0;
  772. }
  773. *bufp = nbufp;
  774. }
  775. strcpy( *bufp + lcur, line );
  776. lcur += len;
  777. }
  778. return( found_entry );
  779. }