591a887abb664631007a5f1a8d4987b1cc33686d
[krb5.git] / src / lib / krb5 / keytab / file / ktf_g_ent.c
1 /*
2  * lib/krb5/keytab/file/ktf_get_en.c
3  *
4  * Copyright 1990 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.  M.I.T. makes no representations about the suitability of
20  * this software for any purpose.  It is provided "as is" without express
21  * or implied warranty.
22  * 
23  *
24  * This is the get_entry routine for the file based keytab implementation.
25  * It opens the keytab file, and either retrieves the entry or returns
26  * an error.
27  */
28
29 #include "k5-int.h"
30 #include "ktfile.h"
31
32 krb5_error_code INTERFACE
33 krb5_ktfile_get_entry(context, id, principal, kvno, keytype, entry)
34    krb5_context context;
35    krb5_keytab id;
36    krb5_principal principal;
37    krb5_kvno kvno;
38    krb5_keytype keytype;
39    krb5_keytab_entry * entry;
40 {
41     krb5_keytab_entry cur_entry, new_entry;
42     krb5_error_code kerror = 0;
43
44     /* Open the keyfile for reading */
45     if (kerror = krb5_ktfileint_openr(context, id))
46         return(kerror);
47     
48     /* 
49      * For efficiency and simplicity, we'll use a while true that 
50      * is exited with a break statement.
51      */
52     cur_entry.principal = 0;
53     cur_entry.vno = 0;
54     cur_entry.key.contents = 0;
55     while (TRUE) {
56         if (kerror = krb5_ktfileint_read_entry(context, id, &new_entry))
57             break;
58
59         if (krb5_principal_compare(context, principal, new_entry.principal)) {
60                 if (kvno == IGNORE_VNO) {
61                         if (cur_entry.vno < new_entry.vno) {
62                                 krb5_kt_free_entry(context, &cur_entry);
63                                 cur_entry = new_entry;
64                         }
65                 } else {
66                         cur_entry = new_entry;
67                         break;
68                 }
69         } else {
70                 krb5_kt_free_entry(context, &new_entry);
71         }
72     }
73     if (kerror == KRB5_KT_END)
74             kerror = cur_entry.principal ? 0 : KRB5_KT_NOTFOUND;
75     if (kerror) {
76         (void) krb5_ktfileint_close(context, id);
77         krb5_kt_free_entry(context, &cur_entry);
78         return kerror;
79     }
80     if ((kerror = krb5_ktfileint_close(context, id)) != 0) {
81         krb5_kt_free_entry(context, &cur_entry);
82         return kerror;
83     }
84     *entry = cur_entry;
85     return 0;
86 }