xref: /freebsd/crypto/krb5/src/clients/kinit/kinit_kdb.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* clients/kinit/kinit_kdb.c */
3 /*
4  * Copyright (C) 2010 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 
27 /**
28  *    @file kinit_kdb.c
29  *    Operations to open the KDB and make the KDB key table available
30  *    for kinit.
31  */
32 
33 
34 #include <k5-int.h>
35 #include <kadm5/admin.h>
36 #include <kdb.h>
37 #include "extern.h"
38 
39 /* Server handle */
40 static void *server_handle;
41 
42 /* Free and reinitialize *pcontext with the KDB opened to the given realm, so
43  * that it can be used with the KDB keytab type. */
44 krb5_error_code
kinit_kdb_init(krb5_context * pcontext,char * realm)45 kinit_kdb_init(krb5_context *pcontext, char *realm)
46 {
47     kadm5_config_params config;
48     krb5_error_code ret;
49 
50     if (*pcontext) {
51         krb5_free_context(*pcontext);
52         *pcontext = NULL;
53     }
54     memset(&config, 0, sizeof config);
55 
56     ret = kadm5_init_krb5_context(pcontext);
57     if (ret)
58         return ret;
59 
60     config.mask = KADM5_CONFIG_REALM;
61     config.realm = realm;
62     ret = kadm5_init(*pcontext, "kinit", NULL, "kinit", &config,
63                      KADM5_STRUCT_VERSION, KADM5_API_VERSION_4, NULL,
64                      &server_handle);
65     if (ret)
66         return ret;
67 
68     return krb5_db_register_keytab(*pcontext);
69 }
70 
71 void
kinit_kdb_fini(void)72 kinit_kdb_fini(void)
73 {
74     kadm5_destroy(server_handle);
75 }
76