xref: /titanic_50/usr/src/lib/smbsrv/libsmb/common/smb_kmod.c (revision cc4ec4394cda0c382f50cf9d771b6fcdeffa8c8d)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/ioccom.h>
29 #include <sys/param.h>
30 #include <stddef.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 
39 #include <smbsrv/smb_xdr.h>
40 #include <smbsrv/smbinfo.h>
41 #include <smbsrv/smb_ioctl.h>
42 #include <smbsrv/smb_ioctl.h>
43 #include <smbsrv/libsmb.h>
44 
45 #define	SMBDRV_DEVICE_PATH		"/dev/smbsrv"
46 #define	SMB_IOC_DATA_SIZE		(256 * 1024)
47 
48 static int smb_kmod_ioctl(int, smb_ioc_header_t *, uint32_t);
49 
50 
51 int	smbdrv_fd = -1;
52 
53 int
54 smb_kmod_bind(void)
55 {
56 	if (smbdrv_fd != -1)
57 		(void) close(smbdrv_fd);
58 
59 	if ((smbdrv_fd = open(SMBDRV_DEVICE_PATH, 0)) < 0) {
60 		smbdrv_fd = -1;
61 		return (errno);
62 	}
63 
64 	return (0);
65 }
66 
67 boolean_t
68 smb_kmod_isbound(void)
69 {
70 	return ((smbdrv_fd == -1) ? B_FALSE : B_TRUE);
71 }
72 
73 int
74 smb_kmod_setcfg(smb_kmod_cfg_t *cfg)
75 {
76 	smb_ioc_cfg_t ioc;
77 
78 	ioc.maxworkers = cfg->skc_maxworkers;
79 	ioc.maxconnections = cfg->skc_maxconnections;
80 	ioc.keepalive = cfg->skc_keepalive;
81 	ioc.restrict_anon = cfg->skc_restrict_anon;
82 	ioc.signing_enable = cfg->skc_signing_enable;
83 	ioc.signing_required = cfg->skc_signing_required;
84 	ioc.oplock_enable = cfg->skc_oplock_enable;
85 	ioc.sync_enable = cfg->skc_sync_enable;
86 	ioc.secmode = cfg->skc_secmode;
87 	ioc.ipv6_enable = cfg->skc_ipv6_enable;
88 	ioc.print_enable = cfg->skc_print_enable;
89 	ioc.exec_flags = cfg->skc_execflags;
90 	ioc.version = cfg->skc_version;
91 
92 	(void) strlcpy(ioc.nbdomain, cfg->skc_nbdomain, sizeof (ioc.nbdomain));
93 	(void) strlcpy(ioc.fqdn, cfg->skc_fqdn, sizeof (ioc.fqdn));
94 	(void) strlcpy(ioc.hostname, cfg->skc_hostname, sizeof (ioc.hostname));
95 	(void) strlcpy(ioc.system_comment, cfg->skc_system_comment,
96 	    sizeof (ioc.system_comment));
97 
98 	return (smb_kmod_ioctl(SMB_IOC_CONFIG, &ioc.hdr, sizeof (ioc)));
99 }
100 
101 int
102 smb_kmod_setgmtoff(int32_t gmtoff)
103 {
104 	smb_ioc_gmt_t ioc;
105 
106 	ioc.offset = gmtoff;
107 	return (smb_kmod_ioctl(SMB_IOC_GMTOFF, &ioc.hdr,
108 	    sizeof (ioc)));
109 }
110 
111 int
112 smb_kmod_start(int opipe, int lmshr, int udoor)
113 {
114 	smb_ioc_start_t ioc;
115 
116 	ioc.opipe = opipe;
117 	ioc.lmshrd = lmshr;
118 	ioc.udoor = udoor;
119 	return (smb_kmod_ioctl(SMB_IOC_START, &ioc.hdr, sizeof (ioc)));
120 }
121 
122 void
123 smb_kmod_stop(void)
124 {
125 	smb_ioc_header_t ioc;
126 
127 	(void) smb_kmod_ioctl(SMB_IOC_STOP, &ioc, sizeof (ioc));
128 }
129 
130 int
131 smb_kmod_event_notify(uint32_t txid)
132 {
133 	smb_ioc_event_t ioc;
134 
135 	ioc.txid = txid;
136 	return (smb_kmod_ioctl(SMB_IOC_EVENT, &ioc.hdr, sizeof (ioc)));
137 }
138 
139 int
140 smb_kmod_share(nvlist_t *shrlist)
141 {
142 	smb_ioc_share_t *ioc;
143 	uint32_t ioclen;
144 	char *shrbuf = NULL;
145 	size_t bufsz;
146 	int rc = ENOMEM;
147 
148 	if ((rc = nvlist_pack(shrlist, &shrbuf, &bufsz, NV_ENCODE_XDR, 0)) != 0)
149 		return (rc);
150 
151 	ioclen = sizeof (smb_ioc_share_t) + bufsz;
152 
153 	if ((ioc = malloc(ioclen)) != NULL) {
154 		ioc->shrlen = bufsz;
155 		bcopy(shrbuf, ioc->shr, bufsz);
156 		rc = smb_kmod_ioctl(SMB_IOC_SHARE, &ioc->hdr, ioclen);
157 		free(ioc);
158 	}
159 
160 	free(shrbuf);
161 	return (rc);
162 }
163 
164 int
165 smb_kmod_unshare(nvlist_t *shrlist)
166 {
167 	smb_ioc_share_t *ioc;
168 	uint32_t ioclen;
169 	char *shrbuf = NULL;
170 	size_t bufsz;
171 	int rc = ENOMEM;
172 
173 	if ((rc = nvlist_pack(shrlist, &shrbuf, &bufsz, NV_ENCODE_XDR, 0)) != 0)
174 		return (rc);
175 
176 	ioclen = sizeof (smb_ioc_share_t) + bufsz;
177 
178 	if ((ioc = malloc(ioclen)) != NULL) {
179 		ioc->shrlen = bufsz;
180 		bcopy(shrbuf, ioc->shr, bufsz);
181 		rc = smb_kmod_ioctl(SMB_IOC_UNSHARE, &ioc->hdr, ioclen);
182 		free(ioc);
183 	}
184 
185 	free(shrbuf);
186 	return (rc);
187 }
188 
189 int
190 smb_kmod_shareinfo(char *shrname, boolean_t *shortnames)
191 {
192 	smb_ioc_shareinfo_t ioc;
193 	int rc;
194 
195 	bzero(&ioc, sizeof (ioc));
196 	(void) strlcpy(ioc.shrname, shrname, MAXNAMELEN);
197 
198 	rc = smb_kmod_ioctl(SMB_IOC_SHAREINFO, &ioc.hdr, sizeof (ioc));
199 	if (rc == 0)
200 		*shortnames = ioc.shortnames;
201 	else
202 		*shortnames = B_TRUE;
203 
204 	return (rc);
205 }
206 
207 int
208 smb_kmod_get_open_num(smb_opennum_t *opennum)
209 {
210 	smb_ioc_opennum_t ioc;
211 	int rc;
212 
213 	bzero(&ioc, sizeof (ioc));
214 	ioc.qualtype = opennum->qualtype;
215 	(void) strlcpy(ioc.qualifier, opennum->qualifier, MAXNAMELEN);
216 
217 	rc = smb_kmod_ioctl(SMB_IOC_NUMOPEN, &ioc.hdr, sizeof (ioc));
218 	if (rc == 0) {
219 		opennum->open_users = ioc.open_users;
220 		opennum->open_trees = ioc.open_trees;
221 		opennum->open_files = ioc.open_files;
222 	}
223 
224 	return (rc);
225 }
226 
227 int
228 smb_kmod_get_spool_doc(uint32_t *spool_num, char *username,
229     char *path, smb_inaddr_t *ipaddr)
230 {
231 	smb_ioc_spooldoc_t ioc;
232 	int rc;
233 
234 	bzero(&ioc, sizeof (ioc));
235 	rc = smb_kmod_ioctl(SMB_IOC_SPOOLDOC, &ioc.hdr, sizeof (ioc));
236 	if (rc == 0) {
237 		*spool_num = ioc.spool_num;
238 		(void) strlcpy(username, ioc.username, MAXNAMELEN);
239 		(void) strlcpy(path, ioc.path, MAXPATHLEN);
240 		*ipaddr = ioc.ipaddr;
241 	}
242 	return (rc);
243 }
244 
245 /*
246  * Initialization for an smb_kmod_enum request.  If this call succeeds,
247  * smb_kmod_enum_fini() must be called later to deallocate resources.
248  */
249 smb_netsvc_t *
250 smb_kmod_enum_init(smb_svcenum_t *request)
251 {
252 	smb_netsvc_t		*ns;
253 	smb_svcenum_t		*svcenum;
254 	smb_ioc_svcenum_t	*ioc;
255 	uint32_t		ioclen;
256 
257 	if ((ns = calloc(1, sizeof (smb_netsvc_t))) == NULL)
258 		return (NULL);
259 
260 	ioclen = sizeof (smb_ioc_svcenum_t) + SMB_IOC_DATA_SIZE;
261 	if ((ioc = malloc(ioclen)) == NULL) {
262 		free(ns);
263 		return (NULL);
264 	}
265 
266 	bzero(ioc, ioclen);
267 	svcenum = &ioc->svcenum;
268 	svcenum->se_type   = request->se_type;
269 	svcenum->se_level  = request->se_level;
270 	svcenum->se_bavail = SMB_IOC_DATA_SIZE;
271 	svcenum->se_nlimit = request->se_nlimit;
272 	svcenum->se_nskip = request->se_nskip;
273 	svcenum->se_buflen = SMB_IOC_DATA_SIZE;
274 
275 	list_create(&ns->ns_list, sizeof (smb_netsvcitem_t),
276 	    offsetof(smb_netsvcitem_t, nsi_lnd));
277 
278 	ns->ns_ioc = ioc;
279 	ns->ns_ioclen = ioclen;
280 	return (ns);
281 }
282 
283 /*
284  * Cleanup resources allocated via smb_kmod_enum_init and smb_kmod_enum.
285  */
286 void
287 smb_kmod_enum_fini(smb_netsvc_t *ns)
288 {
289 	list_t			*lst;
290 	smb_netsvcitem_t	*item;
291 	smb_netuserinfo_t	*user;
292 	smb_netconnectinfo_t	*tree;
293 	smb_netfileinfo_t	*ofile;
294 	uint32_t		se_type;
295 
296 	if (ns == NULL)
297 		return;
298 
299 	lst = &ns->ns_list;
300 	se_type = ns->ns_ioc->svcenum.se_type;
301 
302 	while ((item = list_head(lst)) != NULL) {
303 		list_remove(lst, item);
304 
305 		switch (se_type) {
306 		case SMB_SVCENUM_TYPE_USER:
307 			user = &item->nsi_un.nsi_user;
308 			free(user->ui_domain);
309 			free(user->ui_account);
310 			free(user->ui_workstation);
311 			break;
312 		case SMB_SVCENUM_TYPE_TREE:
313 			tree = &item->nsi_un.nsi_tree;
314 			free(tree->ci_username);
315 			free(tree->ci_share);
316 			break;
317 		case SMB_SVCENUM_TYPE_FILE:
318 			ofile = &item->nsi_un.nsi_ofile;
319 			free(ofile->fi_path);
320 			free(ofile->fi_username);
321 			break;
322 		default:
323 			break;
324 		}
325 	}
326 
327 	list_destroy(&ns->ns_list);
328 	free(ns->ns_items);
329 	free(ns->ns_ioc);
330 	free(ns);
331 }
332 
333 /*
334  * Enumerate users, connections or files.
335  */
336 int
337 smb_kmod_enum(smb_netsvc_t *ns)
338 {
339 	smb_ioc_svcenum_t	*ioc;
340 	uint32_t		ioclen;
341 	smb_svcenum_t		*svcenum;
342 	smb_netsvcitem_t	*items;
343 	smb_netuserinfo_t	*user;
344 	smb_netconnectinfo_t	*tree;
345 	smb_netfileinfo_t	*ofile;
346 	uint8_t			*data;
347 	uint32_t		len;
348 	uint32_t		se_type;
349 	uint_t			nbytes;
350 	int			i;
351 	int			rc;
352 
353 	ioc = ns->ns_ioc;
354 	ioclen = ns->ns_ioclen;
355 	rc = smb_kmod_ioctl(SMB_IOC_SVCENUM, &ioc->hdr, ioclen);
356 	if (rc != 0)
357 		return (rc);
358 
359 	svcenum = &ioc->svcenum;
360 	items = calloc(svcenum->se_nitems, sizeof (smb_netsvcitem_t));
361 	if (items == NULL)
362 		return (ENOMEM);
363 
364 	ns->ns_items = items;
365 	se_type = ns->ns_ioc->svcenum.se_type;
366 	data = svcenum->se_buf;
367 	len = svcenum->se_bused;
368 
369 	for (i = 0; i < svcenum->se_nitems; ++i) {
370 		switch (se_type) {
371 		case SMB_SVCENUM_TYPE_USER:
372 			user = &items->nsi_un.nsi_user;
373 			rc = smb_netuserinfo_decode(user, data, len, &nbytes);
374 			break;
375 		case SMB_SVCENUM_TYPE_TREE:
376 			tree = &items->nsi_un.nsi_tree;
377 			rc = smb_netconnectinfo_decode(tree, data, len,
378 			    &nbytes);
379 			break;
380 		case SMB_SVCENUM_TYPE_FILE:
381 			ofile = &items->nsi_un.nsi_ofile;
382 			rc = smb_netfileinfo_decode(ofile, data, len, &nbytes);
383 			break;
384 		default:
385 			rc = -1;
386 			break;
387 		}
388 
389 		if (rc != 0)
390 			return (EINVAL);
391 
392 		list_insert_tail(&ns->ns_list, items);
393 
394 		++items;
395 		data += nbytes;
396 		len -= nbytes;
397 	}
398 
399 	return (0);
400 }
401 
402 /*
403  * A NULL pointer is a wildcard indicator, which we pass on
404  * as an empty string (by virtue of the bzero).
405  */
406 int
407 smb_kmod_session_close(const char *client, const char *username)
408 {
409 	smb_ioc_session_t ioc;
410 	int rc;
411 
412 	bzero(&ioc, sizeof (ioc));
413 
414 	if (client != NULL)
415 		(void) strlcpy(ioc.client, client, MAXNAMELEN);
416 	if (username != NULL)
417 		(void) strlcpy(ioc.username, username, MAXNAMELEN);
418 
419 	rc = smb_kmod_ioctl(SMB_IOC_SESSION_CLOSE, &ioc.hdr, sizeof (ioc));
420 	return (rc);
421 }
422 
423 int
424 smb_kmod_file_close(uint32_t uniqid)
425 {
426 	smb_ioc_fileid_t ioc;
427 	int rc;
428 
429 	bzero(&ioc, sizeof (ioc));
430 	ioc.uniqid = uniqid;
431 
432 	rc = smb_kmod_ioctl(SMB_IOC_FILE_CLOSE, &ioc.hdr, sizeof (ioc));
433 	return (rc);
434 }
435 
436 void
437 smb_kmod_unbind(void)
438 {
439 	if (smbdrv_fd != -1) {
440 		(void) close(smbdrv_fd);
441 		smbdrv_fd = -1;
442 	}
443 }
444 
445 static int
446 smb_kmod_ioctl(int cmd, smb_ioc_header_t *ioc, uint32_t len)
447 {
448 	int rc = EINVAL;
449 
450 	ioc->version = SMB_IOC_VERSION;
451 	ioc->cmd = cmd;
452 	ioc->len = len;
453 	ioc->crc = 0;
454 	ioc->crc = smb_crc_gen((uint8_t *)ioc, sizeof (smb_ioc_header_t));
455 
456 	if (smbdrv_fd != -1) {
457 		if (ioctl(smbdrv_fd, cmd, ioc) < 0)
458 			rc = errno;
459 		else
460 			rc = 0;
461 	}
462 	return (rc);
463 }
464