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.
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.
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]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
29 #include <smbsrv/libsmb.h>
31 extern int smb_pwd_num(void);
32 extern int smb_lgrp_numbydomain(smb_domain_type_t
, int *);
34 static uint32_t smb_sam_lookup_user(char *, smb_sid_t
**);
35 static uint32_t smb_sam_lookup_group(char *, smb_sid_t
**);
38 * Local well-known accounts data structure table and prototypes
40 typedef struct smb_lwka
{
46 static smb_lwka_t lwka_tbl
[] = {
47 { 500, "Administrator", SidTypeUser
},
48 { 501, "Guest", SidTypeUser
},
49 { 502, "KRBTGT", SidTypeUser
},
50 { 512, "Domain Admins", SidTypeGroup
},
51 { 513, "Domain Users", SidTypeGroup
},
52 { 514, "Domain Guests", SidTypeGroup
},
53 { 516, "Domain Controllers", SidTypeGroup
},
54 { 517, "Cert Publishers", SidTypeGroup
},
55 { 518, "Schema Admins", SidTypeGroup
},
56 { 519, "Enterprise Admins", SidTypeGroup
},
57 { 520, "Global Policy Creator Owners", SidTypeGroup
},
58 { 533, "RAS and IAS Servers", SidTypeGroup
}
61 #define SMB_LWKA_NUM (sizeof (lwka_tbl)/sizeof (lwka_tbl[0]))
63 static smb_lwka_t
*smb_lwka_lookup_name(char *);
64 static smb_lwka_t
*smb_lwka_lookup_sid(smb_sid_t
*);
67 * Looks up the given name in local account databases:
69 * SMB Local users are looked up in /var/smb/smbpasswd
70 * SMB Local groups are looked up in /var/smb/smbgroup.db
72 * If the account is found, its information is populated
73 * in the passed smb_account_t structure. Caller must free
74 * allocated memories by calling smb_account_free() upon
77 * The type of account is specified by 'type', which can be user,
78 * alias (local group) or unknown. If the caller doesn't know
79 * whether the name is a user or group name then SidTypeUnknown
82 * If a local user and group have the same name, the user will
83 * always be picked. Note that this situation cannot happen on
86 * If a SMB local user/group is found but it turns out that
87 * it'll be mapped to a domain user/group the lookup is considered
88 * failed and NT_STATUS_NONE_MAPPED is returned.
92 * NT_STATUS_NOT_FOUND This is not a local account
93 * NT_STATUS_NONE_MAPPED It's a local account but cannot be
95 * other error status codes.
98 smb_sam_lookup_name(char *domain
, char *name
, uint16_t type
,
99 smb_account_t
*account
)
106 bzero(account
, sizeof (smb_account_t
));
108 if (domain
!= NULL
) {
109 if (!smb_domain_lookup_name(domain
, &di
) ||
110 (di
.di_type
!= SMB_DOMAIN_LOCAL
))
111 return (NT_STATUS_NOT_FOUND
);
113 /* Only Netbios hostname is accepted */
114 if (smb_strcasecmp(domain
, di
.di_nbname
, 0) != 0)
115 return (NT_STATUS_NONE_MAPPED
);
117 if (!smb_domain_lookup_type(SMB_DOMAIN_LOCAL
, &di
))
118 return (NT_STATUS_CANT_ACCESS_DOMAIN_INFO
);
121 if (smb_strcasecmp(name
, di
.di_nbname
, 0) == 0) {
122 /* This is the local domain name */
123 account
->a_type
= SidTypeDomain
;
124 account
->a_name
= strdup("");
125 account
->a_domain
= strdup(di
.di_nbname
);
126 account
->a_sid
= smb_sid_dup(di
.di_binsid
);
127 account
->a_domsid
= smb_sid_dup(di
.di_binsid
);
128 account
->a_rid
= (uint32_t)-1;
130 if (!smb_account_validate(account
)) {
131 smb_account_free(account
);
132 return (NT_STATUS_NO_MEMORY
);
135 return (NT_STATUS_SUCCESS
);
138 if ((lwka
= smb_lwka_lookup_name(name
)) != NULL
) {
139 sid
= smb_sid_splice(di
.di_binsid
, lwka
->lwka_rid
);
140 type
= lwka
->lwka_type
;
144 status
= smb_sam_lookup_user(name
, &sid
);
145 if (status
!= NT_STATUS_SUCCESS
)
150 status
= smb_sam_lookup_group(name
, &sid
);
151 if (status
!= NT_STATUS_SUCCESS
)
157 status
= smb_sam_lookup_user(name
, &sid
);
158 if (status
== NT_STATUS_SUCCESS
)
161 if (status
== NT_STATUS_NONE_MAPPED
)
165 status
= smb_sam_lookup_group(name
, &sid
);
166 if (status
!= NT_STATUS_SUCCESS
)
171 return (NT_STATUS_INVALID_PARAMETER
);
175 account
->a_name
= strdup(name
);
176 account
->a_sid
= sid
;
177 account
->a_domain
= strdup(di
.di_nbname
);
178 account
->a_domsid
= smb_sid_split(sid
, &account
->a_rid
);
179 account
->a_type
= type
;
181 if (!smb_account_validate(account
)) {
182 smb_account_free(account
);
183 return (NT_STATUS_NO_MEMORY
);
186 return (NT_STATUS_SUCCESS
);
190 * Looks up the given SID in local account databases:
192 * SMB Local users are looked up in /var/smb/smbpasswd
193 * SMB Local groups are looked up in /var/smb/smbgroup.db
195 * If the account is found, its information is populated
196 * in the passed smb_account_t structure. Caller must free
197 * allocated memories by calling smb_account_free() upon
202 * NT_STATUS_NOT_FOUND This is not a local account
203 * NT_STATUS_NONE_MAPPED It's a local account but cannot be
205 * other error status codes.
208 smb_sam_lookup_sid(smb_sid_t
*sid
, smb_account_t
*account
)
210 char hostname
[MAXHOSTNAMELEN
];
220 bzero(account
, sizeof (smb_account_t
));
222 if (!smb_domain_lookup_type(SMB_DOMAIN_LOCAL
, &di
))
223 return (NT_STATUS_CANT_ACCESS_DOMAIN_INFO
);
225 if (smb_sid_cmp(sid
, di
.di_binsid
)) {
226 /* This is the local domain SID */
227 account
->a_type
= SidTypeDomain
;
228 account
->a_name
= strdup("");
229 account
->a_domain
= strdup(di
.di_nbname
);
230 account
->a_sid
= smb_sid_dup(sid
);
231 account
->a_domsid
= smb_sid_dup(sid
);
232 account
->a_rid
= (uint32_t)-1;
234 if (!smb_account_validate(account
)) {
235 smb_account_free(account
);
236 return (NT_STATUS_NO_MEMORY
);
239 return (NT_STATUS_SUCCESS
);
242 if (!smb_sid_indomain(di
.di_binsid
, sid
)) {
243 /* This is not a local SID */
244 return (NT_STATUS_NOT_FOUND
);
247 if ((lwka
= smb_lwka_lookup_sid(sid
)) != NULL
) {
248 account
->a_type
= lwka
->lwka_type
;
249 account
->a_name
= strdup(lwka
->lwka_name
);
251 id_type
= SMB_IDMAP_UNKNOWN
;
252 if (smb_idmap_getid(sid
, &id
, &id_type
) != IDMAP_SUCCESS
)
253 return (NT_STATUS_NONE_MAPPED
);
257 account
->a_type
= SidTypeUser
;
258 if (smb_pwd_getpwuid(id
, &smbpw
) == NULL
)
259 return (NT_STATUS_NO_SUCH_USER
);
261 account
->a_name
= strdup(smbpw
.pw_name
);
264 case SMB_IDMAP_GROUP
:
265 account
->a_type
= SidTypeAlias
;
266 (void) smb_sid_getrid(sid
, &rid
);
267 rc
= smb_lgrp_getbyrid(rid
, SMB_DOMAIN_LOCAL
, &grp
);
268 if (rc
!= SMB_LGRP_SUCCESS
)
269 return (NT_STATUS_NO_SUCH_ALIAS
);
271 account
->a_name
= strdup(grp
.sg_name
);
276 return (NT_STATUS_NONE_MAPPED
);
280 if (smb_getnetbiosname(hostname
, MAXHOSTNAMELEN
) == 0)
281 account
->a_domain
= strdup(hostname
);
282 account
->a_sid
= smb_sid_dup(sid
);
283 account
->a_domsid
= smb_sid_split(sid
, &account
->a_rid
);
285 if (!smb_account_validate(account
)) {
286 smb_account_free(account
);
287 return (NT_STATUS_NO_MEMORY
);
290 return (NT_STATUS_SUCCESS
);
294 * Returns number of SMB users, i.e. users who have entry
295 * in /var/smb/smbpasswd
298 smb_sam_usr_cnt(void)
300 return (smb_pwd_num());
304 * Updates a list of groups in which the given user is a member
305 * by adding any local (SAM) groups.
307 * We are a member of local groups where the local group
308 * contains either the user's primary SID, or any of their
309 * other SIDs such as from domain groups, SID history, etc.
310 * We can have indirect membership via domain groups.
313 smb_sam_usr_groups(smb_sid_t
*user_sid
, smb_ids_t
*gids
)
316 smb_id_t
*ids
, *new_ids
;
319 int i
, gcnt
, total_cnt
;
324 * First pass: count groups to be added (gcnt)
327 if (smb_lgrp_iteropen(&gi
) != SMB_LGRP_SUCCESS
)
328 return (NT_STATUS_INTERNAL_ERROR
);
330 while (smb_lgrp_iterate(&gi
, &lgrp
) == SMB_LGRP_SUCCESS
) {
332 if (smb_lgrp_is_member(&lgrp
, user_sid
))
334 else for (i
= 0, ids
= gids
->i_ids
;
335 i
< gids
->i_cnt
; i
++, ids
++) {
336 if (smb_lgrp_is_member(&lgrp
, ids
->i_sid
)) {
341 /* Careful: only count lgrp once */
344 smb_lgrp_free(&lgrp
);
346 smb_lgrp_iterclose(&gi
);
349 return (NT_STATUS_SUCCESS
);
352 * Second pass: add to groups list.
353 * Do not modify gcnt after here.
355 if (smb_lgrp_iteropen(&gi
) != SMB_LGRP_SUCCESS
)
356 return (NT_STATUS_INTERNAL_ERROR
);
359 * Expand the list (copy to a new, larger one)
360 * Note: were're copying pointers from the old
361 * array to the new (larger) array, and then
362 * adding new pointers after what we copied.
365 new_gids
.i_cnt
= gids
->i_cnt
;
366 total_cnt
= gids
->i_cnt
+ gcnt
;
367 new_gids
.i_ids
= malloc(total_cnt
* sizeof (smb_id_t
));
368 if (new_gids
.i_ids
== NULL
) {
369 ret
= NT_STATUS_NO_MEMORY
;
372 (void) memcpy(new_gids
.i_ids
, gids
->i_ids
,
373 gids
->i_cnt
* sizeof (smb_id_t
));
374 new_ids
= new_gids
.i_ids
+ gids
->i_cnt
;
375 (void) memset(new_ids
, 0, gcnt
* sizeof (smb_id_t
));
378 * Add group SIDs starting at the end of the
379 * previous list. (new_ids)
381 while (smb_lgrp_iterate(&gi
, &lgrp
) == SMB_LGRP_SUCCESS
) {
383 if (smb_lgrp_is_member(&lgrp
, user_sid
))
385 else for (i
= 0, ids
= gids
->i_ids
;
386 i
< gids
->i_cnt
; i
++, ids
++) {
387 if (smb_lgrp_is_member(&lgrp
, ids
->i_sid
)) {
392 if (member
&& (new_gids
.i_cnt
< (gids
->i_cnt
+ gcnt
))) {
393 new_ids
->i_sid
= smb_sid_dup(lgrp
.sg_id
.gs_sid
);
394 if (new_ids
->i_sid
== NULL
) {
395 smb_lgrp_free(&lgrp
);
396 ret
= NT_STATUS_NO_MEMORY
;
399 new_ids
->i_attrs
= lgrp
.sg_attr
;
403 smb_lgrp_free(&lgrp
);
407 smb_lgrp_iterclose(&gi
);
410 if (new_gids
.i_ids
!= NULL
) {
412 * Free only the new sids we added.
413 * The old ones were copied ptrs.
415 ids
= new_gids
.i_ids
+ gids
->i_cnt
;
416 for (i
= 0; i
< gcnt
; i
++, ids
++) {
417 smb_sid_free(ids
->i_sid
);
419 free(new_gids
.i_ids
);
425 * Success! Update passed gids and
426 * free the old array.
431 return (NT_STATUS_SUCCESS
);
435 * Returns the number of built-in or local groups stored
436 * in /var/smb/smbgroup.db
439 smb_sam_grp_cnt(smb_domain_type_t dtype
)
445 case SMB_DOMAIN_BUILTIN
:
446 rc
= smb_lgrp_numbydomain(SMB_DOMAIN_BUILTIN
, &grpcnt
);
449 case SMB_DOMAIN_LOCAL
:
450 rc
= smb_lgrp_numbydomain(SMB_DOMAIN_LOCAL
, &grpcnt
);
454 rc
= SMB_LGRP_INVALID_ARG
;
457 return ((rc
== SMB_LGRP_SUCCESS
) ? grpcnt
: 0);
461 * Determines whether the given SID is a member of the group
462 * specified by gname.
465 smb_sam_grp_ismember(const char *gname
, smb_sid_t
*sid
)
468 boolean_t ismember
= B_FALSE
;
470 if (smb_lgrp_getbyname((char *)gname
, &grp
) == SMB_LGRP_SUCCESS
) {
471 ismember
= smb_lgrp_is_member(&grp
, sid
);
479 * Frees memories allocated for the passed account fields.
482 smb_account_free(smb_account_t
*account
)
484 free(account
->a_name
);
485 free(account
->a_domain
);
486 smb_sid_free(account
->a_sid
);
487 smb_sid_free(account
->a_domsid
);
491 * Validates the given account.
494 smb_account_validate(smb_account_t
*account
)
496 return ((account
->a_name
!= NULL
) && (account
->a_sid
!= NULL
) &&
497 (account
->a_domain
!= NULL
) && (account
->a_domsid
!= NULL
));
501 * Lookup local SMB user account database (/var/smb/smbpasswd)
502 * if there's a match query its SID from idmap service and make
503 * sure the SID is a local SID.
505 * The memory for the returned SID must be freed by the caller.
508 smb_sam_lookup_user(char *name
, smb_sid_t
**sid
)
512 if (smb_pwd_getpwnam(name
, &smbpw
) == NULL
)
513 return (NT_STATUS_NO_SUCH_USER
);
515 if (smb_idmap_getsid(smbpw
.pw_uid
, SMB_IDMAP_USER
, sid
)
517 return (NT_STATUS_NONE_MAPPED
);
519 if (!smb_sid_islocal(*sid
)) {
521 return (NT_STATUS_NONE_MAPPED
);
524 return (NT_STATUS_SUCCESS
);
528 * Lookup local SMB group account database (/var/smb/smbgroup.db)
529 * The memory for the returned SID must be freed by the caller.
532 smb_sam_lookup_group(char *name
, smb_sid_t
**sid
)
536 if (smb_lgrp_getbyname(name
, &grp
) != SMB_LGRP_SUCCESS
)
537 return (NT_STATUS_NO_SUCH_ALIAS
);
539 *sid
= smb_sid_dup(grp
.sg_id
.gs_sid
);
542 return ((*sid
== NULL
) ? NT_STATUS_NO_MEMORY
: NT_STATUS_SUCCESS
);
546 smb_lwka_lookup_name(char *name
)
550 for (i
= 0; i
< SMB_LWKA_NUM
; i
++) {
551 if (smb_strcasecmp(name
, lwka_tbl
[i
].lwka_name
, 0) == 0)
552 return (&lwka_tbl
[i
]);
559 smb_lwka_lookup_sid(smb_sid_t
*sid
)
564 (void) smb_sid_getrid(sid
, &rid
);
568 for (i
= 0; i
< SMB_LWKA_NUM
; i
++) {
569 if (rid
== lwka_tbl
[i
].lwka_rid
)
570 return (&lwka_tbl
[i
]);
579 * Check a SID to see if it belongs to the local domain.
582 smb_sid_islocal(smb_sid_t
*sid
)
585 boolean_t islocal
= B_FALSE
;
587 if (smb_domain_lookup_type(SMB_DOMAIN_LOCAL
, &di
))
588 islocal
= smb_sid_indomain(di
.di_binsid
, sid
);
594 smb_ids_free(smb_ids_t
*ids
)
599 if ((ids
!= NULL
) && (ids
->i_ids
!= NULL
)) {
601 for (i
= 0; i
< ids
->i_cnt
; i
++, id
++)
602 smb_sid_free(id
->i_sid
);