catch error from as.*printf
[heimdal.git] / lib / krb5 / scache.c
blobb7ccdcc7d44e888ed39accab5923f06ad0ec1ff0
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 *
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 krb5_error_code ret;
313 ret = get_def_name(context, &s->name);
314 if (ret)
315 s->name = strdup(SCACHE_DEF_NAME);
316 } else
317 s->name = strdup(name);
319 file = strrchr(s->name, ':');
320 if (file) {
321 *file++ = '\0';
322 s->file = strdup(file);
323 ret = 0;
324 } else {
325 ret = _krb5_expand_default_cc_name(context, KRB5_SCACHE_DB, &s->file);
327 } else {
328 _krb5_expand_default_cc_name(context, KRB5_SCACHE_DB, &s->file);
329 ret = asprintf(&s->name, "unique-%p", s);
331 if (ret < 0 || s->file == NULL || s->name == NULL) {
332 scc_free(s);
333 return NULL;
336 return s;
339 static krb5_error_code
340 open_database(krb5_context context, krb5_scache *s, int flags)
342 int ret;
344 ret = sqlite3_open_v2(s->file, &s->db, SQLITE_OPEN_READWRITE|flags, NULL);
345 if (ret) {
346 if (s->db) {
347 krb5_set_error_message(context, ENOENT,
348 N_("Error opening scache file %s: %s", ""),
349 s->file, sqlite3_errmsg(s->db));
350 sqlite3_close(s->db);
351 s->db = NULL;
352 } else
353 krb5_set_error_message(context, ENOENT,
354 N_("malloc: out of memory", ""));
355 return ENOENT;
357 return 0;
360 static krb5_error_code
361 create_cache(krb5_context context, krb5_scache *s)
363 int ret;
365 sqlite3_bind_text(s->icache, 1, s->name, -1, NULL);
366 do {
367 ret = sqlite3_step(s->icache);
368 } while (ret == SQLITE_ROW);
369 if (ret != SQLITE_DONE) {
370 krb5_set_error_message(context, KRB5_CC_IO,
371 N_("Failed to add scache: %d", ""), ret);
372 return KRB5_CC_IO;
374 sqlite3_reset(s->icache);
376 s->cid = sqlite3_last_insert_rowid(s->db);
378 return 0;
381 static krb5_error_code
382 make_database(krb5_context context, krb5_scache *s)
384 int created_file = 0;
385 int ret;
387 if (s->db)
388 return 0;
390 ret = open_database(context, s, 0);
391 if (ret) {
392 mode_t oldumask = umask(077);
393 ret = open_database(context, s, SQLITE_OPEN_CREATE);
394 umask(oldumask);
395 if (ret) goto out;
397 created_file = 1;
399 ret = exec_stmt(context, s->db, SQL_CMASTER, KRB5_CC_IO);
400 if (ret) goto out;
401 ret = exec_stmt(context, s->db, SQL_CCACHE, KRB5_CC_IO);
402 if (ret) goto out;
403 ret = exec_stmt(context, s->db, SQL_CCREDS, KRB5_CC_IO);
404 if (ret) goto out;
405 ret = exec_stmt(context, s->db, SQL_CPRINCIPALS, KRB5_CC_IO);
406 if (ret) goto out;
407 ret = exec_stmt(context, s->db, SQL_SETUP_MASTER, KRB5_CC_IO);
408 if (ret) goto out;
410 ret = exec_stmt(context, s->db, SQL_TCACHE, KRB5_CC_IO);
411 if (ret) goto out;
412 ret = exec_stmt(context, s->db, SQL_TCRED, KRB5_CC_IO);
413 if (ret) goto out;
416 #ifdef TRACEME
417 sqlite3_trace(s->db, trace, NULL);
418 #endif
420 ret = prepare_stmt(context, s->db, &s->icred, SQL_ICRED);
421 if (ret) goto out;
422 ret = prepare_stmt(context, s->db, &s->dcred, SQL_DCRED);
423 if (ret) goto out;
424 ret = prepare_stmt(context, s->db, &s->iprincipal, SQL_IPRINCIPAL);
425 if (ret) goto out;
426 ret = prepare_stmt(context, s->db, &s->icache, SQL_ICACHE);
427 if (ret) goto out;
428 ret = prepare_stmt(context, s->db, &s->ucachen, SQL_UCACHE_NAME);
429 if (ret) goto out;
430 ret = prepare_stmt(context, s->db, &s->ucachep, SQL_UCACHE_PRINCIPAL);
431 if (ret) goto out;
432 ret = prepare_stmt(context, s->db, &s->dcache, SQL_DCACHE);
433 if (ret) goto out;
434 ret = prepare_stmt(context, s->db, &s->scache, SQL_SCACHE);
435 if (ret) goto out;
436 ret = prepare_stmt(context, s->db, &s->scache_name, SQL_SCACHE_NAME);
437 if (ret) goto out;
438 ret = prepare_stmt(context, s->db, &s->umaster, SQL_UMASTER);
439 if (ret) goto out;
441 return 0;
443 out:
444 if (s->db)
445 sqlite3_close(s->db);
446 if (created_file)
447 unlink(s->file);
449 return ret;
452 static krb5_error_code
453 bind_principal(krb5_context context,
454 sqlite3 *db,
455 sqlite3_stmt *stmt,
456 int col,
457 krb5_const_principal principal)
459 krb5_error_code ret;
460 char *str;
462 ret = krb5_unparse_name(context, principal, &str);
463 if (ret)
464 return ret;
466 ret = sqlite3_bind_text(stmt, col, str, -1, free_krb5);
467 if (ret != SQLITE_OK) {
468 krb5_xfree(str);
469 krb5_set_error_message(context, ENOMEM,
470 N_("scache bind principal: %s", ""),
471 sqlite3_errmsg(db));
472 return ENOMEM;
474 return 0;
481 static const char*
482 scc_get_name(krb5_context context,
483 krb5_ccache id)
485 return SCACHE(id)->name;
488 static krb5_error_code
489 scc_resolve(krb5_context context, krb5_ccache *id, const char *res)
491 krb5_scache *s;
492 int ret;
494 s = scc_alloc(context, res);
495 if (s == NULL) {
496 krb5_set_error_message(context, KRB5_CC_NOMEM,
497 N_("malloc: out of memory", ""));
498 return KRB5_CC_NOMEM;
501 ret = make_database(context, s);
502 if (ret) {
503 scc_free(s);
504 return ret;
507 ret = sqlite3_bind_text(s->scache_name, 1, s->name, -1, NULL);
508 if (ret != SQLITE_OK) {
509 krb5_set_error_message(context, ENOMEM,
510 "bind name: %s", sqlite3_errmsg(s->db));
511 scc_free(s);
512 return ENOMEM;
515 if (sqlite3_step(s->scache_name) == SQLITE_ROW) {
517 if (sqlite3_column_type(s->scache_name, 0) != SQLITE_INTEGER) {
518 sqlite3_reset(s->scache_name);
519 krb5_set_error_message(context, KRB5_CC_END,
520 N_("Cache name of wrong type "
521 "for scache %ld", ""),
522 (unsigned long)s->name);
523 scc_free(s);
524 return KRB5_CC_END;
527 s->cid = sqlite3_column_int(s->scache_name, 0);
528 } else {
529 s->cid = SCACHE_INVALID_CID;
531 sqlite3_reset(s->scache_name);
533 (*id)->data.data = s;
534 (*id)->data.length = sizeof(*s);
536 return 0;
539 static krb5_error_code
540 scc_gen_new(krb5_context context, krb5_ccache *id)
542 krb5_scache *s;
544 s = scc_alloc(context, NULL);
546 if (s == NULL) {
547 krb5_set_error_message(context, KRB5_CC_NOMEM,
548 N_("malloc: out of memory", ""));
549 return KRB5_CC_NOMEM;
552 (*id)->data.data = s;
553 (*id)->data.length = sizeof(*s);
555 return 0;
558 static krb5_error_code
559 scc_initialize(krb5_context context,
560 krb5_ccache id,
561 krb5_principal primary_principal)
563 krb5_scache *s = SCACHE(id);
564 krb5_error_code ret;
566 ret = make_database(context, s);
567 if (ret)
568 return ret;
570 ret = exec_stmt(context, s->db, "BEGIN IMMEDIATE TRANSACTION", KRB5_CC_IO);
571 if (ret) return ret;
573 if (s->cid == SCACHE_INVALID_CID) {
574 ret = create_cache(context, s);
575 if (ret)
576 goto rollback;
577 } else {
578 sqlite3_bind_int(s->dcred, 1, s->cid);
579 do {
580 ret = sqlite3_step(s->dcred);
581 } while (ret == SQLITE_ROW);
582 sqlite3_reset(s->dcred);
583 if (ret != SQLITE_DONE) {
584 ret = KRB5_CC_IO;
585 krb5_set_error_message(context, ret,
586 N_("Failed to delete old "
587 "credentials: %s", ""),
588 sqlite3_errmsg(s->db));
589 goto rollback;
593 ret = bind_principal(context, s->db, s->ucachep, 1, primary_principal);
594 if (ret)
595 goto rollback;
596 sqlite3_bind_int(s->ucachep, 2, s->cid);
598 do {
599 ret = sqlite3_step(s->ucachep);
600 } while (ret == SQLITE_ROW);
601 sqlite3_reset(s->ucachep);
602 if (ret != SQLITE_DONE) {
603 ret = KRB5_CC_IO;
604 krb5_set_error_message(context, ret,
605 N_("Failed to bind principal to cache %s", ""),
606 sqlite3_errmsg(s->db));
607 goto rollback;
610 ret = exec_stmt(context, s->db, "COMMIT", KRB5_CC_IO);
611 if (ret) return ret;
613 return 0;
615 rollback:
616 exec_stmt(context, s->db, "ROLLBACK", 0);
618 return ret;
622 static krb5_error_code
623 scc_close(krb5_context context,
624 krb5_ccache id)
626 scc_free(SCACHE(id));
627 return 0;
630 static krb5_error_code
631 scc_destroy(krb5_context context,
632 krb5_ccache id)
634 krb5_scache *s = SCACHE(id);
635 int ret;
637 if (s->cid == SCACHE_INVALID_CID)
638 return 0;
640 sqlite3_bind_int(s->dcache, 1, s->cid);
641 do {
642 ret = sqlite3_step(s->dcache);
643 } while (ret == SQLITE_ROW);
644 sqlite3_reset(s->dcache);
645 if (ret != SQLITE_DONE) {
646 krb5_set_error_message(context, KRB5_CC_IO,
647 N_("Failed to destroy cache %s: %s", ""),
648 s->name, sqlite3_errmsg(s->db));
649 return KRB5_CC_IO;
651 return 0;
654 static krb5_error_code
655 encode_creds(krb5_context context, krb5_creds *creds, krb5_data *data)
657 krb5_error_code ret;
658 krb5_storage *sp;
660 sp = krb5_storage_emem();
661 if (sp == NULL) {
662 krb5_set_error_message(context, ENOMEM,
663 N_("malloc: out of memory", ""));
664 return ENOMEM;
667 ret = krb5_store_creds(sp, creds);
668 if (ret) {
669 krb5_set_error_message(context, ret,
670 N_("Failed to store credential in scache", ""));
671 krb5_storage_free(sp);
672 return ret;
675 ret = krb5_storage_to_data(sp, data);
676 krb5_storage_free(sp);
677 if (ret)
678 krb5_set_error_message(context, ret,
679 N_("Failed to encode credential in scache", ""));
680 return ret;
683 static krb5_error_code
684 decode_creds(krb5_context context, const void *data, size_t length,
685 krb5_creds *creds)
687 krb5_error_code ret;
688 krb5_storage *sp;
690 sp = krb5_storage_from_readonly_mem(data, length);
691 if (sp == NULL) {
692 krb5_set_error_message(context, ENOMEM,
693 N_("malloc: out of memory", ""));
694 return ENOMEM;
697 ret = krb5_ret_creds(sp, creds);
698 krb5_storage_free(sp);
699 if (ret) {
700 krb5_set_error_message(context, ret,
701 N_("Failed to read credential in scache", ""));
702 return ret;
704 return 0;
708 static krb5_error_code
709 scc_store_cred(krb5_context context,
710 krb5_ccache id,
711 krb5_creds *creds)
713 sqlite_uint64 credid;
714 krb5_scache *s = SCACHE(id);
715 krb5_error_code ret;
716 krb5_data data;
718 ret = make_database(context, s);
719 if (ret)
720 return ret;
722 ret = encode_creds(context, creds, &data);
723 if (ret)
724 return ret;
726 sqlite3_bind_int(s->icred, 1, s->cid);
728 krb5_enctype etype = 0;
729 int kvno = 0;
730 Ticket t;
731 size_t len;
733 ret = decode_Ticket(creds->ticket.data,
734 creds->ticket.length, &t, &len);
735 if (ret == 0) {
736 if(t.enc_part.kvno)
737 kvno = *t.enc_part.kvno;
739 etype = t.enc_part.etype;
741 free_Ticket(&t);
744 sqlite3_bind_int(s->icred, 2, kvno);
745 sqlite3_bind_int(s->icred, 3, etype);
749 sqlite3_bind_blob(s->icred, 4, data.data, data.length, free_data);
750 sqlite3_bind_int(s->icred, 5, time(NULL));
752 ret = exec_stmt(context, s->db, "BEGIN IMMEDIATE TRANSACTION", KRB5_CC_IO);
753 if (ret) return ret;
755 do {
756 ret = sqlite3_step(s->icred);
757 } while (ret == SQLITE_ROW);
758 sqlite3_reset(s->icred);
759 if (ret != SQLITE_DONE) {
760 ret = KRB5_CC_IO;
761 krb5_set_error_message(context, ret,
762 N_("Failed to add credential: %s", ""),
763 sqlite3_errmsg(s->db));
764 goto rollback;
767 credid = sqlite3_last_insert_rowid(s->db);
770 bind_principal(context, s->db, s->iprincipal, 1, creds->server);
771 sqlite3_bind_int(s->iprincipal, 2, 1);
772 sqlite3_bind_int(s->iprincipal, 3, credid);
774 do {
775 ret = sqlite3_step(s->iprincipal);
776 } while (ret == SQLITE_ROW);
777 sqlite3_reset(s->iprincipal);
778 if (ret != SQLITE_DONE) {
779 ret = KRB5_CC_IO;
780 krb5_set_error_message(context, ret,
781 N_("Failed to add principal: %s", ""),
782 sqlite3_errmsg(s->db));
783 goto rollback;
788 bind_principal(context, s->db, s->iprincipal, 1, creds->client);
789 sqlite3_bind_int(s->iprincipal, 2, 0);
790 sqlite3_bind_int(s->iprincipal, 3, credid);
792 do {
793 ret = sqlite3_step(s->iprincipal);
794 } while (ret == SQLITE_ROW);
795 sqlite3_reset(s->iprincipal);
796 if (ret != SQLITE_DONE) {
797 ret = KRB5_CC_IO;
798 krb5_set_error_message(context, ret,
799 N_("Failed to add principal: %s", ""),
800 sqlite3_errmsg(s->db));
801 goto rollback;
805 ret = exec_stmt(context, s->db, "COMMIT", KRB5_CC_IO);
806 if (ret) return ret;
808 return 0;
810 rollback:
811 exec_stmt(context, s->db, "ROLLBACK", 0);
813 return ret;
816 static krb5_error_code
817 scc_get_principal(krb5_context context,
818 krb5_ccache id,
819 krb5_principal *principal)
821 krb5_scache *s = SCACHE(id);
822 krb5_error_code ret;
823 const char *str;
825 *principal = NULL;
827 ret = make_database(context, s);
828 if (ret)
829 return ret;
831 sqlite3_bind_int(s->scache, 1, s->cid);
833 if (sqlite3_step(s->scache) != SQLITE_ROW) {
834 sqlite3_reset(s->scache);
835 krb5_set_error_message(context, KRB5_CC_END,
836 N_("No principal for cache SCC:%s:%s", ""),
837 s->name, s->file);
838 return KRB5_CC_END;
841 if (sqlite3_column_type(s->scache, 0) != SQLITE_TEXT) {
842 sqlite3_reset(s->scache);
843 krb5_set_error_message(context, KRB5_CC_END,
844 N_("Principal data of wrong type "
845 "for SCC:%s:%s", ""),
846 s->name, s->file);
847 return KRB5_CC_END;
850 str = (const char *)sqlite3_column_text(s->scache, 0);
851 if (str == NULL) {
852 sqlite3_reset(s->scache);
853 krb5_set_error_message(context, KRB5_CC_END,
854 N_("Principal not set for SCC:%s:%s", ""),
855 s->name, s->file);
856 return KRB5_CC_END;
859 ret = krb5_parse_name(context, str, principal);
861 sqlite3_reset(s->scache);
863 return ret;
866 struct cred_ctx {
867 char *drop;
868 sqlite3_stmt *stmt;
869 sqlite3_stmt *credstmt;
872 static krb5_error_code
873 scc_get_first (krb5_context context,
874 krb5_ccache id,
875 krb5_cc_cursor *cursor)
877 krb5_scache *s = SCACHE(id);
878 krb5_error_code ret;
879 struct cred_ctx *ctx;
880 char *str, *name;
882 *cursor = NULL;
884 ctx = calloc(1, sizeof(*ctx));
885 if (ctx == NULL) {
886 krb5_set_error_message(context, ENOMEM,
887 N_("malloc: out of memory", ""));
888 return ENOMEM;
891 ret = make_database(context, s);
892 if (ret) {
893 free(ctx);
894 return ret;
897 if (s->cid == SCACHE_INVALID_CID) {
898 krb5_set_error_message(context, KRB5_CC_END,
899 N_("Iterating a invalid scache %s", ""),
900 s->name);
901 free(ctx);
902 return KRB5_CC_END;
905 asprintf(&name, "credIteration%luPid%d",
906 (unsigned long)ctx, (int)getpid());
907 if (name == NULL) {
908 krb5_set_error_message(context, ENOMEM,
909 N_("malloc: out of memory", ""));
910 free(ctx);
911 return ENOMEM;
914 asprintf(&ctx->drop, "DROP TABLE %s", name);
915 if (ctx->drop == NULL) {
916 krb5_set_error_message(context, ENOMEM,
917 N_("malloc: out of memory", ""));
918 free(name);
919 free(ctx);
920 return ENOMEM;
923 asprintf(&str, "CREATE TEMPORARY TABLE %s "
924 "AS SELECT oid,created_at FROM credentials WHERE cid = %lu",
925 name, (unsigned long)s->cid);
927 ret = exec_stmt(context, s->db, str, KRB5_CC_IO);
928 free(str);
929 if (ret) {
930 free(ctx->drop);
931 free(name);
932 free(ctx);
933 return ret;
936 asprintf(&str, "SELECT oid FROM %s ORDER BY created_at", name);
937 if (str == NULL) {
938 exec_stmt(context, s->db, ctx->drop, 0);
939 free(ctx->drop);
940 free(name);
941 free(ctx);
942 return ret;
945 ret = prepare_stmt(context, s->db, &ctx->stmt, str);
946 free(str);
947 free(name);
948 if (ret) {
949 exec_stmt(context, s->db, ctx->drop, 0);
950 free(ctx->drop);
951 free(ctx);
952 return ret;
955 ret = prepare_stmt(context, s->db, &ctx->credstmt,
956 "SELECT cred FROM credentials WHERE oid = ?");
957 if (ret) {
958 sqlite3_finalize(ctx->stmt);
959 exec_stmt(context, s->db, ctx->drop, 0);
960 free(ctx->drop);
961 free(ctx);
962 return ret;
965 *cursor = ctx;
967 return 0;
970 static krb5_error_code
971 scc_get_next (krb5_context context,
972 krb5_ccache id,
973 krb5_cc_cursor *cursor,
974 krb5_creds *creds)
976 struct cred_ctx *ctx = *cursor;
977 krb5_scache *s = SCACHE(id);
978 krb5_error_code ret;
979 sqlite_uint64 oid;
980 const void *data = NULL;
981 size_t len = 0;
983 next:
984 ret = sqlite3_step(ctx->stmt);
985 if (ret == SQLITE_DONE) {
986 krb5_clear_error_message(context);
987 return KRB5_CC_END;
988 } else if (ret != SQLITE_ROW) {
989 krb5_set_error_message(context, KRB5_CC_IO,
990 N_("scache Database failed: %s", ""),
991 sqlite3_errmsg(s->db));
992 return KRB5_CC_IO;
995 oid = sqlite3_column_int64(ctx->stmt, 0);
997 /* read cred from credentials table */
999 sqlite3_bind_int(ctx->credstmt, 1, oid);
1001 ret = sqlite3_step(ctx->credstmt);
1002 if (ret != SQLITE_ROW) {
1003 sqlite3_reset(ctx->credstmt);
1004 goto next;
1007 if (sqlite3_column_type(ctx->credstmt, 0) != SQLITE_BLOB) {
1008 krb5_set_error_message(context, KRB5_CC_END,
1009 N_("credential of wrong type for SCC:%s:%s", ""),
1010 s->name, s->file);
1011 sqlite3_reset(ctx->credstmt);
1012 return KRB5_CC_END;
1015 data = sqlite3_column_blob(ctx->credstmt, 0);
1016 len = sqlite3_column_bytes(ctx->credstmt, 0);
1018 ret = decode_creds(context, data, len, creds);
1019 sqlite3_reset(ctx->credstmt);
1020 return ret;
1023 static krb5_error_code
1024 scc_end_get (krb5_context context,
1025 krb5_ccache id,
1026 krb5_cc_cursor *cursor)
1028 struct cred_ctx *ctx = *cursor;
1029 krb5_scache *s = SCACHE(id);
1031 sqlite3_finalize(ctx->stmt);
1032 sqlite3_finalize(ctx->credstmt);
1034 exec_stmt(context, s->db, ctx->drop, 0);
1036 free(ctx->drop);
1037 free(ctx);
1039 return 0;
1042 static krb5_error_code
1043 scc_remove_cred(krb5_context context,
1044 krb5_ccache id,
1045 krb5_flags which,
1046 krb5_creds *mcreds)
1048 krb5_scache *s = SCACHE(id);
1049 krb5_error_code ret;
1050 sqlite3_stmt *stmt;
1051 sqlite_uint64 credid = 0;
1052 const void *data = NULL;
1053 size_t len = 0;
1055 ret = make_database(context, s);
1056 if (ret)
1057 return ret;
1059 ret = prepare_stmt(context, s->db, &stmt,
1060 "SELECT cred,oid FROM credentials "
1061 "WHERE cid = ?");
1062 if (ret)
1063 return ret;
1065 sqlite3_bind_int(stmt, 1, s->cid);
1067 /* find credential... */
1068 while (1) {
1069 krb5_creds creds;
1071 ret = sqlite3_step(stmt);
1072 if (ret == SQLITE_DONE) {
1073 ret = 0;
1074 break;
1075 } else if (ret != SQLITE_ROW) {
1076 ret = KRB5_CC_IO;
1077 krb5_set_error_message(context, ret,
1078 N_("scache Database failed: %s", ""),
1079 sqlite3_errmsg(s->db));
1080 break;
1083 if (sqlite3_column_type(stmt, 0) != SQLITE_BLOB) {
1084 ret = KRB5_CC_END;
1085 krb5_set_error_message(context, ret,
1086 N_("Credential of wrong type "
1087 "for SCC:%s:%s", ""),
1088 s->name, s->file);
1089 break;
1092 data = sqlite3_column_blob(stmt, 0);
1093 len = sqlite3_column_bytes(stmt, 0);
1095 ret = decode_creds(context, data, len, &creds);
1096 if (ret)
1097 break;
1099 ret = krb5_compare_creds(context, which, mcreds, &creds);
1100 krb5_free_cred_contents(context, &creds);
1101 if (ret) {
1102 credid = sqlite3_column_int64(stmt, 1);
1103 ret = 0;
1104 break;
1108 sqlite3_finalize(stmt);
1110 if (id) {
1111 ret = prepare_stmt(context, s->db, &stmt,
1112 "DELETE FROM credentials WHERE oid=?");
1113 if (ret)
1114 return ret;
1115 sqlite3_bind_int(stmt, 1, credid);
1117 do {
1118 ret = sqlite3_step(stmt);
1119 } while (ret == SQLITE_ROW);
1120 sqlite3_finalize(stmt);
1121 if (ret != SQLITE_DONE) {
1122 ret = KRB5_CC_IO;
1123 krb5_set_error_message(context, ret,
1124 N_("failed to delete scache credental", ""));
1125 } else
1126 ret = 0;
1129 return ret;
1132 static krb5_error_code
1133 scc_set_flags(krb5_context context,
1134 krb5_ccache id,
1135 krb5_flags flags)
1137 return 0; /* XXX */
1140 struct cache_iter {
1141 char *drop;
1142 sqlite3 *db;
1143 sqlite3_stmt *stmt;
1146 static krb5_error_code
1147 scc_get_cache_first(krb5_context context, krb5_cc_cursor *cursor)
1149 struct cache_iter *ctx;
1150 krb5_error_code ret;
1151 char *name, *str;
1153 *cursor = NULL;
1155 ctx = calloc(1, sizeof(*ctx));
1156 if (ctx == NULL) {
1157 krb5_set_error_message(context, ENOMEM,
1158 N_("malloc: out of memory", ""));
1159 return ENOMEM;
1162 ret = default_db(context, &ctx->db);
1163 if (ctx->db == NULL) {
1164 free(ctx);
1165 return ret;
1168 asprintf(&name, "cacheIteration%luPid%d",
1169 (unsigned long)ctx, (int)getpid());
1170 if (name == NULL) {
1171 krb5_set_error_message(context, ENOMEM,
1172 N_("malloc: out of memory", ""));
1173 sqlite3_close(ctx->db);
1174 free(ctx);
1175 return ENOMEM;
1178 asprintf(&ctx->drop, "DROP TABLE %s", name);
1179 if (ctx->drop == NULL) {
1180 krb5_set_error_message(context, ENOMEM,
1181 N_("malloc: out of memory", ""));
1182 sqlite3_close(ctx->db);
1183 free(name);
1184 free(ctx);
1185 return ENOMEM;
1188 asprintf(&str, "CREATE TEMPORARY TABLE %s AS SELECT name FROM caches",
1189 name);
1190 if (str == NULL) {
1191 krb5_set_error_message(context, ENOMEM,
1192 N_("malloc: out of memory", ""));
1193 sqlite3_close(ctx->db);
1194 free(name);
1195 free(ctx->drop);
1196 free(ctx);
1197 return ENOMEM;
1200 ret = exec_stmt(context, ctx->db, str, KRB5_CC_IO);
1201 free(str);
1202 if (ret) {
1203 sqlite3_close(ctx->db);
1204 free(name);
1205 free(ctx->drop);
1206 free(ctx);
1207 return ret;
1210 asprintf(&str, "SELECT name FROM %s", name);
1211 free(name);
1212 if (str == NULL) {
1213 exec_stmt(context, ctx->db, ctx->drop, 0);
1214 sqlite3_close(ctx->db);
1215 free(name);
1216 free(ctx->drop);
1217 free(ctx);
1218 return ENOMEM;
1221 ret = prepare_stmt(context, ctx->db, &ctx->stmt, str);
1222 free(str);
1223 if (ret) {
1224 exec_stmt(context, ctx->db, ctx->drop, 0);
1225 sqlite3_close(ctx->db);
1226 free(ctx->drop);
1227 free(ctx);
1228 return ret;
1231 *cursor = ctx;
1233 return 0;
1236 static krb5_error_code
1237 scc_get_cache_next(krb5_context context,
1238 krb5_cc_cursor cursor,
1239 krb5_ccache *id)
1241 struct cache_iter *ctx = cursor;
1242 krb5_error_code ret;
1243 const char *name;
1245 again:
1246 ret = sqlite3_step(ctx->stmt);
1247 if (ret == SQLITE_DONE) {
1248 krb5_clear_error_message(context);
1249 return KRB5_CC_END;
1250 } else if (ret != SQLITE_ROW) {
1251 krb5_set_error_message(context, KRB5_CC_IO,
1252 N_("Database failed: %s", ""),
1253 sqlite3_errmsg(ctx->db));
1254 return KRB5_CC_IO;
1257 if (sqlite3_column_type(ctx->stmt, 0) != SQLITE_TEXT)
1258 goto again;
1260 name = (const char *)sqlite3_column_text(ctx->stmt, 0);
1261 if (name == NULL)
1262 goto again;
1264 ret = _krb5_cc_allocate(context, &krb5_scc_ops, id);
1265 if (ret)
1266 return ret;
1268 return scc_resolve(context, id, name);
1271 static krb5_error_code
1272 scc_end_cache_get(krb5_context context, krb5_cc_cursor cursor)
1274 struct cache_iter *ctx = cursor;
1276 exec_stmt(context, ctx->db, ctx->drop, 0);
1277 sqlite3_finalize(ctx->stmt);
1278 sqlite3_close(ctx->db);
1279 free(ctx->drop);
1280 free(ctx);
1281 return 0;
1284 static krb5_error_code
1285 scc_move(krb5_context context, krb5_ccache from, krb5_ccache to)
1287 krb5_scache *sfrom = SCACHE(from);
1288 krb5_scache *sto = SCACHE(to);
1289 krb5_error_code ret;
1291 if (strcmp(sfrom->file, sto->file) != 0) {
1292 krb5_set_error_message(context, KRB5_CC_BADNAME,
1293 N_("Can't handle cross database "
1294 "credential move: %s -> %s", ""),
1295 sfrom->file, sto->file);
1296 return KRB5_CC_BADNAME;
1299 ret = make_database(context, sfrom);
1300 if (ret)
1301 return ret;
1303 ret = exec_stmt(context, sfrom->db,
1304 "BEGIN IMMEDIATE TRANSACTION", KRB5_CC_IO);
1305 if (ret) return ret;
1307 if (sto->cid != SCACHE_INVALID_CID) {
1308 /* drop old cache entry */
1310 sqlite3_bind_int(sfrom->dcache, 1, sto->cid);
1311 do {
1312 ret = sqlite3_step(sfrom->dcache);
1313 } while (ret == SQLITE_ROW);
1314 sqlite3_reset(sfrom->dcache);
1315 if (ret != SQLITE_DONE) {
1316 krb5_set_error_message(context, KRB5_CC_IO,
1317 N_("Failed to delete old cache: %d", ""),
1318 (int)ret);
1319 goto rollback;
1323 sqlite3_bind_text(sfrom->ucachen, 1, sto->name, -1, NULL);
1324 sqlite3_bind_int(sfrom->ucachen, 2, sfrom->cid);
1326 do {
1327 ret = sqlite3_step(sfrom->ucachen);
1328 } while (ret == SQLITE_ROW);
1329 sqlite3_reset(sfrom->ucachen);
1330 if (ret != SQLITE_DONE) {
1331 krb5_set_error_message(context, KRB5_CC_IO,
1332 N_("Failed to update new cache: %d", ""),
1333 (int)ret);
1334 goto rollback;
1337 sto->cid = sfrom->cid;
1339 ret = exec_stmt(context, sfrom->db, "COMMIT", KRB5_CC_IO);
1340 if (ret) return ret;
1342 scc_free(sfrom);
1344 return 0;
1346 rollback:
1347 exec_stmt(context, sfrom->db, "ROLLBACK", 0);
1348 scc_free(sfrom);
1350 return KRB5_CC_IO;
1353 static krb5_error_code
1354 scc_get_default_name(krb5_context context, char **str)
1356 krb5_error_code ret;
1357 char *name;
1359 ret = get_def_name(context, &name);
1360 if (ret)
1361 return _krb5_expand_default_cc_name(context, KRB5_SCACHE_NAME, str);
1363 asprintf(str, "SCC:%s", name);
1364 free(name);
1365 if (*str == NULL) {
1366 krb5_set_error_message(context, ENOMEM,
1367 N_("malloc: out of memory", ""));
1368 return ENOMEM;
1370 return 0;
1373 static krb5_error_code
1374 scc_set_default(krb5_context context, krb5_ccache id)
1376 krb5_scache *s = SCACHE(id);
1377 krb5_error_code ret;
1379 if (s->cid == SCACHE_INVALID_CID) {
1380 krb5_set_error_message(context, KRB5_CC_IO,
1381 N_("Trying to set a invalid cache "
1382 "as default %s", ""),
1383 s->name);
1384 return KRB5_CC_IO;
1387 ret = sqlite3_bind_text(s->umaster, 1, s->name, -1, NULL);
1388 if (ret) {
1389 sqlite3_reset(s->umaster);
1390 krb5_set_error_message(context, KRB5_CC_IO,
1391 N_("Failed to set name of default cache", ""));
1392 return KRB5_CC_IO;
1395 do {
1396 ret = sqlite3_step(s->umaster);
1397 } while (ret == SQLITE_ROW);
1398 sqlite3_reset(s->umaster);
1399 if (ret != SQLITE_DONE) {
1400 krb5_set_error_message(context, KRB5_CC_IO,
1401 N_("Failed to update default cache", ""));
1402 return KRB5_CC_IO;
1405 return 0;
1409 * Variable containing the SCC based credential cache implemention.
1411 * @ingroup krb5_ccache
1414 KRB5_LIB_VARIABLE const krb5_cc_ops krb5_scc_ops = {
1415 KRB5_CC_OPS_VERSION,
1416 "SCC",
1417 scc_get_name,
1418 scc_resolve,
1419 scc_gen_new,
1420 scc_initialize,
1421 scc_destroy,
1422 scc_close,
1423 scc_store_cred,
1424 NULL, /* scc_retrieve */
1425 scc_get_principal,
1426 scc_get_first,
1427 scc_get_next,
1428 scc_end_get,
1429 scc_remove_cred,
1430 scc_set_flags,
1431 NULL,
1432 scc_get_cache_first,
1433 scc_get_cache_next,
1434 scc_end_cache_get,
1435 scc_move,
1436 scc_get_default_name,
1437 scc_set_default
1440 #endif