summaryrefslogtreecommitdiff
path: root/usr/src/lib/smbsrv/libsmb/common/smb_privilege.c
blob: 2b60a8d549fa1849fcd0fe7c8b0f26db66a6260e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * This module provides the interface to the built-in privilege names
 * and id's. NT privileges are known on the network using strings. Each
 * system assigns locally unique identifiers (LUID) for use within the
 * system. Each built-in privilege also has a display-name, which is a
 * short description of the privilege. The functions here provide an
 * interface to map between LUIDs, names and display names.
 */

#include <string.h>
#include <syslog.h>

#include <smbsrv/string.h>
#include <smbsrv/libsmb.h>
#include <smbsrv/smb_privilege.h>

static char *smb_priv_getname(uint32_t id);

/*
 * Table of built-in privilege id's, names and display strings. This
 * table matches the response from an NT4.0 PDC LSARPC service.
 * Requests for values 0 and 1 return STATUS_NO_SUCH_PRIVILEGE.
 *
 * SE_UNSOLICITED_INPUT_NAME/SeUnsolicitedInputPrivilege is defined in
 * winnt.h but doesn't appear in the list reported by the NT4.0 LSA.
 */
static smb_privinfo_t priv_table[] = {
	{  0, "", "", 0 },
	{  1, "", "", 0 },
	{  2, SE_CREATE_TOKEN_NAME, "Create a token object", 0 },
	{  3, SE_ASSIGNPRIMARYTOKEN_NAME, "Replace a process level token", 0 },
	{  4, SE_LOCK_MEMORY_NAME, "Lock pages in memory", 0 },
	{  5, SE_INCREASE_QUOTA_NAME, "Increase quotas", 0 },
	{  6, SE_MACHINE_ACCOUNT_NAME, "Add workstations to domain", 0 },
	{  7, SE_TCB_NAME, "Act as part of the operating system", 0 },
	{  8, SE_SECURITY_NAME, "Manage auditing and security log", 0 },
	{  9, SE_TAKE_OWNERSHIP_NAME,
	    "Take ownership of files or other objects", PF_PRESENTABLE },
	{ 10, SE_LOAD_DRIVER_NAME, "Load and unload device drivers", 0 },
	{ 11, SE_SYSTEM_PROFILE_NAME, "Profile system performance", 0 },
	{ 12, SE_SYSTEMTIME_NAME, "Change the system time", 0 },
	{ 13, SE_PROF_SINGLE_PROCESS_NAME, "Profile single process", 0 },
	{ 14, SE_INC_BASE_PRIORITY_NAME, "Increase scheduling priority", 0 },
	{ 15, SE_CREATE_PAGEFILE_NAME, "Create a pagefile", 0 },
	{ 16, SE_CREATE_PERMANENT_NAME, "Create permanent shared objects", 0 },
	{ 17, SE_BACKUP_NAME, "Back up files and directories",
	    PF_PRESENTABLE },
	{ 18, SE_RESTORE_NAME, "Restore files and directories",
	    PF_PRESENTABLE },
	{ 19, SE_SHUTDOWN_NAME, "Shut down the system", 0 },
	{ 20, SE_DEBUG_NAME, "Debug programs", 0 },
	{ 21, SE_AUDIT_NAME, "Generate security audits", 0 },
	{ 22, SE_SYSTEM_ENVIRONMENT_NAME,
	    "Modify firmware environment values", 0 },
	{ 23, SE_CHANGE_NOTIFY_NAME, "Bypass traverse checking", 0 },
	{ 24, SE_REMOTE_SHUTDOWN_NAME,
	    "Force shutdown from a remote system", 0 }
};

/*
 * smb_priv_presentable_num
 *
 * Returns number of presentable privileges
 */
int
smb_priv_presentable_num()
{
	int i, num;

	num = 0;
	for (i = SE_MIN_LUID; i <= SE_MAX_LUID; i++)
		if (priv_table[i].flags == PF_PRESENTABLE)
			num++;

	return (num);
}

/*
 * smb_priv_presentable_ids
 *
 * Returns IDs of presentable privileges
 * Returns 0 in case of invalid parameter and 1 on success.
 */
int
smb_priv_presentable_ids(uint32_t *ids, int num)
{
	int i, j;

	if (ids == NULL || num <= 0)
		return (0);

	for (i = SE_MIN_LUID, j = 0; i <= SE_MAX_LUID; i++)
		if (priv_table[i].flags == PF_PRESENTABLE)
			ids[j++] = priv_table[i].id;

	return (1);
}

/*
 * smb_priv_getbyvalue
 *
 * Return the privilege info for the specified id (low part of the LUID).
 * Returns a null pointer if id is out-of-range.
 */
smb_privinfo_t *
smb_priv_getbyvalue(uint32_t id)
{
	if (id < SE_MIN_LUID || id > SE_MAX_LUID)
		return (0);

	return (&priv_table[id]);
}


/*
 * smb_priv_getbyname
 *
 * Return the privilege info for the specified name. Returns a null
 * pointer if we can't find a matching name in the table.
 */
