1/*
2 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include "kcm_locl.h"
37
38void
39kcm_service(void *ctx, const heim_idata *req,
40	    const heim_icred cred,
41	    heim_ipc_complete complete,
42	    heim_sipc_call cctx)
43{
44    kcm_client peercred;
45    krb5_error_code ret;
46    krb5_data request, rep;
47    unsigned char *buf;
48    size_t len;
49
50    krb5_data_zero(&rep);
51
52    peercred.uid = heim_ipc_cred_get_uid(cred);
53    peercred.gid = heim_ipc_cred_get_gid(cred);
54    peercred.pid = heim_ipc_cred_get_pid(cred);
55    peercred.session = heim_ipc_cred_get_session(cred);
56
57    if (req->length < 4) {
58	kcm_log(1, "malformed request from process %d (too short)",
59		peercred.pid);
60	(*complete)(cctx, EINVAL, NULL);
61	return;
62    }
63
64    buf = req->data;
65    len = req->length;
66
67    if (buf[0] != KCM_PROTOCOL_VERSION_MAJOR ||
68	buf[1] != KCM_PROTOCOL_VERSION_MINOR) {
69	kcm_log(1, "incorrect protocol version %d.%d from process %d",
70		buf[0], buf[1], peercred.pid);
71	(*complete)(cctx, EINVAL, NULL);
72	return;
73    }
74
75    request.data = buf + 2;
76    request.length = len - 2;
77
78    /* buf is now pointing at opcode */
79
80    ret = kcm_dispatch(kcm_context, &peercred, &request, &rep);
81
82    (*complete)(cctx, ret, &rep);
83    krb5_data_free(&rep);
84}
85