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