1 /* $NetBSD: scache.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $ */
4 * Copyright (c) 2008 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "krb5_locl.h"
42 typedef struct krb5_scache
{
51 sqlite3_stmt
*iprincipal
;
54 sqlite3_stmt
*ucachen
;
55 sqlite3_stmt
*ucachep
;
58 sqlite3_stmt
*scache_name
;
59 sqlite3_stmt
*umaster
;
63 #define SCACHE(X) ((krb5_scache *)(X)->data.data)
65 #define SCACHE_DEF_NAME "Default-cache"
66 #ifdef KRB5_USE_PATH_TOKENS
67 #define KRB5_SCACHE_DB "%{TEMP}/krb5scc_%{uid}"
69 #define KRB5_SCACHE_DB "/tmp/krb5scc_%{uid}"
71 #define KRB5_SCACHE_NAME "SCC:" SCACHE_DEF_NAME ":" KRB5_SCACHE_DB
73 #define SCACHE_INVALID_CID ((sqlite_uint64)-1)
79 #define SQL_CMASTER "" \
80 "CREATE TABLE master (" \
81 "oid INTEGER PRIMARY KEY," \
82 "version INTEGER NOT NULL," \
83 "defaultcache TEXT NOT NULL" \
86 #define SQL_SETUP_MASTER \
87 "INSERT INTO master (version,defaultcache) VALUES(2, \"" SCACHE_DEF_NAME "\")"
88 #define SQL_UMASTER "UPDATE master SET defaultcache=? WHERE version=2"
90 #define SQL_CCACHE "" \
91 "CREATE TABLE caches (" \
92 "oid INTEGER PRIMARY KEY," \
94 "name TEXT NOT NULL" \
97 #define SQL_TCACHE "" \
98 "CREATE TRIGGER CacheDropCreds AFTER DELETE ON caches " \
99 "FOR EACH ROW BEGIN " \
100 "DELETE FROM credentials WHERE cid=old.oid;" \
103 #define SQL_ICACHE "INSERT INTO caches (name) VALUES(?)"
104 #define SQL_UCACHE_NAME "UPDATE caches SET name=? WHERE OID=?"
105 #define SQL_UCACHE_PRINCIPAL "UPDATE caches SET principal=? WHERE OID=?"
106 #define SQL_DCACHE "DELETE FROM caches WHERE OID=?"
107 #define SQL_SCACHE "SELECT principal,name FROM caches WHERE OID=?"
108 #define SQL_SCACHE_NAME "SELECT oid FROM caches WHERE NAME=?"
110 #define SQL_CCREDS "" \
111 "CREATE TABLE credentials (" \
112 "oid INTEGER PRIMARY KEY," \
113 "cid INTEGER NOT NULL," \
114 "kvno INTEGER NOT NULL," \
115 "etype INTEGER NOT NULL," \
116 "created_at INTEGER NOT NULL," \
117 "cred BLOB NOT NULL" \
120 #define SQL_TCRED "" \
121 "CREATE TRIGGER credDropPrincipal AFTER DELETE ON credentials " \
122 "FOR EACH ROW BEGIN " \
123 "DELETE FROM principals WHERE credential_id=old.oid;" \
126 #define SQL_ICRED "INSERT INTO credentials (cid, kvno, etype, cred, created_at) VALUES (?,?,?,?,?)"
127 #define SQL_DCRED "DELETE FROM credentials WHERE cid=?"
129 #define SQL_CPRINCIPALS "" \
130 "CREATE TABLE principals (" \
131 "oid INTEGER PRIMARY KEY," \
132 "principal TEXT NOT NULL," \
133 "type INTEGER NOT NULL," \
134 "credential_id INTEGER NOT NULL" \
137 #define SQL_IPRINCIPAL "INSERT INTO principals (principal, type, credential_id) VALUES (?,?,?)"
144 free_data(void *data
)
156 scc_free(krb5_scache
*s
)
164 sqlite3_finalize(s
->icred
);
166 sqlite3_finalize(s
->dcred
);
168 sqlite3_finalize(s
->iprincipal
);
170 sqlite3_finalize(s
->icache
);
172 sqlite3_finalize(s
->ucachen
);
174 sqlite3_finalize(s
->ucachep
);
176 sqlite3_finalize(s
->dcache
);
178 sqlite3_finalize(s
->scache
);
180 sqlite3_finalize(s
->scache_name
);
182 sqlite3_finalize(s
->umaster
);
185 sqlite3_close(s
->db
);
191 trace(void* ptr
, const char * str
)
193 printf("SQL: %s\n", str
);
197 static krb5_error_code
198 prepare_stmt(krb5_context context
, sqlite3
*db
,
199 sqlite3_stmt
**stmt
, const char *str
)
203 ret
= sqlite3_prepare_v2(db
, str
, -1, stmt
, NULL
);
204 if (ret
!= SQLITE_OK
) {
205 krb5_set_error_message(context
, ENOENT
,
206 N_("Failed to prepare stmt %s: %s", ""),
207 str
, sqlite3_errmsg(db
));
213 static krb5_error_code
214 exec_stmt(krb5_context context
, sqlite3
*db
, const char *str
,
215 krb5_error_code code
)
219 ret
= sqlite3_exec(db
, str
, NULL
, NULL
, NULL
);
220 if (ret
!= SQLITE_OK
&& code
) {
221 krb5_set_error_message(context
, code
,
222 N_("scache execute %s: %s", ""), str
,
229 static krb5_error_code
230 default_db(krb5_context context
, sqlite3
**db
)
235 ret
= _krb5_expand_default_cc_name(context
, KRB5_SCACHE_DB
, &name
);
239 ret
= sqlite3_open_v2(name
, db
, SQLITE_OPEN_READWRITE
, NULL
);
241 if (ret
!= SQLITE_OK
) {
242 krb5_clear_error_message(context
);
247 sqlite3_trace(*db
, trace
, NULL
);
253 static krb5_error_code
254 get_def_name(krb5_context context
, char **str
)
261 ret
= default_db(context
, &db
);
265 ret
= prepare_stmt(context
, db
, &stmt
, "SELECT defaultcache FROM master");
271 ret
= sqlite3_step(stmt
);
272 if (ret
!= SQLITE_ROW
)
275 if (sqlite3_column_type(stmt
, 0) != SQLITE_TEXT
)
278 name
= (const char *)sqlite3_column_text(stmt
, 0);
286 sqlite3_finalize(stmt
);
290 sqlite3_finalize(stmt
);
292 krb5_clear_error_message(context
);
298 static krb5_scache
* KRB5_CALLCONV
299 scc_alloc(krb5_context context
, const char *name
)
308 s
->cid
= SCACHE_INVALID_CID
;
315 ret
= get_def_name(context
, &s
->name
);
317 s
->name
= strdup(SCACHE_DEF_NAME
);
319 s
->name
= strdup(name
);
321 file
= strrchr(s
->name
, ':');
324 s
->file
= strdup(file
);
327 ret
= _krb5_expand_default_cc_name(context
, KRB5_SCACHE_DB
, &s
->file
);
330 _krb5_expand_default_cc_name(context
, KRB5_SCACHE_DB
, &s
->file
);
331 ret
= asprintf(&s
->name
, "unique-%p", s
);
333 if (ret
< 0 || s
->file
== NULL
|| s
->name
== NULL
) {
341 static krb5_error_code
342 open_database(krb5_context context
, krb5_scache
*s
, int flags
)
346 ret
= sqlite3_open_v2(s
->file
, &s
->db
, SQLITE_OPEN_READWRITE
|flags
, NULL
);
349 krb5_set_error_message(context
, ENOENT
,
350 N_("Error opening scache file %s: %s", ""),
351 s
->file
, sqlite3_errmsg(s
->db
));
352 sqlite3_close(s
->db
);
355 krb5_set_error_message(context
, ENOENT
,
356 N_("malloc: out of memory", ""));
362 static krb5_error_code
363 create_cache(krb5_context context
, krb5_scache
*s
)
367 sqlite3_bind_text(s
->icache
, 1, s
->name
, -1, NULL
);
369 ret
= sqlite3_step(s
->icache
);
370 } while (ret
== SQLITE_ROW
);
371 if (ret
!= SQLITE_DONE
) {
372 krb5_set_error_message(context
, KRB5_CC_IO
,
373 N_("Failed to add scache: %d", ""), ret
);
376 sqlite3_reset(s
->icache
);
378 s
->cid
= sqlite3_last_insert_rowid(s
->db
);
383 static krb5_error_code
384 make_database(krb5_context context
, krb5_scache
*s
)
386 int created_file
= 0;
392 ret
= open_database(context
, s
, 0);
394 mode_t oldumask
= umask(077);
395 ret
= open_database(context
, s
, SQLITE_OPEN_CREATE
);
401 ret
= exec_stmt(context
, s
->db
, SQL_CMASTER
, KRB5_CC_IO
);
403 ret
= exec_stmt(context
, s
->db
, SQL_CCACHE
, KRB5_CC_IO
);
405 ret
= exec_stmt(context
, s
->db
, SQL_CCREDS
, KRB5_CC_IO
);
407 ret
= exec_stmt(context
, s
->db
, SQL_CPRINCIPALS
, KRB5_CC_IO
);
409 ret
= exec_stmt(context
, s
->db
, SQL_SETUP_MASTER
, KRB5_CC_IO
);
412 ret
= exec_stmt(context
, s
->db
, SQL_TCACHE
, KRB5_CC_IO
);
414 ret
= exec_stmt(context
, s
->db
, SQL_TCRED
, KRB5_CC_IO
);
419 sqlite3_trace(s
->db
, trace
, NULL
);
422 ret
= prepare_stmt(context
, s
->db
, &s
->icred
, SQL_ICRED
);
424 ret
= prepare_stmt(context
, s
->db
, &s
->dcred
, SQL_DCRED
);
426 ret
= prepare_stmt(context
, s
->db
, &s
->iprincipal
, SQL_IPRINCIPAL
);
428 ret
= prepare_stmt(context
, s
->db
, &s
->icache
, SQL_ICACHE
);
430 ret
= prepare_stmt(context
, s
->db
, &s
->ucachen
, SQL_UCACHE_NAME
);
432 ret
= prepare_stmt(context
, s
->db
, &s
->ucachep
, SQL_UCACHE_PRINCIPAL
);
434 ret
= prepare_stmt(context
, s
->db
, &s
->dcache
, SQL_DCACHE
);
436 ret
= prepare_stmt(context
, s
->db
, &s
->scache
, SQL_SCACHE
);
438 ret
= prepare_stmt(context
, s
->db
, &s
->scache_name
, SQL_SCACHE_NAME
);
440 ret
= prepare_stmt(context
, s
->db
, &s
->umaster
, SQL_UMASTER
);
447 sqlite3_close(s
->db
);
454 static krb5_error_code
455 bind_principal(krb5_context context
,
459 krb5_const_principal principal
)
464 ret
= krb5_unparse_name(context
, principal
, &str
);
468 ret
= sqlite3_bind_text(stmt
, col
, str
, -1, free_krb5
);
469 if (ret
!= SQLITE_OK
) {
471 krb5_set_error_message(context
, ENOMEM
,
472 N_("scache bind principal: %s", ""),
483 static const char* KRB5_CALLCONV
484 scc_get_name(krb5_context context
,
487 return SCACHE(id
)->name
;
490 static krb5_error_code KRB5_CALLCONV
491 scc_resolve(krb5_context context
, krb5_ccache
*id
, const char *res
)
496 s
= scc_alloc(context
, res
);
498 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
499 N_("malloc: out of memory", ""));
500 return KRB5_CC_NOMEM
;
503 ret
= make_database(context
, s
);
509 ret
= sqlite3_bind_text(s
->scache_name
, 1, s
->name
, -1, NULL
);
510 if (ret
!= SQLITE_OK
) {
511 krb5_set_error_message(context
, ENOMEM
,
512 "bind name: %s", sqlite3_errmsg(s
->db
));
517 if (sqlite3_step(s
->scache_name
) == SQLITE_ROW
) {
519 if (sqlite3_column_type(s
->scache_name
, 0) != SQLITE_INTEGER
) {
520 sqlite3_reset(s
->scache_name
);
521 krb5_set_error_message(context
, KRB5_CC_END
,
522 N_("Cache name of wrong type "
523 "for scache %s", ""),
529 s
->cid
= sqlite3_column_int(s
->scache_name
, 0);
531 s
->cid
= SCACHE_INVALID_CID
;
533 sqlite3_reset(s
->scache_name
);
535 (*id
)->data
.data
= s
;
536 (*id
)->data
.length
= sizeof(*s
);
541 static krb5_error_code KRB5_CALLCONV
542 scc_gen_new(krb5_context context
, krb5_ccache
*id
)
546 s
= scc_alloc(context
, NULL
);
549 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
550 N_("malloc: out of memory", ""));
551 return KRB5_CC_NOMEM
;
554 (*id
)->data
.data
= s
;
555 (*id
)->data
.length
= sizeof(*s
);
560 static krb5_error_code KRB5_CALLCONV
561 scc_initialize(krb5_context context
,
563 krb5_principal primary_principal
)
565 krb5_scache
*s
= SCACHE(id
);
568 ret
= make_database(context
, s
);
572 ret
= exec_stmt(context
, s
->db
, "BEGIN IMMEDIATE TRANSACTION", KRB5_CC_IO
);
575 if (s
->cid
== SCACHE_INVALID_CID
) {
576 ret
= create_cache(context
, s
);
580 sqlite3_bind_int(s
->dcred
, 1, s
->cid
);
582 ret
= sqlite3_step(s
->dcred
);
583 } while (ret
== SQLITE_ROW
);
584 sqlite3_reset(s
->dcred
);
585 if (ret
!= SQLITE_DONE
) {
587 krb5_set_error_message(context
, ret
,
588 N_("Failed to delete old "
589 "credentials: %s", ""),
590 sqlite3_errmsg(s
->db
));
595 ret
= bind_principal(context
, s
->db
, s
->ucachep
, 1, primary_principal
);
598 sqlite3_bind_int(s
->ucachep
, 2, s
->cid
);
601 ret
= sqlite3_step(s
->ucachep
);
602 } while (ret
== SQLITE_ROW
);
603 sqlite3_reset(s
->ucachep
);
604 if (ret
!= SQLITE_DONE
) {
606 krb5_set_error_message(context
, ret
,
607 N_("Failed to bind principal to cache %s", ""),
608 sqlite3_errmsg(s
->db
));
612 ret
= exec_stmt(context
, s
->db
, "COMMIT", KRB5_CC_IO
);
618 exec_stmt(context
, s
->db
, "ROLLBACK", 0);
624 static krb5_error_code KRB5_CALLCONV
625 scc_close(krb5_context context
,
628 scc_free(SCACHE(id
));
632 static krb5_error_code KRB5_CALLCONV
633 scc_destroy(krb5_context context
,
636 krb5_scache
*s
= SCACHE(id
);
639 if (s
->cid
== SCACHE_INVALID_CID
)
642 sqlite3_bind_int(s
->dcache
, 1, s
->cid
);
644 ret
= sqlite3_step(s
->dcache
);
645 } while (ret
== SQLITE_ROW
);
646 sqlite3_reset(s
->dcache
);
647 if (ret
!= SQLITE_DONE
) {
648 krb5_set_error_message(context
, KRB5_CC_IO
,
649 N_("Failed to destroy cache %s: %s", ""),
650 s
->name
, sqlite3_errmsg(s
->db
));
656 static krb5_error_code
657 encode_creds(krb5_context context
, krb5_creds
*creds
, krb5_data
*data
)
662 sp
= krb5_storage_emem();
664 krb5_set_error_message(context
, ENOMEM
,
665 N_("malloc: out of memory", ""));
669 ret
= krb5_store_creds(sp
, creds
);
671 krb5_set_error_message(context
, ret
,
672 N_("Failed to store credential in scache", ""));
673 krb5_storage_free(sp
);
677 ret
= krb5_storage_to_data(sp
, data
);
678 krb5_storage_free(sp
);
680 krb5_set_error_message(context
, ret
,
681 N_("Failed to encode credential in scache", ""));
685 static krb5_error_code
686 decode_creds(krb5_context context
, const void *data
, size_t length
,
692 sp
= krb5_storage_from_readonly_mem(data
, length
);
694 krb5_set_error_message(context
, ENOMEM
,
695 N_("malloc: out of memory", ""));
699 ret
= krb5_ret_creds(sp
, creds
);
700 krb5_storage_free(sp
);
702 krb5_set_error_message(context
, ret
,
703 N_("Failed to read credential in scache", ""));
710 static krb5_error_code KRB5_CALLCONV
711 scc_store_cred(krb5_context context
,
715 sqlite_uint64 credid
;
716 krb5_scache
*s
= SCACHE(id
);
720 ret
= make_database(context
, s
);
724 ret
= encode_creds(context
, creds
, &data
);
728 sqlite3_bind_int(s
->icred
, 1, s
->cid
);
730 krb5_enctype etype
= 0;
735 ret
= decode_Ticket(creds
->ticket
.data
,
736 creds
->ticket
.length
, &t
, &len
);
739 kvno
= *t
.enc_part
.kvno
;
741 etype
= t
.enc_part
.etype
;
746 sqlite3_bind_int(s
->icred
, 2, kvno
);
747 sqlite3_bind_int(s
->icred
, 3, etype
);
751 sqlite3_bind_blob(s
->icred
, 4, data
.data
, data
.length
, free_data
);
752 sqlite3_bind_int(s
->icred
, 5, time(NULL
));
754 ret
= exec_stmt(context
, s
->db
, "BEGIN IMMEDIATE TRANSACTION", KRB5_CC_IO
);
758 ret
= sqlite3_step(s
->icred
);
759 } while (ret
== SQLITE_ROW
);
760 sqlite3_reset(s
->icred
);
761 if (ret
!= SQLITE_DONE
) {
763 krb5_set_error_message(context
, ret
,
764 N_("Failed to add credential: %s", ""),
765 sqlite3_errmsg(s
->db
));
769 credid
= sqlite3_last_insert_rowid(s
->db
);
772 bind_principal(context
, s
->db
, s
->iprincipal
, 1, creds
->server
);
773 sqlite3_bind_int(s
->iprincipal
, 2, 1);
774 sqlite3_bind_int(s
->iprincipal
, 3, credid
);
777 ret
= sqlite3_step(s
->iprincipal
);
778 } while (ret
== SQLITE_ROW
);
779 sqlite3_reset(s
->iprincipal
);
780 if (ret
!= SQLITE_DONE
) {
782 krb5_set_error_message(context
, ret
,
783 N_("Failed to add principal: %s", ""),
784 sqlite3_errmsg(s
->db
));
790 bind_principal(context
, s
->db
, s
->iprincipal
, 1, creds
->client
);
791 sqlite3_bind_int(s
->iprincipal
, 2, 0);
792 sqlite3_bind_int(s
->iprincipal
, 3, credid
);
795 ret
= sqlite3_step(s
->iprincipal
);
796 } while (ret
== SQLITE_ROW
);
797 sqlite3_reset(s
->iprincipal
);
798 if (ret
!= SQLITE_DONE
) {
800 krb5_set_error_message(context
, ret
,
801 N_("Failed to add principal: %s", ""),
802 sqlite3_errmsg(s
->db
));
807 ret
= exec_stmt(context
, s
->db
, "COMMIT", KRB5_CC_IO
);
813 exec_stmt(context
, s
->db
, "ROLLBACK", 0);
818 static krb5_error_code KRB5_CALLCONV
819 scc_get_principal(krb5_context context
,
821 krb5_principal
*principal
)
823 krb5_scache
*s
= SCACHE(id
);
829 ret
= make_database(context
, s
);
833 sqlite3_bind_int(s
->scache
, 1, s
->cid
);
835 if (sqlite3_step(s
->scache
) != SQLITE_ROW
) {
836 sqlite3_reset(s
->scache
);
837 krb5_set_error_message(context
, KRB5_CC_END
,
838 N_("No principal for cache SCC:%s:%s", ""),
843 if (sqlite3_column_type(s
->scache
, 0) != SQLITE_TEXT
) {
844 sqlite3_reset(s
->scache
);
845 krb5_set_error_message(context
, KRB5_CC_END
,
846 N_("Principal data of wrong type "
847 "for SCC:%s:%s", ""),
852 str
= (const char *)sqlite3_column_text(s
->scache
, 0);
854 sqlite3_reset(s
->scache
);
855 krb5_set_error_message(context
, KRB5_CC_END
,
856 N_("Principal not set for SCC:%s:%s", ""),
861 ret
= krb5_parse_name(context
, str
, principal
);
863 sqlite3_reset(s
->scache
);
871 sqlite3_stmt
*credstmt
;
874 static krb5_error_code KRB5_CALLCONV
875 scc_get_first (krb5_context context
,
877 krb5_cc_cursor
*cursor
)
879 krb5_scache
*s
= SCACHE(id
);
881 struct cred_ctx
*ctx
;
882 char *str
= NULL
, *name
= NULL
;
886 ctx
= calloc(1, sizeof(*ctx
));
888 krb5_set_error_message(context
, ENOMEM
,
889 N_("malloc: out of memory", ""));
893 ret
= make_database(context
, s
);
899 if (s
->cid
== SCACHE_INVALID_CID
) {
900 krb5_set_error_message(context
, KRB5_CC_END
,
901 N_("Iterating a invalid scache %s", ""),
907 ret
= asprintf(&name
, "credIteration%pPid%d",
909 if (ret
< 0 || name
== NULL
) {
910 krb5_set_error_message(context
, ENOMEM
,
911 N_("malloc: out of memory", ""));
916 ret
= asprintf(&ctx
->drop
, "DROP TABLE %s", name
);
917 if (ret
< 0 || ctx
->drop
== NULL
) {
918 krb5_set_error_message(context
, ENOMEM
,
919 N_("malloc: out of memory", ""));
925 ret
= asprintf(&str
, "CREATE TEMPORARY TABLE %s "
926 "AS SELECT oid,created_at FROM credentials WHERE cid = %lu",
927 name
, (unsigned long)s
->cid
);
928 if (ret
< 0 || str
== NULL
) {
935 ret
= exec_stmt(context
, s
->db
, str
, KRB5_CC_IO
);
945 ret
= asprintf(&str
, "SELECT oid FROM %s ORDER BY created_at", name
);
946 if (ret
< 0 || str
== NULL
) {
947 exec_stmt(context
, s
->db
, ctx
->drop
, 0);
954 ret
= prepare_stmt(context
, s
->db
, &ctx
->stmt
, str
);
959 exec_stmt(context
, s
->db
, ctx
->drop
, 0);
965 ret
= prepare_stmt(context
, s
->db
, &ctx
->credstmt
,
966 "SELECT cred FROM credentials WHERE oid = ?");
968 sqlite3_finalize(ctx
->stmt
);
969 exec_stmt(context
, s
->db
, ctx
->drop
, 0);
980 static krb5_error_code KRB5_CALLCONV
981 scc_get_next (krb5_context context
,
983 krb5_cc_cursor
*cursor
,
986 struct cred_ctx
*ctx
= *cursor
;
987 krb5_scache
*s
= SCACHE(id
);
990 const void *data
= NULL
;
994 ret
= sqlite3_step(ctx
->stmt
);
995 if (ret
== SQLITE_DONE
) {
996 krb5_clear_error_message(context
);
998 } else if (ret
!= SQLITE_ROW
) {
999 krb5_set_error_message(context
, KRB5_CC_IO
,
1000 N_("scache Database failed: %s", ""),
1001 sqlite3_errmsg(s
->db
));
1005 oid
= sqlite3_column_int64(ctx
->stmt
, 0);
1007 /* read cred from credentials table */
1009 sqlite3_bind_int(ctx
->credstmt
, 1, oid
);
1011 ret
= sqlite3_step(ctx
->credstmt
);
1012 if (ret
!= SQLITE_ROW
) {
1013 sqlite3_reset(ctx
->credstmt
);
1017 if (sqlite3_column_type(ctx
->credstmt
, 0) != SQLITE_BLOB
) {
1018 krb5_set_error_message(context
, KRB5_CC_END
,
1019 N_("credential of wrong type for SCC:%s:%s", ""),
1021 sqlite3_reset(ctx
->credstmt
);
1025 data
= sqlite3_column_blob(ctx
->credstmt
, 0);
1026 len
= sqlite3_column_bytes(ctx
->credstmt
, 0);
1028 ret
= decode_creds(context
, data
, len
, creds
);
1029 sqlite3_reset(ctx
->credstmt
);
1033 static krb5_error_code KRB5_CALLCONV
1034 scc_end_get (krb5_context context
,
1036 krb5_cc_cursor
*cursor
)
1038 struct cred_ctx
*ctx
= *cursor
;
1039 krb5_scache
*s
= SCACHE(id
);
1041 sqlite3_finalize(ctx
->stmt
);
1042 sqlite3_finalize(ctx
->credstmt
);
1044 exec_stmt(context
, s
->db
, ctx
->drop
, 0);
1052 static krb5_error_code KRB5_CALLCONV
1053 scc_remove_cred(krb5_context context
,
1058 krb5_scache
*s
= SCACHE(id
);
1059 krb5_error_code ret
;
1061 sqlite_uint64 credid
= 0;
1062 const void *data
= NULL
;
1065 ret
= make_database(context
, s
);
1069 ret
= prepare_stmt(context
, s
->db
, &stmt
,
1070 "SELECT cred,oid FROM credentials "
1075 sqlite3_bind_int(stmt
, 1, s
->cid
);
1077 /* find credential... */
1081 ret
= sqlite3_step(stmt
);
1082 if (ret
== SQLITE_DONE
) {
1085 } else if (ret
!= SQLITE_ROW
) {
1087 krb5_set_error_message(context
, ret
,
1088 N_("scache Database failed: %s", ""),
1089 sqlite3_errmsg(s
->db
));
1093 if (sqlite3_column_type(stmt
, 0) != SQLITE_BLOB
) {
1095 krb5_set_error_message(context
, ret
,
1096 N_("Credential of wrong type "
1097 "for SCC:%s:%s", ""),
1102 data
= sqlite3_column_blob(stmt
, 0);
1103 len
= sqlite3_column_bytes(stmt
, 0);
1105 ret
= decode_creds(context
, data
, len
, &creds
);
1109 ret
= krb5_compare_creds(context
, which
, mcreds
, &creds
);
1110 krb5_free_cred_contents(context
, &creds
);
1112 credid
= sqlite3_column_int64(stmt
, 1);
1118 sqlite3_finalize(stmt
);
1121 ret
= prepare_stmt(context
, s
->db
, &stmt
,
1122 "DELETE FROM credentials WHERE oid=?");
1125 sqlite3_bind_int(stmt
, 1, credid
);
1128 ret
= sqlite3_step(stmt
);
1129 } while (ret
== SQLITE_ROW
);
1130 sqlite3_finalize(stmt
);
1131 if (ret
!= SQLITE_DONE
) {
1133 krb5_set_error_message(context
, ret
,
1134 N_("failed to delete scache credental", ""));
1142 static krb5_error_code KRB5_CALLCONV
1143 scc_set_flags(krb5_context context
,
1156 static krb5_error_code KRB5_CALLCONV
1157 scc_get_cache_first(krb5_context context
, krb5_cc_cursor
*cursor
)
1159 struct cache_iter
*ctx
;
1160 krb5_error_code ret
;
1161 char *name
= NULL
, *str
= NULL
;
1165 ctx
= calloc(1, sizeof(*ctx
));
1167 krb5_set_error_message(context
, ENOMEM
,
1168 N_("malloc: out of memory", ""));
1172 ret
= default_db(context
, &ctx
->db
);
1173 if (ctx
->db
== NULL
) {
1178 ret
= asprintf(&name
, "cacheIteration%pPid%d",
1179 ctx
, (int)getpid());
1180 if (ret
< 0 || name
== NULL
) {
1181 krb5_set_error_message(context
, ENOMEM
,
1182 N_("malloc: out of memory", ""));
1183 sqlite3_close(ctx
->db
);
1188 ret
= asprintf(&ctx
->drop
, "DROP TABLE %s", name
);
1189 if (ret
< 0 || ctx
->drop
== NULL
) {
1190 krb5_set_error_message(context
, ENOMEM
,
1191 N_("malloc: out of memory", ""));
1192 sqlite3_close(ctx
->db
);
1198 ret
= asprintf(&str
, "CREATE TEMPORARY TABLE %s AS SELECT name FROM caches",
1200 if (ret
< 0 || str
== NULL
) {
1201 krb5_set_error_message(context
, ENOMEM
,
1202 N_("malloc: out of memory", ""));
1203 sqlite3_close(ctx
->db
);
1210 ret
= exec_stmt(context
, ctx
->db
, str
, KRB5_CC_IO
);
1214 sqlite3_close(ctx
->db
);
1221 ret
= asprintf(&str
, "SELECT name FROM %s", name
);
1223 if (ret
< 0 || str
== NULL
) {
1224 exec_stmt(context
, ctx
->db
, ctx
->drop
, 0);
1225 sqlite3_close(ctx
->db
);
1232 ret
= prepare_stmt(context
, ctx
->db
, &ctx
->stmt
, str
);
1235 exec_stmt(context
, ctx
->db
, ctx
->drop
, 0);
1236 sqlite3_close(ctx
->db
);
1247 static krb5_error_code KRB5_CALLCONV
1248 scc_get_cache_next(krb5_context context
,
1249 krb5_cc_cursor cursor
,
1252 struct cache_iter
*ctx
= cursor
;
1253 krb5_error_code ret
;
1257 ret
= sqlite3_step(ctx
->stmt
);
1258 if (ret
== SQLITE_DONE
) {
1259 krb5_clear_error_message(context
);
1261 } else if (ret
!= SQLITE_ROW
) {
1262 krb5_set_error_message(context
, KRB5_CC_IO
,
1263 N_("Database failed: %s", ""),
1264 sqlite3_errmsg(ctx
->db
));
1268 if (sqlite3_column_type(ctx
->stmt
, 0) != SQLITE_TEXT
)
1271 name
= (const char *)sqlite3_column_text(ctx
->stmt
, 0);
1275 ret
= _krb5_cc_allocate(context
, &krb5_scc_ops
, id
);
1279 return scc_resolve(context
, id
, name
);
1282 static krb5_error_code KRB5_CALLCONV
1283 scc_end_cache_get(krb5_context context
, krb5_cc_cursor cursor
)
1285 struct cache_iter
*ctx
= cursor
;
1287 exec_stmt(context
, ctx
->db
, ctx
->drop
, 0);
1288 sqlite3_finalize(ctx
->stmt
);
1289 sqlite3_close(ctx
->db
);
1295 static krb5_error_code KRB5_CALLCONV
1296 scc_move(krb5_context context
, krb5_ccache from
, krb5_ccache to
)
1298 krb5_scache
*sfrom
= SCACHE(from
);
1299 krb5_scache
*sto
= SCACHE(to
);
1300 krb5_error_code ret
;
1302 if (strcmp(sfrom
->file
, sto
->file
) != 0) {
1303 krb5_set_error_message(context
, KRB5_CC_BADNAME
,
1304 N_("Can't handle cross database "
1305 "credential move: %s -> %s", ""),
1306 sfrom
->file
, sto
->file
);
1307 return KRB5_CC_BADNAME
;
1310 ret
= make_database(context
, sfrom
);
1314 ret
= exec_stmt(context
, sfrom
->db
,
1315 "BEGIN IMMEDIATE TRANSACTION", KRB5_CC_IO
);
1316 if (ret
) return ret
;
1318 if (sto
->cid
!= SCACHE_INVALID_CID
) {
1319 /* drop old cache entry */
1321 sqlite3_bind_int(sfrom
->dcache
, 1, sto
->cid
);
1323 ret
= sqlite3_step(sfrom
->dcache
);
1324 } while (ret
== SQLITE_ROW
);
1325 sqlite3_reset(sfrom
->dcache
);
1326 if (ret
!= SQLITE_DONE
) {
1327 krb5_set_error_message(context
, KRB5_CC_IO
,
1328 N_("Failed to delete old cache: %d", ""),
1334 sqlite3_bind_text(sfrom
->ucachen
, 1, sto
->name
, -1, NULL
);
1335 sqlite3_bind_int(sfrom
->ucachen
, 2, sfrom
->cid
);
1338 ret
= sqlite3_step(sfrom
->ucachen
);
1339 } while (ret
== SQLITE_ROW
);
1340 sqlite3_reset(sfrom
->ucachen
);
1341 if (ret
!= SQLITE_DONE
) {
1342 krb5_set_error_message(context
, KRB5_CC_IO
,
1343 N_("Failed to update new cache: %d", ""),
1348 sto
->cid
= sfrom
->cid
;
1350 ret
= exec_stmt(context
, sfrom
->db
, "COMMIT", KRB5_CC_IO
);
1351 if (ret
) return ret
;
1358 exec_stmt(context
, sfrom
->db
, "ROLLBACK", 0);
1364 static krb5_error_code KRB5_CALLCONV
1365 scc_get_default_name(krb5_context context
, char **str
)
1367 krb5_error_code ret
;
1372 ret
= get_def_name(context
, &name
);
1374 return _krb5_expand_default_cc_name(context
, KRB5_SCACHE_NAME
, str
);
1376 ret
= asprintf(str
, "SCC:%s", name
);
1378 if (ret
< 0 || *str
== NULL
) {
1379 krb5_set_error_message(context
, ENOMEM
,
1380 N_("malloc: out of memory", ""));
1386 static krb5_error_code KRB5_CALLCONV
1387 scc_set_default(krb5_context context
, krb5_ccache id
)
1389 krb5_scache
*s
= SCACHE(id
);
1390 krb5_error_code ret
;
1392 if (s
->cid
== SCACHE_INVALID_CID
) {
1393 krb5_set_error_message(context
, KRB5_CC_IO
,
1394 N_("Trying to set a invalid cache "
1395 "as default %s", ""),
1400 ret
= sqlite3_bind_text(s
->umaster
, 1, s
->name
, -1, NULL
);
1402 sqlite3_reset(s
->umaster
);
1403 krb5_set_error_message(context
, KRB5_CC_IO
,
1404 N_("Failed to set name of default cache", ""));
1409 ret
= sqlite3_step(s
->umaster
);
1410 } while (ret
== SQLITE_ROW
);
1411 sqlite3_reset(s
->umaster
);
1412 if (ret
!= SQLITE_DONE
) {
1413 krb5_set_error_message(context
, KRB5_CC_IO
,
1414 N_("Failed to update default cache", ""));
1422 * Variable containing the SCC based credential cache implemention.
1424 * @ingroup krb5_ccache
1427 KRB5_LIB_VARIABLE
const krb5_cc_ops krb5_scc_ops
= {
1428 KRB5_CC_OPS_VERSION
,
1437 NULL
, /* scc_retrieve */
1445 scc_get_cache_first
,
1449 scc_get_default_name
,