Use anon realm for anonymous PKINIT
[heimdal.git] / lib / krb5 / scache.c
blob7b04a5197395049fbc444198574846347bb969ee
1 /*
2 * Copyright (c) 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
36 #ifdef HAVE_SCC
38 #include <sqlite3.h>
40 typedef struct krb5_scache {
41 char *name;
42 char *file;
43 sqlite3 *db;
45 sqlite_uint64 cid;
47 sqlite3_stmt *icred;
48 sqlite3_stmt *dcred;
49 sqlite3_stmt *iprincipal;
51 sqlite3_stmt *icache;
52 sqlite3_stmt *ucachen;
53 sqlite3_stmt *ucachep;
54 sqlite3_stmt *dcache;
55 sqlite3_stmt *scache;
56 sqlite3_stmt *scache_name;
57 sqlite3_stmt *umaster;
59 } krb5_scache;
61 #define SCACHE(X) ((krb5_scache *)(X)->data.data)
63 #define SCACHE_DEF_NAME "Default-cache"
64 #ifdef KRB5_USE_PATH_TOKENS
65 #define KRB5_SCACHE_DB "%{TEMP}/krb5scc_%{uid}"
66 #else
67 #define KRB5_SCACHE_DB "/tmp/krb5scc_%{uid}"
68 #endif
69 #define KRB5_SCACHE_NAME "SCC:" SCACHE_DEF_NAME ":" KRB5_SCACHE_DB
71 #define SCACHE_INVALID_CID ((sqlite_uint64)-1)
77 #define SQL_CMASTER "" \
78 "CREATE TABLE master (" \
79 "oid INTEGER PRIMARY KEY," \
80 "version INTEGER NOT NULL," \
81 "defaultcache TEXT NOT NULL" \
82 ")"
84 #define SQL_SETUP_MASTER \
85 "INSERT INTO master (version,defaultcache) VALUES(2, \"" SCACHE_DEF_NAME "\")"
86 #define SQL_UMASTER "UPDATE master SET defaultcache=? WHERE version=2"
88 #define SQL_CCACHE "" \
89 "CREATE TABLE caches (" \
90 "oid INTEGER PRIMARY KEY," \
91 "principal TEXT," \
92 "name TEXT NOT NULL" \
93 ")"
95 #define SQL_TCACHE "" \
96 "CREATE TRIGGER CacheDropCreds AFTER DELETE ON caches " \
97 "FOR EACH ROW BEGIN " \
98 "DELETE FROM credentials WHERE cid=old.oid;" \
99 "END"
101 #define SQL_ICACHE "INSERT INTO caches (name) VALUES(?)"
102 #define SQL_UCACHE_NAME "UPDATE caches SET name=? WHERE OID=?"
103 #define SQL_UCACHE_PRINCIPAL "UPDATE caches SET principal=? WHERE OID=?"
104 #define SQL_DCACHE "DELETE FROM caches WHERE OID=?"
105 #define SQL_SCACHE "SELECT principal,name FROM caches WHERE OID=?"
106 #define SQL_SCACHE_NAME "SELECT oid FROM caches WHERE NAME=?"
108 #define SQL_CCREDS "" \
109 "CREATE TABLE credentials (" \
110 "oid INTEGER PRIMARY KEY," \
111 "cid INTEGER NOT NULL," \
112 "kvno INTEGER NOT NULL," \
113 "etype INTEGER NOT NULL," \
114 "created_at INTEGER NOT NULL," \
115 "cred BLOB NOT NULL" \
118 #define SQL_TCRED "" \
119 "CREATE TRIGGER credDropPrincipal AFTER DELETE ON credentials " \
120 "FOR EACH ROW BEGIN " \
121 "DELETE FROM principals WHERE credential_id=old.oid;" \
122 "END"
124 #define SQL_ICRED "INSERT INTO credentials (cid, kvno, etype, cred, created_at) VALUES (?,?,?,?,?)"
125 #define SQL_DCRED "DELETE FROM credentials WHERE cid=?"
127 #define SQL_CPRINCIPALS "" \
128 "CREATE TABLE principals (" \
129 "oid INTEGER PRIMARY KEY," \
130 "principal TEXT NOT NULL," \
131 "type INTEGER NOT NULL," \
132 "credential_id INTEGER NOT NULL" \
135 #define SQL_IPRINCIPAL "INSERT INTO principals (principal, type, credential_id) VALUES (?,?,?)"
138 * sqlite destructors
141 static void
142 free_data(void *data)
144 free(data);
147 static void
148 free_krb5(void *str)
150 krb5_xfree(str);
153 static void
154 scc_free(krb5_scache *s)
156 if (s->file)
157 free(s->file);
158 if (s->name)
159 free(s->name);
161 if (s->icred)
162 sqlite3_finalize(s->icred);
163 if (s->dcred)
164 sqlite3_finalize(s->dcred);
165 if (s->iprincipal)
166 sqlite3_finalize(s->iprincipal);
167 if (s->icache)
168 sqlite3_finalize(s->icache);
169 if (s->ucachen)
170 sqlite3_finalize(s->ucachen);
171 if (s->ucachep)
172 sqlite3_finalize(s->ucachep);
173 if (s->dcache)
174 sqlite3_finalize(s->dcache);
175 if (s->scache)
176 sqlite3_finalize(s->scache);
177 if (s->scache_name)
178 sqlite3_finalize(s->scache_name);
179 if (s->umaster)
180 sqlite3_finalize(s->umaster);
182 if (s->db)
183 sqlite3_close(s->db);
184 free(s);
187 #ifdef TRACEME
188 static void
189 trace(void* ptr, const char * str)
191 printf("SQL: %s\n", str);
193 #endif
195 static krb5_error_code
196 prepare_stmt(krb5_context context, sqlite3 *db,
197 sqlite3_stmt **stmt, const char *str)
199 int ret;
201 ret = sqlite3_prepare_v2(db, str, -1, stmt, NULL);
202 if (ret != SQLITE_OK) {
203 krb5_set_error_message(context, ENOENT,
204 N_("Failed to prepare stmt %s: %s", ""),
205 str, sqlite3_errmsg(db));
206 return ENOENT;
208 return 0;
211 static krb5_error_code
212 exec_stmt(krb5_context context, sqlite3 *db, const char *str,
213 krb5_error_code code)
215 int ret;
217 ret = sqlite3_exec(db, str, NULL, NULL, NULL);
218 if (ret != SQLITE_OK && code) {
219 krb5_set_error_message(context, code,
220 N_("scache execute %s: %s", ""), str,
221 sqlite3_errmsg(db));
222 return code;
224 return 0;
227 static krb5_error_code
228 default_db(krb5_context context, sqlite3 **db)
230 char *name;
231 int ret;
233 ret = _krb5_expand_default_cc_name(context, KRB5_SCACHE_DB, &name);
234 if (ret)
235 return ret;
237 ret = sqlite3_open_v2(name, db, SQLITE_OPEN_READWRITE, NULL);
238 free(name);
239 if (ret != SQLITE_OK) {
240 krb5_clear_error_message(context);
241 return ENOENT;
244 #ifdef TRACEME
245 sqlite3_trace(*db, trace, NULL);
246 #endif
248 return 0;
251 static krb5_error_code
252 get_def_name(krb5_context context, char **str)
254 krb5_error_code ret;
255 sqlite3_stmt *stmt;
256 const char *name;
257 sqlite3 *db;
259 ret = default_db(context, &db);
260 if (ret)
261 return ret;
263 ret = prepare_stmt(context, db, &stmt, "SELECT defaultcache FROM master");
264 if (ret) {
265 sqlite3_close(db);
266 return ret;
269 ret = sqlite3_step(stmt);
270 if (ret != SQLITE_ROW)
271 goto out;
273 if (sqlite3_column_type(stmt, 0) != SQLITE_TEXT)
274 goto out;
276 name = (const char *)sqlite3_column_text(stmt, 0);
277 if (name == NULL)
278 goto out;
280 *str = strdup(name);
281 if (*str == NULL)
282 goto out;
284 sqlite3_finalize(stmt);
285 sqlite3_close(db);
286 return 0;
287 out:
288 sqlite3_finalize(stmt);
289 sqlite3_close(db);
290 krb5_clear_error_message(context);
291 return ENOENT;
296 static krb5_scache * KRB5_CALLCONV
297 scc_alloc(krb5_context context, const char *name)
299 krb5_error_code ret;
300 krb5_scache *s;
302 ALLOC(s, 1);
303 if(s == NULL)
304 return NULL;
306 s->cid = SCACHE_INVALID_CID;
308 if (name) {
309 char *file;
311 if (*name == '\0') {
312 ret = get_def_name(context, &s->name);
313 if (ret)
314 s->name = strdup(SCACHE_DEF_NAME);
315 } else
316 s->name = strdup(name);
318 file = strrchr(s->name, ':');
319 if (file) {
320 *file++ = '\0';
321 s->file = strdup(file);
322 ret = 0;
323 } else {
324 ret = _krb5_expand_default_cc_name(context, KRB5_SCACHE_DB, &s->file);
326 } else {
327 _krb5_expand_default_cc_name(context, KRB5_SCACHE_DB, &s->file);
328 ret = asprintf(&s->name, "unique-%p", s);
330 if (ret < 0 || s->file == NULL || s->name == NULL) {
331 scc_free(s);
332 return NULL;
335 return s;
338 static krb5_error_code
339 open_database(krb5_context context, krb5_scache *s, int flags)
341 int ret;
343 ret = sqlite3_open_v2(s->file, &s->db, SQLITE_OPEN_READWRITE|flags, NULL);
344 if (ret) {
345 if (s->db) {
346 krb5_set_error_message(context, ENOENT,
347 N_("Error opening scache file %s: %s", ""),
348 s->file, sqlite3_errmsg(s->db));
349 sqlite3_close(s->db);
350 s->db = NULL;
351 } else
352 krb5_set_error_message(context, ENOENT,
353 N_("malloc: out of memory", ""));
354 return ENOENT;
356 return 0;
359 static krb5_error_code
360 create_cache(krb5_context context, krb5_scache *s)
362 int ret;
364 sqlite3_bind_text(s->icache, 1, s->name, -1, NULL);
365 do {
366 ret = sqlite3_step(s->icache);
367 } while (ret == SQLITE_ROW);
368 if (ret != SQLITE_DONE) {
369 krb5_set_error_message(context, KRB5_CC_IO,
370 N_("Failed to add scache: %d", ""), ret);
371 return KRB5_CC_IO;
373 sqlite3_reset(s->icache);
375 s->cid = sqlite3_last_insert_rowid(s->db);
377 return 0;
380 static krb5_error_code
381 make_database(krb5_context context, krb5_scache *s)
383 int created_file = 0;
384 int ret;
386 if (s->db)
387 return 0;
389 ret = open_database(context, s, 0);
390 if (ret) {
391 mode_t oldumask = umask(077);
392 ret = open_database(context, s, SQLITE_OPEN_CREATE);
393 umask(oldumask);
394 if (ret) goto out;
396 created_file = 1;
398 ret = exec_stmt(context, s->db, SQL_CMASTER, KRB5_CC_IO);
399 if (ret) goto out;
400 ret = exec_stmt(context, s->db, SQL_CCACHE, KRB5_CC_IO);
401 if (ret) goto out;
402 ret = exec_stmt(context, s->db, SQL_CCREDS, KRB5_CC_IO);
403 if (ret) goto out;
404 ret = exec_stmt(context, s->db, SQL_CPRINCIPALS, KRB5_CC_IO);
405 if (ret) goto out;
406 ret = exec_stmt(context, s->db, SQL_SETUP_MASTER, KRB5_CC_IO);
407 if (ret) goto out;
409 ret = exec_stmt(context, s->db, SQL_TCACHE, KRB5_CC_IO);
410 if (ret) goto out;
411 ret = exec_stmt(context, s->db, SQL_TCRED, KRB5_CC_IO);
412 if (ret) goto out;
415 #ifdef TRACEME
416 sqlite3_trace(s->db, trace, NULL);
417 #endif
419 ret = prepare_stmt(context, s->db, &s->icred, SQL_ICRED);
420 if (ret) goto out;
421 ret = prepare_stmt(context, s->db, &s->dcred, SQL_DCRED);
422 if (ret) goto out;
423 ret = prepare_stmt(context, s->db, &s->iprincipal, SQL_IPRINCIPAL);
424 if (ret) goto out;
425 ret = prepare_stmt(context, s->db, &s->icache, SQL_ICACHE);
426 if (ret) goto out;
427 ret = prepare_stmt(context, s->db, &s->ucachen, SQL_UCACHE_NAME);
428 if (ret) goto out;
429 ret = prepare_stmt(context, s->db, &s->ucachep, SQL_UCACHE_PRINCIPAL);
430 if (ret) goto out;
431 ret = prepare_stmt(context, s->db, &s->dcache, SQL_DCACHE);
432 if (ret) goto out;
433 ret = prepare_stmt(context, s->db, &s->scache, SQL_SCACHE);
434 if (ret) goto out;
435 ret = prepare_stmt(context, s->db, &s->scache_name, SQL_SCACHE_NAME);
436 if (ret) goto out;
437 ret = prepare_stmt(context, s->db, &s->umaster, SQL_UMASTER);
438 if (ret) goto out;
440 return 0;
442 out:
443 if (s->db)
444 sqlite3_close(s->db);
445 if (created_file)
446 unlink(s->file);
448 return ret;
451 static krb5_error_code
452 bind_principal(krb5_context context,
453 sqlite3 *db,
454 sqlite3_stmt *stmt,
455 int col,
456 krb5_const_principal principal)
458 krb5_error_code ret;
459 char *str;
461 ret = krb5_unparse_name(context, principal, &str);
462 if (ret)
463 return ret;
465 ret = sqlite3_bind_text(stmt, col, str, -1, free_krb5);
466 if (ret != SQLITE_OK) {
467 krb5_xfree(str);
468 krb5_set_error_message(context, ENOMEM,
469 N_("scache bind principal: %s", ""),
470 sqlite3_errmsg(db));
471 return ENOMEM;
473 return 0;
480 static const char* KRB5_CALLCONV
481 scc_get_name(krb5_context context,
482 krb5_ccache id)
484 return SCACHE(id)->name;
487 static krb5_error_code KRB5_CALLCONV
488 scc_resolve(krb5_context context, krb5_ccache *id, const char *res)
490 krb5_scache *s;
491 int ret;
493 s = scc_alloc(context, res);
494 if (s == NULL) {
495 krb5_set_error_message(context, KRB5_CC_NOMEM,
496 N_("malloc: out of memory", ""));
497 return KRB5_CC_NOMEM;
500 ret = make_database(context, s);
501 if (ret) {
502 scc_free(s);
503 return ret;
506 ret = sqlite3_bind_text(s->scache_name, 1, s->name, -1, NULL);
507 if (ret != SQLITE_OK) {
508 krb5_set_error_message(context, ENOMEM,
509 "bind name: %s", sqlite3_errmsg(s->db));
510 scc_free(s);
511 return ENOMEM;
514 if (sqlite3_step(s->scache_name) == SQLITE_ROW) {
516 if (sqlite3_column_type(s->scache_name, 0) != SQLITE_INTEGER) {
517 sqlite3_reset(s->scache_name);
518 krb5_set_error_message(context, KRB5_CC_END,
519 N_("Cache name of wrong type "
520 "for scache %s", ""),
521 s->name);
522 scc_free(s);
523 return KRB5_CC_END;
526 s->cid = sqlite3_column_int(s->scache_name, 0);
527 } else {
528 s->cid = SCACHE_INVALID_CID;
530 sqlite3_reset(s->scache_name);
532 (*id)->data.data = s;
533 (*id)->data.length = sizeof(*s);
535 return 0;
538 static krb5_error_code KRB5_CALLCONV
539 scc_gen_new(krb5_context context, krb5_ccache *id)
541 krb5_scache *s;
543 s = scc_alloc(context, NULL);
545 if (s == NULL) {
546 krb5_set_error_message(context, KRB5_CC_NOMEM,
547 N_("malloc: out of memory", ""));
548 return KRB5_CC_NOMEM;
551 (*id)->data.data = s;
552 (*id)->data.length = sizeof(*s);
554 return 0;
557 static krb5_error_code KRB5_CALLCONV
558 scc_initialize(krb5_context context,
559 krb5_ccache id,
560 krb5_principal primary_principal)
562 krb5_scache *s = SCACHE(id);
563 krb5_error_code ret;
565 ret = make_database(context, s);
566 if (ret)
567 return ret;
569 ret = exec_stmt(context, s->db, "BEGIN IMMEDIATE TRANSACTION", KRB5_CC_IO);
570 if (ret) return ret;
572 if (s->cid == SCACHE_INVALID_CID) {
573 ret = create_cache(context, s);
574 if (ret)
575 goto rollback;
576 } else {
577 sqlite3_bind_int(s->dcred, 1, s->cid);
578 do {
579 ret = sqlite3_step(s->dcred);
580 } while (ret == SQLITE_ROW);
581 sqlite3_reset(s->dcred);
582 if (ret != SQLITE_DONE) {
583 ret = KRB5_CC_IO;
584 krb5_set_error_message(context, ret,
585 N_("Failed to delete old "
586 "credentials: %s", ""),
587 sqlite3_errmsg(s->db));
588 goto rollback;
592 ret = bind_principal(context, s->db, s->ucachep, 1, primary_principal);
593 if (ret)
594 goto rollback;
595 sqlite3_bind_int(s->ucachep, 2, s->cid);
597 do {
598 ret = sqlite3_step(s->ucachep);
599 } while (ret == SQLITE_ROW);
600 sqlite3_reset(s->ucachep);
601 if (ret != SQLITE_DONE) {
602 ret = KRB5_CC_IO;
603 krb5_set_error_message(context, ret,
604 N_("Failed to bind principal to cache %s", ""),
605 sqlite3_errmsg(s->db));
606 goto rollback;
609 ret = exec_stmt(context, s->db, "COMMIT", KRB5_CC_IO);
610 if (ret) return ret;
612 return 0;
614 rollback:
615 exec_stmt(context, s->db, "ROLLBACK", 0);
617 return ret;
621 static krb5_error_code KRB5_CALLCONV
622 scc_close(krb5_context context,
623 krb5_ccache id)
625 scc_free(SCACHE(id));
626 return 0;
629 static krb5_error_code KRB5_CALLCONV
630 scc_destroy(krb5_context context,
631 krb5_ccache id)
633 krb5_scache *s = SCACHE(id);
634 int ret;
636 if (s->cid == SCACHE_INVALID_CID)
637 return 0;
639 sqlite3_bind_int(s->dcache, 1, s->cid);
640 do {
641 ret = sqlite3_step(s->dcache);
642 } while (ret == SQLITE_ROW);
643 sqlite3_reset(s->dcache);
644 if (ret != SQLITE_DONE) {
645 krb5_set_error_message(context, KRB5_CC_IO,
646 N_("Failed to destroy cache %s: %s", ""),
647 s->name, sqlite3_errmsg(s->db));
648 return KRB5_CC_IO;
650 return 0;
653 static krb5_error_code
654 encode_creds(krb5_context context, krb5_creds *creds, krb5_data *data)
656 krb5_error_code ret;
657 krb5_storage *sp;
659 sp = krb5_storage_emem();
660 if (sp == NULL)
661 return krb5_enomem(context);
663 ret = krb5_store_creds(sp, creds);
664 if (ret) {
665 krb5_set_error_message(context, ret,
666 N_("Failed to store credential in scache", ""));
667 krb5_storage_free(sp);
668 return ret;
671 ret = krb5_storage_to_data(sp, data);
672 krb5_storage_free(sp);
673 if (ret)
674 krb5_set_error_message(context, ret,
675 N_("Failed to encode credential in scache", ""));
676 return ret;
679 static krb5_error_code
680 decode_creds(krb5_context context, const void *data, size_t length,
681 krb5_creds *creds)
683 krb5_error_code ret;
684 krb5_storage *sp;
686 sp = krb5_storage_from_readonly_mem(data, length);
687 if (sp == NULL)
688 return krb5_enomem(context);
690 ret = krb5_ret_creds(sp, creds);
691 krb5_storage_free(sp);
692 if (ret) {
693 krb5_set_error_message(context, ret,
694 N_("Failed to read credential in scache", ""));
695 return ret;
697 return 0;
701 static krb5_error_code KRB5_CALLCONV
702 scc_store_cred(krb5_context context,
703 krb5_ccache id,
704 krb5_creds *creds)
706 sqlite_uint64 credid;
707 krb5_scache *s = SCACHE(id);
708 krb5_error_code ret;
709 krb5_data data;
711 ret = make_database(context, s);
712 if (ret)
713 return ret;
715 ret = encode_creds(context, creds, &data);
716 if (ret)
717 return ret;
719 sqlite3_bind_int(s->icred, 1, s->cid);
721 krb5_enctype etype = 0;
722 int kvno = 0;
723 Ticket t;
724 size_t len;
726 ret = decode_Ticket(creds->ticket.data,
727 creds->ticket.length, &t, &len);
728 if (ret == 0) {
729 if(t.enc_part.kvno)
730 kvno = *t.enc_part.kvno;
732 etype = t.enc_part.etype;
734 free_Ticket(&t);
737 sqlite3_bind_int(s->icred, 2, kvno);
738 sqlite3_bind_int(s->icred, 3, etype);
742 sqlite3_bind_blob(s->icred, 4, data.data, data.length, free_data);
743 sqlite3_bind_int(s->icred, 5, time(NULL));
745 ret = exec_stmt(context, s->db, "BEGIN IMMEDIATE TRANSACTION", KRB5_CC_IO);
746 if (ret) return ret;
748 do {
749 ret = sqlite3_step(s->icred);
750 } while (ret == SQLITE_ROW);
751 sqlite3_reset(s->icred);
752 if (ret != SQLITE_DONE) {
753 ret = KRB5_CC_IO;
754 krb5_set_error_message(context, ret,
755 N_("Failed to add credential: %s", ""),
756 sqlite3_errmsg(s->db));
757 goto rollback;
760 credid = sqlite3_last_insert_rowid(s->db);
763 bind_principal(context, s->db, s->iprincipal, 1, creds->server);
764 sqlite3_bind_int(s->iprincipal, 2, 1);
765 sqlite3_bind_int(s->iprincipal, 3, credid);
767 do {
768 ret = sqlite3_step(s->iprincipal);
769 } while (ret == SQLITE_ROW);
770 sqlite3_reset(s->iprincipal);
771 if (ret != SQLITE_DONE) {
772 ret = KRB5_CC_IO;
773 krb5_set_error_message(context, ret,
774 N_("Failed to add principal: %s", ""),
775 sqlite3_errmsg(s->db));
776 goto rollback;
781 bind_principal(context, s->db, s->iprincipal, 1, creds->client);
782 sqlite3_bind_int(s->iprincipal, 2, 0);
783 sqlite3_bind_int(s->iprincipal, 3, credid);
785 do {
786 ret = sqlite3_step(s->iprincipal);
787 } while (ret == SQLITE_ROW);
788 sqlite3_reset(s->iprincipal);
789 if (ret != SQLITE_DONE) {
790 ret = KRB5_CC_IO;
791 krb5_set_error_message(context, ret,
792 N_("Failed to add principal: %s", ""),
793 sqlite3_errmsg(s->db));
794 goto rollback;
798 ret = exec_stmt(context, s->db, "COMMIT", KRB5_CC_IO);
799 if (ret) return ret;
801 return 0;
803 rollback:
804 exec_stmt(context, s->db, "ROLLBACK", 0);
806 return ret;
809 static krb5_error_code KRB5_CALLCONV
810 scc_get_principal(krb5_context context,
811 krb5_ccache id,
812 krb5_principal *principal)
814 krb5_scache *s = SCACHE(id);
815 krb5_error_code ret;
816 const char *str;
818 *principal = NULL;
820 ret = make_database(context, s);
821 if (ret)
822 return ret;
824 sqlite3_bind_int(s->scache, 1, s->cid);
826 if (sqlite3_step(s->scache) != SQLITE_ROW) {
827 sqlite3_reset(s->scache);
828 krb5_set_error_message(context, KRB5_CC_END,
829 N_("No principal for cache SCC:%s:%s", ""),
830 s->name, s->file);
831 return KRB5_CC_END;
834 if (sqlite3_column_type(s->scache, 0) != SQLITE_TEXT) {
835 sqlite3_reset(s->scache);
836 krb5_set_error_message(context, KRB5_CC_END,
837 N_("Principal data of wrong type "
838 "for SCC:%s:%s", ""),
839 s->name, s->file);
840 return KRB5_CC_END;
843 str = (const char *)sqlite3_column_text(s->scache, 0);
844 if (str == NULL) {
845 sqlite3_reset(s->scache);
846 krb5_set_error_message(context, KRB5_CC_END,
847 N_("Principal not set for SCC:%s:%s", ""),
848 s->name, s->file);
849 return KRB5_CC_END;
852 ret = krb5_parse_name(context, str, principal);
854 sqlite3_reset(s->scache);
856 return ret;
859 struct cred_ctx {
860 char *drop;
861 sqlite3_stmt *stmt;
862 sqlite3_stmt *credstmt;
865 static krb5_error_code KRB5_CALLCONV
866 scc_get_first (krb5_context context,
867 krb5_ccache id,
868 krb5_cc_cursor *cursor)
870 krb5_scache *s = SCACHE(id);
871 krb5_error_code ret;
872 struct cred_ctx *ctx;
873 char *str = NULL, *name = NULL;
875 *cursor = NULL;
877 ctx = calloc(1, sizeof(*ctx));
878 if (ctx == NULL)
879 return krb5_enomem(context);
881 ret = make_database(context, s);
882 if (ret) {
883 free(ctx);
884 return ret;
887 if (s->cid == SCACHE_INVALID_CID) {
888 krb5_set_error_message(context, KRB5_CC_END,
889 N_("Iterating a invalid scache %s", ""),
890 s->name);
891 free(ctx);
892 return KRB5_CC_END;
895 ret = asprintf(&name, "credIteration%pPid%d",
896 ctx, (int)getpid());
897 if (ret < 0 || name == NULL) {
898 free(ctx);
899 return krb5_enomem(context);
902 ret = asprintf(&ctx->drop, "DROP TABLE %s", name);
903 if (ret < 0 || ctx->drop == NULL) {
904 free(name);
905 free(ctx);
906 return krb5_enomem(context);
909 ret = asprintf(&str, "CREATE TEMPORARY TABLE %s "
910 "AS SELECT oid,created_at FROM credentials WHERE cid = %lu",
911 name, (unsigned long)s->cid);
912 if (ret < 0 || str == NULL) {
913 free(ctx->drop);
914 free(name);
915 free(ctx);
916 return krb5_enomem(context);
919 ret = exec_stmt(context, s->db, str, KRB5_CC_IO);
920 free(str);
921 str = NULL;
922 if (ret) {
923 free(ctx->drop);
924 free(name);
925 free(ctx);
926 return ret;
929 ret = asprintf(&str, "SELECT oid FROM %s ORDER BY created_at", name);
930 if (ret < 0 || str == NULL) {
931 exec_stmt(context, s->db, ctx->drop, 0);
932 free(ctx->drop);
933 free(name);
934 free(ctx);
935 return ret;
938 ret = prepare_stmt(context, s->db, &ctx->stmt, str);
939 free(str);
940 str = NULL;
941 free(name);
942 if (ret) {
943 exec_stmt(context, s->db, ctx->drop, 0);
944 free(ctx->drop);
945 free(ctx);
946 return ret;
949 ret = prepare_stmt(context, s->db, &ctx->credstmt,
950 "SELECT cred FROM credentials WHERE oid = ?");
951 if (ret) {
952 sqlite3_finalize(ctx->stmt);
953 exec_stmt(context, s->db, ctx->drop, 0);
954 free(ctx->drop);
955 free(ctx);
956 return ret;
959 *cursor = ctx;
961 return 0;
964 static krb5_error_code KRB5_CALLCONV
965 scc_get_next (krb5_context context,
966 krb5_ccache id,
967 krb5_cc_cursor *cursor,
968 krb5_creds *creds)
970 struct cred_ctx *ctx = *cursor;
971 krb5_scache *s = SCACHE(id);
972 krb5_error_code ret;
973 sqlite_uint64 oid;
974 const void *data = NULL;
975 size_t len = 0;
977 next:
978 ret = sqlite3_step(ctx->stmt);
979 if (ret == SQLITE_DONE) {
980 krb5_clear_error_message(context);
981 return KRB5_CC_END;
982 } else if (ret != SQLITE_ROW) {
983 krb5_set_error_message(context, KRB5_CC_IO,
984 N_("scache Database failed: %s", ""),
985 sqlite3_errmsg(s->db));
986 return KRB5_CC_IO;
989 oid = sqlite3_column_int64(ctx->stmt, 0);
991 /* read cred from credentials table */
993 sqlite3_bind_int(ctx->credstmt, 1, oid);
995 ret = sqlite3_step(ctx->credstmt);
996 if (ret != SQLITE_ROW) {
997 sqlite3_reset(ctx->credstmt);
998 goto next;
1001 if (sqlite3_column_type(ctx->credstmt, 0) != SQLITE_BLOB) {
1002 krb5_set_error_message(context, KRB5_CC_END,
1003 N_("credential of wrong type for SCC:%s:%s", ""),
1004 s->name, s->file);
1005 sqlite3_reset(ctx->credstmt);
1006 return KRB5_CC_END;
1009 data = sqlite3_column_blob(ctx->credstmt, 0);
1010 len = sqlite3_column_bytes(ctx->credstmt, 0);
1012 ret = decode_creds(context, data, len, creds);
1013 sqlite3_reset(ctx->credstmt);
1014 return ret;
1017 static krb5_error_code KRB5_CALLCONV
1018 scc_end_get (krb5_context context,
1019 krb5_ccache id,
1020 krb5_cc_cursor *cursor)
1022 struct cred_ctx *ctx = *cursor;
1023 krb5_scache *s = SCACHE(id);
1025 sqlite3_finalize(ctx->stmt);
1026 sqlite3_finalize(ctx->credstmt);
1028 exec_stmt(context, s->db, ctx->drop, 0);
1030 free(ctx->drop);
1031 free(ctx);
1033 return 0;
1036 static krb5_error_code KRB5_CALLCONV
1037 scc_remove_cred(krb5_context context,
1038 krb5_ccache id,
1039 krb5_flags which,
1040 krb5_creds *mcreds)
1042 krb5_scache *s = SCACHE(id);
1043 krb5_error_code ret;
1044 sqlite3_stmt *stmt;
1045 sqlite_uint64 credid = 0;
1046 const void *data = NULL;
1047 size_t len = 0;
1049 ret = make_database(context, s);
1050 if (ret)
1051 return ret;
1053 ret = prepare_stmt(context, s->db, &stmt,
1054 "SELECT cred,oid FROM credentials "
1055 "WHERE cid = ?");
1056 if (ret)
1057 return ret;
1059 sqlite3_bind_int(stmt, 1, s->cid);
1061 /* find credential... */
1062 while (1) {
1063 krb5_creds creds;
1065 ret = sqlite3_step(stmt);
1066 if (ret == SQLITE_DONE) {
1067 ret = 0;
1068 break;
1069 } else if (ret != SQLITE_ROW) {
1070 ret = KRB5_CC_IO;
1071 krb5_set_error_message(context, ret,
1072 N_("scache Database failed: %s", ""),
1073 sqlite3_errmsg(s->db));
1074 break;
1077 if (sqlite3_column_type(stmt, 0) != SQLITE_BLOB) {
1078 ret = KRB5_CC_END;
1079 krb5_set_error_message(context, ret,
1080 N_("Credential of wrong type "
1081 "for SCC:%s:%s", ""),
1082 s->name, s->file);
1083 break;
1086 data = sqlite3_column_blob(stmt, 0);
1087 len = sqlite3_column_bytes(stmt, 0);
1089 ret = decode_creds(context, data, len, &creds);
1090 if (ret)
1091 break;
1093 ret = krb5_compare_creds(context, which, mcreds, &creds);
1094 krb5_free_cred_contents(context, &creds);
1095 if (ret) {
1096 credid = sqlite3_column_int64(stmt, 1);
1097 ret = 0;
1098 break;
1102 sqlite3_finalize(stmt);
1104 if (id) {
1105 ret = prepare_stmt(context, s->db, &stmt,
1106 "DELETE FROM credentials WHERE oid=?");
1107 if (ret)
1108 return ret;
1109 sqlite3_bind_int(stmt, 1, credid);
1111 do {
1112 ret = sqlite3_step(stmt);
1113 } while (ret == SQLITE_ROW);
1114 sqlite3_finalize(stmt);
1115 if (ret != SQLITE_DONE) {
1116 ret = KRB5_CC_IO;
1117 krb5_set_error_message(context, ret,
1118 N_("failed to delete scache credental", ""));
1119 } else
1120 ret = 0;
1123 return ret;
1126 static krb5_error_code KRB5_CALLCONV
1127 scc_set_flags(krb5_context context,
1128 krb5_ccache id,
1129 krb5_flags flags)
1131 return 0; /* XXX */
1134 struct cache_iter {
1135 char *drop;
1136 sqlite3 *db;
1137 sqlite3_stmt *stmt;
1140 static krb5_error_code KRB5_CALLCONV
1141 scc_get_cache_first(krb5_context context, krb5_cc_cursor *cursor)
1143 struct cache_iter *ctx;
1144 krb5_error_code ret;
1145 char *name = NULL, *str = NULL;
1147 *cursor = NULL;
1149 ctx = calloc(1, sizeof(*ctx));
1150 if (ctx == NULL)
1151 return krb5_enomem(context);
1153 ret = default_db(context, &ctx->db);
1154 if (ctx->db == NULL) {
1155 free(ctx);
1156 return ret;
1159 ret = asprintf(&name, "cacheIteration%pPid%d",
1160 ctx, (int)getpid());
1161 if (ret < 0 || name == NULL) {
1162 sqlite3_close(ctx->db);
1163 free(ctx);
1164 return krb5_enomem(context);
1167 ret = asprintf(&ctx->drop, "DROP TABLE %s", name);
1168 if (ret < 0 || ctx->drop == NULL) {
1169 sqlite3_close(ctx->db);
1170 free(name);
1171 free(ctx);
1172 return krb5_enomem(context);
1175 ret = asprintf(&str, "CREATE TEMPORARY TABLE %s AS SELECT name FROM caches",
1176 name);
1177 if (ret < 0 || str == NULL) {
1178 sqlite3_close(ctx->db);
1179 free(name);
1180 free(ctx->drop);
1181 free(ctx);
1182 return krb5_enomem(context);
1185 ret = exec_stmt(context, ctx->db, str, KRB5_CC_IO);
1186 free(str);
1187 str = NULL;
1188 if (ret) {
1189 sqlite3_close(ctx->db);
1190 free(name);
1191 free(ctx->drop);
1192 free(ctx);
1193 return ret;
1196 ret = asprintf(&str, "SELECT name FROM %s", name);
1197 free(name);
1198 if (ret < 0 || str == NULL) {
1199 exec_stmt(context, ctx->db, ctx->drop, 0);
1200 sqlite3_close(ctx->db);
1201 free(name);
1202 free(ctx->drop);
1203 free(ctx);
1204 return krb5_enomem(context);
1207 ret = prepare_stmt(context, ctx->db, &ctx->stmt, str);
1208 free(str);
1209 if (ret) {
1210 exec_stmt(context, ctx->db, ctx->drop, 0);
1211 sqlite3_close(ctx->db);
1212 free(ctx->drop);
1213 free(ctx);
1214 return ret;
1217 *cursor = ctx;
1219 return 0;
1222 static krb5_error_code KRB5_CALLCONV
1223 scc_get_cache_next(krb5_context context,
1224 krb5_cc_cursor cursor,
1225 krb5_ccache *id)
1227 struct cache_iter *ctx = cursor;
1228 krb5_error_code ret;
1229 const char *name;
1231 again:
1232 ret = sqlite3_step(ctx->stmt);
1233 if (ret == SQLITE_DONE) {
1234 krb5_clear_error_message(context);
1235 return KRB5_CC_END;
1236 } else if (ret != SQLITE_ROW) {
1237 krb5_set_error_message(context, KRB5_CC_IO,
1238 N_("Database failed: %s", ""),
1239 sqlite3_errmsg(ctx->db));
1240 return KRB5_CC_IO;
1243 if (sqlite3_column_type(ctx->stmt, 0) != SQLITE_TEXT)
1244 goto again;
1246 name = (const char *)sqlite3_column_text(ctx->stmt, 0);
1247 if (name == NULL)
1248 goto again;
1250 ret = _krb5_cc_allocate(context, &krb5_scc_ops, id);
1251 if (ret)
1252 return ret;
1254 return scc_resolve(context, id, name);
1257 static krb5_error_code KRB5_CALLCONV
1258 scc_end_cache_get(krb5_context context, krb5_cc_cursor cursor)
1260 struct cache_iter *ctx = cursor;
1262 exec_stmt(context, ctx->db, ctx->drop, 0);
1263 sqlite3_finalize(ctx->stmt);
1264 sqlite3_close(ctx->db);
1265 free(ctx->drop);
1266 free(ctx);
1267 return 0;
1270 static krb5_error_code KRB5_CALLCONV
1271 scc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
1273 krb5_scache *sfrom = SCACHE(from);
1274 krb5_scache *sto = SCACHE(to);
1275 krb5_error_code ret;
1277 if (strcmp(sfrom->file, sto->file) != 0) {
1278 krb5_set_error_message(context, KRB5_CC_BADNAME,
1279 N_("Can't handle cross database "
1280 "credential move: %s -> %s", ""),
1281 sfrom->file, sto->file);
1282 return KRB5_CC_BADNAME;
1285 ret = make_database(context, sfrom);
1286 if (ret)
1287 return ret;
1289 ret = exec_stmt(context, sfrom->db,
1290 "BEGIN IMMEDIATE TRANSACTION", KRB5_CC_IO);
1291 if (ret) return ret;
1293 if (sto->cid != SCACHE_INVALID_CID) {
1294 /* drop old cache entry */
1296 sqlite3_bind_int(sfrom->dcache, 1, sto->cid);
1297 do {
1298 ret = sqlite3_step(sfrom->dcache);
1299 } while (ret == SQLITE_ROW);
1300 sqlite3_reset(sfrom->dcache);
1301 if (ret != SQLITE_DONE) {
1302 krb5_set_error_message(context, KRB5_CC_IO,
1303 N_("Failed to delete old cache: %d", ""),
1304 (int)ret);
1305 goto rollback;
1309 sqlite3_bind_text(sfrom->ucachen, 1, sto->name, -1, NULL);
1310 sqlite3_bind_int(sfrom->ucachen, 2, sfrom->cid);
1312 do {
1313 ret = sqlite3_step(sfrom->ucachen);
1314 } while (ret == SQLITE_ROW);
1315 sqlite3_reset(sfrom->ucachen);
1316 if (ret != SQLITE_DONE) {
1317 krb5_set_error_message(context, KRB5_CC_IO,
1318 N_("Failed to update new cache: %d", ""),
1319 (int)ret);
1320 goto rollback;
1323 sto->cid = sfrom->cid;
1325 ret = exec_stmt(context, sfrom->db, "COMMIT", KRB5_CC_IO);
1326 if (ret) return ret;
1328 scc_free(sfrom);
1330 return 0;
1332 rollback:
1333 exec_stmt(context, sfrom->db, "ROLLBACK", 0);
1334 scc_free(sfrom);
1336 return KRB5_CC_IO;
1339 static krb5_error_code KRB5_CALLCONV
1340 scc_get_default_name(krb5_context context, char **str)
1342 krb5_error_code ret;
1343 char *name;
1345 *str = NULL;
1347 ret = get_def_name(context, &name);
1348 if (ret)
1349 return _krb5_expand_default_cc_name(context, KRB5_SCACHE_NAME, str);
1351 ret = asprintf(str, "SCC:%s", name);
1352 free(name);
1353 if (ret < 0 || *str == NULL)
1354 return krb5_enomem(context);
1355 return 0;
1358 static krb5_error_code KRB5_CALLCONV
1359 scc_set_default(krb5_context context, krb5_ccache id)
1361 krb5_scache *s = SCACHE(id);
1362 krb5_error_code ret;
1364 if (s->cid == SCACHE_INVALID_CID) {
1365 krb5_set_error_message(context, KRB5_CC_IO,
1366 N_("Trying to set a invalid cache "
1367 "as default %s", ""),
1368 s->name);
1369 return KRB5_CC_IO;
1372 ret = sqlite3_bind_text(s->umaster, 1, s->name, -1, NULL);
1373 if (ret) {
1374 sqlite3_reset(s->umaster);
1375 krb5_set_error_message(context, KRB5_CC_IO,
1376 N_("Failed to set name of default cache", ""));
1377 return KRB5_CC_IO;
1380 do {
1381 ret = sqlite3_step(s->umaster);
1382 } while (ret == SQLITE_ROW);
1383 sqlite3_reset(s->umaster);
1384 if (ret != SQLITE_DONE) {
1385 krb5_set_error_message(context, KRB5_CC_IO,
1386 N_("Failed to update default cache", ""));
1387 return KRB5_CC_IO;
1390 return 0;
1394 * Variable containing the SCC based credential cache implemention.
1396 * @ingroup krb5_ccache
1399 KRB5_LIB_VARIABLE const krb5_cc_ops krb5_scc_ops = {
1400 KRB5_CC_OPS_VERSION,
1401 "SCC",
1402 scc_get_name,
1403 scc_resolve,
1404 scc_gen_new,
1405 scc_initialize,
1406 scc_destroy,
1407 scc_close,
1408 scc_store_cred,
1409 NULL, /* scc_retrieve */
1410 scc_get_principal,
1411 scc_get_first,
1412 scc_get_next,
1413 scc_end_get,
1414 scc_remove_cred,
1415 scc_set_flags,
1416 NULL,
1417 scc_get_cache_first,
1418 scc_get_cache_next,
1419 scc_end_cache_get,
1420 scc_move,
1421 scc_get_default_name,
1422 scc_set_default,
1423 NULL,
1424 NULL,
1425 NULL
1428 #endif