2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2012 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/stdlib.h>
22 #include <ac/string.h>
25 #include <sys/types.h>
27 #endif /* HAVE_FSTAT */
32 /* Get a password from a file. */
34 lutil_get_filed_password(
36 struct berval *passwd )
38 size_t nread, nleft, nr;
39 FILE *f = fopen( filename, "r" );
46 passwd->bv_val = NULL;
47 passwd->bv_len = 4096;
52 if ( fstat( fileno( f ), &sb ) == 0 ) {
53 if( sb.st_mode & 006 ) {
54 fprintf( stderr, _("Warning: Password file %s"
55 " is publicly readable/writeable\n"),
60 passwd->bv_len = sb.st_size;
63 #endif /* HAVE_FSTAT */
65 passwd->bv_val = (char *) ber_memalloc( passwd->bv_len + 1 );
66 if( passwd->bv_val == NULL ) {
73 nleft = passwd->bv_len;
76 /* double the buffer size */
77 char *p = (char *) ber_memrealloc( passwd->bv_val,
78 2 * passwd->bv_len + 1 );
80 ber_memfree( passwd->bv_val );
81 passwd->bv_val = NULL;
86 nleft = passwd->bv_len;
91 nr = fread( &passwd->bv_val[nread], 1, nleft, f );
93 if( nr < nleft && ferror( f ) ) {
94 ber_memfree( passwd->bv_val );
95 passwd->bv_val = NULL;
103 } while ( !feof(f) );
105 passwd->bv_len = nread;
106 passwd->bv_val[nread] = '\0';