line64.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /* --- BEGIN COPYRIGHT BLOCK ---
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2005 Red Hat, Inc.
  35. * All rights reserved.
  36. * --- END COPYRIGHT BLOCK --- */
  37. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  38. *
  39. * The contents of this file are subject to the Netscape Public License
  40. * Version 1.0 (the "NPL"); you may not use this file except in
  41. * compliance with the NPL. You may obtain a copy of the NPL at
  42. * http://www.mozilla.org/NPL/
  43. *
  44. * Software distributed under the NPL is distributed on an "AS IS" basis,
  45. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  46. * for the specific language governing rights and limitations under the
  47. * NPL.
  48. *
  49. * The Initial Developer of this code under the NPL is Netscape
  50. * Communications Corporation. Portions created by Netscape are
  51. * Copyright (C) 1998 Netscape Communications Corporation. All Rights
  52. * Reserved.
  53. */
  54. /* line64.c - routines for dealing with the slapd line format */
  55. #include <stdio.h>
  56. #include <string.h>
  57. #include <stdlib.h>
  58. #include <ctype.h>
  59. #ifndef macintosh
  60. #include <sys/types.h>
  61. #endif
  62. #ifdef _WIN32
  63. #include <windows.h>
  64. #elif !defined( macintosh )
  65. #include <sys/socket.h>
  66. #endif
  67. #include "ldaplog.h"
  68. #include "ldif.h"
  69. #include <errno.h>
  70. #include "lber.h"
  71. #include <sys/stat.h>
  72. #include "fileurl.h"
  73. #include "nspr.h"
  74. #ifndef isascii
  75. #define isascii( c ) (!((c) & ~0177))
  76. #endif
  77. #define RIGHT2 0x03
  78. #define RIGHT4 0x0f
  79. #define CONTINUED_LINE_MARKER '\001'
  80. #define ISBLANK(c) (c == ' ' || c == '\t' || c == '\n') /* not "\r\v\f" */
  81. #define LDIF_OPT_ISSET( value, opt ) (((value) & (opt)) != 0 )
  82. static char nib2b64[0x40] =
  83. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  84. static unsigned char b642nib[0x80] = {
  85. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  86. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  87. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  88. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  89. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  90. 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
  91. 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  92. 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  93. 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  94. 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
  95. 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
  96. 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
  97. 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
  98. 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
  99. 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
  100. 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
  101. };
  102. static int ldif_base64_encode_internal( unsigned char *src, char *dst, int srclen,
  103. int lenused, int wraplen );
  104. static int ldif_fromfile( char *path, struct berval *bv );
  105. extern int errno;
  106. /*
  107. * ldif_parse_line - takes a line of the form "type:[:] value" and splits it
  108. * into components "type" and "value". if a double colon separates type from
  109. * value, then value is encoded in base 64, and parse_line un-decodes it
  110. * (in place) before returning.
  111. * Success return might be 0 (value is returned in place) or 1 (value has
  112. * been malloc'ed)
  113. */
  114. int
  115. ldif_parse_line(
  116. char *line,
  117. char **type,
  118. char **value,
  119. int *vlen,
  120. char **errmsg
  121. )
  122. {
  123. char *p, *s, *d;
  124. int b64;
  125. int url = 0;
  126. int rc = 0;
  127. *errmsg = NULL;
  128. /* skip any leading space */
  129. while ( ISBLANK( *line ) ) {
  130. line++;
  131. }
  132. *type = line;
  133. for ( s = line; *s && *s != ':'; s++ )
  134. ; /* NULL */
  135. if ( *s == '\0' ) {
  136. /* Comment-out while we address calling libldif from ns-back-ldbm
  137. on NT. 1 of 3 */
  138. #if defined( _WIN32 )
  139. /*
  140. #endif
  141. LDAPDebug( LDAP_DEBUG_PARSE, "ldif_parse_line: missing ':' "
  142. "on line \"%s\"\n", line, 0, 0 );
  143. #if defined( _WIN32 )
  144. */
  145. #endif
  146. return( -1 );
  147. }
  148. /* trim any space between type and : */
  149. for ( p = s - 1; p > line && ISBLANK( *p ); p-- ) {
  150. *p = '\0';
  151. }
  152. *s++ = '\0';
  153. /* check for double : - indicates base 64 encoded value */
  154. if ( *s == ':' ) {
  155. s++;
  156. b64 = 1;
  157. /* single : - normally encoded value */
  158. } else {
  159. /* check for ":<" - indicates value is actually an url */
  160. if (*s == '<') {
  161. s++;
  162. url = 1;
  163. }
  164. b64 = 0;
  165. }
  166. /* skip space between : and value */
  167. while ( ISBLANK( *s ) ) {
  168. s++;
  169. }
  170. /*
  171. * If no value is present, return a zero-length string for
  172. * *value, with *vlen set to zero.
  173. */
  174. if ( *s == '\0' ) {
  175. *value = s;
  176. *vlen = 0;
  177. return( 0 );
  178. }
  179. /* check for continued line markers that should be deleted */
  180. for ( p = s, d = s; *p; p++ ) {
  181. if ( *p != CONTINUED_LINE_MARKER )
  182. *d++ = *p;
  183. }
  184. *d = '\0';
  185. *value = s;
  186. if ( b64 ) {
  187. if (( *vlen = ldif_base64_decode( s, (unsigned char *)s ))
  188. < 0 ) {
  189. /* Comment-out while we address calling libldif from ns-back-ldbm
  190. on NT. 3 of 3 */
  191. #if defined( _WIN32 )
  192. /*
  193. #endif
  194. LDAPDebug( LDAP_DEBUG_ANY,
  195. "ldif_parse_line: invalid base 64 char on line \"%s\"\n",
  196. line, 0, 0 );
  197. #if defined( _WIN32 )
  198. */
  199. #endif
  200. return( -1 );
  201. }
  202. s[ *vlen ] = '\0';
  203. } else if (url) {
  204. char *path;
  205. struct stat fstats;
  206. struct berval bv;
  207. bv.bv_val = NULL;
  208. /*
  209. * We only support file:// URLs for now.
  210. */
  211. switch( ldif_fileurl2path( s, &path )) {
  212. case LDIF_FILEURL_NOTAFILEURL:
  213. *errmsg = PR_smprintf(
  214. "ldif_parse_line: unsupported URL \"%s\";"
  215. " use a file:// URL instead.\n", s);
  216. rc = -1;
  217. break;
  218. case LDIF_FILEURL_MISSINGPATH:
  219. *errmsg = PR_smprintf(
  220. "ldif_parse_line: unable to process URL \"%s\" --"
  221. " missing path..\n", s);
  222. rc = -1;
  223. break;
  224. case LDIF_FILEURL_NONLOCAL:
  225. *errmsg = PR_smprintf(
  226. "ldif_parse_line: unable to process URL \"%s\" --"
  227. " only local file:// URLs are supported.\n", s);
  228. rc = -1;
  229. break;
  230. case LDIF_FILEURL_NOMEMORY:
  231. perror( "ldif_fileurl2path" );
  232. rc = -1;
  233. break;
  234. case LDIF_FILEURL_SUCCESS:
  235. if ( stat( path, &fstats ) != 0 ) {
  236. perror( path );
  237. rc = -1;
  238. } else if ( fstats.st_mode & S_IFDIR ) {
  239. *errmsg = PR_smprintf(
  240. "ldif_parse_line: %s is a directory, not a file.\n", path);
  241. rc = -1;
  242. } else if ( ldif_fromfile( path, &bv ) < 0 ) {
  243. *errmsg = PR_smprintf(
  244. "ldif_parse_line: unable to retrieve information"
  245. " from file %s.\n", path);
  246. rc = -1;
  247. }
  248. free( path );
  249. break;
  250. default:
  251. *errmsg = PR_smprintf(
  252. "ldif_parse_line: unable to process URL \"%s\" --"
  253. " unknown error.\n", s);
  254. rc = -1;
  255. }
  256. if ( rc != 0 ) {
  257. if (bv.bv_val)
  258. free(bv.bv_val);
  259. } else {
  260. *value = bv.bv_val;
  261. *vlen = bv.bv_len;
  262. rc = 1;
  263. }
  264. } else {
  265. *vlen = (int) (d - s);
  266. }
  267. return( rc );
  268. }
  269. /*
  270. * ldif_base64_decode - take the BASE64-encoded characters in "src"
  271. * (a zero-terminated string) and decode them into the the buffer "dst".
  272. * "src" and "dst" can be the same if in-place decoding is desired.
  273. * "dst" must be large enough to hold the decoded octets. No more than
  274. * 3 * strlen( src ) / 4 bytes will be produced.
  275. * "dst" may contain zero octets anywhere within it, but it is not
  276. * zero-terminated by this function.
  277. *
  278. * The number of bytes copied to "dst" is returned if all goes well.
  279. * -1 is returned if the BASE64 encoding in "src" is invalid.
  280. */
  281. int
  282. ldif_base64_decode( char *src, unsigned char *dst )
  283. {
  284. char *p, *stop;
  285. unsigned char nib, *byte;
  286. int i, len;
  287. stop = strchr( src, '\0' );
  288. byte = dst;
  289. for ( p = src, len = 0; p < stop; p += 4, len += 3 ) {
  290. for ( i = 0; i < 4; i++ ) {
  291. if ( p[i] != '=' && (p[i] & 0x80 ||
  292. b642nib[ p[i] & 0x7f ] > 0x3f) ) {
  293. return( -1 );
  294. }
  295. }
  296. /* first digit */
  297. nib = b642nib[ p[0] & 0x7f ];
  298. byte[0] = nib << 2;
  299. /* second digit */
  300. nib = b642nib[ p[1] & 0x7f ];
  301. byte[0] |= nib >> 4;
  302. /* third digit */
  303. if ( p[2] == '=' ) {
  304. len += 1;
  305. break;
  306. }
  307. byte[1] = (nib & RIGHT4) << 4;
  308. nib = b642nib[ p[2] & 0x7f ];
  309. byte[1] |= nib >> 2;
  310. /* fourth digit */
  311. if ( p[3] == '=' ) {
  312. len += 2;
  313. break;
  314. }
  315. byte[2] = (nib & RIGHT2) << 6;
  316. nib = b642nib[ p[3] & 0x7f ];
  317. byte[2] |= nib;
  318. byte += 3;
  319. }
  320. return( len );
  321. }
  322. /*
  323. * ldif_getline - return the next "line" (minus newline) of input from a
  324. * string buffer of lines separated by newlines, terminated by \n\n
  325. * or \0. this routine handles continued lines, bundling them into
  326. * a single big line before returning. if a line begins with a white
  327. * space character, it is a continuation of the previous line. the white
  328. * space character (nb: only one char), and preceeding newline are changed
  329. * into CONTINUED_LINE_MARKER chars, to be deleted later by the
  330. * ldif_parse_line() routine above.
  331. *
  332. * it takes a pointer to a pointer to the buffer on the first call,
  333. * which it updates and must be supplied on subsequent calls.
  334. *
  335. * XXX need to update this function to also support <CR><LF> as EOL.
  336. * XXX supports <CR><LF> as of 07/29/1998 (richm)
  337. */
  338. char *
  339. ldif_getline( char **next )
  340. {
  341. char *l;
  342. char c;
  343. char *p;
  344. if ( *next == NULL || **next == '\n' || **next == '\0' ) {
  345. return( NULL );
  346. }
  347. while ( **next == '#' ) { /* skip comment lines */
  348. if (( *next = strchr( *next, '\n' )) == NULL ) {
  349. return( NULL );
  350. }
  351. (*next)++;
  352. }
  353. l = *next;
  354. while ( (*next = strchr( *next, '\n' )) != NULL ) {
  355. p = *next - 1; /* pointer to character previous to the newline */
  356. c = *(*next + 1); /* character after the newline */
  357. if ( ISBLANK( c ) && c != '\n' ) {
  358. /* DOS EOL is \r\n, so if the character before */
  359. /* the \n is \r, continue it too */
  360. if (*p == '\r')
  361. *p = CONTINUED_LINE_MARKER;
  362. **next = CONTINUED_LINE_MARKER;
  363. *(*next+1) = CONTINUED_LINE_MARKER;
  364. } else {
  365. /* DOS EOL is \r\n, so if the character before */
  366. /* the \n is \r, null it too */
  367. if (*p == '\r')
  368. *p = '\0';
  369. *(*next)++ = '\0';
  370. break;
  371. }
  372. (*next)++;
  373. }
  374. return( l );
  375. }
  376. #define LDIF_SAFE_CHAR( c ) ( (c) != '\r' && (c) != '\n' )
  377. #define LDIF_CONSERVATIVE_CHAR( c ) ( LDIF_SAFE_CHAR(c) && isascii((c)) \
  378. && ( isprint((c)) || (c) == '\t' ))
  379. #define LDIF_SAFE_INITCHAR( c ) ( LDIF_SAFE_CHAR(c) && (c) != ':' \
  380. && (c) != ' ' && (c) != '<' )
  381. #define LDIF_CONSERVATIVE_INITCHAR( c ) ( LDIF_SAFE_INITCHAR( c ) && \
  382. ! ( isascii((c)) && isspace((c))))
  383. #define LDIF_CONSERVATIVE_FINALCHAR( c ) ( (c) != ' ' )
  384. void
  385. ldif_put_type_and_value_with_options( char **out, char *t, char *val,
  386. int vlen, unsigned long options )
  387. {
  388. unsigned char *p, *byte, *stop;
  389. char *save;
  390. int b64, len, savelen, wraplen;
  391. len = 0;
  392. if ( LDIF_OPT_ISSET( options, LDIF_OPT_NOWRAP )) {
  393. wraplen = -1;
  394. } else {
  395. wraplen = LDIF_MAX_LINE_WIDTH;
  396. }
  397. /* put the type + ": " */
  398. for ( p = (unsigned char *) t; *p; p++, len++ ) {
  399. *(*out)++ = *p;
  400. }
  401. *(*out)++ = ':';
  402. len++;
  403. if ( LDIF_OPT_ISSET( options, LDIF_OPT_VALUE_IS_URL )) {
  404. *(*out)++ = '<'; /* add '<' for URLs */
  405. len++;
  406. }
  407. save = *out;
  408. savelen = len;
  409. b64 = 0;
  410. stop = (unsigned char *)val;
  411. if ( val && vlen > 0 ) {
  412. *(*out)++ = ' ';
  413. stop = (unsigned char *) (val + vlen);
  414. if ( LDIF_OPT_ISSET( options, LDIF_OPT_MINIMAL_ENCODING )) {
  415. if ( !LDIF_SAFE_INITCHAR( val[0] )) {
  416. b64 = 1;
  417. }
  418. } else {
  419. if ( !LDIF_CONSERVATIVE_INITCHAR( val[0] ) ||
  420. !LDIF_CONSERVATIVE_FINALCHAR( val[vlen-1] )) {
  421. b64 = 1;
  422. }
  423. }
  424. }
  425. if ( !b64 ) {
  426. for ( byte = (unsigned char *) val; byte < stop;
  427. byte++, len++ ) {
  428. if ( LDIF_OPT_ISSET( options,
  429. LDIF_OPT_MINIMAL_ENCODING )) {
  430. if ( !LDIF_SAFE_CHAR( *byte )) {
  431. b64 = 1;
  432. break;
  433. }
  434. } else if ( !LDIF_CONSERVATIVE_CHAR( *byte )) {
  435. b64 = 1;
  436. break;
  437. }
  438. if ( wraplen != -1 && len > wraplen ) {
  439. *(*out)++ = '\n';
  440. *(*out)++ = ' ';
  441. len = 1;
  442. }
  443. *(*out)++ = *byte;
  444. }
  445. }
  446. if ( b64 ) {
  447. *out = save;
  448. *(*out)++ = ':';
  449. *(*out)++ = ' ';
  450. len = ldif_base64_encode_internal( (unsigned char *)val, *out, vlen,
  451. savelen + 2, wraplen );
  452. *out += len;
  453. }
  454. *(*out)++ = '\n';
  455. }
  456. void
  457. ldif_put_type_and_value( char **out, char *t, char *val, int vlen )
  458. {
  459. ldif_put_type_and_value_with_options( out, t, val, vlen, 0 );
  460. }
  461. void
  462. ldif_put_type_and_value_nowrap( char **out, char *t, char *val, int vlen )
  463. {
  464. ldif_put_type_and_value_with_options( out, t, val, vlen, LDIF_OPT_NOWRAP );
  465. }
  466. /*
  467. * ldif_base64_encode_internal - encode "srclen" bytes in "src", place BASE64
  468. * encoded bytes in "dst" and return the length of the BASE64
  469. * encoded string. "dst" is also zero-terminated by this function.
  470. *
  471. * If "lenused" >= 0, newlines will be included in "dst" and "lenused" if
  472. * appropriate. "lenused" should be a count of characters already used
  473. * on the current line. The LDIF lines we create will contain at most
  474. * "wraplen" characters on each line, unless "wraplen" is -1, in which
  475. * case output line length is unlimited.
  476. *
  477. * If "lenused" < 0, no newlines will be included, and the LDIF_BASE64_LEN()
  478. * macro can be used to determine how many bytes will be placed in "dst."
  479. */
  480. static int
  481. ldif_base64_encode_internal( unsigned char *src, char *dst, int srclen, int lenused, int wraplen )
  482. {
  483. unsigned char *byte, *stop;
  484. unsigned char buf[3];
  485. char *out;
  486. unsigned long bits;
  487. int i, pad, len;
  488. len = 0;
  489. out = dst;
  490. stop = src + srclen;
  491. /* convert to base 64 (3 bytes => 4 base 64 digits) */
  492. for ( byte = src; byte < stop - 2; byte += 3 ) {
  493. bits = (byte[0] & 0xff) << 16;
  494. bits |= (byte[1] & 0xff) << 8;
  495. bits |= (byte[2] & 0xff);
  496. for ( i = 0; i < 4; i++, bits <<= 6 ) {
  497. if ( wraplen != -1 && lenused >= 0 && lenused++ > wraplen ) {
  498. *out++ = '\n';
  499. *out++ = ' ';
  500. lenused = 2;
  501. }
  502. /* get b64 digit from high order 6 bits */
  503. *out++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
  504. }
  505. }
  506. /* add padding if necessary */
  507. if ( byte < stop ) {
  508. for ( i = 0; byte + i < stop; i++ ) {
  509. buf[i] = byte[i];
  510. }
  511. for ( pad = 0; i < 3; i++, pad++ ) {
  512. buf[i] = '\0';
  513. }
  514. byte = buf;
  515. bits = (byte[0] & 0xff) << 16;
  516. bits |= (byte[1] & 0xff) << 8;
  517. bits |= (byte[2] & 0xff);
  518. for ( i = 0; i < 4; i++, bits <<= 6 ) {
  519. if ( wraplen != -1 && lenused >= 0 && lenused++ > wraplen ) {
  520. *out++ = '\n';
  521. *out++ = ' ';
  522. lenused = 2;
  523. }
  524. if (( i == 3 && pad > 0 ) || ( i == 2 && pad == 2 )) {
  525. /* Pad as appropriate */
  526. *out++ = '=';
  527. } else {
  528. /* get b64 digit from low order 6 bits */
  529. *out++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
  530. }
  531. }
  532. }
  533. *out = '\0';
  534. return( out - dst );
  535. }
  536. int
  537. ldif_base64_encode( unsigned char *src, char *dst, int srclen, int lenused )
  538. {
  539. return ldif_base64_encode_internal( src, dst, srclen, lenused, LDIF_MAX_LINE_WIDTH );
  540. }
  541. int
  542. ldif_base64_encode_nowrap( unsigned char *src, char *dst, int srclen, int lenused )
  543. {
  544. return ldif_base64_encode_internal( src, dst, srclen, lenused, -1 );
  545. }
  546. /*
  547. * return malloc'd, zero-terminated LDIF line
  548. */
  549. char *
  550. ldif_type_and_value_with_options( char *type, char *val, int vlen,
  551. unsigned long options )
  552. {
  553. char *buf, *p;
  554. int tlen;
  555. tlen = strlen( type );
  556. if (( buf = (char *)malloc( LDIF_SIZE_NEEDED( tlen, vlen ) + 1 )) !=
  557. NULL ) {
  558. p = buf;
  559. ldif_put_type_and_value_with_options( &p, type, val, vlen, options );
  560. *p = '\0';
  561. }
  562. return( buf );
  563. }
  564. char *
  565. ldif_type_and_value( char *type, char *val, int vlen )
  566. {
  567. return ldif_type_and_value_with_options( type, val, vlen, 0 );
  568. }
  569. char *
  570. ldif_type_and_value_nowrap( char *type, char *val, int vlen )
  571. {
  572. return ldif_type_and_value_with_options( type, val, vlen, LDIF_OPT_NOWRAP );
  573. }
  574. /*
  575. * ldif_get_entry - read the next ldif entry from the FILE referenced
  576. * by fp. return a pointer to a malloc'd, null-terminated buffer. also
  577. * returned is the last line number read, in *lineno.
  578. */
  579. char *
  580. ldif_get_entry( FILE *fp, int *lineno )
  581. {
  582. char line[BUFSIZ];
  583. char *buf;
  584. int max, cur, len, gotsome;
  585. buf = NULL;
  586. max = cur = gotsome = 0;
  587. while ( fgets( line, sizeof(line), fp ) != NULL ) {
  588. if ( lineno != NULL ) {
  589. (*lineno)++;
  590. }
  591. /* ldif entries are terminated by a \n on a line by itself */
  592. if ( line[0] == '\0' || line[0] == '\n'
  593. #if !defined( XP_WIN32 )
  594. || ( line[0] == '\r' && line[1] == '\n' ) /* DOS format */
  595. #endif
  596. ) {
  597. if ( gotsome ) {
  598. break;
  599. } else {
  600. continue;
  601. }
  602. } else if ( line[0] == '#' ) {
  603. continue;
  604. }
  605. gotsome = 1;
  606. len = strlen( line );
  607. #if !defined( XP_WIN32 )
  608. /* DOS format */
  609. if ( len > 0 && line[len-1] == '\r' ) {
  610. --len;
  611. line[len] = '\0';
  612. } else if ( len > 1 && line[len-2] == '\r' && line[len-1] == '\n' ) {
  613. --len;
  614. line[len-1] = line[len];
  615. line[len] = '\0';
  616. }
  617. #endif
  618. while ( cur + (len + 1) > max ) {
  619. if ( buf == NULL ) {
  620. max += BUFSIZ;
  621. buf = (char *) malloc( max );
  622. } else {
  623. max *= 2;
  624. buf = (char *) realloc( buf, max );
  625. }
  626. if ( buf == NULL ) {
  627. return( NULL );
  628. }
  629. }
  630. memcpy( buf + cur, line, len + 1 );
  631. cur += len;
  632. }
  633. return( buf );
  634. }
  635. static int
  636. ldif_fromfile( char *path, struct berval *bv )
  637. {
  638. FILE *fp;
  639. long rlen;
  640. int eof;
  641. #if defined( XP_WIN32 )
  642. char mode[20] = "r+b";
  643. #else
  644. char mode[20] = "r";
  645. #endif
  646. if (( fp = fopen( path, mode )) == NULL ) {
  647. perror( path );
  648. return( -1 );
  649. }
  650. if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
  651. perror( path );
  652. fclose( fp );
  653. return( -1 );
  654. }
  655. bv->bv_len = ftell( fp );
  656. if (( bv->bv_val = (char *)malloc( bv->bv_len + 1 )) == NULL ) {
  657. perror( "malloc" );
  658. fclose( fp );
  659. return( -1 );
  660. }
  661. if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
  662. perror( path );
  663. fclose( fp );
  664. return( -1 );
  665. }
  666. rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
  667. eof = feof( fp );
  668. fclose( fp );
  669. if ( rlen != (long)bv->bv_len ) {
  670. perror( path );
  671. free( bv->bv_val );
  672. return( -1 );
  673. }
  674. bv->bv_val[ bv->bv_len ] = '\0';
  675. return( bv->bv_len );
  676. }