summaryrefslogtreecommitdiffstats
path: root/src/ccapi/lib/mac/ccapi_os_ipc.c
blob: bde357e16bba7e8297fdbf02361853948d3c97b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * $Header$
 *
 * Copyright 2006 Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 * require a specific license from the United States Government.
 * It is the responsibility of any person or organization contemplating
 * export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */

#include "ccapi_os_ipc.h"

#include <Kerberos/kipc_client.h>
#include "cci_mig_request.h"
#include "cci_mig_replyServer.h"
#include "k5-thread.h"

#define cci_server_bundle_id "edu.mit.Kerberos.CCacheServer"

/* ------------------------------------------------------------------------ */

cc_int32 cci_os_ipc_thread_init (void)
{
    cc_int32 err = ccNoError;

    err = k5_key_register (K5_KEY_CCAPI_REQUEST_PORT, free);
    
    if (!err) {
        err = k5_key_register (K5_KEY_CCAPI_REPLY_STREAM, NULL);
    }
    
    if (!err) {
        err = k5_key_register (K5_KEY_CCAPI_SERVER_DIED, NULL);
    }
    
    return err;
}

/* ------------------------------------------------------------------------ */

void cci_os_ipc_thread_fini (void)
{    
    k5_key_delete (K5_KEY_CCAPI_REQUEST_PORT);
    k5_key_delete (K5_KEY_CCAPI_REPLY_STREAM);
    k5_key_delete (K5_KEY_CCAPI_SERVER_DIED);
}

#pragma mark -

/* ------------------------------------------------------------------------ */

static boolean_t cci_server_demux (mach_msg_header_t *request, 
                                   mach_msg_header_t *reply) 
{
    boolean_t handled = false;
    
    if (!handled && request->msgh_id == MACH_NOTIFY_NO_SENDERS) {
        cc_int32 *server_died = k5_getspecific (K5_KEY_CCAPI_SERVER_DIED);
        if (!server_died) { 
            *server_died = 1;
        }
        
        handled = 1; /* server died */
    }
    
    if (!handled) {
        handled = cci_server (request, reply);
    }
    
    return handled;    
}

/* ------------------------------------------------------------------------ */

kern_return_t cci_mipc_reply (mach_port_t             in_reply_port,
                              cci_mipc_inl_reply_t    in_inl_reply,
                              mach_msg_type_number_t  in_inl_replyCnt,
                              cci_mipc_ool_reply_t    in_ool_reply,
                              mach_msg_type_number_t  in_ool_replyCnt)
{
    kern_return_t err = KERN_SUCCESS;
    cci_stream_t reply_stream = NULL;
    
    if (!err) {
        reply_stream = k5_getspecific (K5_KEY_CCAPI_REPLY_STREAM);
        if (!reply_stream) { err = cci_check_error (ccErrBadInternalMessage); }
    }
    
    if (!err) {
        if (in_inl_replyCnt) {
            err = cci_stream_write (reply_stream, in_inl_reply, in_inl_replyCnt);
            
        } else if (in_ool_replyCnt) {
            err = cci_stream_write (reply_stream, in_ool_reply, in_ool_replyCnt);
            
        } else {
            err = cci_check_error (ccErrBadInternalMessage);
        }
    }
    
    if (in_ool_replyCnt) { vm_deallocate (mach_task_self (), (vm_address_t) in_ool_reply, in_ool_replyCnt); }
    
    return cci_check_error (err);
}

#pragma mark -

/* ------------------------------------------------------------------------ */