smb_privinfo_t *
smb_priv_getbyname(char *name)
{
	smb_privinfo_t *entry;
	int i;

	if (name == 0)
		return (0);

	for (i = SE_MIN_LUID; i <= SE_MAX_LUID; ++i) {
		entry = &priv_table[i];

		if (smb_strcasecmp(name, entry->name, 0) == 0)
			return (entry);
	}

	return (0);
}

/*
 * smb_privset_size
 *
 * Returns the memory block size needed to keep a complete
 * set of privileges in a smb_privset_t structure.
 */
int
smb_privset_size()
{
	int pcnt = SE_MAX_LUID - SE_MIN_LUID + 1;

	return (2 * sizeof (uint32_t) +
	    pcnt * sizeof (smb_luid_attrs_t));
}

/*
 * smb_privset_validate
 *
 * Validates the given privilege set structure
 * Returns 1 if the structure is Ok, otherwise returns 0.
 */
int
smb_privset_validate(smb_privset_t *privset)
{
	int count;
	uint32_t i;

	if (privset == 0) {
		return (0);
	}

	count = SE_MAX_LUID - SE_MIN_LUID + 1;

	if (privset->priv_cnt != count) {
		return (0);
	}

	for (i = 0; i < count; i++) {
		if (privset->priv[i].luid.hi_part != 0) {
			return (0);
		}

		if (privset->priv[i].luid.lo_part !=
		    i + SE_MIN_LUID) {
			return (0);
		}
	}

	return (1);
}

/*
 * smb_privset_init
 *
 * initialize all privileges in disable state.
 */
void
smb_privset_init(smb_privset_t *privset)
{
	int count;
	uint32_t i;

	if (privset == 0)
		return;

	count = SE_MAX_LUID - SE_MIN_LUID + 1;

	privset->priv_cnt = count;
	privset->control = 0;
	for (i = 0; i < count; i++) {
		privset->priv[i].luid.hi_part = 0;
		privset->priv[i].luid.lo_part = i + SE_MIN_LUID;
		privset->priv[i].attrs = 0;
	}
}

/*
 * smb_privset_new
 *
 * Allocate memory and initialize all privileges in disable state.
 * Returns pointer to allocated space or NULL if there is not
 * enough memory.
 */
smb_privset_t *
smb_privset_new()
{
	smb_privset_t *privset;

	privset = malloc(smb_privset_size());
	if (privset == NULL)
		return (NULL);

	smb_privset_init(privset);

	return (privset);
}

/*
 * smb_privset_copy
 *
 * Copy privleges information specified by 'src' to the
 * buffer specified by dst.
 */
void
smb_privset_copy(smb_privset_t *dst, smb_privset_t *src)
{
	if (src == 0 || dst == 0)
		return;

	(void) memcpy(dst, src, smb_privset_size());
}

/*
 * smb_privset_merge
 *
 * Enable the privileges that are enabled in src in dst
 */
void
smb_privset_merge(smb_privset_t *dst, smb_privset_t *src)
{
	int i;

	if (src == NULL || dst == NULL)
		return;

	for (i = 0; i < src->priv_cnt; i++) {
		if (src->priv[i].attrs == SE_PRIVILEGE_ENABLED)
			smb_privset_enable(dst, src->priv[i].luid.lo_part);
	}
}

/*
 * smb_privset_free
 *
 * This will free the memory allocated by the 'privset'.
 */
void
smb_privset_free(smb_privset_t *privset)
{
	free(privset);
}

void
smb_privset_enable(smb_privset_t *privset, uint32_t id)
{
	int i;

	if (privset == NULL)
		return;

	for (i = 0; i < privset->priv_cnt; i++) {
		if (privset->priv[i].luid.lo_part == id)
			privset->priv[i].attrs = SE_PRIVILEGE_ENABLED;
	}
}

void
smb_privset_log(smb_privset_t *privset)
{
	smb_luid_t *luid;
	int i, ecnt;

	if (privset == NULL)
		return;

	for (i = 0, ecnt = 0; i < privset->priv_cnt; ++i) {
		if (privset->priv[i].attrs != 0) {
			ecnt++;
		}
	}

	syslog(LOG_DEBUG, "   Privilege Count: %d (Enable=%d)",
	    privset->priv_cnt, ecnt);

	for (i = 0; i < privset->priv_cnt; ++i) {
		if (privset->priv[i].attrs != 0) {
			luid = &privset->priv[i].luid;
			syslog(LOG_DEBUG, "    %s",
			    smb_priv_getname(luid->lo_part));
		}
	}
}

int
smb_privset_query(smb_privset_t *privset, uint32_t id)
{
	int i;

	if (privset == NULL)
		return (0);

	for (i = 0; privset->priv_cnt; i++) {
		if (privset->priv[i].luid.lo_part == id) {
			if (privset->priv[i].attrs == SE_PRIVILEGE_ENABLED)
				return (1);
			else
				return (0);
		}
	}

	return (0);
}

static char *
smb_priv_getname(uint32_t id)
{
	if (id < SE_MIN_LUID || id > SE_MAX_LUID)
		return ("Unknown Privilege");

	return (priv_table[id].name);
}