xref: /illumos-gate/usr/src/lib/smbsrv/libsmbns/common/smbns_krb.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
25  */
26 /*
27  * Copyright 1990 by the Massachusetts Institute of Technology.
28  * All Rights Reserved.
29  *
30  * Export of this software from the United States of America may
31  *   require a specific license from the United States Government.
32  *   It is the responsibility of any person or organization contemplating
33  *   export to obtain such a license before exporting.
34  *
35  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
36  * distribute this software and its documentation for any purpose and
37  * without fee is hereby granted, provided that the above copyright
38  * notice appear in all copies and that both that copyright notice and
39  * this permission notice appear in supporting documentation, and that
40  * the name of M.I.T. not be used in advertising or publicity pertaining
41  * to distribution of the software without specific, written prior
42  * permission.  Furthermore if you modify this software you must label
43  * your software as modified software and not distribute it in such a
44  * fashion that it might be confused with the original M.I.T. software.
45  * M.I.T. makes no representations about the suitability of
46  * this software for any purpose.  It is provided "as is" without express
47  * or implied warranty.
48  *
49  *
50  * Initialize a credentials cache.
51  */
52 #include <kerberosv5/krb5.h>
53 #include <kerberosv5/com_err.h>
54 #include <assert.h>
55 #include <stdio.h>
56 #include <syslog.h>
57 #include <errno.h>
58 
59 #include <smbsrv/libsmbns.h>
60 #include <smbns_krb.h>
61 
62 int
63 smb_kinit(char *domain_name, char *principal_name, char *principal_passwd)
64 {
65 	char default_realm[MAXHOSTNAMELEN];
66 	krb5_context ctx = NULL;
67 	krb5_ccache cc = NULL;
68 	krb5_principal me = NULL;
69 	krb5_creds my_creds;
70 	krb5_error_code code;
71 	const char *doing = NULL;
72 	smb_ads_status_t err;
73 
74 	assert(principal_name != NULL);
75 	assert(principal_passwd != NULL);
76 
77 	(void) memset(&my_creds, 0, sizeof (my_creds));
78 
79 	/*
80 	 * From this point on, we can goto cleanup because the key variables
81 	 * are initialized.
82 	 */
83 
84 	code = krb5_init_context(&ctx);
85 	if (code) {
86 		err = SMB_ADS_KRB5_INIT_CTX;
87 		doing = "smbns_krb: initializing context";
88 		goto cleanup;
89 	}
90 
91 	/*
92 	 * In case krb5.conf is not configured, set the default realm.
93 	 */
94 	(void) strlcpy(default_realm, domain_name, sizeof (default_realm));
95 	(void) smb_strupr(default_realm);
96 	(void) krb5_set_default_realm(ctx, default_realm);
97 
98 	code = krb5_cc_default(ctx, &cc);
99 	if (code != 0) {
100 		err = SMB_ADS_KRB5_CC_DEFAULT;
101 		doing = "smbns_krb: resolve default credentials cache";
102 		goto cleanup;
103 	}
104 
105 	/* Use specified name */
106 	code = krb5_parse_name(ctx, principal_name, &me);
107 	if (code != 0) {
108 		err = SMB_ADS_KRB5_PARSE_PRINCIPAL;
109 		doing = "smbns_krb: parsing principal name";
110 		goto cleanup;
111 	}
112 
113 	code = krb5_get_init_creds_password(ctx, &my_creds, me,
114 	    principal_passwd, NULL, 0, (krb5_deltat)0,
115 	    NULL, NULL);
116 	if (code != 0) {
117 		doing = "smbns_krb: getting initial credentials";
118 		switch (code) {
119 
120 		case KRB5KRB_AP_ERR_BAD_INTEGRITY:
121 			err = SMB_ADS_KRB5_GET_INIT_CREDS_PW;
122 			break;
123 
124 		case KRB5KRB_AP_ERR_SKEW:
125 			err = SMB_ADS_KRB5_GET_INIT_CREDS_SKEW;
126 			break;
127 
128 		default:
129 			err = SMB_ADS_KRB5_GET_INIT_CREDS_OTHER;
130 			break;
131 		}
132 
133 		goto cleanup;
134 	}
135 
136 	code = krb5_cc_initialize(ctx, cc, me);
137 	if (code != 0) {
138 		err = SMB_ADS_KRB5_CC_INITIALIZE;
139 		doing = "smbns_krb: initializing cache";
140 		goto cleanup;
141 	}
142 
143 	code = krb5_cc_store_cred(ctx, cc, &my_creds);
144 	if (code != 0) {
145 		err = SMB_ADS_KRB5_CC_STORE_CRED;
146 		doing = "smbns_krb: storing credentials";
147 		goto cleanup;
148 	}
149 
150 	/* SUCCESS! */
151 	err = SMB_ADS_SUCCESS;
152 
153 cleanup:
154 	if (code != 0) {
155 		smb_krb5_log_errmsg(ctx, doing, code);
156 	}
157 
158 	if (my_creds.client == me) {
159 		my_creds.client = NULL;
160 	}
161 	krb5_free_cred_contents(ctx, &my_creds);
162 
163 	if (me)
164 		krb5_free_principal(ctx, me);
165 	if (cc)
166 		(void) krb5_cc_close(ctx, cc);
167 	if (ctx)
168 		krb5_free_context(ctx);
169 
170 	return (err);
171 }
172 
173 /*
174  * Invoke krb5_get_error_message() to generate a richer error message.
175  */
176 void
177 smb_krb5_log_errmsg(krb5_context ctx, const char *prefix, krb5_error_code code)
178 {
179 	const char *msg;
180 
181 	msg = krb5_get_error_message(ctx, code);
182 	syslog(LOG_ERR, "%s (%s)", prefix, msg);
183 	krb5_free_error_message(ctx, msg);
184 }
185 
186 /*
187  * smb_ccache_init
188  *
189  * Creates the directory where the Kerberos ccache file is located
190  * and set KRB5CCNAME in the environment.
191  *
192  * Returns 0 upon succcess.  Otherwise, returns
193  * -1 if it fails to create the specified directory fails.
194  * -2 if it fails to set the KRB5CCNAME environment variable.
195  */
196 int
197 smb_ccache_init(char *dir, char *filename)
198 {
199 	static char buf[MAXPATHLEN];
200 
201 	if ((mkdir(dir, 0700) < 0) && (errno != EEXIST))
202 		return (-1);
203 
204 	(void) snprintf(buf, MAXPATHLEN, "KRB5CCNAME=%s/%s", dir, filename);
205 	if (putenv(buf) != 0)
206 		return (-2);
207 	return (0);
208 }
209 
210 void
211 smb_ccache_remove(char *path)
212 {
213 	if ((remove(path) < 0) && (errno != ENOENT))
214 		syslog(LOG_ERR, "smbns_krb: failed to remove ccache (%s)",
215 		    path);
216 }
217