Fix make dist missing files (#228)
[heimdal.git] / lib / krb5 / scache.c
blob61a9b4fa14450224d0adc9bc100590705c250801
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 krb5_data_zero(data);
660 sp = krb5_storage_emem();
661 if (sp == NULL)
662 return krb5_enomem(context);
664 ret = krb5_store_creds(sp, creds);
665 if (ret) {
666 krb5_set_error_message(context, ret,
667 N_("Failed to store credential in scache", ""));
668 krb5_storage_free(sp);
669 return ret;
672 ret = krb5_storage_to_data(sp, data);
673 krb5_storage_free(sp);
674 if (ret)
675 krb5_set_error_message(context, ret,
676 N_("Failed to encode credential in scache", ""));
677 return ret;
680 static krb5_error_code
681 decode_creds(krb5_context context, const void *data, size_t length,
682 krb5_creds *creds)
684 krb5_error_code ret;
685 krb5_storage *sp;
687 sp = krb5_storage_from_readonly_mem(data, length);
688 if (sp == NULL)
689 return krb5_enomem(context);
691 ret = krb5_ret_creds(sp, creds);
692 krb5_storage_free(sp);
693 if (ret) {
694 krb5_set_error_message(context, ret,
695 N_("Failed to read credential in scache", ""));
696 return ret;
698 return 0;
702 static krb5_error_code KRB5_CALLCONV
703 scc_store_cred(krb5_context context,
704 krb5_ccache id,
705 krb5_creds *creds)
707 sqlite_uint64 credid;
708 krb5_scache *s = SCACHE(id);
709 krb5_error_code ret;
710 krb5_data data;
712 ret = make_database(context, s);
713 if (ret)
714 return ret;
716 ret = encode_creds(context, creds, &data);
717 if (ret)
718 return ret;
720 sqlite3_bind_int(s->icred, 1, s->cid);
722 krb5_enctype etype = 0;
723 int kvno = 0;
724 Ticket t;
725 size_t len;
727 ret = decode_Ticket(creds->ticket.data,
728 creds->ticket.length, &t, &len);
729 if (ret == 0) {
730 if(t.enc_part.kvno)
731 kvno = *t.enc_part.kvno;
733 etype = t.enc_part.etype;
735 free_Ticket(&t);
738 sqlite3_bind_int(s->icred, 2, kvno);
739 sqlite3_bind_int(s->icred, 3, etype);
743 sqlite3_bind_blob(s->icred, 4, data.data, data.length, free_data);
744 sqlite3_bind_int(s->icred, 5, time(NULL));
746 ret = exec_stmt(context, s->db, "BEGIN IMMEDIATE TRANSACTION", KRB5_CC_IO);
747 if (ret) return ret;
749 do {
750 ret = sqlite3_step(s->icred);
751 } while (ret == SQLITE_ROW);
752 sqlite3_reset(s->icred);
753 if (ret != SQLITE_DONE) {
754 ret = KRB5_CC_IO;
755 krb5_set_error_message(context, ret,
756 N_("Failed to add credential: %s", ""),
757 sqlite3_errmsg(s->db));
758 goto rollback;
761 credid = sqlite3_last_insert_rowid(s->db);
764 bind_principal(context, s->db, s->iprincipal, 1, creds->server);
765 sqlite3_bind_int(s->iprincipal, 2, 1);
766 sqlite3_bind_int(s->iprincipal, 3, credid);
768 do {
769 ret = sqlite3_step(s->iprincipal);
770 } while (ret == SQLITE_ROW);
771 sqlite3_reset(s->iprincipal);
772 if (ret != SQLITE_DONE) {
773 ret = KRB5_CC_IO;
774 krb5_set_error_message(context, ret,
775 N_("Failed to add principal: %s", ""),
776 sqlite3_errmsg(s->db));
777 goto rollback;
782 bind_principal(context, s->db, s->iprincipal, 1, creds->client);
783 sqlite3_bind_int(s->iprincipal, 2, 0);
784 sqlite3_bind_int(s->iprincipal, 3, credid);
786 do {
787 ret = sqlite3_step(s->iprincipal);
788 } while (ret == SQLITE_ROW);
789 sqlite3_reset(s->iprincipal);
790 if (ret != SQLITE_DONE) {
791 ret = KRB5_CC_IO;
792 krb5_set_error_message(context, ret,
793 N_("Failed to add principal: %s", ""),
794 sqlite3_errmsg(s->db));
795 goto rollback;
799 ret = exec_stmt(context, s->db, "COMMIT", KRB5_CC_IO);
800 if (ret) return ret;
802 return 0;
804 rollback:
805 exec_stmt(context, s->db, "ROLLBACK", 0);
807 return ret;
810 static krb5_error_code KRB5_CALLCONV
811 scc_get_principal(krb5_context context,
812 krb5_ccache id,
813 krb5_principal *principal)
815 krb5_scache *s = SCACHE(id);
816 krb5_error_code ret;
817 const char *str;
819 *principal = NULL;
821 ret = make_database(context, s);
822 if (ret)
823 return ret;
825 sqlite3_bind_int(s->scache, 1, s->cid);
827 if (sqlite3_step(s->scache) != SQLITE_ROW) {
828 sqlite3_reset(s->scache);
829 krb5_set_error_message(context, KRB5_CC_END,
830 N_("No principal for cache SCC:%s:%s", ""),
831 s->name, s->file);
832 return KRB5_CC_END;
835 if (sqlite3_column_type(s->scache, 0) != SQLITE_TEXT) {
836 sqlite3_reset(s->scache);
837 krb5_set_error_message(context, KRB5_CC_END,
838 N_("Principal data of wrong type "
839 "for SCC:%s:%s", ""),
840 s->name, s->file);
841 return KRB5_CC_END;
844 str = (const char *)sqlite3_column_text(s->scache, 0);
845 if (str == NULL) {
846 sqlite3_reset(s->scache);
847 krb5_set_error_message(context, KRB5_CC_END,
848 N_("Principal not set for SCC:%s:%s", ""),
849 s->name, s->file);
850 return KRB5_CC_END;
853 ret = krb5_parse_name(context, str, principal);
855 sqlite3_reset(s->scache);
857 return ret;
860 struct cred_ctx {
861 char *drop;
862 sqlite3_stmt *stmt;
863 sqlite3_stmt *credstmt;
866 static krb5_error_code KRB5_CALLCONV
867 scc_get_first (krb5_context context,
868 krb5_ccache id,
869 krb5_cc_cursor *cursor)
871 krb5_scache *s = SCACHE(id);
872 krb5_error_code ret;
873 struct cred_ctx *ctx;
874 char *str = NULL, *name = NULL;
876 *cursor = NULL;
878 ctx = calloc(1, sizeof(*ctx));
879 if (ctx == NULL)
880 return krb5_enomem(context);
882 ret = make_database(context, s);
883 if (ret) {
884 free(ctx);
885 return ret;
888 if (s->cid == SCACHE_INVALID_CID) {
889 krb5_set_error_message(context, KRB5_CC_END,
890 N_("Iterating a invalid scache %s", ""),
891 s->name);
892 free(ctx);
893 return KRB5_CC_END;
896 ret = asprintf(&name, "credIteration%pPid%d",
897 ctx, (int)getpid());
898 if (ret < 0 || name == NULL) {
899 free(ctx);
900 return krb5_enomem(context);
903 ret = asprintf(&ctx->drop, "DROP TABLE %s", name);
904 if (ret < 0 || ctx->drop == NULL) {
905 free(name);
906 free(ctx);
907 return krb5_enomem(context);
910 ret = asprintf(&str, "CREATE TEMPORARY TABLE %s "
911 "AS SELECT oid,created_at FROM credentials WHERE cid = %lu",
912 name, (unsigned long)s->cid);
913 if (ret < 0 || str == NULL) {
914 free(ctx->drop);
915 free(name);
916 free(ctx);
917 return krb5_enomem(context);
920 ret = exec_stmt(context, s->db, str, KRB5_CC_IO);
921 free(str);
922 str = NULL;
923 if (ret) {
924 free(ctx->drop);
925 free(name);
926 free(ctx);
927 return ret;
930 ret = asprintf(&str, "SELECT oid FROM %s ORDER BY created_at", name);
931 if (ret < 0 || str == NULL) {
932 exec_stmt(context, s->db, ctx->drop, 0);
933 free(ctx->drop);
934 free(name);
935 free(ctx);
936 return ret;
939 ret = prepare_stmt(context, s->db, &ctx->stmt, str);
940 free(str);
941 str = NULL;
942 free(name);
943 if (ret) {
944 exec_stmt(context, s->db, ctx->drop, 0);
945 free(ctx->drop);
946 free(ctx);
947 return ret;
950 ret = prepare_stmt(context, s->db, &ctx->credstmt,
951 "SELECT cred FROM credentials WHERE oid = ?");
952 if (ret) {
953 sqlite3_finalize(ctx->stmt);
954 exec_stmt(context, s->db, ctx->drop, 0);
955 free(ctx->drop);
956 free(ctx);
957 return ret;
960 *cursor = ctx;
962 return 0;
965 static krb5_error_code KRB5_CALLCONV
966 scc_get_next (krb5_context context,
967 krb5_ccache id,
968 krb5_cc_cursor *cursor,
969 krb5_creds *creds)
971 struct cred_ctx *ctx = *cursor;
972 krb5_scache *s = SCACHE(id);
973 krb5_error_code ret;
974 sqlite_uint64 oid;
975 const void *data = NULL;
976 size_t len = 0;
978 next:
979 ret = sqlite3_step(ctx->stmt);
980 if (ret == SQLITE_DONE) {
981 krb5_clear_error_message(context);
982 return KRB5_CC_END;
983 } else if (ret != SQLITE_ROW) {
984 krb5_set_error_message(context, KRB5_CC_IO,
985 N_("scache Database failed: %s", ""),
986 sqlite3_errmsg(s->db));
987 return KRB5_CC_IO;
990 oid = sqlite3_column_int64(ctx->stmt, 0);
992 /* read cred from credentials table */
994 sqlite3_bind_int(ctx->credstmt, 1, oid);
996 ret = sqlite3_step(ctx->credstmt);
997 if (ret != SQLITE_ROW) {
998 sqlite3_reset(ctx->credstmt);
999 goto next;
1002 if (sqlite3_column_type(ctx->credstmt, 0) != SQLITE_BLOB) {
1003 krb5_set_error_message(context, KRB5_CC_END,
1004 N_("credential of wrong type for SCC:%s:%s", ""),
1005 s->name, s->file);
1006 sqlite3_reset(ctx->credstmt);
1007 return KRB5_CC_END;
1010 data = sqlite3_column_blob(ctx->credstmt, 0);
1011 len = sqlite3_column_bytes(ctx->credstmt, 0);
1013 ret = decode_creds(context, data, len, creds);
1014 sqlite3_reset(ctx->credstmt);
1015 return ret;
1018 static krb5_error_code KRB5_CALLCONV
1019 scc_end_get (krb5_context context,
1020 krb5_ccache id,
1021 krb5_cc_cursor *cursor)
1023 struct cred_ctx *ctx = *cursor;
1024 krb5_scache *s = SCACHE(id);
1026 sqlite3_finalize(ctx->stmt);
1027 sqlite3_finalize(ctx->credstmt);
1029 exec_stmt(context, s->db, ctx->drop, 0);
1031 free(ctx->drop);
1032 free(ctx);
1034 return 0;
1037 static krb5_error_code KRB5_CALLCONV
1038 scc_remove_cred(krb5_context context,
1039 krb5_ccache id,
1040 krb5_flags which,
1041 krb5_creds *mcreds)
1043 krb5_scache *s = SCACHE(id);
1044 krb5_error_code ret;
1045 sqlite3_stmt *stmt;
1046 sqlite_uint64 credid = 0;
1047 const void *data = NULL;
1048 size_t len = 0;
1050 ret = make_database(context, s);
1051 if (ret)
1052 return ret;
1054 ret = prepare_stmt(context, s->db, &stmt,
1055 "SELECT cred,oid FROM credentials "
1056 "WHERE cid = ?");
1057 if (ret)
1058 return ret;
1060 sqlite3_bind_int(stmt, 1, s->cid);
1062 /* find credential... */
1063 while (1) {
1064 krb5_creds creds;
1066 ret = sqlite3_step(stmt);
1067 if (ret == SQLITE_DONE) {
1068 ret = 0;
1069 break;
1070 } else if (ret != SQLITE_ROW) {
1071 ret = KRB5_CC_IO;
1072 krb5_set_error_message(context, ret,
1073 N_("scache Database failed: %s", ""),
1074 sqlite3_errmsg(s->db));
1075 break;
1078 if (sqlite3_column_type(stmt, 0) != SQLITE_BLOB) {
1079 ret = KRB5_CC_END;
1080 krb5_set_error_message(context, ret,
1081 N_("Credential of wrong type "
1082 "for SCC:%s:%s", ""),
1083 s->name, s->file);
1084 break;
1087 data = sqlite3_column_blob(stmt, 0);
1088 len = sqlite3_column_bytes(stmt, 0);
1090 ret = decode_creds(context, data, len, &creds);
1091 if (ret)
1092 break;
1094 ret = krb5_compare_creds(context, which, mcreds, &creds);
1095 krb5_free_cred_contents(context, &creds);
1096 if (ret) {
1097 credid = sqlite3_column_int64(stmt, 1);
1098 ret = 0;
1099 break;
1103 sqlite3_finalize(stmt);
1105 if (id) {
1106 ret = prepare_stmt(context, s->db, &stmt,
1107 "DELETE FROM credentials WHERE oid=?");
1108 if (ret)
1109 return ret;
1110 sqlite3_bind_int(stmt, 1, credid);
1112 do {
1113 ret = sqlite3_step(stmt);
1114 } while (ret == SQLITE_ROW);
1115 sqlite3_finalize(stmt);
1116 if (ret != SQLITE_DONE) {
1117 ret = KRB5_CC_IO;
1118 krb5_set_error_message(context, ret,
1119 N_("failed to delete scache credental", ""));
1120 } else
1121 ret = 0;
1124 return ret;
1127 static krb5_error_code KRB5_CALLCONV
1128 scc_set_flags(krb5_context context,
1129 krb5_ccache id,
1130 krb5_flags flags)
1132 return 0; /* XXX */
1135 struct cache_iter {
1136 char *drop;
1137 sqlite3 *db;
1138 sqlite3_stmt *stmt;
1141 static krb5_error_code KRB5_CALLCONV
1142 scc_get_cache_first(krb5_context context, krb5_cc_cursor *cursor)
1144 struct cache_iter *ctx;
1145 krb5_error_code ret;
1146 char *name = NULL, *str = NULL;
1148 *cursor = NULL;
1150 ctx = calloc(1, sizeof(*ctx));
1151 if (ctx == NULL)
1152 return krb5_enomem(context);
1154 ret = default_db(context, &ctx->db);
1155 if (ctx->db == NULL) {
1156 free(ctx);
1157 return ret;
1160 ret = asprintf(&name, "cacheIteration%pPid%d",
1161 ctx, (int)getpid());
1162 if (ret < 0 || name == NULL) {
1163 sqlite3_close(ctx->db);
1164 free(ctx);
1165 return krb5_enomem(context);
1168 ret = asprintf(&ctx->drop, "DROP TABLE %s", name);
1169 if (ret < 0 || ctx->drop == NULL) {
1170 sqlite3_close(ctx->db);
1171 free(name);
1172 free(ctx);
1173 return krb5_enomem(context);
1176 ret = asprintf(&str, "CREATE TEMPORARY TABLE %s AS SELECT name FROM caches",
1177 name);
1178 if (ret < 0 || str == NULL) {
1179 sqlite3_close(ctx->db);
1180 free(name);
1181 free(ctx->drop);
1182 free(ctx);
1183 return krb5_enomem(context);
1186 ret = exec_stmt(context, ctx->db, str, KRB5_CC_IO);
1187 free(str);
1188 str = NULL;
1189 if (ret) {
1190 sqlite3_close(ctx->db);
1191 free(name);
1192 free(ctx->drop);
1193 free(ctx);
1194 return ret;
1197 ret = asprintf(&str, "SELECT name FROM %s", 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);
1206 free(name);
1208 ret = prepare_stmt(context, ctx->db, &ctx->stmt, str);
1209 free(str);
1210 if (ret) {
1211 exec_stmt(context, ctx->db, ctx->drop, 0);
1212 sqlite3_close(ctx->db);
1213 free(ctx->drop);
1214 free(ctx);
1215 return ret;
1218 *cursor = ctx;
1220 return 0;
1223 static krb5_error_code KRB5_CALLCONV
1224 scc_get_cache_next(krb5_context context,
1225 krb5_cc_cursor cursor,
1226 krb5_ccache *id)
1228 struct cache_iter *ctx = cursor;
1229 krb5_error_code ret;
1230 const char *name;
1232 again:
1233 ret = sqlite3_step(ctx->stmt);
1234 if (ret == SQLITE_DONE) {
1235 krb5_clear_error_message(context);
1236 return KRB5_CC_END;
1237 } else if (ret != SQLITE_ROW) {
1238 krb5_set_error_message(context, KRB5_CC_IO,
1239 N_("Database failed: %s", ""),
1240 sqlite3_errmsg(ctx->db));
1241 return KRB5_CC_IO;
1244 if (sqlite3_column_type(ctx->stmt, 0) != SQLITE_TEXT)
1245 goto again;
1247 name = (const char *)sqlite3_column_text(ctx->stmt, 0);
1248 if (name == NULL)
1249 goto again;
1251 ret = _krb5_cc_allocate(context, &krb5_scc_ops, id);
1252 if (ret)
1253 return ret;
1255 return scc_resolve(context, id, name);
1258 static krb5_error_code KRB5_CALLCONV
1259 scc_end_cache_get(krb5_context context, krb5_cc_cursor cursor)
1261 struct cache_iter *ctx = cursor;
1263 exec_stmt(context, ctx->db, ctx->drop, 0);
1264 sqlite3_finalize(ctx->stmt);
1265 sqlite3_close(ctx->db);
1266 free(ctx->drop);
1267 free(ctx);
1268 return 0;
1271 static krb5_error_code KRB5_CALLCONV
1272 scc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
1274 krb5_scache *sfrom = SCACHE(from);
1275 krb5_scache *sto = SCACHE(to);
1276 krb5_error_code ret;
1278 if (strcmp(sfrom->file, sto->file) != 0) {
1279 krb5_set_error_message(context, KRB5_CC_BADNAME,
1280 N_("Can't handle cross database "
1281 "credential move: %s -> %s", ""),
1282 sfrom->file, sto->file);
1283 return KRB5_CC_BADNAME;
1286 ret = make_database(context, sfrom);
1287 if (ret)
1288 return ret;
1290 ret = exec_stmt(context, sfrom->db,
1291 "BEGIN IMMEDIATE TRANSACTION", KRB5_CC_IO);
1292 if (ret) return ret;
1294 if (sto->cid != SCACHE_INVALID_CID) {
1295 /* drop old cache entry */
1297 sqlite3_bind_int(sfrom->dcache, 1, sto->cid);
1298 do {
1299 ret = sqlite3_step(sfrom->dcache);
1300 } while (ret == SQLITE_ROW);
1301 sqlite3_reset(sfrom->dcache);
1302 if (ret != SQLITE_DONE) {
1303 krb5_set_error_message(context, KRB5_CC_IO,
1304 N_("Failed to delete old cache: %d", ""),
1305 (int)ret);
1306 goto rollback;
1310 sqlite3_bind_text(sfrom->ucachen, 1, sto->name, -1, NULL);
1311 sqlite3_bind_int(sfrom->ucachen, 2, sfrom->cid);
1313 do {
1314 ret = sqlite3_step(sfrom->ucachen);
1315 } while (ret == SQLITE_ROW);
1316 sqlite3_reset(sfrom->ucachen);
1317 if (ret != SQLITE_DONE) {
1318 krb5_set_error_message(context, KRB5_CC_IO,
1319 N_("Failed to update new cache: %d", ""),
1320 (int)ret);
1321 goto rollback;
1324 sto->cid = sfrom->cid;
1326 ret = exec_stmt(context, sfrom->db, "COMMIT", KRB5_CC_IO);
1327 if (ret) return ret;
1329 scc_free(sfrom);
1331 return 0;
1333 rollback:
1334 exec_stmt(context, sfrom->db, "ROLLBACK", 0);
1335 scc_free(sfrom);
1337 return KRB5_CC_IO;
1340 static krb5_error_code KRB5_CALLCONV
1341 scc_get_default_name(krb5_context context, char **str)
1343 krb5_error_code ret;
1344 char *name;
1346 *str = NULL;
1348 ret = get_def_name(context, &name);
1349 if (ret)
1350 return _krb5_expand_default_cc_name(context, KRB5_SCACHE_NAME, str);
1352 ret = asprintf(str, "SCC:%s", name);
1353 free(name);
1354 if (ret < 0 || *str == NULL)
1355 return krb5_enomem(context);
1356 return 0;
1359 static krb5_error_code KRB5_CALLCONV
1360 scc_set_default(krb5_context context, krb5_ccache id)
1362 krb5_scache *s = SCACHE(id);
1363 krb5_error_code ret;
1365 if (s->cid == SCACHE_INVALID_CID) {
1366 krb5_set_error_message(context, KRB5_CC_IO,
1367 N_("Trying to set a invalid cache "
1368 "as default %s", ""),
1369 s->name);
1370 return KRB5_CC_IO;
1373 ret = sqlite3_bind_text(s->umaster, 1, s->name, -1, NULL);
1374 if (ret) {
1375 sqlite3_reset(s->umaster);
1376 krb5_set_error_message(context, KRB5_CC_IO,
1377 N_("Failed to set name of default cache", ""));
1378 return KRB5_CC_IO;
1381 do {
1382 ret = sqlite3_step(s->umaster);
1383 } while (ret == SQLITE_ROW);
1384 sqlite3_reset(s->umaster);
1385 if (ret != SQLITE_DONE) {
1386 krb5_set_error_message(context, KRB5_CC_IO,
1387 N_("Failed to update default cache", ""));
1388 return KRB5_CC_IO;
1391 return 0;
1395 * Variable containing the SCC based credential cache implemention.
1397 * @ingroup krb5_ccache
1400 KRB5_LIB_VARIABLE const krb5_cc_ops krb5_scc_ops = {
1401 KRB5_CC_OPS_VERSION,
1402 "SCC",
1403 scc_get_name,
1404 scc_resolve,
1405 scc_gen_new,
1406 scc_initialize,
1407 scc_destroy,
1408 scc_close,
1409 scc_store_cred,
1410 NULL, /* scc_retrieve */
1411 scc_get_principal,
1412 scc_get_first,
1413 scc_get_next,
1414 scc_end_get,
1415 scc_remove_cred,
1416 scc_set_flags,
1417 NULL,
1418 scc_get_cache_first,
1419 scc_get_cache_next,
1420 scc_end_cache_get,
1421 scc_move,
1422 scc_get_default_name,
1423 scc_set_default,
1424 NULL,
1425 NULL,
1426 NULL
1429 #endif