5917 User-mode SMB server
[unleashed.git] / usr / src / cmd / smbsrv / fksmbd / fksmbd_opipe.c
blob7fd8df64d3fa7b2f71998fcbccf42bb0f6c05566
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/list.h>
26 #include <assert.h>
27 #include <alloca.h>
28 #include <door.h>
29 #include <errno.h>
30 #include <syslog.h>
31 #include <unistd.h>
32 #include <stdio.h>
33 #include <synch.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <pthread.h>
39 #include <strings.h>
40 #include <umem.h>
42 #include <smbsrv/smb_door.h>
43 #include <smbsrv/smb_xdr.h>
44 #include <smbsrv/smb_token.h>
45 #include <smbsrv/libmlsvc.h>
46 #include <smbsrv/libsmbns.h>
47 #include "smbd.h"
49 static int smbd_opipe_exec(uint32_t fid);
53 * Process smbd opipe requests.
55 * This is a special version of smb_opipe_dispatch()
56 * for the "fake" smbsrv (running in user space).
57 * This is called via function pointer from
58 * smbsrv: smb_opipe_door_call()
60 * Very similar to smbd_opipe_dispatch()
62 int
63 fksmbd_opipe_dispatch(door_arg_t *da)
65 uint8_t *buf = (uint8_t *)da->data_ptr;
66 smb_doorhdr_t hdr;
67 size_t hdr_size;
68 uint8_t *data;
69 uint32_t datalen;
71 if (!smbd_online())
72 return (-1);
74 bzero(&hdr, sizeof (smb_doorhdr_t));
75 hdr_size = xdr_sizeof(smb_doorhdr_xdr, &hdr);
77 if (da->data_ptr == NULL || da->data_size < hdr_size)
78 return (-1);
80 if (smb_doorhdr_decode(&hdr, buf, hdr_size) == -1)
81 return (-1);
83 if ((hdr.dh_magic != SMB_OPIPE_HDR_MAGIC) || (hdr.dh_fid == 0))
84 return (-1);
86 if (hdr.dh_datalen > SMB_OPIPE_DOOR_BUFSIZE)
87 hdr.dh_datalen = SMB_OPIPE_DOOR_BUFSIZE;
89 data = buf + hdr_size;
90 datalen = hdr.dh_datalen;
92 switch (hdr.dh_op) {
93 case SMB_OPIPE_OPEN:
94 hdr.dh_door_rc = ndr_pipe_open(hdr.dh_fid, data, datalen);
95 hdr.dh_datalen = 0;
96 hdr.dh_resid = 0;
97 datalen = hdr_size;
98 break;
100 case SMB_OPIPE_CLOSE:
101 hdr.dh_door_rc = ndr_pipe_close(hdr.dh_fid);
102 hdr.dh_datalen = 0;
103 hdr.dh_resid = 0;
104 datalen = hdr_size;
105 break;
107 case SMB_OPIPE_READ:
108 data = (uint8_t *)buf + hdr_size;
109 datalen = hdr.dh_datalen;
110 hdr.dh_door_rc = ndr_pipe_read(hdr.dh_fid, data, &datalen,
111 &hdr.dh_resid);
112 hdr.dh_datalen = datalen;
113 datalen += hdr_size;
114 break;
116 case SMB_OPIPE_WRITE:
117 hdr.dh_door_rc = ndr_pipe_write(hdr.dh_fid, data, datalen);
118 hdr.dh_datalen = 0;
119 hdr.dh_resid = 0;
120 datalen = hdr_size;
121 break;
123 case SMB_OPIPE_EXEC:
124 hdr.dh_door_rc = smbd_opipe_exec(hdr.dh_fid);
125 hdr.dh_datalen = 0;
126 hdr.dh_resid = 0;
127 datalen = hdr_size;
128 break;
130 default:
131 return (-1);
134 (void) smb_doorhdr_encode(&hdr, (uint8_t *)buf, hdr_size);
135 return (0);
139 * Normal (from a real kernel) up calls get a thread here.
140 * In the "fake" kernel (all user space) we don't need that.
141 * NB: arg will be freed by ndr_pipe_transact()
143 static int
144 smbd_opipe_exec(uint32_t fid)
146 uint32_t *arg;
148 if ((arg = malloc(sizeof (uint32_t))) == NULL)
149 return (ENOMEM);
151 *arg = fid;
153 (void) ndr_pipe_transact(arg);
155 return (0);