Back to index

openldap  2.4.31
Functions
passfile.c File Reference
#include "portable.h"
#include <stdio.h>
#include <ac/stdlib.h>
#include <ac/ctype.h>
#include <ac/string.h>
#include <lber.h>
#include <lutil.h>

Go to the source code of this file.

Functions

int lutil_get_filed_password (const char *filename, struct berval *passwd)

Function Documentation

int lutil_get_filed_password ( const char *  filename,
struct berval passwd 
)

Definition at line 34 of file passfile.c.

{
       size_t nread, nleft, nr;
       FILE *f = fopen( filename, "r" );

       if( f == NULL ) {
              perror( filename );
              return -1;
       }

       passwd->bv_val = NULL;
       passwd->bv_len = 4096;

#ifdef HAVE_FSTAT
       {
              struct stat sb;
              if ( fstat( fileno( f ), &sb ) == 0 ) {
                     if( sb.st_mode & 006 ) {
                            fprintf( stderr, _("Warning: Password file %s"
                                   " is publicly readable/writeable\n"),
                                   filename );
                     }

                     if ( sb.st_size )
                            passwd->bv_len = sb.st_size;
              }
       }
#endif /* HAVE_FSTAT */

       passwd->bv_val = (char *) ber_memalloc( passwd->bv_len + 1 );
       if( passwd->bv_val == NULL ) {
              perror( filename );
              fclose( f );
              return -1;
       }

       nread = 0;
       nleft = passwd->bv_len;
       do {
              if( nleft == 0 ) {
                     /* double the buffer size */
                     char *p = (char *) ber_memrealloc( passwd->bv_val,
                            2 * passwd->bv_len + 1 );
                     if( p == NULL ) {
                            ber_memfree( passwd->bv_val );
                            passwd->bv_val = NULL;
                            passwd->bv_len = 0;
                            fclose( f );
                            return -1;
                     }
                     nleft = passwd->bv_len;
                     passwd->bv_len *= 2;
                     passwd->bv_val = p;
              }

              nr = fread( &passwd->bv_val[nread], 1, nleft, f );

              if( nr < nleft && ferror( f ) ) {
                     ber_memfree( passwd->bv_val );
                     passwd->bv_val = NULL;
                     passwd->bv_len = 0;
                     fclose( f );
                     return -1;
              }

              nread += nr;
              nleft -= nr;
       } while ( !feof(f) );

       passwd->bv_len = nread;
       passwd->bv_val[nread] = '\0';

       fclose( f );
       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function: