summaryrefslogtreecommitdiffstats
path: root/kdm/backend/krb5auth.c
blob: 16b640a35e416136dd7c1a5d2e366a27b6d875a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*

Copyright 1994, 1998  The Open Group
Copyright 2003 Oswald Buddenhagen <ossi@kde.org>

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from the copyright holder.

*/

/*
 * xdm - display manager daemon
 * Author: Stephen Gildea, The Open Group
 *
 * generate Kerberos Version 5 authorization records
 */

#include <config.h>

#ifdef K5AUTH

#include "dm.h"
#include "dm_auth.h"
#include "dm_error.h"

#include <sys/types.h>
#include <sys/stat.h>

#include <krb5/krb5.h>

static krb5_context ctx;

/*ARGSUSED*/
void
Krb5InitAuth( unsigned short name_len ATTR_UNUSED, const char *name ATTR_UNUSED )
{
	if (krb5_init_context( &ctx ))
		LogError( "Error while initializing Krb5 context\n" );
}

/*
 * Returns malloc'ed string that is the credentials cache name.
 * name should be freed by caller.
 */
static char *
Krb5CCacheName( const char *dname )
{
	char *name;
	const char *tmpdir;
	int dnl, nl;

	tmpdir = getenv( "TMPDIR" );
	if (!tmpdir)
		tmpdir = "/tmp";
	dnl = strlen( dname );
	name = Malloc( strlen( tmpdir ) + dnl + 20 );
	if (!name)
		return NULL;
	nl = sprintf( name, "FILE:%s/K5C", tmpdir );
	CleanUpFileName( dname, name + nl, dnl + 1 );
	return name;
}

Xauth *
Krb5GetAuthFor( unsigned short namelen, const char *name, const char *dname )
{
	Xauth *new;
	char *filename;

	if (!(new = (Xauth *)Malloc( sizeof(*new) )))
		return (Xauth *)0;
	new->family = FamilyWild;
	new->address_length = 0;
	new->address = 0;
	new->number_length = 0;
	new->number = 0;

	if (dname) {
		if (!(filename = Krb5CCacheName( dname ))) {
			free( (char *)new );
			return (Xauth *)0;
		}
		new->data = 0;
		if (!StrApp( &new->data, "UU:", filename, (char *)0 )) {
			free( filename );
			free( (char *)new );
			return (Xauth *)0;
		}
		free( filename );
		new->data_length = strlen( new->data );
	} else {
		new->data = NULL;
		new->data_length = 0;
	}

	if (!(new->name = (char *)Malloc( namelen ))) {
		free( (char *)new->data );
		free( (char *)new );
		return (Xauth *)0;
	}
	memmove( new->name, name, namelen );
	new->name_length = namelen;
	return new;
}


Xauth *
Krb5GetAuth( unsigned short namelen, const char *name )
{
	return Krb5GetAuthFor( namelen, name, NULL );
}


static krb5_error_code
Krb5DisplayCCache( const char *dname, krb5_ccache *ccache_return, char **name )
{
	char *ccname;
	krb5_error_code code;

	if (!(ccname = Krb5CCacheName( dname )))
		return ENOMEM;
	Debug( "resolving Kerberos cache %s\n", ccname );
	if ((code = krb5_cc_resolve( ctx, ccname, ccache_return )) || !name)
		free( ccname );
	else
		*name = ccname;
	return code;
}

char *
Krb5Init( const char *user, const char *passwd, const char *dname )
{
	krb5_error_code code;
	krb5_get_init_creds_opt options;
	krb5_principal me;
	krb5_creds my_creds;
	krb5_ccache ccache;
	char *ccname;

	if (!ctx)
		return 0;

	if ((code = krb5_parse_name( ctx, user, &me ))) {
		LogError( "%s while parsing Krb5 user %\"s\n",
		          error_message( code ), user );
		return 0;
	}

	krb5_get_init_creds_opt_init( &options );
	/*krb5_get_init_creds_opt_set_tkt_life (&options, 60*60*8);*/ /* 8 hours */

	if ((code = krb5_get_init_creds_password( ctx, &my_creds,
	                                          me, /* principal */
	                                          (char * /* for MIT */) passwd,
	                                          0, /* prompter */
	                                          0, /* prompter ctx */
	                                          0, /* start time delta */
	                                          0, /* service */
	                                          &options )))
	{
		char *my_name = NULL;
		int code2 = krb5_unparse_name( ctx, me, &my_name );
		if (code == KRB5KRB_AP_ERR_BAD_INTEGRITY)
			LogError( "Password incorrect for Krb5 principal %\"s\n",
			          code2 ? user : my_name );
		else
			LogError( "%s while getting initial Krb5 credentials for %\"s\n",
			          error_message( code ), code2 ? user : my_name );
		if (my_name)
			free( my_name );
		goto err3;
	}

	if ((code = Krb5DisplayCCache( dname, &ccache, &ccname ))) {
		LogError( "%s while getting Krb5 ccache for %\"s\n",
		          error_message( code ), dname );
		goto err2;
	}

	if ((code = krb5_cc_initialize( ctx, ccache, me ))) {
		LogError( "%s while initializing Krb5 cache %\"s\n",
		          error_message( code ), ccname );
		goto err1;
	}

	if ((code = krb5_cc_store_cred( ctx, ccache, &my_creds ))) {
		LogError( "%s while storing Krb5 credentials to cache %\"s\n",
		          error_message( code ), ccname );
	  err1:
		krb5_cc_close( ctx, ccache );
		free( ccname );
	  err2:
		krb5_free_cred_contents( ctx, &my_creds );
	  err3:
		krb5_free_principal( ctx, me );
		return 0;
	}

	krb5_cc_close( ctx, ccache );
	krb5_free_cred_contents( ctx, &my_creds );
	krb5_free_principal( ctx, me );
	return ccname;
}

void
Krb5Destroy( const char *dname )
{
	krb5_error_code code;
	krb5_ccache ccache;

	if (!ctx)
		return;

	if ((code = Krb5DisplayCCache( dname, &ccache, 0 )))
		LogError( "%s while getting Krb5 ccache to destroy\n",
		          error_message( code ) );
	else {
		if ((code = krb5_cc_destroy( ctx, ccache ))) {
			if (code == KRB5_FCC_NOFILE)
				Debug( "no Kerberos ccache file found to destroy\n" );
			else
				LogError( "%s while destroying Krb5 credentials cache\n",
				          error_message( code ) );
		} else
			Debug( "kerberos ccache destroyed\n" );
	}
}

#endif