ldif.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <memory.h>
  16. #include <sys/types.h>
  17. #include <unistd.h> /* for read() */
  18. #include <sys/socket.h>
  19. #include "ldap.h"
  20. #include "ldif.h"
  21. int ldap_syslog;
  22. int ldap_syslog_level;
  23. #if defined(USE_OPENLDAP)
  24. static char *
  25. ldif_type_and_value(const char *type, const char *val, int vlen)
  26. {
  27. char *buf, *p;
  28. int tlen;
  29. tlen = strlen( type );
  30. if (( buf = (char *)malloc( LDIF_SIZE_NEEDED( tlen, vlen ) + 1 )) !=
  31. NULL ) {
  32. p = buf;
  33. ldif_sput( &p, LDIF_PUT_VALUE, type, val, vlen );
  34. *p = '\0';
  35. }
  36. return( buf );
  37. }
  38. #endif
  39. static void
  40. display_usage( char *name )
  41. {
  42. fprintf( stderr, "usage: %s [-b] <attrtype>\n", name );
  43. }
  44. int main( int argc, char **argv )
  45. {
  46. char *type, *out;
  47. int binary = 0;
  48. if (argc < 2 || argc > 3 ) {
  49. display_usage( argv[0] );
  50. return( 1 );
  51. }
  52. if ( argc == 3 ) {
  53. if ( strcmp( argv[1], "-b" ) != 0 ) {
  54. display_usage( argv[0] );
  55. return( 1 );
  56. } else {
  57. binary = 1;
  58. type = argv[2];
  59. }
  60. } else {
  61. if ( strcmp( argv[1], "-b" ) == 0 ) {
  62. display_usage( argv[0] );
  63. return( 1 );
  64. }
  65. type = argv[1];
  66. }
  67. /* if the -b flag was used, read single binary value from stdin */
  68. if ( binary ) {
  69. char buf[BUFSIZ];
  70. char *val;
  71. int nread, max, cur;
  72. if (( val = (char *) malloc( BUFSIZ )) == NULL ) {
  73. perror( "malloc" );
  74. return( 1 );
  75. }
  76. max = BUFSIZ;
  77. cur = 0;
  78. while ( (nread = read( 0, buf, BUFSIZ )) != 0 ) {
  79. if (nread < 0) {
  80. perror( "read error" );
  81. return( 1 );
  82. }
  83. if ( nread + cur > max ) {
  84. max += BUFSIZ;
  85. if (( val = (char *) realloc( val, max )) ==
  86. NULL ) {
  87. perror( "realloc" );
  88. return( 1 );
  89. }
  90. }
  91. memcpy( val + cur, buf, nread );
  92. cur += nread;
  93. }
  94. if (( out = ldif_type_and_value( type, val, cur )) == NULL ) {
  95. perror( "ldif_type_and_value" );
  96. free( val );
  97. return( 1 );
  98. }
  99. fputs( out, stdout );
  100. free( out );
  101. free( val );
  102. return( 0 );
  103. } else {
  104. /* not binary: one value per line... */
  105. char *buf;
  106. int curlen, maxlen = BUFSIZ;
  107. if( (buf = malloc(BUFSIZ)) == NULL ) {
  108. perror( "malloc" );
  109. return( 1 );
  110. }
  111. while ( (buf = fgets(buf, maxlen, stdin)) ) {
  112. /* if buffer was filled, expand and keep reading unless last char
  113. is linefeed, in which case it is OK for buffer to be full */
  114. while( ((curlen = strlen(buf)) == (maxlen - 1)) && buf[curlen-1] != '\n' ) {
  115. maxlen *= 2;
  116. if( (buf = (char *)realloc(buf, maxlen)) == NULL ) {
  117. perror( "realloc" );
  118. free( buf );
  119. return( 1 );
  120. }
  121. (void)fgets(buf+curlen, maxlen/2 + 1, stdin);
  122. }
  123. /* we have a full line, chop potential newline and turn into ldif */
  124. if( buf[curlen-1] == '\n' )
  125. buf[curlen-1]='\0';
  126. if (( out = ldif_type_and_value( type, buf, strlen( buf ) ))
  127. == NULL ) {
  128. perror( "ldif_type_and_value" );
  129. free( buf );
  130. return( 1 );
  131. }
  132. fputs( out, stdout );
  133. free( out );
  134. }
  135. free( buf );
  136. }
  137. return( 0 );
  138. }