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