1 /*
2  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 /*
9  * lib/krb5/keytab/read_servi.c
10  *
11  * Copyright 1990 by the Massachusetts Institute of Technology.
12  * All Rights Reserved.
13  *
14  * Export of this software from the United States of America may
15  *   require a specific license from the United States Government.
16  *   It is the responsibility of any person or organization contemplating
17  *   export to obtain such a license before exporting.
18  *
19  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
20  * distribute this software and its documentation for any purpose and
21  * without fee is hereby granted, provided that the above copyright
22  * notice appear in all copies and that both that copyright notice and
23  * this permission notice appear in supporting documentation, and that
24  * the name of M.I.T. not be used in advertising or publicity pertaining
25  * to distribution of the software without specific, written prior
26  * permission.  Furthermore if you modify this software you must label
27  * your software as modified software and not distribute it in such a
28  * fashion that it might be confused with the original M.I.T. software.
29  * M.I.T. makes no representations about the suitability of
30  * this software for any purpose.  It is provided "as is" without express
31  * or implied warranty.
32  *
33  *
34  * This routine is designed to be passed to krb5_rd_req.
35  * It is a convenience function that reads a key out of a keytab.
36  * It handles all of the opening and closing of the keytab
37  * internally.
38  */
39 
40 #include <k5-int.h>
41 
42 #define KSUCCESS 0
43 
44 /*
45  * effects: If keyprocarg is not NULL, it is taken to be the name of a
46  *	keytab.  Otherwise, the default keytab will be used.  This
47  *	routine opens the keytab and finds the principal associated with
48  *	principal, vno, and enctype and returns the resulting key in *key
49  *	or returning an error code if it is not	found.
50  * returns: Either KSUCCESS or error code.
51  * errors: error code if not found or keyprocarg is invalid.
52  */
53 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
54 krb5_kt_read_service_key(context, keyprocarg, principal, vno, enctype, key)
55     krb5_context context;
56     krb5_pointer keyprocarg;
57     krb5_principal principal;
58     krb5_kvno vno;
59     krb5_enctype enctype;
60     krb5_keyblock FAR * FAR * key;
61 {
62     krb5_error_code kerror = KSUCCESS;
63     char keytabname[MAX_KEYTAB_NAME_LEN + 1]; /* + 1 for NULL termination */
64     krb5_keytab id;
65     krb5_keytab_entry entry;
66 
67     /*
68      * Get the name of the file that we should use.
69      */
70     if (!keyprocarg) {
71 	if ((kerror = krb5_kt_default_name(context, (char *)keytabname,
72 					   sizeof(keytabname) - 1))!= KSUCCESS)
73 	    return (kerror);
74     } else {
75 	memset(keytabname, 0, sizeof(keytabname));
76 	(void) strncpy(keytabname, (char *)keyprocarg,
77 		       sizeof(keytabname) - 1);
78     }
79 
80     if ((kerror = krb5_kt_resolve(context, (char *)keytabname, &id)))
81 	return (kerror);
82 
83     kerror = krb5_kt_get_entry(context, id, principal, vno, enctype, &entry);
84     (void) krb5_kt_close(context, id);
85 
86     if (kerror)
87 	return(kerror);
88 
89     krb5_copy_keyblock(context, &entry.key, key);
90 
91     krb5_kt_free_entry(context, &entry);
92 
93     return (KSUCCESS);
94 }
95