2 * Unix SMB/Netbios implementation.
4 * RPC Pipe client / server routines
5 * Copyright (C) Andrew Tridgell 1992-2000,
6 * Copyright (C) Jean François Micouleau 1998-2001.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern int DEBUGLEVEL;
26 extern DOM_SID global_sam_sid;
28 static TDB_CONTEXT *tdb; /* used for driver files */
30 #define DATABASE_VERSION 1
31 #define GROUP_PREFIX "UNIXGROUP/"
34 {SE_PRIV_NONE, "no_privs", "No privilege"},
35 {SE_PRIV_ADD_USERS, "add_users", "add users"},
36 {SE_PRIV_ADD_MACHINES, "add_computers", ""},
37 {SE_PRIV_PRINT_OPERATOR, "print_op", ""},
38 {SE_PRIV_ALL, "all_privs", ""}
42 { 2, "SeCreateTokenPrivilege" },
43 { 3, "SeAssignPrimaryTokenPrivilege" },
44 { 4, "SeLockMemoryPrivilege" },
45 { 5, "SeIncreaseQuotaPrivilege" },
46 { 6, "SeMachineAccountPrivilege" },
47 { 7, "SeTcbPrivilege" },
48 { 8, "SeSecurityPrivilege" },
49 { 9, "SeTakeOwnershipPrivilege" },
50 { 10, "SeLoadDriverPrivilege" },
51 { 11, "SeSystemProfilePrivilege" },
52 { 12, "SeSystemtimePrivilege" },
53 { 13, "SeProfileSingleProcessPrivilege" },
54 { 14, "SeIncreaseBasePriorityPrivilege" },
55 { 15, "SeCreatePagefilePrivilege" },
56 { 16, "SeCreatePermanentPrivilege" },
57 { 17, "SeBackupPrivilege" },
58 { 18, "SeRestorePrivilege" },
59 { 19, "SeShutdownPrivilege" },
60 { 20, "SeDebugPrivilege" },
61 { 21, "SeAuditPrivilege" },
62 { 22, "SeSystemEnvironmentPrivilege" },
63 { 23, "SeChangeNotifyPrivilege" },
64 { 24, "SeRemoteShutdownPrivilege" },
69 /****************************************************************************
70 check if the user has the required privilege.
71 ****************************************************************************/
72 static BOOL se_priv_access_check(NT_USER_TOKEN *token, uint32 privilege)
74 /* no token, no privilege */
78 if ((token->privilege & privilege)==privilege)
85 /****************************************************************************
86 dump the mapping group mapping to a text file
87 ****************************************************************************/
88 char *decode_sid_name_use(fstring group, enum SID_NAME_USE name_use)
90 static fstring group_type;
94 fstrcpy(group_type,"User");
96 case SID_NAME_DOM_GRP:
97 fstrcpy(group_type,"Domain group");
100 fstrcpy(group_type,"Domain");
103 fstrcpy(group_type,"Local group");
105 case SID_NAME_WKN_GRP:
106 fstrcpy(group_type,"Builtin group");
108 case SID_NAME_DELETED:
109 fstrcpy(group_type,"Deleted");
111 case SID_NAME_INVALID:
112 fstrcpy(group_type,"Invalid");
114 case SID_NAME_UNKNOWN:
116 fstrcpy(group_type,"Unknown type");
120 fstrcpy(group, group_type);
124 /****************************************************************************
125 open the group mapping tdb
126 ****************************************************************************/
127 BOOL init_group_mapping(void)
129 static pid_t local_pid;
130 char *vstring = "INFO/version";
132 if (tdb && local_pid == sys_getpid()) return True;
133 tdb = tdb_open(lock_path("group_mapping.tdb"), 0, 0, O_RDWR|O_CREAT, 0600);
135 DEBUG(0,("Failed to open group mapping database\n"));
139 local_pid = sys_getpid();
141 /* handle a Samba upgrade */
142 tdb_lock_bystring(tdb, vstring);
143 if (tdb_fetch_int(tdb, vstring) != DATABASE_VERSION) {
144 tdb_traverse(tdb, (tdb_traverse_func)tdb_delete, NULL);
145 tdb_store_int(tdb, vstring, DATABASE_VERSION);
147 tdb_unlock_bystring(tdb, vstring);
153 /****************************************************************************
154 ****************************************************************************/
155 BOOL add_mapping_entry(GROUP_MAP *map, int flag)
162 sid_to_string(string_sid, &map->sid);
164 len = tdb_pack(buf, sizeof(buf), "ddffd",
165 map->gid, map->sid_name_use, map->nt_name, map->comment, map->privilege);
167 if (len > sizeof(buf)) return False;
169 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
171 kbuf.dsize = strlen(key)+1;
175 if (tdb_store(tdb, kbuf, dbuf, flag) != 0) return False;
180 /****************************************************************************
181 initialise first time the mapping list
182 ****************************************************************************/
183 BOOL add_initial_entry(gid_t gid, fstring sid, enum SID_NAME_USE sid_name_use,
184 fstring nt_name, fstring comment, uint32 privilege)
189 string_to_sid(&map.sid, sid);
190 map.sid_name_use=sid_name_use;
191 fstrcpy(map.nt_name, nt_name);
192 fstrcpy(map.comment, comment);
193 map.privilege=privilege;
195 add_mapping_entry(&map, TDB_INSERT);
200 /****************************************************************************
201 initialise first time the mapping list
202 ****************************************************************************/
203 BOOL default_group_mapping(void)
213 /* Add the Wellknown groups */
215 add_initial_entry(-1, "S-1-5-32-544", SID_NAME_WKN_GRP, "Administrators", "", SE_PRIV_ALL);
216 add_initial_entry(-1, "S-1-5-32-545", SID_NAME_WKN_GRP, "Users", "", SE_PRIV_NONE);
217 add_initial_entry(-1, "S-1-5-32-546", SID_NAME_WKN_GRP, "Guests", "", SE_PRIV_NONE);
218 add_initial_entry(-1, "S-1-5-32-547", SID_NAME_WKN_GRP, "Power Users", "", SE_PRIV_NONE);
220 add_initial_entry(-1, "S-1-5-32-548", SID_NAME_WKN_GRP, "Account Operators", "", SE_PRIV_NONE);
221 add_initial_entry(-1, "S-1-5-32-549", SID_NAME_WKN_GRP, "System Operators", "", SE_PRIV_NONE);
222 add_initial_entry(-1, "S-1-5-32-550", SID_NAME_WKN_GRP, "Print Operators", "", SE_PRIV_PRINT_OPERATOR);
223 add_initial_entry(-1, "S-1-5-32-551", SID_NAME_WKN_GRP, "Backup Operators", "", SE_PRIV_NONE);
225 add_initial_entry(-1, "S-1-5-32-552", SID_NAME_WKN_GRP, "Replicators", "", SE_PRIV_NONE);
227 /* Add the defaults domain groups */
229 sid_copy(&sid_admins, &global_sam_sid);
230 sid_append_rid(&sid_admins, DOMAIN_GROUP_RID_ADMINS);
231 sid_to_string(str_admins, &sid_admins);
232 add_initial_entry(-1, str_admins, SID_NAME_DOM_GRP, "Domain Admins", "", SE_PRIV_ALL);
234 sid_copy(&sid_users, &global_sam_sid);
235 sid_append_rid(&sid_users, DOMAIN_GROUP_RID_USERS);
236 sid_to_string(str_users, &sid_users);
237 add_initial_entry(-1, str_users, SID_NAME_DOM_GRP, "Domain Users", "", SE_PRIV_NONE);
239 sid_copy(&sid_guests, &global_sam_sid);
240 sid_append_rid(&sid_guests, DOMAIN_GROUP_RID_GUESTS);
241 sid_to_string(str_guests, &sid_guests);
242 add_initial_entry(-1, str_guests, SID_NAME_DOM_GRP, "Domain Guests", "", SE_PRIV_NONE);
248 /****************************************************************************
249 return the sid and the type of the unix group
250 ****************************************************************************/
251 BOOL get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
258 /* the key is the SID, retrieving is direct */
260 sid_to_string(string_sid, &sid);
261 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
264 kbuf.dsize = strlen(key)+1;
266 dbuf = tdb_fetch(tdb, kbuf);
267 if (!dbuf.dptr) return False;
269 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
270 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment, &map->privilege);
272 safe_free(dbuf.dptr);
273 if (ret != dbuf.dsize) {
274 DEBUG(0,("get_group_map_from_sid: mapping TDB corrupted ?\n"));
278 sid_copy(&map->sid, &sid);
284 /****************************************************************************
285 return the sid and the type of the unix group
286 ****************************************************************************/
287 BOOL get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
289 TDB_DATA kbuf, dbuf, newkey;
293 /* we need to enumerate the TDB to find the GID */
295 for (kbuf = tdb_firstkey(tdb);
297 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
299 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
301 dbuf = tdb_fetch(tdb, kbuf);
302 if (!dbuf.dptr) continue;
304 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
306 string_to_sid(&map->sid, string_sid);
308 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
309 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment, &map->privilege);
311 safe_free(dbuf.dptr);
312 if (ret != dbuf.dsize) continue;
321 /****************************************************************************
322 return the sid and the type of the unix group
323 ****************************************************************************/
324 BOOL get_group_map_from_ntname(char *name, GROUP_MAP *map)
326 TDB_DATA kbuf, dbuf, newkey;
330 /* we need to enumerate the TDB to find the GID */
332 for (kbuf = tdb_firstkey(tdb);
334 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
336 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
338 dbuf = tdb_fetch(tdb, kbuf);
339 if (!dbuf.dptr) continue;
341 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
343 string_to_sid(&map->sid, string_sid);
345 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
346 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment, &map->privilege);
348 safe_free(dbuf.dptr);
349 if (ret != dbuf.dsize) continue;
351 if (StrCaseCmp(name, map->nt_name)==0)
359 /****************************************************************************
360 enumerate the group mapping
361 ****************************************************************************/
362 BOOL group_map_remove(DOM_SID sid)
368 /* the key is the SID, retrieving is direct */
370 sid_to_string(string_sid, &sid);
371 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
374 kbuf.dsize = strlen(key)+1;
376 dbuf = tdb_fetch(tdb, kbuf);
377 if (!dbuf.dptr) return False;
379 safe_free(dbuf.dptr);
381 if(tdb_delete(tdb, kbuf) != TDB_SUCCESS)
388 /****************************************************************************
389 enumerate the group mapping
390 ****************************************************************************/
391 BOOL enum_group_mapping(enum SID_NAME_USE sid_name_use, GROUP_MAP **rmap, int *num_entries)
393 TDB_DATA kbuf, dbuf, newkey;
397 GROUP_MAP *mapt=NULL;
404 for (kbuf = tdb_firstkey(tdb);
406 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
408 if (strncmp(kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
410 dbuf = tdb_fetch(tdb, kbuf);
411 if (!dbuf.dptr) continue;
413 fstrcpy(string_sid, kbuf.dptr+strlen(GROUP_PREFIX));
415 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddffd",
416 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment, &map.privilege);
418 safe_free(dbuf.dptr);
419 if (ret != dbuf.dsize) continue;
421 /* list only the type or everything if UNKNOWN */
422 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) continue;
424 string_to_sid(&map.sid, string_sid);
426 decode_sid_name_use(group_type, map.sid_name_use);
428 mapt=(GROUP_MAP *)Realloc(mapt, (entries+1)*sizeof(GROUP_MAP));
430 mapt[entries].gid = map.gid;
431 sid_copy( &mapt[entries].sid, &map.sid);
432 mapt[entries].sid_name_use = map.sid_name_use;
433 fstrcpy(mapt[entries].nt_name, map.nt_name);
434 fstrcpy(mapt[entries].comment, map.comment);
435 mapt[entries].privilege = map.privilege;
441 *num_entries=entries;
446 /****************************************************************************
447 convert a privilege list to a privilege value
448 ****************************************************************************/
449 void convert_priv_from_text(uint32 *se_priv, char *privilege)
455 /* By default no privilege */
461 while(next_token(&p, tok, " ", sizeof(tok)) ) {
462 for (i=0; i<=PRIV_ALL_INDEX; i++) {
463 if (StrCaseCmp(privs[i].priv, tok)==0)
464 (*se_priv)+=privs[i].se_priv;
469 /****************************************************************************
470 convert a privilege value to a privilege list
471 ****************************************************************************/
472 void convert_priv_to_text(uint32 se_priv, char *privilege)
479 ZERO_STRUCTP(privilege);
481 if (se_priv==SE_PRIV_NONE) {
482 fstrcat(privilege, privs[0].priv);
486 if (se_priv==SE_PRIV_ALL) {
487 fstrcat(privilege, privs[PRIV_ALL_INDEX].priv);
491 for (i=1; privs[i].se_priv!=SE_PRIV_ALL; i++) {
492 if ( (se_priv & privs[i].se_priv) == privs[i].se_priv) {
493 fstrcat(privilege, privs[i].priv);
494 fstrcat(privilege, " ");
502 * High level functions
503 * better to use them than the lower ones.
505 * we are checking if the group is in the mapping file
506 * and if the group is an existing unix group
510 /* get a domain group from it's SID */
512 BOOL get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map)
516 /* if the group is NOT in the database, it CAN NOT be a domain group */
517 if(!get_group_map_from_sid(sid, map))
520 /* if it's not a domain group, continue */
521 if (map->sid_name_use!=SID_NAME_DOM_GRP)
527 if ( (grp=getgrgid(map->gid)) == NULL)
534 /* get a local (alias) group from it's SID */
536 BOOL get_local_group_from_sid(DOM_SID sid, GROUP_MAP *map)
540 /* The group is in the mapping table */
541 if(get_group_map_from_sid(sid, map)) {
542 if (map->sid_name_use!=SID_NAME_ALIAS)
548 if ( (grp=getgrgid(map->gid)) == NULL)
551 /* the group isn't in the mapping table.
552 * make one based on the unix information */
555 sid_split_rid(&sid, &alias_rid);
556 map->gid=pdb_user_rid_to_gid(alias_rid);
558 if ((grp=getgrgid(map->gid)) == NULL)
561 map->sid_name_use=SID_NAME_ALIAS;
563 fstrcpy(map->nt_name, grp->gr_name);
564 fstrcpy(map->comment, "Local Unix Group");
566 map->privilege=SE_PRIV_NONE;
573 /* get a builtin group from it's SID */
575 BOOL get_builtin_group_from_sid(DOM_SID sid, GROUP_MAP *map)
579 if(!get_group_map_from_sid(sid, map))
582 if (map->sid_name_use!=SID_NAME_WKN_GRP)
588 if ( (grp=getgrgid(map->gid)) == NULL)
596 /****************************************************************************
597 Returns a GROUP_MAP struct based on the gid.
598 ****************************************************************************/
599 BOOL get_group_from_gid(gid_t gid, GROUP_MAP *map)
605 if ( (grp=getgrgid(gid)) == NULL)
609 * make a group map from scratch if doesn't exist.
611 if (!get_group_map_from_gid(gid, map)) {
613 map->sid_name_use=SID_NAME_ALIAS;
614 map->privilege=SE_PRIV_NONE;
616 rid=pdb_gid_to_group_rid(gid);
617 sid_copy(&sid, &global_sam_sid);
618 sid_append_rid(&sid, rid);
620 fstrcpy(map->nt_name, grp->gr_name);
621 fstrcpy(map->comment, "Local Unix Group");
630 /****************************************************************************
631 Get the member users of a group and
632 all the users who have that group as primary.
634 give back an array of uid
635 return the grand number of users
638 TODO: sort the list and remove duplicate. JFM.
640 ****************************************************************************/
642 BOOL get_uid_list_of_group(gid_t gid, uid_t **uid, int *num_uids)
651 if ( (grp=getgrgid(gid)) == NULL)
655 DEBUG(10, ("getting members\n"));
657 while (gr && (*gr != (char)NULL)) {
658 (*uid)=Realloc((*uid), sizeof(uid_t)*(*num_uids+1));
660 if( (pwd=getpwnam(gr)) !=NULL) {
661 (*uid)[*num_uids]=pwd->pw_uid;
664 gr = grp->gr_mem[++i];
666 DEBUG(10, ("got [%d] members\n", *num_uids));
669 while ((pwd=getpwent()) != NULL) {
670 if (pwd->pw_gid==gid) {
671 (*uid)=Realloc((*uid), sizeof(uid_t)*(*num_uids+1));
672 (*uid)[*num_uids]=pwd->pw_uid;
678 DEBUG(10, ("got primary groups, members: [%d]\n", *num_uids));
683 /****************************************************************************
684 Create a UNIX group on demand.
685 ****************************************************************************/
687 int smb_create_group(char *unix_group)
692 pstrcpy(add_script, lp_addgroup_script());
693 if (! *add_script) return -1;
694 pstring_sub(add_script, "%g", unix_group);
695 ret = smbrun(add_script,NULL);
696 DEBUG(3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret));
700 /****************************************************************************
701 Delete a UNIX group on demand.
702 ****************************************************************************/
704 int smb_delete_group(char *unix_group)
709 pstrcpy(del_script, lp_delgroup_script());
710 if (! *del_script) return -1;
711 pstring_sub(del_script, "%g", unix_group);
712 ret = smbrun(del_script,NULL);
713 DEBUG(3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret));
717 /****************************************************************************
718 Create a UNIX group on demand.
719 ****************************************************************************/
721 int smb_add_user_group(char *unix_group, char *unix_user)
726 pstrcpy(add_script, lp_addusertogroup_script());
727 if (! *add_script) return -1;
728 pstring_sub(add_script, "%g", unix_group);
729 pstring_sub(add_script, "%u", unix_user);
730 ret = smbrun(add_script,NULL);
731 DEBUG(3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret));
735 /****************************************************************************
736 Delete a UNIX group on demand.
737 ****************************************************************************/
739 int smb_delete_user_group(char *unix_group, char *unix_user)
744 pstrcpy(del_script, lp_deluserfromgroup_script());
745 if (! *del_script) return -1;
746 pstring_sub(del_script, "%g", unix_group);
747 pstring_sub(del_script, "%u", unix_user);
748 ret = smbrun(del_script,NULL);
749 DEBUG(3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret));