Initial import from FreeBSD RELENG_4:
[games.git] / crypto / kerberosIV / lib / kdb / krb_lib.c
1 /* 
2   Copyright (C) 1989 by the Massachusetts Institute of Technology
3
4    Export of this software from the United States of America is assumed
5    to require a specific license from the United States Government.
6    It is the responsibility of any person or organization contemplating
7    export to obtain such a license before exporting.
8
9 WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
10 distribute this software and its documentation for any purpose and
11 without fee is hereby granted, provided that the above copyright
12 notice appear in all copies and that both that copyright notice and
13 this permission notice appear in supporting documentation, and that
14 the name of M.I.T. not be used in advertising or publicity pertaining
15 to distribution of the software without specific, written prior
16 permission.  M.I.T. makes no representations about the suitability of
17 this software for any purpose.  It is provided "as is" without express
18 or implied warranty.
19
20   */
21
22 #include "kdb_locl.h"
23
24 RCSID("$Id: krb_lib.c,v 1.13 1998/11/22 09:41:43 assar Exp $");
25
26 #ifdef DEBUG
27 extern int debug;
28 extern char *progname;
29 long    kerb_debug;
30 #endif
31
32 static int init = 0;
33
34 /*
35  * initialization routine for data base 
36  */
37
38 int
39 kerb_init(void)
40 {
41 #ifdef DEBUG
42     if (!init) {
43         char *dbg = getenv("KERB_DBG");
44         if (dbg)
45             sscanf(dbg, "%d", &kerb_debug);
46         init = 1;
47     }
48 #endif
49     kerb_db_init();
50
51 #ifdef CACHE
52     kerb_cache_init();
53 #endif
54
55     /* successful init, return 0, else errcode */
56     return (0);
57 }
58
59 /*
60  * finalization routine for database -- NOTE: MUST be called by any
61  * program using kerb_init.  ALSO will have to be modified to finalize
62  * caches, if they're ever really implemented. 
63  */
64
65 void
66 kerb_fini(void)
67 {
68     kerb_db_fini();
69 }
70
71
72 int
73 kerb_delete_principal(char *name, char *inst)
74 {
75     int ret;
76     
77     if (!init)
78         kerb_init();
79
80     ret = kerb_db_delete_principal(name, inst);
81 #ifdef CACHE
82     if(ret == 0){
83         kerb_cache_delete_principal(name, inst);
84     }
85 #endif
86     return ret;
87 }
88
89
90 /*
91  * look up a principal in the cache or data base returns number of
92  * principals found 
93  */
94
95 int
96 kerb_get_principal(char *name,  /* could have wild card */
97                    char *inst,  /* could have wild card */
98                    Principal *principal,
99                    unsigned int max, /* max number of name structs to return */
100                    int *more)   /* more tuples than room for */
101 {
102     int     found = 0;
103 #ifdef CACHE
104     static int wild = 0;
105 #endif
106     if (!init)
107         kerb_init();
108
109 #ifdef DEBUG
110     if (kerb_debug & 1)
111         fprintf(stderr, "\n%s: kerb_get_principal for %s %s max = %d\n",
112             progname, name, inst, max);
113 #endif
114     
115     /*
116      * if this is a request including a wild card, have to go to db
117      * since the cache may not be exhaustive. 
118      */
119
120     /* clear the principal area */
121     memset(principal, 0, max * sizeof(Principal));
122
123 #ifdef CACHE
124     /*
125      * so check to see if the name contains a wildcard "*" or "?", not
126      * preceeded by a backslash. 
127      */
128     wild = 0;
129     if (index(name, '*') || index(name, '?') ||
130         index(inst, '*') || index(inst, '?'))
131         wild = 1;
132
133     if (!wild) {
134         /* try the cache first */
135         found = kerb_cache_get_principal(name, inst, principal, max, more);
136         if (found)
137             return (found);
138     }
139 #endif
140     /* If we didn't try cache, or it wasn't there, try db */
141     found = kerb_db_get_principal(name, inst, principal, max, more);
142     /* try to insert principal(s) into cache if it was found */
143 #ifdef CACHE
144     if (found > 0) {
145         kerb_cache_put_principal(principal, found);
146     }
147 #endif
148     return (found);
149 }
150
151 /* principals */
152 int
153 kerb_put_principal(Principal *principal,
154                    unsigned int n)                         
155                                 /* number of principal structs to write */
156 {
157     /* set mod date */
158     principal->mod_date = time((time_t *)0);
159     /* and mod date string */
160
161     strftime(principal->mod_date_txt, 
162              sizeof(principal->mod_date_txt), 
163              "%Y-%m-%d", k_localtime(&principal->mod_date));
164     strftime(principal->exp_date_txt, 
165              sizeof(principal->exp_date_txt), 
166              "%Y-%m-%d", k_localtime(&principal->exp_date));
167 #ifdef DEBUG
168     if (kerb_debug & 1) {
169         int i;
170         fprintf(stderr, "\nkerb_put_principal...");
171         for (i = 0; i < n; i++) {
172             krb_print_principal(&principal[i]);
173         }
174     }
175 #endif
176     /* write database */
177     if (kerb_db_put_principal(principal, n) < 0) {
178 #ifdef DEBUG
179         if (kerb_debug & 1)
180             fprintf(stderr, "\n%s: kerb_db_put_principal err", progname);
181         /* watch out for cache */
182 #endif
183         return -1;
184     }
185 #ifdef CACHE
186     /* write cache */
187     if (!kerb_cache_put_principal(principal, n)) {
188 #ifdef DEBUG
189         if (kerb_debug & 1)
190             fprintf(stderr, "\n%s: kerb_cache_put_principal err", progname);
191 #endif
192         return -1;
193     }
194 #endif
195     return 0;
196 }
197
198 int
199 kerb_get_dba(char *name,        /* could have wild card */
200              char *inst,        /* could have wild card */
201              Dba *dba,
202              unsigned int max,  /* max number of name structs to return */
203              int *more)         /* more tuples than room for */
204 {
205     int     found = 0;
206 #ifdef CACHE
207     static int wild = 0;
208 #endif
209     if (!init)
210         kerb_init();
211
212 #ifdef DEBUG
213     if (kerb_debug & 1)
214         fprintf(stderr, "\n%s: kerb_get_dba for %s %s max = %d\n",
215             progname, name, inst, max);
216 #endif
217     /*
218      * if this is a request including a wild card, have to go to db
219      * since the cache may not be exhaustive. 
220      */
221
222     /* clear the dba area */
223     memset(dba, 0, max * sizeof(Dba));
224
225 #ifdef CACHE
226     /*
227      * so check to see if the name contains a wildcard "*" or "?", not
228      * preceeded by a backslash. 
229      */
230
231     wild = 0;
232     if (index(name, '*') || index(name, '?') ||
233         index(inst, '*') || index(inst, '?'))
234         wild = 1;
235
236     if (!wild) {
237         /* try the cache first */
238         found = kerb_cache_get_dba(name, inst, dba, max, more);
239         if (found)
240             return (found);
241     }
242 #endif
243     /* If we didn't try cache, or it wasn't there, try db */
244     found = kerb_db_get_dba(name, inst, dba, max, more);
245 #ifdef CACHE
246     /* try to insert dba(s) into cache if it was found */
247     if (found) {
248         kerb_cache_put_dba(dba, found);
249     }
250 #endif
251     return (found);
252 }