Back to index

openldap  2.4.31
passfile.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 
00016 #include "portable.h"
00017 
00018 #include <stdio.h>
00019 
00020 #include <ac/stdlib.h>
00021 #include <ac/ctype.h>
00022 #include <ac/string.h>
00023 
00024 #ifdef HAVE_FSTAT
00025 #include <sys/types.h>
00026 #include <sys/stat.h>
00027 #endif /* HAVE_FSTAT */
00028 
00029 #include <lber.h>
00030 #include <lutil.h>
00031 
00032 /* Get a password from a file. */
00033 int
00034 lutil_get_filed_password(
00035        const char *filename,
00036        struct berval *passwd )
00037 {
00038        size_t nread, nleft, nr;
00039        FILE *f = fopen( filename, "r" );
00040 
00041        if( f == NULL ) {
00042               perror( filename );
00043               return -1;
00044        }
00045 
00046        passwd->bv_val = NULL;
00047        passwd->bv_len = 4096;
00048 
00049 #ifdef HAVE_FSTAT
00050        {
00051               struct stat sb;
00052               if ( fstat( fileno( f ), &sb ) == 0 ) {
00053                      if( sb.st_mode & 006 ) {
00054                             fprintf( stderr, _("Warning: Password file %s"
00055                                    " is publicly readable/writeable\n"),
00056                                    filename );
00057                      }
00058 
00059                      if ( sb.st_size )
00060                             passwd->bv_len = sb.st_size;
00061               }
00062        }
00063 #endif /* HAVE_FSTAT */
00064 
00065        passwd->bv_val = (char *) ber_memalloc( passwd->bv_len + 1 );
00066        if( passwd->bv_val == NULL ) {
00067               perror( filename );
00068               fclose( f );
00069               return -1;
00070        }
00071 
00072        nread = 0;
00073        nleft = passwd->bv_len;
00074        do {
00075               if( nleft == 0 ) {
00076                      /* double the buffer size */
00077                      char *p = (char *) ber_memrealloc( passwd->bv_val,
00078                             2 * passwd->bv_len + 1 );
00079                      if( p == NULL ) {
00080                             ber_memfree( passwd->bv_val );
00081                             passwd->bv_val = NULL;
00082                             passwd->bv_len = 0;
00083                             fclose( f );
00084                             return -1;
00085                      }
00086                      nleft = passwd->bv_len;
00087                      passwd->bv_len *= 2;
00088                      passwd->bv_val = p;
00089               }
00090 
00091               nr = fread( &passwd->bv_val[nread], 1, nleft, f );
00092 
00093               if( nr < nleft && ferror( f ) ) {
00094                      ber_memfree( passwd->bv_val );
00095                      passwd->bv_val = NULL;
00096                      passwd->bv_len = 0;
00097                      fclose( f );
00098                      return -1;
00099               }
00100 
00101               nread += nr;
00102               nleft -= nr;
00103        } while ( !feof(f) );
00104 
00105        passwd->bv_len = nread;
00106        passwd->bv_val[nread] = '\0';
00107 
00108        fclose( f );
00109        return 0;
00110 }