s3-lib Remove very unused (#if 0) strncpyn
[samba.git] / libcli / auth / krb5_wrap.c
1 /*
2    Unix SMB/CIFS implementation.
3    simple kerberos5 routines for active directory
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) Luke Howard 2002-2003
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7    Copyright (C) Guenther Deschner 2005-2009
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "libcli/auth/krb5_wrap.h"
25
26 #ifdef HAVE_KRB5
27
28 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
29 int create_kerberos_key_from_string_direct(krb5_context context,
30                                                   krb5_principal host_princ,
31                                                   krb5_data *password,
32                                                   krb5_keyblock *key,
33                                                   krb5_enctype enctype)
34 {
35         int ret = 0;
36         krb5_data salt;
37         krb5_encrypt_block eblock;
38
39         ret = krb5_principal2salt(context, host_princ, &salt);
40         if (ret) {
41                 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
42                 return ret;
43         }
44         krb5_use_enctype(context, &eblock, enctype);
45         ret = krb5_string_to_key(context, &eblock, key, password, &salt);
46         SAFE_FREE(salt.data);
47
48         return ret;
49 }
50 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
51 int create_kerberos_key_from_string_direct(krb5_context context,
52                                                   krb5_principal host_princ,
53                                                   krb5_data *password,
54                                                   krb5_keyblock *key,
55                                                   krb5_enctype enctype)
56 {
57         int ret;
58         krb5_salt salt;
59
60         ret = krb5_get_pw_salt(context, host_princ, &salt);
61         if (ret) {
62                 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
63                 return ret;
64         }
65
66         ret = krb5_string_to_key_salt(context, enctype, (const char *)password->data, salt, key);
67         krb5_free_salt(context, salt);
68
69         return ret;
70 }
71 #else
72 #error UNKNOWN_CREATE_KEY_FUNCTIONS
73 #endif
74
75  void kerberos_free_data_contents(krb5_context context, krb5_data *pdata)
76 {
77 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
78         if (pdata->data) {
79                 krb5_free_data_contents(context, pdata);
80         }
81 #elif defined(HAVE_KRB5_DATA_FREE)
82         krb5_data_free(context, pdata);
83 #else
84         SAFE_FREE(pdata->data);
85 #endif
86 }
87
88
89  krb5_error_code smb_krb5_kt_free_entry(krb5_context context, krb5_keytab_entry *kt_entry)
90 {
91 /* Try krb5_free_keytab_entry_contents first, since
92  * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
93  * krb5_kt_free_entry but only has a prototype for the first, while the
94  * second is considered private.
95  */
96 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
97         return krb5_free_keytab_entry_contents(context, kt_entry);
98 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
99         return krb5_kt_free_entry(context, kt_entry);
100 #else
101 #error UNKNOWN_KT_FREE_FUNCTION
102 #endif
103 }
104
105  char *smb_get_krb5_error_message(krb5_context context, krb5_error_code code, TALLOC_CTX *mem_ctx)
106 {
107         char *ret;
108
109 #if defined(HAVE_KRB5_GET_ERROR_MESSAGE) && defined(HAVE_KRB5_FREE_ERROR_MESSAGE)
110         const char *context_error = krb5_get_error_message(context, code);
111         if (context_error) {
112                 ret = talloc_asprintf(mem_ctx, "%s: %s", error_message(code), context_error);
113                 krb5_free_error_message(context, context_error);
114                 return ret;
115         }
116 #endif
117         ret = talloc_strdup(mem_ctx, error_message(code));
118         return ret;
119 }
120
121 #endif