ldif.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <memory.h>
  41. #include <sys/types.h>
  42. #if defined( _WINDOWS ) || defined( _WIN32 )
  43. #include <windows.h>
  44. #include <io.h>
  45. #include <fcntl.h>
  46. #else
  47. #include <unistd.h> /* for read() */
  48. #include <sys/socket.h>
  49. #endif
  50. #include "ldap.h"
  51. #include "ldif.h"
  52. int ldap_syslog;
  53. int ldap_syslog_level;
  54. static void
  55. display_usage( char *name )
  56. {
  57. fprintf( stderr, "usage: %s [-b] <attrtype>\n", name );
  58. }
  59. int main( int argc, char **argv )
  60. {
  61. char *type, *out;
  62. int binary = 0;
  63. if (argc < 2 || argc > 3 ) {
  64. display_usage( argv[0] );
  65. return( 1 );
  66. }
  67. if ( argc == 3 ) {
  68. if ( strcmp( argv[1], "-b" ) != 0 ) {
  69. display_usage( argv[0] );
  70. return( 1 );
  71. } else {
  72. binary = 1;
  73. type = argv[2];
  74. }
  75. } else {
  76. if ( strcmp( argv[1], "-b" ) == 0 ) {
  77. display_usage( argv[0] );
  78. return( 1 );
  79. }
  80. type = argv[1];
  81. }
  82. /* if the -b flag was used, read single binary value from stdin */
  83. if ( binary ) {
  84. char buf[BUFSIZ];
  85. char *val;
  86. int nread, max, cur;
  87. #if defined( _WINDOWS ) || defined( _WIN32 )
  88. _setmode( _fileno( stdin ), _O_BINARY );
  89. #endif
  90. if (( val = (char *) malloc( BUFSIZ )) == NULL ) {
  91. perror( "malloc" );
  92. return( 1 );
  93. }
  94. max = BUFSIZ;
  95. cur = 0;
  96. while ( (nread = read( 0, buf, BUFSIZ )) != 0 ) {
  97. if ( nread + cur > max ) {
  98. max += BUFSIZ;
  99. if (( val = (char *) realloc( val, max )) ==
  100. NULL ) {
  101. perror( "realloc" );
  102. return( 1 );
  103. }
  104. }
  105. memcpy( val + cur, buf, nread );
  106. cur += nread;
  107. }
  108. if (( out = ldif_type_and_value( type, val, cur )) == NULL ) {
  109. perror( "ldif_type_and_value" );
  110. return( 1 );
  111. }
  112. fputs( out, stdout );
  113. free( out );
  114. free( val );
  115. return( 0 );
  116. } else {
  117. /* not binary: one value per line... */
  118. char *buf;
  119. int curlen, maxlen = BUFSIZ;
  120. if( (buf = malloc(BUFSIZ)) == NULL ) {
  121. perror( "malloc" );
  122. return( 1 );
  123. }
  124. while ( (buf = fgets(buf, maxlen, stdin)) ) {
  125. /* if buffer was filled, expand and keep reading unless last char
  126. is linefeed, in which case it is OK for buffer to be full */
  127. while( ((curlen = strlen(buf)) == (maxlen - 1)) && buf[curlen-1] != '\n' ) {
  128. maxlen *= 2;
  129. if( (buf = (char *)realloc(buf, maxlen)) == NULL ) {
  130. perror( "realloc" );
  131. return( 1 );
  132. }
  133. fgets(buf+curlen, maxlen/2 + 1, stdin);
  134. }
  135. /* we have a full line, chop potential newline and turn into ldif */
  136. if( buf[curlen-1] == '\n' )
  137. buf[curlen-1]='\0';
  138. if (( out = ldif_type_and_value( type, buf, strlen( buf ) ))
  139. == NULL ) {
  140. perror( "ldif_type_and_value" );
  141. return( 1 );
  142. }
  143. fputs( out, stdout );
  144. free( out );
  145. }
  146. free( buf );
  147. }
  148. return( 0 );
  149. }