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