xref: /freebsd/crypto/krb5/src/ccapi/common/cci_message.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* ccapi/common/cci_message.c */
2 /*
3  * Copyright 2006 Massachusetts Institute of Technology.
4  * All Rights Reserved.
5  *
6  * Export of this software from the United States of America may
7  * require a specific license from the United States Government.
8  * It is the responsibility of any person or organization contemplating
9  * export to obtain such a license before exporting.
10  *
11  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12  * distribute this software and its documentation for any purpose and
13  * without fee is hereby granted, provided that the above copyright
14  * notice appear in all copies and that both that copyright notice and
15  * this permission notice appear in supporting documentation, and that
16  * the name of M.I.T. not be used in advertising or publicity pertaining
17  * to distribution of the software without specific, written prior
18  * permission.  Furthermore if you modify this software you must label
19  * your software as modified software and not distribute it in such a
20  * fashion that it might be confused with the original M.I.T. software.
21  * M.I.T. makes no representations about the suitability of
22  * this software for any purpose.  It is provided "as is" without express
23  * or implied warranty.
24  */
25 
26 #include "cci_common.h"
27 
28 /* ------------------------------------------------------------------------ */
29 
cci_message_invalid_object_err(enum cci_msg_id_t in_request_name)30 cc_int32 cci_message_invalid_object_err (enum cci_msg_id_t in_request_name)
31 {
32     cc_int32 err = ccNoError;
33 
34     if (in_request_name > cci_context_first_msg_id &&
35         in_request_name < cci_context_last_msg_id) {
36         err = ccErrInvalidContext;
37 
38     } else if (in_request_name > cci_ccache_first_msg_id &&
39                in_request_name < cci_ccache_last_msg_id) {
40         err = ccErrInvalidCCache;
41 
42     } else if (in_request_name > cci_ccache_iterator_first_msg_id &&
43                in_request_name < cci_ccache_iterator_last_msg_id) {
44         err = ccErrInvalidCCacheIterator;
45 
46     } else if (in_request_name > cci_credentials_iterator_first_msg_id &&
47                in_request_name < cci_credentials_iterator_last_msg_id) {
48         err = ccErrInvalidCredentialsIterator;
49 
50     } else {
51         err = ccErrBadInternalMessage;
52     }
53 
54     return cci_check_error (err);
55 }
56 
57 /* ------------------------------------------------------------------------ */
58 
cci_message_new_request_header(k5_ipc_stream * out_request,enum cci_msg_id_t in_request_name,cci_identifier_t in_identifier)59 cc_int32 cci_message_new_request_header (k5_ipc_stream      *out_request,
60                                          enum cci_msg_id_t  in_request_name,
61                                          cci_identifier_t   in_identifier)
62 {
63     cc_int32 err = ccNoError;
64     k5_ipc_stream request = NULL;
65 
66     if (!out_request) { err = cci_check_error (ccErrBadParam); }
67 
68     if (!err) {
69         err = krb5int_ipc_stream_new (&request);
70     }
71 
72     if (!err) {
73         err = krb5int_ipc_stream_write_uint32 (request, in_request_name);
74     }
75 
76     if (!err) {
77         err = cci_identifier_write (in_identifier, request);
78     }
79 
80     if (!err) {
81         *out_request = request;
82         request = NULL;
83     }
84 
85     krb5int_ipc_stream_release (request);
86 
87     return cci_check_error (err);
88 }
89 
90 /* ------------------------------------------------------------------------ */
91 
cci_message_read_request_header(k5_ipc_stream in_request,enum cci_msg_id_t * out_request_name,cci_identifier_t * out_identifier)92 cc_int32 cci_message_read_request_header (k5_ipc_stream       in_request,
93                                           enum cci_msg_id_t *out_request_name,
94                                           cci_identifier_t  *out_identifier)
95 {
96     cc_int32 err = ccNoError;
97     cc_uint32 request_name;
98     cci_identifier_t identifier = NULL;
99 
100     if (!in_request      ) { err = cci_check_error (ccErrBadParam); }
101     if (!out_request_name) { err = cci_check_error (ccErrBadParam); }
102     if (!out_identifier  ) { err = cci_check_error (ccErrBadParam); }
103 
104     if (!err) {
105         err = krb5int_ipc_stream_read_uint32 (in_request, &request_name);
106     }
107 
108     if (!err) {
109         err = cci_identifier_read (&identifier, in_request);
110     }
111 
112     if (!err) {
113         *out_request_name = request_name;
114         *out_identifier = identifier;
115         identifier = NULL; /* take ownership */
116     }
117 
118     cci_identifier_release (identifier);
119 
120     return cci_check_error (err);
121 }
122 
123 /* ------------------------------------------------------------------------ */
124 
cci_message_new_reply_header(k5_ipc_stream * out_reply,cc_int32 in_error)125 cc_int32 cci_message_new_reply_header (k5_ipc_stream     *out_reply,
126                                        cc_int32          in_error)
127 {
128     cc_int32 err = ccNoError;
129     k5_ipc_stream reply = NULL;
130 
131     if (!out_reply) { err = cci_check_error (ccErrBadParam); }
132 
133     if (!err) {
134         err = krb5int_ipc_stream_new (&reply);
135     }
136 
137     if (!err) {
138         err = krb5int_ipc_stream_write_int32 (reply, in_error);
139     }
140 
141     if (!err) {
142         *out_reply = reply;
143         reply = NULL;
144     }
145 
146     krb5int_ipc_stream_release (reply);
147 
148     return cci_check_error (err);
149 }
150 
151 /* ------------------------------------------------------------------------ */
152 
cci_message_read_reply_header(k5_ipc_stream in_reply,cc_int32 * out_reply_error)153 cc_int32 cci_message_read_reply_header (k5_ipc_stream      in_reply,
154                                         cc_int32         *out_reply_error)
155 {
156     cc_int32 err = ccNoError;
157     cc_int32 reply_err = 0;
158 
159     if (!in_reply       ) { err = cci_check_error (ccErrBadParam); }
160     if (!out_reply_error) { err = cci_check_error (ccErrBadParam); }
161 
162     if (!err) {
163         err = krb5int_ipc_stream_read_int32 (in_reply, &reply_err);
164     }
165 
166     if (!err) {
167         *out_reply_error = reply_err;
168     }
169 
170     return cci_check_error (err);
171 }
172 
173 #ifdef TARGET_OS_MAC
174 #pragma mark -
175 #endif
176 
177 /* ------------------------------------------------------------------------ */
178 
krb5int_ipc_stream_read_time(k5_ipc_stream io_stream,cc_time_t * out_time)179 uint32_t krb5int_ipc_stream_read_time (k5_ipc_stream  io_stream,
180                                   cc_time_t     *out_time)
181 {
182     int32_t err = 0;
183     int64_t t = 0;
184 
185     if (!io_stream) { err = cci_check_error (ccErrBadParam); }
186     if (!out_time ) { err = cci_check_error (ccErrBadParam); }
187 
188     if (!err) {
189         err = krb5int_ipc_stream_read_int64 (io_stream, &t);
190     }
191 
192     if (!err) {
193         *out_time = t;
194     }
195 
196     return cci_check_error (err);
197 }
198 
199 /* ------------------------------------------------------------------------ */
200 
krb5int_ipc_stream_write_time(k5_ipc_stream io_stream,cc_time_t in_time)201 uint32_t krb5int_ipc_stream_write_time (k5_ipc_stream io_stream,
202 					cc_time_t     in_time)
203 {
204     int32_t err = 0;
205 
206     if (!io_stream) { err = cci_check_error (ccErrBadParam); }
207 
208     if (!err) {
209         err = krb5int_ipc_stream_write_int64 (io_stream, in_time);
210     }
211 
212     return cci_check_error (err);
213 }
214