3 * Copyright 2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/stdlib.h>
13 #include <ac/string.h>
16 #include <sys/types.h>
18 #endif /* HAVE_FSTAT */
23 /* Get a password from a file. */
25 lutil_get_filed_password(
27 struct berval *passwd )
29 size_t nread, nleft, nr;
30 FILE *f = fopen( filename, "r" );
37 passwd->bv_val = NULL;
38 passwd->bv_len = 4196;
43 if ( fstat( fileno( f ), &sb ) == 0 ) {
44 if( sb.st_mode & 006 ) {
46 "Warning: Password file %s is publicly readable/writeable\n",
50 passwd->bv_len = sb.st_size;
53 #endif /* HAVE_FSTAT */
55 passwd->bv_val = (char *) malloc( passwd->bv_len + 1 );
56 if( passwd->bv_val == NULL ) {
62 nleft = passwd->bv_len;
65 /* double the buffer size */
66 char *p = (char *) realloc( passwd->bv_val,
67 2 * passwd->bv_len + 1 );
69 free( passwd->bv_val );
70 passwd->bv_val = NULL;
74 nleft = passwd->bv_len;
79 nr = fread( &passwd->bv_val[nread], 1, nleft, f );
81 if( nr < nleft && ferror( f ) ) {
82 free( passwd->bv_val );
83 passwd->bv_val = NULL;
92 passwd->bv_len = nread;
93 passwd->bv_val[nread] = '\0';