xref: /freebsd/crypto/krb5/src/plugins/kdb/ldap/libkdb_ldap/kdb_xdr.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* plugins/kdb/ldap/libkdb_ldap/kdb_xdr.c */
3 /*
4  * Copyright 1995 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 #include <k5-int.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <kdb.h>
32 
33 #define safe_realloc(p,n) ((p)?(realloc(p,n)):(malloc(n)))
34 
35 krb5_error_code
krb5_dbe_update_tl_data(krb5_context context,krb5_db_entry * entry,krb5_tl_data * new_tl_data)36 krb5_dbe_update_tl_data(krb5_context context, krb5_db_entry *entry,
37                         krb5_tl_data *new_tl_data)
38 {
39     krb5_tl_data        * tl_data;
40     krb5_octet          * tmp;
41 
42     /* copy the new data first, so we can fail cleanly if malloc()
43        fails */
44 
45     if ((tmp = (krb5_octet *) malloc(new_tl_data->tl_data_length)) == NULL)
46         return(ENOMEM);
47 
48     /* Find an existing entry of the specified type and point at
49        it, or NULL if not found */
50 
51     for (tl_data = entry->tl_data; tl_data; tl_data = tl_data->tl_data_next)
52         if (tl_data->tl_data_type == new_tl_data->tl_data_type)
53             break;
54 
55     /* if necessary, chain a new record in the beginning and point at it */
56 
57     if (!tl_data) {
58         if ((tl_data = (krb5_tl_data *) calloc(1, sizeof(krb5_tl_data)))
59             == NULL) {
60             free(tmp);
61             return(ENOMEM);
62         }
63         tl_data->tl_data_next = entry->tl_data;
64         entry->tl_data = tl_data;
65         entry->n_tl_data++;
66     }
67 
68     /* fill in the record */
69 
70     if (tl_data->tl_data_contents)
71         free(tl_data->tl_data_contents);
72 
73     tl_data->tl_data_type = new_tl_data->tl_data_type;
74     tl_data->tl_data_length = new_tl_data->tl_data_length;
75     tl_data->tl_data_contents = tmp;
76     memcpy(tmp, new_tl_data->tl_data_contents, tl_data->tl_data_length);
77 
78     return(0);
79 }
80 
81 krb5_error_code
krb5_dbe_lookup_tl_data(krb5_context context,krb5_db_entry * entry,krb5_tl_data * ret_tl_data)82 krb5_dbe_lookup_tl_data(krb5_context context, krb5_db_entry *entry,
83                         krb5_tl_data *ret_tl_data)
84 {
85     krb5_tl_data *tl_data;
86 
87     for (tl_data = entry->tl_data; tl_data; tl_data = tl_data->tl_data_next) {
88         if (tl_data->tl_data_type == ret_tl_data->tl_data_type) {
89             *ret_tl_data = *tl_data;
90             return(0);
91         }
92     }
93 
94     /* if the requested record isn't found, return zero bytes.
95        if it ever means something to have a zero-length tl_data,
96        this code and its callers will have to be changed */
97 
98     ret_tl_data->tl_data_length = 0;
99     ret_tl_data->tl_data_contents = NULL;
100     return(0);
101 }
102 
103 krb5_error_code
krb5_dbe_update_last_pwd_change(krb5_context context,krb5_db_entry * entry,krb5_timestamp stamp)104 krb5_dbe_update_last_pwd_change(krb5_context context, krb5_db_entry *entry,
105                                 krb5_timestamp stamp)
106 {
107     krb5_tl_data        tl_data;
108     krb5_octet          buf[4]; /* this is the encoded size of an int32 */
109 
110     tl_data.tl_data_type = KRB5_TL_LAST_PWD_CHANGE;
111     tl_data.tl_data_length = sizeof(buf);
112     krb5_kdb_encode_int32((krb5_int32) stamp, buf);
113     tl_data.tl_data_contents = buf;
114 
115     return(krb5_dbe_update_tl_data(context, entry, &tl_data));
116 }
117 
118 krb5_error_code
krb5_dbe_lookup_last_pwd_change(krb5_context context,krb5_db_entry * entry,krb5_timestamp * stamp)119 krb5_dbe_lookup_last_pwd_change(krb5_context context, krb5_db_entry *entry,
120                                 krb5_timestamp *stamp)
121 {
122     krb5_tl_data        tl_data;
123     krb5_error_code     code;
124     krb5_int32          tmp;
125 
126     tl_data.tl_data_type = KRB5_TL_LAST_PWD_CHANGE;
127 
128     if ((code = krb5_dbe_lookup_tl_data(context, entry, &tl_data)))
129         return(code);
130 
131     if (tl_data.tl_data_length != 4) {
132         *stamp = 0;
133         return(0);
134     }
135 
136     krb5_kdb_decode_int32(tl_data.tl_data_contents, tmp);
137 
138     *stamp = (krb5_timestamp) tmp;
139 
140     return(0);
141 }
142