]> git.sur5r.net Git - openldap/blob - libraries/liblutil/passfile.c
Happy new year
[openldap] / libraries / liblutil / passfile.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 2002-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/stdlib.h>
12 #include <ac/ctype.h>
13 #include <ac/string.h>
14
15 #ifdef HAVE_FSTAT
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #endif /* HAVE_FSTAT */
19
20 #include <lber.h>
21 #include <lutil.h>
22
23 /* Get a password from a file. */
24 int
25 lutil_get_filed_password(
26         const char *filename,
27         struct berval *passwd )
28 {
29         size_t nread, nleft, nr;
30         FILE *f = fopen( filename, "r" );
31
32         if( f == NULL ) {
33                 perror( filename );
34                 return -1;
35         }
36
37         passwd->bv_val = NULL;
38         passwd->bv_len = 4196;
39
40 #ifdef HAVE_FSTAT
41         {
42                 struct stat sb;
43                 if ( fstat( fileno( f ), &sb ) == 0 ) {
44                         if( sb.st_mode & 006 ) {
45                                 fprintf( stderr,
46                                         "Warning: Password file %s is publicly readable/writeable\n",
47                                         filename );
48                         }
49
50                         passwd->bv_len = sb.st_size;
51                 }
52         }
53 #endif /* HAVE_FSTAT */
54
55         passwd->bv_val = (char *) malloc( passwd->bv_len + 1 );
56         if( passwd->bv_val == NULL ) {
57                 perror( filename );
58                 return -1;
59         }
60
61         nread = 0;
62         nleft = passwd->bv_len;
63         do {
64                 if( nleft == 0 ) {
65                         /* double the buffer size */
66                         char *p = (char *) realloc( passwd->bv_val,
67                                 2 * passwd->bv_len + 1 );
68                         if( p == NULL ) {
69                                 free( passwd->bv_val );
70                                 passwd->bv_val = NULL;
71                                 passwd->bv_len = 0;
72                                 return -1;
73                         }
74                         nleft = passwd->bv_len;
75                         passwd->bv_len *= 2;
76                         passwd->bv_val = p;
77                 }
78
79                 nr = fread( &passwd->bv_val[nread], 1, nleft, f );
80
81                 if( nr < nleft && ferror( f ) ) {
82                         free( passwd->bv_val );
83                         passwd->bv_val = NULL;
84                         passwd->bv_len = 0;
85                         return -1;
86                 }
87
88                 nread += nr;
89                 nleft -= nr;
90         } while ( !feof(f) );
91
92         passwd->bv_len = nread;
93         passwd->bv_val[nread] = '\0';
94
95         fclose( f );
96         return 0;
97 }