cc_int32 cci_os_ipc (cc_int32      in_launch_server,
                     cci_stream_t  in_request_stream,
                     cci_stream_t *out_reply_stream)
{
    cc_int32 err = ccNoError;
    cc_int32 done = 0;
    cc_int32 try_count = 0;
    cc_int32 server_died = 0;
    mach_port_t server_port = MACH_PORT_NULL;
    mach_port_t *request_port = NULL;
    mach_port_t reply_port = MACH_PORT_NULL;
    const char *inl_request = NULL; /* char * so we can pass the buffer in directly */
    mach_msg_type_number_t inl_request_length = 0;
    cci_mipc_ool_request_t ool_request = NULL;
    mach_msg_type_number_t ool_request_length = 0;
    cci_stream_t reply_stream = NULL;
    
    if (!in_request_stream) { err = cci_check_error (ccErrBadParam); }
    if (!out_reply_stream ) { err = cci_check_error (ccErrBadParam); }
    
    if (!err) {
        /* depending on how big the message is, use the fast inline buffer or  
         * the slow dynamically allocated buffer */
        mach_msg_type_number_t request_length = cci_stream_size (in_request_stream);
        
        if (request_length > kCCAPIMaxILMsgSize) {
            cci_debug_printf ("%s choosing out of line buffer (size is %d)", 
                              __FUNCTION__, request_length);
            
            err = vm_read (mach_task_self (), 
                           (vm_address_t) cci_stream_data (in_request_stream), request_length, 
                           (vm_address_t *) &ool_request, &ool_request_length);        
        } else {
            //cci_debug_printf ("%s choosing in line buffer (size is %d)",
            //                  __FUNCTION__, request_length);
            
            inl_request_length = request_length;
            inl_request = cci_stream_data (in_request_stream);
        }
    }

    if (!err) {
        request_port = k5_getspecific (K5_KEY_CCAPI_REQUEST_PORT);
        
        if (!request_port) {
            request_port = malloc (sizeof (mach_port_t));
            if (!request_port) { err = cci_check_error (ccErrNoMem); }
            
            if (!err) {
                *request_port = MACH_PORT_NULL;
                err = k5_setspecific (K5_KEY_CCAPI_REQUEST_PORT, request_port);
            }
        }
    }
    
    if (!err) {
        err = mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE, &reply_port);
    }

    if (!err) {
        err = kipc_client_lookup_server (cci_server_bundle_id, 
                                         in_launch_server, TRUE, &server_port);
    }
    
    while (!err && !done) {
        if (!err && !MACH_PORT_VALID (*request_port)) {
            err = cci_mipc_create_client_connection (server_port, request_port);
        }
        
        if (!err) {
            err = cci_mipc_request (*request_port, reply_port,
                                    inl_request, inl_request_length,
                                    ool_request, ool_request_length);
            
        }
        
        if (err == MACH_SEND_INVALID_DEST) {
            if (try_count < 2) { 
                try_count++;
                err = ccNoError;
            }

            if (request_port && MACH_PORT_VALID (*request_port)) {
                mach_port_mod_refs (mach_task_self(), *request_port, MACH_PORT_RIGHT_SEND, -1 );
                *request_port = MACH_PORT_NULL;
            }    
            
            /* Look up server name again without using the cached copy */
            err = kipc_client_lookup_server (cci_server_bundle_id,  
                                             in_launch_server, FALSE, &server_port);
            
        } else {
            /* Talked to server, though we may have gotten an error */
            done = 1;
            
            /* Because we use ",dealloc" ool_request will be freed by mach. 
            * Don't double free it. */
            ool_request = NULL; 
            ool_request_length = 0;                
        }            
    }
    
    if (!err) {
        err = cci_stream_new (&reply_stream);
    }
    
    if (!err) {
        err = k5_setspecific (K5_KEY_CCAPI_REPLY_STREAM, reply_stream);
    }
    
    if (!err) {
        err = k5_setspecific (K5_KEY_CCAPI_SERVER_DIED, &server_died);
    }
    
    if (!err) {
        mach_port_t old_notification_target = MACH_PORT_NULL;

        /* request no-senders notification so we can get a message when server dies */
        err = mach_port_request_notification (mach_task_self (), reply_port, 
                                              MACH_NOTIFY_NO_SENDERS, 1, reply_port, 
                                              MACH_MSG_TYPE_MAKE_SEND_ONCE, 
                                              &old_notification_target);
        
        if (!err && old_notification_target != MACH_PORT_NULL) {
            mach_port_deallocate (mach_task_self (), old_notification_target);
        }
    }
    
    if (!err) {
        err = mach_msg_server_once (cci_server_demux, kkipc_max_message_size, 
                                    reply_port, MACH_MSG_TIMEOUT_NONE);
    }
    
    if (!err && server_died) {
        err = cci_check_error (ccErrServerUnavailable);
    }
    
    if (err == BOOTSTRAP_UNKNOWN_SERVICE && !in_launch_server) {
        err = ccNoError;  /* If the server is not running just return an empty stream. */
    }
    
    if (!err) {
        *out_reply_stream = reply_stream;
        reply_stream = NULL;
    }
    
    k5_setspecific (K5_KEY_CCAPI_REPLY_STREAM, NULL);
    k5_setspecific (K5_KEY_CCAPI_SERVER_DIED, NULL);
    if (reply_port != MACH_PORT_NULL) { mach_port_destroy (mach_task_self (), reply_port); }
    if (ool_request_length          ) { vm_deallocate (mach_task_self (), (vm_address_t) ool_request, ool_request_length); }
    if (reply_stream                ) { cci_stream_release (reply_stream); }
    
    return cci_check_error (err);    
}