ldif.c 4.8 KB

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