]> git.sur5r.net Git - openldap/blob - libraries/liblutil/passfile.c
a4b314d0383b8041e8a5a9bebb00f974859a0174
[openldap] / libraries / liblutil / passfile.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19
20 #include <ac/stdlib.h>
21 #include <ac/ctype.h>
22 #include <ac/string.h>
23
24 #ifdef HAVE_FSTAT
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #endif /* HAVE_FSTAT */
28
29 #include <lber.h>
30 #include <lutil.h>
31
32 /* Get a password from a file. */
33 int
34 lutil_get_filed_password(
35         const char *filename,
36         struct berval *passwd )
37 {
38         size_t nread, nleft, nr;
39         FILE *f = fopen( filename, "r" );
40
41         if( f == NULL ) {
42                 perror( filename );
43                 return -1;
44         }
45
46         passwd->bv_val = NULL;
47         passwd->bv_len = 4196;
48
49 #ifdef HAVE_FSTAT
50         {
51                 struct stat sb;
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"),
56                                         filename );
57                         }
58
59                         passwd->bv_len = sb.st_size;
60                 }
61         }
62 #endif /* HAVE_FSTAT */
63
64         passwd->bv_val = (char *) malloc( passwd->bv_len + 1 );
65         if( passwd->bv_val == NULL ) {
66                 perror( filename );
67                 return -1;
68         }
69
70         nread = 0;
71         nleft = passwd->bv_len;
72         do {
73                 if( nleft == 0 ) {
74                         /* double the buffer size */
75                         char *p = (char *) realloc( passwd->bv_val,
76                                 2 * passwd->bv_len + 1 );
77                         if( p == NULL ) {
78                                 free( passwd->bv_val );
79                                 passwd->bv_val = NULL;
80                                 passwd->bv_len = 0;
81                                 return -1;
82                         }
83                         nleft = passwd->bv_len;
84                         passwd->bv_len *= 2;
85                         passwd->bv_val = p;
86                 }
87
88                 nr = fread( &passwd->bv_val[nread], 1, nleft, f );
89
90                 if( nr < nleft && ferror( f ) ) {
91                         free( passwd->bv_val );
92                         passwd->bv_val = NULL;
93                         passwd->bv_len = 0;
94                         return -1;
95                 }
96
97                 nread += nr;
98                 nleft -= nr;
99         } while ( !feof(f) );
100
101         passwd->bv_len = nread;
102         passwd->bv_val[nread] = '\0';
103
104         fclose( f );
105         return 0;
106 }