line64.c 20 KB

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