1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* plugins/kdb/ldap/libkdb_ldap/ldap_service_stash.c */
3 /*
4 * Copyright (c) 2004-2005, Novell, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * * The copyright holder's name is not used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "ldap_main.h"
32 #include "kdb_ldap.h"
33 #include "ldap_service_stash.h"
34 #include <k5-hex.h>
35 #include <ctype.h>
36
37 /* Decode a password of the form {HEX}<hexstring>. */
38 static krb5_error_code
dec_password(krb5_context context,const char * str,char ** password_out)39 dec_password(krb5_context context, const char *str, char **password_out)
40 {
41 krb5_error_code ret;
42 uint8_t *bytes;
43 size_t len;
44
45 *password_out = NULL;
46
47 if (strncmp(str, "{HEX}", 5) != 0) {
48 k5_setmsg(context, EINVAL, _("Not a hexadecimal password"));
49 return EINVAL;
50 }
51
52 ret = k5_hex_decode(str + 5, &bytes, &len);
53 if (ret) {
54 if (ret == EINVAL)
55 k5_setmsg(context, ret, _("Password corrupt"));
56 return ret;
57 }
58
59 *password_out = (char *)bytes;
60 return 0;
61 }
62
63 krb5_error_code
krb5_ldap_readpassword(krb5_context context,const char * filename,const char * name,char ** password_out)64 krb5_ldap_readpassword(krb5_context context, const char *filename,
65 const char *name, char **password_out)
66 {
67 krb5_error_code ret;
68 char line[RECORDLEN], *end;
69 const char *start, *sep, *val = NULL;
70 int namelen = strlen(name);
71 FILE *fp;
72
73 *password_out = NULL;
74
75 fp = fopen(filename, "r");
76 if (fp == NULL) {
77 ret = errno;
78 k5_setmsg(context, ret, _("Cannot open LDAP password file '%s': %s"),
79 filename, error_message(ret));
80 return ret;
81 }
82 set_cloexec_file(fp);
83
84 while (fgets(line, RECORDLEN, fp) != NULL) {
85 /* Remove trailing newline. */
86 end = line + strlen(line);
87 if (end > line && end[-1] == '\n')
88 end[-1] = '\0';
89
90 /* Skip past leading whitespace. */
91 for (start = line; isspace(*start); ++start);
92
93 /* Ignore comment lines */
94 if (*start == '!' || *start == '#')
95 continue;
96
97 sep = strchr(start, '#');
98 if (sep != NULL && sep - start == namelen &&
99 strncasecmp(start, name, namelen) == 0) {
100 val = sep + 1;
101 break;
102 }
103 }
104 fclose(fp);
105
106 if (val == NULL) {
107 k5_setmsg(context, KRB5_KDB_SERVER_INTERNAL_ERR,
108 _("Bind DN entry '%s' missing in LDAP password file '%s'"),
109 name, filename);
110 return KRB5_KDB_SERVER_INTERNAL_ERR;
111 }
112
113 /* Extract the plain password information. */
114 return dec_password(context, val, password_out);
115 }
116