2 * Copyright (c) 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "krb5_locl.h"
38 typedef struct krb5_scache
{
47 sqlite3_stmt
*iprincipal
;
50 sqlite3_stmt
*ucachen
;
51 sqlite3_stmt
*ucachep
;
54 sqlite3_stmt
*scache_name
;
55 sqlite3_stmt
*umaster
;
59 #define SCACHE(X) ((krb5_scache *)(X)->data.data)
61 #define SCACHE_DEF_NAME "Default-cache"
62 #define KRB5_SCACHE_DB "/tmp/krb5scc_%{uid}"
63 #define KRB5_SCACHE_NAME "SCC:" SCACHE_DEF_NAME ":" KRB5_SCACHE_DB
65 #define SCACHE_INVALID_CID ((sqlite_uint64)-1)
71 #define SQL_CMASTER "" \
72 "CREATE TABLE master (" \
73 "oid INTEGER PRIMARY KEY," \
74 "version INTEGER NOT NULL," \
75 "defaultcache TEXT NOT NULL" \
78 #define SQL_SETUP_MASTER \
79 "INSERT INTO master (version,defaultcache) VALUES(2, \"" SCACHE_DEF_NAME "\")"
80 #define SQL_UMASTER "UPDATE master SET defaultcache=? WHERE version=2"
82 #define SQL_CCACHE "" \
83 "CREATE TABLE caches (" \
84 "oid INTEGER PRIMARY KEY," \
86 "name TEXT NOT NULL" \
89 #define SQL_TCACHE "" \
90 "CREATE TRIGGER CacheDropCreds AFTER DELETE ON caches " \
91 "FOR EACH ROW BEGIN " \
92 "DELETE FROM credentials WHERE cid=old.oid;" \
95 #define SQL_ICACHE "INSERT INTO caches (name) VALUES(?)"
96 #define SQL_UCACHE_NAME "UPDATE caches SET name=? WHERE OID=?"
97 #define SQL_UCACHE_PRINCIPAL "UPDATE caches SET principal=? WHERE OID=?"
98 #define SQL_DCACHE "DELETE FROM caches WHERE OID=?"
99 #define SQL_SCACHE "SELECT principal,name FROM caches WHERE OID=?"
100 #define SQL_SCACHE_NAME "SELECT oid FROM caches WHERE NAME=?"
102 #define SQL_CCREDS "" \
103 "CREATE TABLE credentials (" \
104 "oid INTEGER PRIMARY KEY," \
105 "cid INTEGER NOT NULL," \
106 "kvno INTEGER NOT NULL," \
107 "etype INTEGER NOT NULL," \
108 "created_at INTEGER NOT NULL," \
109 "cred BLOB NOT NULL" \
112 #define SQL_TCRED "" \
113 "CREATE TRIGGER credDropPrincipal AFTER DELETE ON credentials " \
114 "FOR EACH ROW BEGIN " \
115 "DELETE FROM principals WHERE credential_id=old.oid;" \
118 #define SQL_ICRED "INSERT INTO credentials (cid, kvno, etype, cred, created_at) VALUES (?,?,?,?,?)"
119 #define SQL_DCRED "DELETE FROM credentials WHERE cid=?"
121 #define SQL_CPRINCIPALS "" \
122 "CREATE TABLE principals (" \
123 "oid INTEGER PRIMARY KEY," \
124 "principal TEXT NOT NULL," \
125 "type INTEGER NOT NULL," \
126 "credential_id INTEGER NOT NULL" \
129 #define SQL_IPRINCIPAL "INSERT INTO principals (principal, type, credential_id) VALUES (?,?,?)"
136 free_data(void *data
)
148 scc_free(krb5_scache
*s
)
156 sqlite3_finalize(s
->icred
);
158 sqlite3_finalize(s
->dcred
);
160 sqlite3_finalize(s
->iprincipal
);
162 sqlite3_finalize(s
->icache
);
164 sqlite3_finalize(s
->ucachen
);
166 sqlite3_finalize(s
->ucachep
);
168 sqlite3_finalize(s
->dcache
);
170 sqlite3_finalize(s
->scache
);
172 sqlite3_finalize(s
->scache_name
);
174 sqlite3_finalize(s
->umaster
);
177 sqlite3_close(s
->db
);
183 trace(void* ptr
, const char * str
)
185 printf("SQL: %s\n", str
);
189 static krb5_error_code
190 prepare_stmt(krb5_context context
, sqlite3
*db
,
191 sqlite3_stmt
**stmt
, const char *str
)
195 ret
= sqlite3_prepare_v2(db
, str
, -1, stmt
, NULL
);
196 if (ret
!= SQLITE_OK
) {
197 krb5_set_error_message(context
, ENOENT
,
198 N_("Failed to prepare stmt %s: %s", ""),
199 str
, sqlite3_errmsg(db
));
205 static krb5_error_code
206 exec_stmt(krb5_context context
, sqlite3
*db
, const char *str
,
207 krb5_error_code code
)
211 ret
= sqlite3_exec(db
, str
, NULL
, NULL
, NULL
);
212 if (ret
!= SQLITE_OK
&& code
) {
213 krb5_set_error_message(context
, code
,
214 N_("scache execute %s: %s", ""), str
,
221 static krb5_error_code
222 default_db(krb5_context context
, sqlite3
**db
)
227 ret
= _krb5_expand_default_cc_name(context
, KRB5_SCACHE_DB
, &name
);
231 ret
= sqlite3_open_v2(name
, db
, SQLITE_OPEN_READWRITE
, NULL
);
233 if (ret
!= SQLITE_OK
) {
234 krb5_clear_error_message(context
);
239 sqlite3_trace(*db
, trace
, NULL
);
245 static krb5_error_code
246 get_def_name(krb5_context context
, char **str
)
253 ret
= default_db(context
, &db
);
257 ret
= prepare_stmt(context
, db
, &stmt
, "SELECT defaultcache FROM master");
263 ret
= sqlite3_step(stmt
);
264 if (ret
!= SQLITE_ROW
)
267 if (sqlite3_column_type(stmt
, 0) != SQLITE_TEXT
)
270 name
= (const char *)sqlite3_column_text(stmt
, 0);
278 sqlite3_finalize(stmt
);
282 sqlite3_finalize(stmt
);
284 krb5_clear_error_message(context
);
291 scc_alloc(krb5_context context
, const char *name
)
299 s
->cid
= SCACHE_INVALID_CID
;
306 ret
= get_def_name(context
, &s
->name
);
308 s
->name
= strdup(SCACHE_DEF_NAME
);
310 s
->name
= strdup(name
);
312 file
= strrchr(s
->name
, ':');
315 s
->file
= strdup(file
);
317 _krb5_expand_default_cc_name(context
, KRB5_SCACHE_DB
, &s
->file
);
320 _krb5_expand_default_cc_name(context
, KRB5_SCACHE_DB
, &s
->file
);
321 asprintf(&s
->name
, "unique-%p", s
);
323 if (s
->file
== NULL
|| s
->name
== NULL
) {
331 static krb5_error_code
332 open_database(krb5_context context
, krb5_scache
*s
, int flags
)
336 ret
= sqlite3_open_v2(s
->file
, &s
->db
, SQLITE_OPEN_READWRITE
|flags
, NULL
);
339 krb5_set_error_message(context
, ENOENT
,
340 N_("Error opening scache file %s: %s", ""),
341 s
->file
, sqlite3_errmsg(s
->db
));
342 sqlite3_close(s
->db
);
345 krb5_set_error_message(context
, ENOENT
,
346 N_("malloc: out of memory", ""));
352 static krb5_error_code
353 create_cache(krb5_context context
, krb5_scache
*s
)
357 sqlite3_bind_text(s
->icache
, 1, s
->name
, -1, NULL
);
359 ret
= sqlite3_step(s
->icache
);
360 } while (ret
== SQLITE_ROW
);
361 if (ret
!= SQLITE_DONE
) {
362 krb5_set_error_message(context
, KRB5_CC_IO
,
363 N_("Failed to add scache: %d", ""), ret
);
366 sqlite3_reset(s
->icache
);
368 s
->cid
= sqlite3_last_insert_rowid(s
->db
);
373 static krb5_error_code
374 make_database(krb5_context context
, krb5_scache
*s
)
376 int created_file
= 0;
382 ret
= open_database(context
, s
, 0);
384 mode_t oldumask
= umask(077);
385 ret
= open_database(context
, s
, SQLITE_OPEN_CREATE
);
391 ret
= exec_stmt(context
, s
->db
, SQL_CMASTER
, KRB5_CC_IO
);
393 ret
= exec_stmt(context
, s
->db
, SQL_CCACHE
, KRB5_CC_IO
);
395 ret
= exec_stmt(context
, s
->db
, SQL_CCREDS
, KRB5_CC_IO
);
397 ret
= exec_stmt(context
, s
->db
, SQL_CPRINCIPALS
, KRB5_CC_IO
);
399 ret
= exec_stmt(context
, s
->db
, SQL_SETUP_MASTER
, KRB5_CC_IO
);
402 ret
= exec_stmt(context
, s
->db
, SQL_TCACHE
, KRB5_CC_IO
);
404 ret
= exec_stmt(context
, s
->db
, SQL_TCRED
, KRB5_CC_IO
);
409 sqlite3_trace(s
->db
, trace
, NULL
);
412 ret
= prepare_stmt(context
, s
->db
, &s
->icred
, SQL_ICRED
);
414 ret
= prepare_stmt(context
, s
->db
, &s
->dcred
, SQL_DCRED
);
416 ret
= prepare_stmt(context
, s
->db
, &s
->iprincipal
, SQL_IPRINCIPAL
);
418 ret
= prepare_stmt(context
, s
->db
, &s
->icache
, SQL_ICACHE
);
420 ret
= prepare_stmt(context
, s
->db
, &s
->ucachen
, SQL_UCACHE_NAME
);
422 ret
= prepare_stmt(context
, s
->db
, &s
->ucachep
, SQL_UCACHE_PRINCIPAL
);
424 ret
= prepare_stmt(context
, s
->db
, &s
->dcache
, SQL_DCACHE
);
426 ret
= prepare_stmt(context
, s
->db
, &s
->scache
, SQL_SCACHE
);
428 ret
= prepare_stmt(context
, s
->db
, &s
->scache_name
, SQL_SCACHE_NAME
);
430 ret
= prepare_stmt(context
, s
->db
, &s
->umaster
, SQL_UMASTER
);
437 sqlite3_close(s
->db
);
444 static krb5_error_code
445 bind_principal(krb5_context context
,
449 krb5_const_principal principal
)
454 ret
= krb5_unparse_name(context
, principal
, &str
);
458 ret
= sqlite3_bind_text(stmt
, col
, str
, -1, free_krb5
);
459 if (ret
!= SQLITE_OK
) {
461 krb5_set_error_message(context
, ENOMEM
,
462 N_("scache bind principal: %s", ""),
474 scc_get_name(krb5_context context
,
477 return SCACHE(id
)->name
;
480 static krb5_error_code
481 scc_resolve(krb5_context context
, krb5_ccache
*id
, const char *res
)
486 s
= scc_alloc(context
, res
);
488 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
489 N_("malloc: out of memory", ""));
490 return KRB5_CC_NOMEM
;
493 ret
= make_database(context
, s
);
499 ret
= sqlite3_bind_text(s
->scache_name
, 1, s
->name
, -1, NULL
);
500 if (ret
!= SQLITE_OK
) {
501 krb5_set_error_message(context
, ENOMEM
,
502 "bind name: %s", sqlite3_errmsg(s
->db
));
507 if (sqlite3_step(s
->scache_name
) == SQLITE_ROW
) {
509 if (sqlite3_column_type(s
->scache_name
, 0) != SQLITE_INTEGER
) {
510 sqlite3_reset(s
->scache_name
);
511 krb5_set_error_message(context
, KRB5_CC_END
,
512 N_("Cache name of wrong type "
513 "for scache %ld", ""),
514 (unsigned long)s
->name
);
519 s
->cid
= sqlite3_column_int(s
->scache_name
, 0);
521 s
->cid
= SCACHE_INVALID_CID
;
523 sqlite3_reset(s
->scache_name
);
525 (*id
)->data
.data
= s
;
526 (*id
)->data
.length
= sizeof(*s
);
531 static krb5_error_code
532 scc_gen_new(krb5_context context
, krb5_ccache
*id
)
536 s
= scc_alloc(context
, NULL
);
539 krb5_set_error_message(context
, KRB5_CC_NOMEM
,
540 N_("malloc: out of memory", ""));
541 return KRB5_CC_NOMEM
;
544 (*id
)->data
.data
= s
;
545 (*id
)->data
.length
= sizeof(*s
);
550 static krb5_error_code
551 scc_initialize(krb5_context context
,
553 krb5_principal primary_principal
)
555 krb5_scache
*s
= SCACHE(id
);
558 ret
= make_database(context
, s
);
562 ret
= exec_stmt(context
, s
->db
, "BEGIN IMMEDIATE TRANSACTION", KRB5_CC_IO
);
565 if (s
->cid
== SCACHE_INVALID_CID
) {
566 ret
= create_cache(context
, s
);
570 sqlite3_bind_int(s
->dcred
, 1, s
->cid
);
572 ret
= sqlite3_step(s
->dcred
);
573 } while (ret
== SQLITE_ROW
);
574 sqlite3_reset(s
->dcred
);
575 if (ret
!= SQLITE_DONE
) {
577 krb5_set_error_message(context
, ret
,
578 N_("Failed to delete old "
579 "credentials: %s", ""),
580 sqlite3_errmsg(s
->db
));
585 ret
= bind_principal(context
, s
->db
, s
->ucachep
, 1, primary_principal
);
588 sqlite3_bind_int(s
->ucachep
, 2, s
->cid
);
591 ret
= sqlite3_step(s
->ucachep
);
592 } while (ret
== SQLITE_ROW
);
593 sqlite3_reset(s
->ucachep
);
594 if (ret
!= SQLITE_DONE
) {
596 krb5_set_error_message(context
, ret
,
597 N_("Failed to bind principal to cache %s", ""),
598 sqlite3_errmsg(s
->db
));
602 ret
= exec_stmt(context
, s
->db
, "COMMIT", KRB5_CC_IO
);
608 exec_stmt(context
, s
->db
, "ROLLBACK", 0);
614 static krb5_error_code
615 scc_close(krb5_context context
,
618 scc_free(SCACHE(id
));
622 static krb5_error_code
623 scc_destroy(krb5_context context
,
626 krb5_scache
*s
= SCACHE(id
);
629 if (s
->cid
== SCACHE_INVALID_CID
)
632 sqlite3_bind_int(s
->dcache
, 1, s
->cid
);
634 ret
= sqlite3_step(s
->dcache
);
635 } while (ret
== SQLITE_ROW
);
636 sqlite3_reset(s
->dcache
);
637 if (ret
!= SQLITE_DONE
) {
638 krb5_set_error_message(context
, KRB5_CC_IO
,
639 N_("Failed to destroy cache %s: %s", ""),
640 s
->name
, sqlite3_errmsg(s
->db
));
646 static krb5_error_code
647 encode_creds(krb5_context context
, krb5_creds
*creds
, krb5_data
*data
)
652 sp
= krb5_storage_emem();
654 krb5_set_error_message(context
, ENOMEM
,
655 N_("malloc: out of memory", ""));
659 ret
= krb5_store_creds(sp
, creds
);
661 krb5_set_error_message(context
, ret
,
662 N_("Failed to store credential in scache", ""));
663 krb5_storage_free(sp
);
667 ret
= krb5_storage_to_data(sp
, data
);
668 krb5_storage_free(sp
);
670 krb5_set_error_message(context
, ret
,
671 N_("Failed to encode credential in scache", ""));
675 static krb5_error_code
676 decode_creds(krb5_context context
, const void *data
, size_t length
,
682 sp
= krb5_storage_from_readonly_mem(data
, length
);
684 krb5_set_error_message(context
, ENOMEM
,
685 N_("malloc: out of memory", ""));
689 ret
= krb5_ret_creds(sp
, creds
);
690 krb5_storage_free(sp
);
692 krb5_set_error_message(context
, ret
,
693 N_("Failed to read credential in scache", ""));
700 static krb5_error_code
701 scc_store_cred(krb5_context context
,
705 sqlite_uint64 credid
;
706 krb5_scache
*s
= SCACHE(id
);
710 ret
= make_database(context
, s
);
714 ret
= encode_creds(context
, creds
, &data
);
718 sqlite3_bind_int(s
->icred
, 1, s
->cid
);
720 krb5_enctype etype
= 0;
725 ret
= decode_Ticket(creds
->ticket
.data
,
726 creds
->ticket
.length
, &t
, &len
);
729 kvno
= *t
.enc_part
.kvno
;
731 etype
= t
.enc_part
.etype
;
736 sqlite3_bind_int(s
->icred
, 2, kvno
);
737 sqlite3_bind_int(s
->icred
, 3, etype
);
741 sqlite3_bind_blob(s
->icred
, 4, data
.data
, data
.length
, free_data
);
742 sqlite3_bind_int(s
->icred
, 5, time(NULL
));
744 ret
= exec_stmt(context
, s
->db
, "BEGIN IMMEDIATE TRANSACTION", KRB5_CC_IO
);
748 ret
= sqlite3_step(s
->icred
);
749 } while (ret
== SQLITE_ROW
);
750 sqlite3_reset(s
->icred
);
751 if (ret
!= SQLITE_DONE
) {
753 krb5_set_error_message(context
, ret
,
754 N_("Failed to add credential: %s", ""),
755 sqlite3_errmsg(s
->db
));
759 credid
= sqlite3_last_insert_rowid(s
->db
);
762 bind_principal(context
, s
->db
, s
->iprincipal
, 1, creds
->server
);
763 sqlite3_bind_int(s
->iprincipal
, 2, 1);
764 sqlite3_bind_int(s
->iprincipal
, 3, credid
);
767 ret
= sqlite3_step(s
->iprincipal
);
768 } while (ret
== SQLITE_ROW
);
769 sqlite3_reset(s
->iprincipal
);
770 if (ret
!= SQLITE_DONE
) {
772 krb5_set_error_message(context
, ret
,
773 N_("Failed to add principal: %s", ""),
774 sqlite3_errmsg(s
->db
));
780 bind_principal(context
, s
->db
, s
->iprincipal
, 1, creds
->client
);
781 sqlite3_bind_int(s
->iprincipal
, 2, 0);
782 sqlite3_bind_int(s
->iprincipal
, 3, credid
);
785 ret
= sqlite3_step(s
->iprincipal
);
786 } while (ret
== SQLITE_ROW
);
787 sqlite3_reset(s
->iprincipal
);
788 if (ret
!= SQLITE_DONE
) {
790 krb5_set_error_message(context
, ret
,
791 N_("Failed to add principal: %s", ""),
792 sqlite3_errmsg(s
->db
));
797 ret
= exec_stmt(context
, s
->db
, "COMMIT", KRB5_CC_IO
);
803 exec_stmt(context
, s
->db
, "ROLLBACK", 0);
808 static krb5_error_code
809 scc_get_principal(krb5_context context
,
811 krb5_principal
*principal
)
813 krb5_scache
*s
= SCACHE(id
);
819 ret
= make_database(context
, s
);
823 sqlite3_bind_int(s
->scache
, 1, s
->cid
);
825 if (sqlite3_step(s
->scache
) != SQLITE_ROW
) {
826 sqlite3_reset(s
->scache
);
827 krb5_set_error_message(context
, KRB5_CC_END
,
828 N_("No principal for cache SCC:%s:%s", ""),
833 if (sqlite3_column_type(s
->scache
, 0) != SQLITE_TEXT
) {
834 sqlite3_reset(s
->scache
);
835 krb5_set_error_message(context
, KRB5_CC_END
,
836 N_("Principal data of wrong type "
837 "for SCC:%s:%s", ""),
842 str
= (const char *)sqlite3_column_text(s
->scache
, 0);
844 sqlite3_reset(s
->scache
);
845 krb5_set_error_message(context
, KRB5_CC_END
,
846 N_("Principal not set for SCC:%s:%s", ""),
851 ret
= krb5_parse_name(context
, str
, principal
);
853 sqlite3_reset(s
->scache
);
861 sqlite3_stmt
*credstmt
;
864 static krb5_error_code
865 scc_get_first (krb5_context context
,
867 krb5_cc_cursor
*cursor
)
869 krb5_scache
*s
= SCACHE(id
);
871 struct cred_ctx
*ctx
;
876 ctx
= calloc(1, sizeof(*ctx
));
878 krb5_set_error_message(context
, ENOMEM
,
879 N_("malloc: out of memory", ""));
883 ret
= make_database(context
, s
);
889 if (s
->cid
== SCACHE_INVALID_CID
) {
890 krb5_set_error_message(context
, KRB5_CC_END
,
891 N_("Iterating a invalid scache %s", ""),
897 asprintf(&name
, "credIteration%luPid%d",
898 (unsigned long)ctx
, (int)getpid());
900 krb5_set_error_message(context
, ENOMEM
,
901 N_("malloc: out of memory", ""));
906 asprintf(&ctx
->drop
, "DROP TABLE %s", name
);
907 if (ctx
->drop
== NULL
) {
908 krb5_set_error_message(context
, ENOMEM
,
909 N_("malloc: out of memory", ""));
915 asprintf(&str
, "CREATE TEMPORARY TABLE %s "
916 "AS SELECT oid,created_at FROM credentials WHERE cid = %lu",
917 name
, (unsigned long)s
->cid
);
919 ret
= exec_stmt(context
, s
->db
, str
, KRB5_CC_IO
);
928 asprintf(&str
, "SELECT oid FROM %s ORDER BY created_at", name
);
930 exec_stmt(context
, s
->db
, ctx
->drop
, 0);
937 ret
= prepare_stmt(context
, s
->db
, &ctx
->stmt
, str
);
941 exec_stmt(context
, s
->db
, ctx
->drop
, 0);
947 ret
= prepare_stmt(context
, s
->db
, &ctx
->credstmt
,
948 "SELECT cred FROM credentials WHERE oid = ?");
950 sqlite3_finalize(ctx
->stmt
);
951 exec_stmt(context
, s
->db
, ctx
->drop
, 0);
962 static krb5_error_code
963 scc_get_next (krb5_context context
,
965 krb5_cc_cursor
*cursor
,
968 struct cred_ctx
*ctx
= *cursor
;
969 krb5_scache
*s
= SCACHE(id
);
972 const void *data
= NULL
;
976 ret
= sqlite3_step(ctx
->stmt
);
977 if (ret
== SQLITE_DONE
) {
978 krb5_clear_error_message(context
);
980 } else if (ret
!= SQLITE_ROW
) {
981 krb5_set_error_message(context
, KRB5_CC_IO
,
982 N_("scache Database failed: %s", ""),
983 sqlite3_errmsg(s
->db
));
987 oid
= sqlite3_column_int64(ctx
->stmt
, 0);
989 /* read cred from credentials table */
991 sqlite3_bind_int(ctx
->credstmt
, 1, oid
);
993 ret
= sqlite3_step(ctx
->credstmt
);
994 if (ret
!= SQLITE_ROW
) {
995 sqlite3_reset(ctx
->credstmt
);
999 if (sqlite3_column_type(ctx
->credstmt
, 0) != SQLITE_BLOB
) {
1000 krb5_set_error_message(context
, KRB5_CC_END
,
1001 N_("credential of wrong type for SCC:%s:%s", ""),
1003 sqlite3_reset(ctx
->credstmt
);
1007 data
= sqlite3_column_blob(ctx
->credstmt
, 0);
1008 len
= sqlite3_column_bytes(ctx
->credstmt
, 0);
1010 ret
= decode_creds(context
, data
, len
, creds
);
1011 sqlite3_reset(ctx
->credstmt
);
1015 static krb5_error_code
1016 scc_end_get (krb5_context context
,
1018 krb5_cc_cursor
*cursor
)
1020 struct cred_ctx
*ctx
= *cursor
;
1021 krb5_scache
*s
= SCACHE(id
);
1023 sqlite3_finalize(ctx
->stmt
);
1024 sqlite3_finalize(ctx
->credstmt
);
1026 exec_stmt(context
, s
->db
, ctx
->drop
, 0);
1034 static krb5_error_code
1035 scc_remove_cred(krb5_context context
,
1040 krb5_scache
*s
= SCACHE(id
);
1041 krb5_error_code ret
;
1043 sqlite_uint64 credid
= 0;
1044 const void *data
= NULL
;
1047 ret
= make_database(context
, s
);
1051 ret
= prepare_stmt(context
, s
->db
, &stmt
,
1052 "SELECT cred,oid FROM credentials "
1057 sqlite3_bind_int(stmt
, 1, s
->cid
);
1059 /* find credential... */
1063 ret
= sqlite3_step(stmt
);
1064 if (ret
== SQLITE_DONE
) {
1067 } else if (ret
!= SQLITE_ROW
) {
1069 krb5_set_error_message(context
, ret
,
1070 N_("scache Database failed: %s", ""),
1071 sqlite3_errmsg(s
->db
));
1075 if (sqlite3_column_type(stmt
, 0) != SQLITE_BLOB
) {
1077 krb5_set_error_message(context
, ret
,
1078 N_("Credential of wrong type "
1079 "for SCC:%s:%s", ""),
1084 data
= sqlite3_column_blob(stmt
, 0);
1085 len
= sqlite3_column_bytes(stmt
, 0);
1087 ret
= decode_creds(context
, data
, len
, &creds
);
1091 ret
= krb5_compare_creds(context
, which
, mcreds
, &creds
);
1092 krb5_free_cred_contents(context
, &creds
);
1094 credid
= sqlite3_column_int64(stmt
, 1);
1100 sqlite3_finalize(stmt
);
1103 ret
= prepare_stmt(context
, s
->db
, &stmt
,
1104 "DELETE FROM credentials WHERE oid=?");
1107 sqlite3_bind_int(stmt
, 1, credid
);
1110 ret
= sqlite3_step(stmt
);
1111 } while (ret
== SQLITE_ROW
);
1112 sqlite3_finalize(stmt
);
1113 if (ret
!= SQLITE_DONE
) {
1115 krb5_set_error_message(context
, ret
,
1116 N_("failed to delete scache credental", ""));
1124 static krb5_error_code
1125 scc_set_flags(krb5_context context
,
1138 static krb5_error_code
1139 scc_get_cache_first(krb5_context context
, krb5_cc_cursor
*cursor
)
1141 struct cache_iter
*ctx
;
1142 krb5_error_code ret
;
1147 ctx
= calloc(1, sizeof(*ctx
));
1149 krb5_set_error_message(context
, ENOMEM
,
1150 N_("malloc: out of memory", ""));
1154 ret
= default_db(context
, &ctx
->db
);
1155 if (ctx
->db
== NULL
) {
1160 asprintf(&name
, "cacheIteration%luPid%d",
1161 (unsigned long)ctx
, (int)getpid());
1163 krb5_set_error_message(context
, ENOMEM
,
1164 N_("malloc: out of memory", ""));
1165 sqlite3_close(ctx
->db
);
1170 asprintf(&ctx
->drop
, "DROP TABLE %s", name
);
1171 if (ctx
->drop
== NULL
) {
1172 krb5_set_error_message(context
, ENOMEM
,
1173 N_("malloc: out of memory", ""));
1174 sqlite3_close(ctx
->db
);
1180 asprintf(&str
, "CREATE TEMPORARY TABLE %s AS SELECT name FROM caches",
1183 krb5_set_error_message(context
, ENOMEM
,
1184 N_("malloc: out of memory", ""));
1185 sqlite3_close(ctx
->db
);
1192 ret
= exec_stmt(context
, ctx
->db
, str
, KRB5_CC_IO
);
1195 sqlite3_close(ctx
->db
);
1202 asprintf(&str
, "SELECT name FROM %s", name
);
1205 exec_stmt(context
, ctx
->db
, ctx
->drop
, 0);
1206 sqlite3_close(ctx
->db
);
1213 ret
= prepare_stmt(context
, ctx
->db
, &ctx
->stmt
, str
);
1216 exec_stmt(context
, ctx
->db
, ctx
->drop
, 0);
1217 sqlite3_close(ctx
->db
);
1228 static krb5_error_code
1229 scc_get_cache_next(krb5_context context
,
1230 krb5_cc_cursor cursor
,
1233 struct cache_iter
*ctx
= cursor
;
1234 krb5_error_code ret
;
1238 ret
= sqlite3_step(ctx
->stmt
);
1239 if (ret
== SQLITE_DONE
) {
1240 krb5_clear_error_message(context
);
1242 } else if (ret
!= SQLITE_ROW
) {
1243 krb5_set_error_message(context
, KRB5_CC_IO
,
1244 N_("Database failed: %s", ""),
1245 sqlite3_errmsg(ctx
->db
));
1249 if (sqlite3_column_type(ctx
->stmt
, 0) != SQLITE_TEXT
)
1252 name
= (const char *)sqlite3_column_text(ctx
->stmt
, 0);
1256 ret
= _krb5_cc_allocate(context
, &krb5_scc_ops
, id
);
1260 return scc_resolve(context
, id
, name
);
1263 static krb5_error_code
1264 scc_end_cache_get(krb5_context context
, krb5_cc_cursor cursor
)
1266 struct cache_iter
*ctx
= cursor
;
1268 exec_stmt(context
, ctx
->db
, ctx
->drop
, 0);
1269 sqlite3_finalize(ctx
->stmt
);
1270 sqlite3_close(ctx
->db
);
1276 static krb5_error_code
1277 scc_move(krb5_context context
, krb5_ccache from
, krb5_ccache to
)
1279 krb5_scache
*sfrom
= SCACHE(from
);
1280 krb5_scache
*sto
= SCACHE(to
);
1281 krb5_error_code ret
;
1283 if (strcmp(sfrom
->file
, sto
->file
) != 0) {
1284 krb5_set_error_message(context
, KRB5_CC_BADNAME
,
1285 N_("Can't handle cross database "
1286 "credential move: %s -> %s", ""),
1287 sfrom
->file
, sto
->file
);
1288 return KRB5_CC_BADNAME
;
1291 ret
= make_database(context
, sfrom
);
1295 ret
= exec_stmt(context
, sfrom
->db
,
1296 "BEGIN IMMEDIATE TRANSACTION", KRB5_CC_IO
);
1297 if (ret
) return ret
;
1299 if (sto
->cid
!= SCACHE_INVALID_CID
) {
1300 /* drop old cache entry */
1302 sqlite3_bind_int(sfrom
->dcache
, 1, sto
->cid
);
1304 ret
= sqlite3_step(sfrom
->dcache
);
1305 } while (ret
== SQLITE_ROW
);
1306 sqlite3_reset(sfrom
->dcache
);
1307 if (ret
!= SQLITE_DONE
) {
1308 krb5_set_error_message(context
, KRB5_CC_IO
,
1309 N_("Failed to delete old cache: %d", ""),
1315 sqlite3_bind_text(sfrom
->ucachen
, 1, sto
->name
, -1, NULL
);
1316 sqlite3_bind_int(sfrom
->ucachen
, 2, sfrom
->cid
);
1319 ret
= sqlite3_step(sfrom
->ucachen
);
1320 } while (ret
== SQLITE_ROW
);
1321 sqlite3_reset(sfrom
->ucachen
);
1322 if (ret
!= SQLITE_DONE
) {
1323 krb5_set_error_message(context
, KRB5_CC_IO
,
1324 N_("Failed to update new cache: %d", ""),
1329 sto
->cid
= sfrom
->cid
;
1331 ret
= exec_stmt(context
, sfrom
->db
, "COMMIT", KRB5_CC_IO
);
1332 if (ret
) return ret
;
1339 exec_stmt(context
, sfrom
->db
, "ROLLBACK", 0);
1345 static krb5_error_code
1346 scc_get_default_name(krb5_context context
, char **str
)
1348 krb5_error_code ret
;
1351 ret
= get_def_name(context
, &name
);
1353 return _krb5_expand_default_cc_name(context
, KRB5_SCACHE_NAME
, str
);
1355 asprintf(str
, "SCC:%s", name
);
1358 krb5_set_error_message(context
, ENOMEM
,
1359 N_("malloc: out of memory", ""));
1365 static krb5_error_code
1366 scc_set_default(krb5_context context
, krb5_ccache id
)
1368 krb5_scache
*s
= SCACHE(id
);
1369 krb5_error_code ret
;
1371 if (s
->cid
== SCACHE_INVALID_CID
) {
1372 krb5_set_error_message(context
, KRB5_CC_IO
,
1373 N_("Trying to set a invalid cache "
1374 "as default %s", ""),
1379 ret
= sqlite3_bind_text(s
->umaster
, 1, s
->name
, -1, NULL
);
1381 sqlite3_reset(s
->umaster
);
1382 krb5_set_error_message(context
, KRB5_CC_IO
,
1383 N_("Failed to set name of default cache", ""));
1388 ret
= sqlite3_step(s
->umaster
);
1389 } while (ret
== SQLITE_ROW
);
1390 sqlite3_reset(s
->umaster
);
1391 if (ret
!= SQLITE_DONE
) {
1392 krb5_set_error_message(context
, KRB5_CC_IO
,
1393 N_("Failed to update default cache", ""));
1401 * Variable containing the SCC based credential cache implemention.
1403 * @ingroup krb5_ccache
1406 KRB5_LIB_VARIABLE
const krb5_cc_ops krb5_scc_ops
= {
1407 KRB5_CC_OPS_VERSION
,
1416 NULL
, /* scc_retrieve */
1424 scc_get_cache_first
,
1428 scc_get_default_name
,