summaryrefslogtreecommitdiff
path: root/source3/nsswitch/winbindd_cred_cache.c
blob: 8fc80584413ddec03861e7a22fbc3f28778964d8 (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
   Unix SMB/CIFS implementation.

   Winbind daemon - krb5 credential cache funcions

   Copyright (C) Guenther Deschner 2005
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "winbindd.h"
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_WINBIND

#define MAX_CCACHES 100 

static struct WINBINDD_CCACHE_ENTRY *ccache_list;

static TALLOC_CTX *mem_ctx;

const char *get_ccache_name_by_username(const char *username) 
{
	struct WINBINDD_CCACHE_ENTRY *entry;

	for (entry = ccache_list; entry; entry = entry->next) {
		if (strequal(entry->username, username)) {
			return entry->ccname;
		}
	}
	return NULL;
}

struct WINBINDD_CCACHE_ENTRY *get_ccache_by_username(const char *username)
{
	struct WINBINDD_CCACHE_ENTRY *entry;

	for (entry = ccache_list; entry; entry = entry->next) {
		if (strequal(entry->username, username)) {
			return entry;
		}
	}
	return NULL;
}

static int ccache_entry_count(void)
{
	struct WINBINDD_CCACHE_ENTRY *entry;
	int i = 0;

	for (entry = ccache_list; entry; entry = entry->next) {
		i++;
	}
	return i;
}

NTSTATUS remove_ccache_by_ccname(const char *ccname)
{
	struct WINBINDD_CCACHE_ENTRY *entry;

	for (entry = ccache_list; entry; entry = entry->next) {
		if (strequal(entry->ccname, ccname)) {
			DLIST_REMOVE(ccache_list, entry);
			TALLOC_FREE(entry->event); /* unregisters events */
			TALLOC_FREE(entry);
			DEBUG(10,("remove_ccache_by_ccname: removed ccache %s\n", ccname));
			return NT_STATUS_OK;
		}
	}
	return NT_STATUS_OBJECT_NAME_NOT_FOUND;
}

static void krb5_ticket_refresh_handler(struct timed_event *te,
					const struct timeval *now,
					void *private_data)
{
	struct WINBINDD_CCACHE_ENTRY *entry =
		talloc_get_type_abort(private_data, struct WINBINDD_CCACHE_ENTRY);
	int ret;
	time_t new_start;
	struct timeval t;


	DEBUG(10,("krb5_ticket_refresh_handler called\n"));
	DEBUGADD(10,("event called for: %s, %s\n", entry->ccname, entry->username));

	TALLOC_FREE(entry->event);

#ifdef HAVE_KRB5

	/* Kinit again if we have the user password and we can't renew the old
	 * tgt anymore */

	if ((entry->renew_until < time(NULL)) && (entry->pass != NULL)) {
	     
		set_effective_uid(entry->uid);

		ret = kerberos_kinit_password_ext(entry->principal_name,
						  entry->pass,
						  0, /* hm, can we do time correction here ? */
						  &entry->refresh_time,
						  &entry->renew_until,
						  entry->ccname,
						  False, /* no PAC required anymore */
						  True,
						  WINBINDD_PAM_AUTH_KRB5_RENEW_TIME);
		gain_root_privilege();

		if (ret) {
			DEBUG(3,("could not re-kinit: %s\n", error_message(ret)));
			TALLOC_FREE(entry->event);
			return;
		}

		DEBUG(10,("successful re-kinit for: %s in ccache: %s\n", 
			entry->principal_name, entry->ccname));

		new_start = entry->refresh_time;

		goto done;
	}

	set_effective_uid(entry->uid);

	ret = smb_krb5_renew_ticket(entry->ccname, 
				    entry->principal_name,
				    entry->service,
				    &new_start);
	gain_root_privilege();

	if (ret) {
		DEBUG(3,("could not renew tickets: %s\n", error_message(ret)));
		/* maybe we are beyond the renewing window */
		return;
	}

done:

	t = timeval_set(new_start, 0);

	entry->event = add_timed_event(mem_ctx, 
				       t,
				       "krb5_ticket_refresh_handler",
				       krb5_ticket_refresh_handler,
				       entry);

#endif
}

NTSTATUS add_ccache_to_list(const char *princ_name,
			    const char *ccname,
			    const char *service,
			    const char *username, 
			    const char *sid_string,
			    const char *pass,
			    uid_t uid,
			    time_t create_time, 
			    time_t ticket_end, 
			    time_t renew_until, 
			    BOOL schedule_refresh_event)
{
	struct WINBINDD_CCACHE_ENTRY *new_entry = NULL;
	struct WINBINDD_CCACHE_ENTRY *old_entry = NULL;
	NTSTATUS status;

	if ((username == NULL && sid_string == NULL && princ_name == NULL) || 
	    ccname == NULL) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	status = init_ccache_list();
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	if (mem_ctx == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	if (ccache_entry_count() + 1 > MAX_CCACHES) {
		DEBUG(10,("add_ccache_to_list: max number of ccaches reached\n"));
		return NT_STATUS_NO_MORE_ENTRIES;
	}

	/* get rid of old entries */
	old_entry = get_ccache_by_username(username);
	if (old_entry) {
		status = remove_ccache_by_ccname(old_entry->ccname);
		if (!NT_STATUS_IS_OK(status)) {
			DEBUG(10,("add_ccache_to_list: failed to delete old ccache entry\n"));
			return status;
		}
	}
	
	new_entry = TALLOC_P(mem_ctx, struct WINBINDD_CCACHE_ENTRY);
	if (new_entry == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	ZERO_STRUCTP(new_entry);

	if (username) {
		new_entry->username = talloc_strdup(mem_ctx, username);
		NT_STATUS_HAVE_NO_MEMORY(new_entry->username);
	}
	if (sid_string) {
		new_entry->sid_string = talloc_strdup(mem_ctx, sid_string);
		NT_STATUS_HAVE_NO_MEMORY(new_entry->sid_string);
	}
	if (princ_name) {
		new_entry->principal_name = talloc_strdup(mem_ctx, princ_name);
		NT_STATUS_HAVE_NO_MEMORY(new_entry->principal_name);
	}
	if (service) {
		new_entry->service = talloc_strdup(mem_ctx, service);
		NT_STATUS_HAVE_NO_MEMORY(new_entry->service);
	}
	if (schedule_refresh_event && pass) {
		new_entry->pass = talloc_strdup(mem_ctx, pass);
		NT_STATUS_HAVE_NO_MEMORY(new_entry->pass);
	}

	new_entry->create_time = create_time;
	new_entry->renew_until = renew_until;
	new_entry->ccname = talloc_strdup(mem_ctx, ccname);
	if (new_entry->ccname == NULL) {
		return NT_STATUS_NO_MEMORY;
	}
	new_entry->uid = uid;


	if (schedule_refresh_event && renew_until > 0) {

		struct timeval t = timeval_set((ticket_end -1 ), 0);

		new_entry->event = add_timed_event(mem_ctx, 
						   t,
						   "krb5_ticket_refresh_handler",
						   krb5_ticket_refresh_handler,
						   new_entry);
	}

	DLIST_ADD(ccache_list, new_entry);

	DEBUG(10,("add_ccache_to_list: added ccache [%s] for user [%s] to the list\n", ccname, username));

	return NT_STATUS_OK;
}

NTSTATUS destroy_ccache_list(void)
{
	return talloc_destroy(mem_ctx) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
}

NTSTATUS init_ccache_list(void)
{
	if (ccache_list) {
		return NT_STATUS_OK;
	}

	mem_ctx = talloc_init("winbindd_ccache_krb5_handling");
	if (mem_ctx == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	ZERO_STRUCTP(ccache_list);

	return NT_STATUS_OK;
}