xref: /illumos-gate/usr/src/cmd/smbsrv/smbadm/smbadm.c (revision bbf21555)
1da6c28aaSamw /*
2da6c28aaSamw  * CDDL HEADER START
3da6c28aaSamw  *
4da6c28aaSamw  * The contents of this file are subject to the terms of the
5da6c28aaSamw  * Common Development and Distribution License (the "License").
6da6c28aaSamw  * You may not use this file except in compliance with the License.
7da6c28aaSamw  *
8da6c28aaSamw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aaSamw  * or http://www.opensolaris.org/os/licensing.
10da6c28aaSamw  * See the License for the specific language governing permissions
11da6c28aaSamw  * and limitations under the License.
12da6c28aaSamw  *
13da6c28aaSamw  * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aaSamw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aaSamw  * If applicable, add the following below this CDDL HEADER, with the
16da6c28aaSamw  * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aaSamw  * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aaSamw  *
19da6c28aaSamw  * CDDL HEADER END
20da6c28aaSamw  */
21da6c28aaSamw /*
22148c5f43SAlan Wright  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
230292c176SMatt Barden  * Copyright 2019 Nexenta by DDN, Inc. All rights reserved.
24da6c28aaSamw  */
25da6c28aaSamw 
26da6c28aaSamw /*
27da6c28aaSamw  * This module contains smbadm CLI which offers smb configuration
28da6c28aaSamw  * functionalities.
29da6c28aaSamw  */
308d7e4166Sjose borrego #include <errno.h>
318d7e4166Sjose borrego #include <err.h>
328d7e4166Sjose borrego #include <ctype.h>
33da6c28aaSamw #include <stdlib.h>
3496a62adaSjoyce mcintosh #include <unistd.h>
35da6c28aaSamw #include <stdio.h>
36da6c28aaSamw #include <syslog.h>
37da6c28aaSamw #include <strings.h>
38da6c28aaSamw #include <limits.h>
39da6c28aaSamw #include <getopt.h>
40da6c28aaSamw #include <libintl.h>
41da6c28aaSamw #include <zone.h>
4296a62adaSjoyce mcintosh #include <pwd.h>
43da6c28aaSamw #include <grp.h>
44da6c28aaSamw #include <libgen.h>
45c8ec8eeaSjose borrego #include <netinet/in.h>
4696a62adaSjoyce mcintosh #include <auth_attr.h>
4796a62adaSjoyce mcintosh #include <locale.h>
48da6c28aaSamw #include <smbsrv/libsmb.h>
49b3700b07SGordon Ross #include <smbsrv/libsmbns.h>
50da6c28aaSamw 
5196a62adaSjoyce mcintosh #if !defined(TEXT_DOMAIN)
5296a62adaSjoyce mcintosh #define	TEXT_DOMAIN "SYS_TEST"
5396a62adaSjoyce mcintosh #endif
5496a62adaSjoyce mcintosh 
55da6c28aaSamw typedef enum {
56da6c28aaSamw 	HELP_ADD_MEMBER,
57da6c28aaSamw 	HELP_CREATE,
58da6c28aaSamw 	HELP_DELETE,
59da6c28aaSamw 	HELP_DEL_MEMBER,
60da6c28aaSamw 	HELP_GET,
61da6c28aaSamw 	HELP_JOIN,
62da6c28aaSamw 	HELP_LIST,
6336a00406SGordon Ross 	HELP_LOOKUP,
64da6c28aaSamw 	HELP_RENAME,
65da6c28aaSamw 	HELP_SET,
66da6c28aaSamw 	HELP_SHOW,
673db3f65cSamw 	HELP_USER_DISABLE,
68ef4cfbfdSMatt Barden 	HELP_USER_ENABLE,
69ef4cfbfdSMatt Barden 	HELP_USER_DELETE
70da6c28aaSamw } smbadm_help_t;
71da6c28aaSamw 
7296a62adaSjoyce mcintosh #define	SMBADM_CMDF_NONE	0x00
733db3f65cSamw #define	SMBADM_CMDF_USER	0x01
743db3f65cSamw #define	SMBADM_CMDF_GROUP	0x02
753db3f65cSamw #define	SMBADM_CMDF_TYPEMASK	0x0F
76faa1795aSjb 
7736a00406SGordon Ross typedef enum {
7836a00406SGordon Ross 	SMBADM_GRP_ADDMEMBER = 0,
7936a00406SGordon Ross 	SMBADM_GRP_DELMEMBER,
8036a00406SGordon Ross } smbadm_grp_action_t;
8136a00406SGordon Ross 
82c8ec8eeaSjose borrego #define	SMBADM_ANSBUFSIZ	64
83c8ec8eeaSjose borrego 
84da6c28aaSamw typedef struct smbadm_cmdinfo {
85da6c28aaSamw 	char *name;
86da6c28aaSamw 	int (*func)(int, char **);
87da6c28aaSamw 	smbadm_help_t usage;
88faa1795aSjb 	uint32_t flags;
8996a62adaSjoyce mcintosh 	char *auth;
90da6c28aaSamw } smbadm_cmdinfo_t;
91da6c28aaSamw 
92da6c28aaSamw smbadm_cmdinfo_t *curcmd;
93da6c28aaSamw static char *progname;
94da6c28aaSamw 
9596a62adaSjoyce mcintosh #define	SMBADM_ACTION_AUTH	"solaris.smf.manage.smb"
9696a62adaSjoyce mcintosh #define	SMBADM_VALUE_AUTH	"solaris.smf.value.smb"
9796a62adaSjoyce mcintosh #define	SMBADM_BASIC_AUTH	"solaris.network.hosts.read"
9896a62adaSjoyce mcintosh 
9996a62adaSjoyce mcintosh static boolean_t smbadm_checkauth(const char *);
10096a62adaSjoyce mcintosh 
1018d7e4166Sjose borrego static void smbadm_usage(boolean_t);
102b3700b07SGordon Ross static int smbadm_join_workgroup(const char *, boolean_t);
103b3700b07SGordon Ross static int smbadm_join_domain(const char *, const char *, boolean_t);
1048d7e4166Sjose borrego static void smbadm_extract_domain(char *, char **, char **);
1058d7e4166Sjose borrego 
106da6c28aaSamw static int smbadm_join(int, char **);
107da6c28aaSamw static int smbadm_list(int, char **);
10836a00406SGordon Ross static int smbadm_lookup(int, char **);
10936a00406SGordon Ross static void smbadm_lookup_name(char *);
11036a00406SGordon Ross static void smbadm_lookup_sid(char *);
111da6c28aaSamw static int smbadm_group_create(int, char **);
112da6c28aaSamw static int smbadm_group_delete(int, char **);
113da6c28aaSamw static int smbadm_group_rename(int, char **);
114da6c28aaSamw static int smbadm_group_show(int, char **);
115fe1c642dSBill Krier static void smbadm_group_show_name(const char *, const char *);
116da6c28aaSamw static int smbadm_group_getprop(int, char **);
117da6c28aaSamw static int smbadm_group_setprop(int, char **);
118da6c28aaSamw static int smbadm_group_addmember(int, char **);
119da6c28aaSamw static int smbadm_group_delmember(int, char **);
12036a00406SGordon Ross static int smbadm_group_add_del_member(char *, char *, smbadm_grp_action_t);
12136a00406SGordon Ross 
122ef4cfbfdSMatt Barden static int smbadm_user_delete(int, char **);
123da6c28aaSamw static int smbadm_user_disable(int, char **);
124da6c28aaSamw static int smbadm_user_enable(int, char **);
125da6c28aaSamw 
126*bbf21555SRichard Lowe /* Please keep the order consistent with smbadm(8) man page */
127da6c28aaSamw static smbadm_cmdinfo_t smbadm_cmdtable[] =
128da6c28aaSamw {
129faa1795aSjb 	{ "create",		smbadm_group_create,	HELP_CREATE,
13096a62adaSjoyce mcintosh 		SMBADM_CMDF_GROUP,	SMBADM_ACTION_AUTH },
131faa1795aSjb 	{ "delete",		smbadm_group_delete,	HELP_DELETE,
13296a62adaSjoyce mcintosh 		SMBADM_CMDF_GROUP,	SMBADM_ACTION_AUTH },
133959eaf32SYuri Pankov 	{ "rename",		smbadm_group_rename,	HELP_RENAME,
134959eaf32SYuri Pankov 		SMBADM_CMDF_GROUP,	SMBADM_ACTION_AUTH },
135959eaf32SYuri Pankov 	{ "show",		smbadm_group_show,	HELP_SHOW,
136959eaf32SYuri Pankov 		SMBADM_CMDF_GROUP,	SMBADM_ACTION_AUTH },
137959eaf32SYuri Pankov 	{ "get",		smbadm_group_getprop,	HELP_GET,
138959eaf32SYuri Pankov 		SMBADM_CMDF_GROUP,	SMBADM_ACTION_AUTH },
139959eaf32SYuri Pankov 	{ "set",		smbadm_group_setprop,	HELP_SET,
140959eaf32SYuri Pankov 		SMBADM_CMDF_GROUP,	SMBADM_ACTION_AUTH },
141959eaf32SYuri Pankov 	{ "add-member",		smbadm_group_addmember,	HELP_ADD_MEMBER,
142959eaf32SYuri Pankov 		SMBADM_CMDF_GROUP,	SMBADM_ACTION_AUTH },
143959eaf32SYuri Pankov 	{ "remove-member",	smbadm_group_delmember,	HELP_DEL_MEMBER,
144959eaf32SYuri Pankov 		SMBADM_CMDF_GROUP,	SMBADM_ACTION_AUTH },
145ef4cfbfdSMatt Barden 	{ "delete-user",	smbadm_user_delete,	HELP_USER_DELETE,
146ef4cfbfdSMatt Barden 		SMBADM_CMDF_USER,	SMBADM_ACTION_AUTH },
1473db3f65cSamw 	{ "disable-user",	smbadm_user_disable,	HELP_USER_DISABLE,
14896a62adaSjoyce mcintosh 		SMBADM_CMDF_USER,	SMBADM_ACTION_AUTH },
1493db3f65cSamw 	{ "enable-user",	smbadm_user_enable,	HELP_USER_ENABLE,
15096a62adaSjoyce mcintosh 		SMBADM_CMDF_USER,	SMBADM_ACTION_AUTH },
15196a62adaSjoyce mcintosh 	{ "join",		smbadm_join,		HELP_JOIN,
15296a62adaSjoyce mcintosh 		SMBADM_CMDF_NONE,	SMBADM_VALUE_AUTH },
15396a62adaSjoyce mcintosh 	{ "list",		smbadm_list,		HELP_LIST,
15496a62adaSjoyce mcintosh 		SMBADM_CMDF_NONE,	SMBADM_BASIC_AUTH },
15536a00406SGordon Ross 	{ "lookup",		smbadm_lookup,		HELP_LOOKUP,
15636a00406SGordon Ross 		SMBADM_CMDF_NONE,	SMBADM_BASIC_AUTH },
157da6c28aaSamw };
158da6c28aaSamw 
159da6c28aaSamw #define	SMBADM_NCMD	(sizeof (smbadm_cmdtable) / sizeof (smbadm_cmdtable[0]))
160da6c28aaSamw 
161da6c28aaSamw typedef struct smbadm_prop {
162da6c28aaSamw 	char *p_name;
163da6c28aaSamw 	char *p_value;
164da6c28aaSamw } smbadm_prop_t;
165da6c28aaSamw 
166da6c28aaSamw typedef struct smbadm_prop_handle {
167da6c28aaSamw 	char *p_name;
168da6c28aaSamw 	char *p_dispvalue;
169da6c28aaSamw 	int (*p_setfn)(char *, smbadm_prop_t *);
170da6c28aaSamw 	int (*p_getfn)(char *, smbadm_prop_t *);
171da6c28aaSamw 	boolean_t (*p_chkfn)(smbadm_prop_t *);
172da6c28aaSamw } smbadm_prop_handle_t;
173da6c28aaSamw 
174da6c28aaSamw static boolean_t smbadm_prop_validate(smbadm_prop_t *prop, boolean_t chkval);
175da6c28aaSamw static int smbadm_prop_parse(char *arg, smbadm_prop_t *prop);
176da6c28aaSamw static smbadm_prop_handle_t *smbadm_prop_gethandle(char *pname);
177da6c28aaSamw 
178da6c28aaSamw static boolean_t smbadm_chkprop_priv(smbadm_prop_t *prop);
179da6c28aaSamw static int smbadm_setprop_tkowner(char *gname, smbadm_prop_t *prop);
180da6c28aaSamw static int smbadm_getprop_tkowner(char *gname, smbadm_prop_t *prop);
1810292c176SMatt Barden static int smbadm_setprop_readfile(char *gname, smbadm_prop_t *prop);
1820292c176SMatt Barden static int smbadm_getprop_readfile(char *gname, smbadm_prop_t *prop);
1830292c176SMatt Barden static int smbadm_setprop_writefile(char *gname, smbadm_prop_t *prop);
1840292c176SMatt Barden static int smbadm_getprop_writefile(char *gname, smbadm_prop_t *prop);
185da6c28aaSamw static int smbadm_setprop_backup(char *gname, smbadm_prop_t *prop);
186da6c28aaSamw static int smbadm_getprop_backup(char *gname, smbadm_prop_t *prop);
187da6c28aaSamw static int smbadm_setprop_restore(char *gname, smbadm_prop_t *prop);
188da6c28aaSamw static int smbadm_getprop_restore(char *gname, smbadm_prop_t *prop);
189da6c28aaSamw static int smbadm_setprop_desc(char *gname, smbadm_prop_t *prop);
190da6c28aaSamw static int smbadm_getprop_desc(char *gname, smbadm_prop_t *prop);
191da6c28aaSamw 
192da6c28aaSamw static smbadm_prop_handle_t smbadm_ptable[] = {
193959eaf32SYuri Pankov 	{"backup",	"on|off",	smbadm_setprop_backup,
194ef4cfbfdSMatt Barden 	smbadm_getprop_backup,	smbadm_chkprop_priv	},
195959eaf32SYuri Pankov 	{"restore",	"on|off",	smbadm_setprop_restore,
196da6c28aaSamw 	smbadm_getprop_restore,	smbadm_chkprop_priv	},
197959eaf32SYuri Pankov 	{"take-ownership", "on|off",	smbadm_setprop_tkowner,
198da6c28aaSamw 	smbadm_getprop_tkowner,	smbadm_chkprop_priv	},
1990292c176SMatt Barden 	{"bypass-read", "on|off",	smbadm_setprop_readfile,
2000292c176SMatt Barden 	smbadm_getprop_readfile,	smbadm_chkprop_priv	},
2010292c176SMatt Barden 	{"bypass-write", "on|off",	smbadm_setprop_writefile,
2020292c176SMatt Barden 	smbadm_getprop_writefile,	smbadm_chkprop_priv	},
203da6c28aaSamw 	{"description",	"<string>",	smbadm_setprop_desc,
204da6c28aaSamw 	smbadm_getprop_desc,	NULL			},
205da6c28aaSamw };
206da6c28aaSamw 
2073db3f65cSamw static int smbadm_init(void);
2083db3f65cSamw static void smbadm_fini(void);
209da6c28aaSamw static const char *smbadm_pwd_strerror(int error);
210da6c28aaSamw 
211da6c28aaSamw /*
212da6c28aaSamw  * Number of supported properties
213da6c28aaSamw  */
214da6c28aaSamw #define	SMBADM_NPROP	(sizeof (smbadm_ptable) / sizeof (smbadm_ptable[0]))
215