xref: /netbsd-src/external/bsd/openldap/dist/libraries/liblutil/passfile.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: passfile.c,v 1.1.1.2 2010/03/08 02:14:20 lukem Exp $	*/
2 
3 /* OpenLDAP: pkg/ldap/libraries/liblutil/passfile.c,v 1.8.2.5 2009/03/09 23:16:48 quanah Exp */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2009 The OpenLDAP Foundation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 
18 #include "portable.h"
19 
20 #include <stdio.h>
21 
22 #include <ac/stdlib.h>
23 #include <ac/ctype.h>
24 #include <ac/string.h>
25 
26 #ifdef HAVE_FSTAT
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #endif /* HAVE_FSTAT */
30 
31 #include <lber.h>
32 #include <lutil.h>
33 
34 /* Get a password from a file. */
35 int
36 lutil_get_filed_password(
37 	const char *filename,
38 	struct berval *passwd )
39 {
40 	size_t nread, nleft, nr;
41 	FILE *f = fopen( filename, "r" );
42 
43 	if( f == NULL ) {
44 		perror( filename );
45 		return -1;
46 	}
47 
48 	passwd->bv_val = NULL;
49 	passwd->bv_len = 4096;
50 
51 #ifdef HAVE_FSTAT
52 	{
53 		struct stat sb;
54 		if ( fstat( fileno( f ), &sb ) == 0 ) {
55 			if( sb.st_mode & 006 ) {
56 				fprintf( stderr, _("Warning: Password file %s"
57 					" is publicly readable/writeable\n"),
58 					filename );
59 			}
60 
61 			if ( sb.st_size )
62 				passwd->bv_len = sb.st_size;
63 		}
64 	}
65 #endif /* HAVE_FSTAT */
66 
67 	passwd->bv_val = (char *) ber_memalloc( passwd->bv_len + 1 );
68 	if( passwd->bv_val == NULL ) {
69 		perror( filename );
70 		return -1;
71 	}
72 
73 	nread = 0;
74 	nleft = passwd->bv_len;
75 	do {
76 		if( nleft == 0 ) {
77 			/* double the buffer size */
78 			char *p = (char *) ber_memrealloc( passwd->bv_val,
79 				2 * passwd->bv_len + 1 );
80 			if( p == NULL ) {
81 				ber_memfree( passwd->bv_val );
82 				passwd->bv_val = NULL;
83 				passwd->bv_len = 0;
84 				return -1;
85 			}
86 			nleft = passwd->bv_len;
87 			passwd->bv_len *= 2;
88 			passwd->bv_val = p;
89 		}
90 
91 		nr = fread( &passwd->bv_val[nread], 1, nleft, f );
92 
93 		if( nr < nleft && ferror( f ) ) {
94 			ber_memfree( passwd->bv_val );
95 			passwd->bv_val = NULL;
96 			passwd->bv_len = 0;
97 			return -1;
98 		}
99 
100 		nread += nr;
101 		nleft -= nr;
102 	} while ( !feof(f) );
103 
104 	passwd->bv_len = nread;
105 	passwd->bv_val[nread] = '\0';
106 
107 	fclose( f );
108 	return 0;
109 }
110