1178825Sdfr/*
2233294Sstas * Copyright (c) 1997-2005 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
5178825Sdfr *
6233294Sstas * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7178825Sdfr *
8233294Sstas * Redistribution and use in source and binary forms, with or without
9233294Sstas * modification, are permitted provided that the following conditions
10233294Sstas * are met:
11178825Sdfr *
12233294Sstas * 1. Redistributions of source code must retain the above copyright
13233294Sstas *    notice, this list of conditions and the following disclaimer.
14178825Sdfr *
15233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
16233294Sstas *    notice, this list of conditions and the following disclaimer in the
17233294Sstas *    documentation and/or other materials provided with the distribution.
18178825Sdfr *
19233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
20233294Sstas *    may be used to endorse or promote products derived from this software
21233294Sstas *    without specific prior written permission.
22233294Sstas *
23233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33233294Sstas * SUCH DAMAGE.
34178825Sdfr */
35178825Sdfr
36178825Sdfr#include "kcm_locl.h"
37178825Sdfr
38233294Sstasvoid
39233294Sstaskcm_service(void *ctx, const heim_idata *req,
40233294Sstas	    const heim_icred cred,
41233294Sstas	    heim_ipc_complete complete,
42233294Sstas	    heim_sipc_call cctx)
43233294Sstas{
44233294Sstas    kcm_client peercred;
45233294Sstas    krb5_error_code ret;
46233294Sstas    krb5_data request, rep;
47178825Sdfr    unsigned char *buf;
48178825Sdfr    size_t len;
49178825Sdfr
50233294Sstas    krb5_data_zero(&rep);
51178825Sdfr
52233294Sstas    peercred.uid = heim_ipc_cred_get_uid(cred);
53233294Sstas    peercred.gid = heim_ipc_cred_get_gid(cred);
54233294Sstas    peercred.pid = heim_ipc_cred_get_pid(cred);
55233294Sstas    peercred.session = heim_ipc_cred_get_session(cred);
56178825Sdfr
57233294Sstas    if (req->length < 4) {
58233294Sstas	kcm_log(1, "malformed request from process %d (too short)",
59233294Sstas		peercred.pid);
60233294Sstas	(*complete)(cctx, EINVAL, NULL);
61178825Sdfr	return;
62178825Sdfr    }
63178825Sdfr
64233294Sstas    buf = req->data;
65233294Sstas    len = req->length;
66178825Sdfr
67178825Sdfr    if (buf[0] != KCM_PROTOCOL_VERSION_MAJOR ||
68178825Sdfr	buf[1] != KCM_PROTOCOL_VERSION_MINOR) {
69178825Sdfr	kcm_log(1, "incorrect protocol version %d.%d from process %d",
70233294Sstas		buf[0], buf[1], peercred.pid);
71233294Sstas	(*complete)(cctx, EINVAL, NULL);
72233294Sstas	return;
73178825Sdfr    }
74178825Sdfr
75233294Sstas    request.data = buf + 2;
76233294Sstas    request.length = len - 2;
77178825Sdfr
78178825Sdfr    /* buf is now pointing at opcode */
79178825Sdfr
80233294Sstas    ret = kcm_dispatch(kcm_context, &peercred, &request, &rep);
81178825Sdfr
82233294Sstas    (*complete)(cctx, ret, &rep);
83233294Sstas    krb5_data_free(&rep);
84178825Sdfr}
85