2 * Unix SMB/Netbios implementation. Version 1.9. SMB parameters and setup
3 * Copyright (C) Andrew Tridgell 1992-1998
4 * Copyright (C) Simo Sorce 2000
5 * Copyright (C) Gerald Carter 2000
6 * Copyright (C) Jeremy Allison 2001
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 675
20 * Mass Ave, Cambridge, MA 02139, USA.
27 #define PDB_VERSION "20010830"
28 #define PASSDB_FILE_NAME "/passdb.tdb"
29 #define TDB_FORMAT_STRING "ddddddBBBBBBBBBBBBddBBwdwdBdd"
30 #define USERPREFIX "USER_"
31 #define RIDPREFIX "RID_"
33 extern int DEBUGLEVEL;
35 struct tdb_enum_info {
36 TDB_CONTEXT *passwd_tdb;
40 static struct tdb_enum_info global_tdb_ent;
41 /*static SAM_ACCOUNT global_sam_pass;*/
43 /**********************************************************************
44 Intialize a SAM_ACCOUNT struct from a BYTE buffer of size len
45 *********************************************************************/
47 static BOOL init_sam_from_buffer (SAM_ACCOUNT *sampass, uint8 *buf, uint32 buflen)
50 /* times are stored as 32bit integer
51 take care on system with 64bit wide time_t
58 pass_must_change_time;
71 uint32 username_len, domain_len, nt_username_len,
72 dir_drive_len, unknown_str_len, munged_dial_len,
73 fullname_len, homedir_len, logon_script_len,
74 profile_path_len, acct_desc_len, workstations_len;
76 uint32 /* uid, gid,*/ user_rid, group_rid, unknown_3, hours_len, unknown_5, unknown_6;
77 uint16 acct_ctrl, logon_divs;
79 static uint8 *lm_pw_ptr, *nt_pw_ptr;
81 uint32 lmpwlen, ntpwlen, hourslen;
84 if(sampass == NULL || buf == NULL) {
85 DEBUG(0, ("init_sam_from_buffer: NULL parameters found!\n"));
89 /* unpack the buffer into variables */
90 len = tdb_unpack (buf, buflen, TDB_FORMAT_STRING,
95 &pass_can_change_time,
96 &pass_must_change_time,
97 &username_len, &username,
99 &nt_username_len, &nt_username,
100 &fullname_len, &fullname,
101 &homedir_len, &homedir,
102 &dir_drive_len, &dir_drive,
103 &logon_script_len, &logon_script,
104 &profile_path_len, &profile_path,
105 &acct_desc_len, &acct_desc,
106 &workstations_len, &workstations,
107 &unknown_str_len, &unknown_str,
108 &munged_dial_len, &munged_dial,
111 &lmpwlen, &lm_pw_ptr,
112 &ntpwlen, &nt_pw_ptr,
126 pdb_set_logon_time(sampass, logon_time);
127 pdb_set_logoff_time(sampass, logoff_time);
128 pdb_set_kickoff_time(sampass, kickoff_time);
129 pdb_set_pass_can_change_time(sampass, pass_can_change_time);
130 pdb_set_pass_must_change_time(sampass, pass_must_change_time);
131 pdb_set_pass_last_set_time(sampass, pass_last_set_time);
133 pdb_set_username (sampass, username_len?username:NULL);
134 pdb_set_domain (sampass, domain_len?domain:NULL);
135 pdb_set_nt_username (sampass, nt_username_len?nt_username:NULL);
136 pdb_set_fullname (sampass, fullname_len?fullname:NULL);
137 pdb_set_homedir (sampass, homedir_len?homedir:NULL, True);
138 pdb_set_dir_drive (sampass, dir_drive_len?dir_drive:NULL, True);
139 pdb_set_logon_script (sampass, logon_script_len?logon_script:NULL, True);
140 pdb_set_profile_path (sampass, profile_path_len?profile_path:NULL, True);
141 pdb_set_acct_desc (sampass, acct_desc_len?acct_desc:NULL);
142 pdb_set_workstations (sampass, workstations_len?workstations:NULL);
143 pdb_set_munged_dial (sampass, munged_dial_len?munged_dial:NULL);
144 if (!pdb_set_lanman_passwd(sampass, lmpwlen?lm_pw_ptr:NULL)) {
148 if (!pdb_set_nt_passwd(sampass, ntpwlen?nt_pw_ptr:NULL)) {
153 /*pdb_set_uid(sampass, uid);
154 pdb_set_gid(sampass, gid);*/
155 pdb_set_user_rid(sampass, user_rid);
156 pdb_set_group_rid(sampass, group_rid);
157 pdb_set_unknown_3(sampass, unknown_3);
158 pdb_set_hours_len(sampass, hours_len);
159 pdb_set_unknown_5(sampass, unknown_5);
160 pdb_set_unknown_6(sampass, unknown_6);
161 pdb_set_acct_ctrl(sampass, acct_ctrl);
162 pdb_set_logon_divs(sampass, logon_divs);
163 pdb_set_hours(sampass, hours);
169 SAFE_FREE(nt_username);
172 SAFE_FREE(dir_drive);
173 SAFE_FREE(logon_script);
174 SAFE_FREE(profile_path);
175 SAFE_FREE(acct_desc);
176 SAFE_FREE(workstations);
177 SAFE_FREE(munged_dial);
182 /**********************************************************************
183 Intialize a BYTE buffer from a SAM_ACCOUNT struct
184 *********************************************************************/
185 static uint32 init_buffer_from_sam (uint8 **buf, const SAM_ACCOUNT *sampass)
189 /* times are stored as 32bit integer
190 take care on system with 64bit wide time_t
196 pass_can_change_time,
197 pass_must_change_time;
198 const char *username;
200 const char *nt_username;
201 const char *dir_drive;
202 const char *unknown_str;
203 const char *munged_dial;
204 const char *fullname;
206 const char *logon_script;
207 const char *profile_path;
208 const char *acct_desc;
209 const char *workstations;
210 uint32 username_len, domain_len, nt_username_len,
211 dir_drive_len, unknown_str_len, munged_dial_len,
212 fullname_len, homedir_len, logon_script_len,
213 profile_path_len, acct_desc_len, workstations_len;
217 uint32 lm_pw_len = 16;
218 uint32 nt_pw_len = 16;
220 /* do we have a valid SAM_ACCOUNT pointer? */
221 if (sampass == NULL) {
222 DEBUG(0, ("init_buffer_from_sam: SAM_ACCOUNT is NULL!\n"));
229 logon_time = (uint32)pdb_get_logon_time(sampass);
230 logoff_time = (uint32)pdb_get_logoff_time(sampass);
231 kickoff_time = (uint32)pdb_get_kickoff_time(sampass);
232 pass_can_change_time = (uint32)pdb_get_pass_can_change_time(sampass);
233 pass_must_change_time = (uint32)pdb_get_pass_must_change_time(sampass);
234 pass_last_set_time = (uint32)pdb_get_pass_last_set_time(sampass);
237 username = pdb_get_username(sampass);
239 username_len = strlen(username) +1;
242 domain = pdb_get_domain(sampass);
244 domain_len = strlen(domain) +1;
247 nt_username = pdb_get_nt_username(sampass);
249 nt_username_len = strlen(nt_username) +1;
252 dir_drive = pdb_get_dirdrive(sampass);
254 dir_drive_len = strlen(dir_drive) +1;
259 munged_dial = pdb_get_munged_dial(sampass);
261 munged_dial_len = strlen(munged_dial) +1;
265 fullname = pdb_get_fullname(sampass);
267 fullname_len = strlen(fullname) +1;
270 homedir = pdb_get_homedir(sampass);
272 homedir_len = strlen(homedir) +1;
275 logon_script = pdb_get_logon_script(sampass);
277 logon_script_len = strlen(logon_script) +1;
279 logon_script_len = 0;
280 profile_path = pdb_get_profile_path(sampass);
282 profile_path_len = strlen(profile_path) +1;
284 profile_path_len = 0;
285 acct_desc = pdb_get_acct_desc(sampass);
287 acct_desc_len = strlen(acct_desc) +1;
290 workstations = pdb_get_workstations(sampass);
292 workstations_len = strlen(workstations) +1;
294 workstations_len = 0;
296 lm_pw = pdb_get_lanman_passwd(sampass);
300 nt_pw = pdb_get_nt_passwd(sampass);
304 /* one time to get the size needed */
305 len = tdb_pack(NULL, 0, TDB_FORMAT_STRING,
310 pass_can_change_time,
311 pass_must_change_time,
312 username_len, username,
314 nt_username_len, nt_username,
315 fullname_len, fullname,
316 homedir_len, homedir,
317 dir_drive_len, dir_drive,
318 logon_script_len, logon_script,
319 profile_path_len, profile_path,
320 acct_desc_len, acct_desc,
321 workstations_len, workstations,
322 unknown_str_len, unknown_str,
323 munged_dial_len, munged_dial,
324 pdb_get_user_rid(sampass),
325 pdb_get_group_rid(sampass),
328 pdb_get_acct_ctrl(sampass),
329 pdb_get_unknown3(sampass),
330 pdb_get_logon_divs(sampass),
331 pdb_get_hours_len(sampass),
332 MAX_HOURS_LEN, pdb_get_hours(sampass),
333 pdb_get_unknown5(sampass),
334 pdb_get_unknown6(sampass));
337 /* malloc the space needed */
338 if ( (*buf=(uint8*)malloc(len)) == NULL) {
339 DEBUG(0,("init_buffer_from_sam: Unable to malloc() memory for buffer!\n"));
343 /* now for the real call to tdb_pack() */
344 buflen = tdb_pack(*buf, len, TDB_FORMAT_STRING,
349 pass_can_change_time,
350 pass_must_change_time,
351 username_len, username,
353 nt_username_len, nt_username,
354 fullname_len, fullname,
355 homedir_len, homedir,
356 dir_drive_len, dir_drive,
357 logon_script_len, logon_script,
358 profile_path_len, profile_path,
359 acct_desc_len, acct_desc,
360 workstations_len, workstations,
361 unknown_str_len, unknown_str,
362 munged_dial_len, munged_dial,
363 pdb_get_user_rid(sampass),
364 pdb_get_group_rid(sampass),
367 pdb_get_acct_ctrl(sampass),
368 pdb_get_unknown3(sampass),
369 pdb_get_logon_divs(sampass),
370 pdb_get_hours_len(sampass),
371 MAX_HOURS_LEN, pdb_get_hours(sampass),
372 pdb_get_unknown5(sampass),
373 pdb_get_unknown6(sampass));
376 /* check to make sure we got it correct */
386 /***************************************************************
387 Open the TDB passwd database for SAM account enumeration.
388 ****************************************************************/
390 BOOL pdb_setsampwent(BOOL update)
394 get_private_directory(tdbfile);
395 pstrcat (tdbfile, PASSDB_FILE_NAME);
397 /* Open tdb passwd */
398 if (!(global_tdb_ent.passwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, update?(O_RDWR|O_CREAT):O_RDONLY, 0600)))
400 DEBUG(0, ("Unable to open/create TDB passwd\n"));
404 global_tdb_ent.key = tdb_firstkey(global_tdb_ent.passwd_tdb);
409 /***************************************************************
410 End enumeration of the TDB passwd list.
411 ****************************************************************/
413 void pdb_endsampwent(void)
415 if (global_tdb_ent.passwd_tdb) {
416 tdb_close(global_tdb_ent.passwd_tdb);
417 global_tdb_ent.passwd_tdb = NULL;
420 DEBUG(7, ("endtdbpwent: closed sam database.\n"));
423 /*****************************************************************
424 Get one SAM_ACCOUNT from the TDB (next in line)
425 *****************************************************************/
427 BOOL pdb_getsampwent(SAM_ACCOUNT *user)
433 char *prefix = USERPREFIX;
434 int prefixlen = strlen (prefix);
436 const char *sam_user;
440 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
444 /* skip all non-USER entries (eg. RIDs) */
445 while ((global_tdb_ent.key.dsize != 0) && (strncmp(global_tdb_ent.key.dptr, prefix, prefixlen)))
446 /* increment to next in line */
447 global_tdb_ent.key = tdb_nextkey(global_tdb_ent.passwd_tdb, global_tdb_ent.key);
449 /* do we have an valid interation pointer? */
450 if(global_tdb_ent.passwd_tdb == NULL) {
451 DEBUG(0,("pdb_get_sampwent: Bad TDB Context pointer.\n"));
455 data = tdb_fetch(global_tdb_ent.passwd_tdb, global_tdb_ent.key);
457 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
461 /* unpack the buffer */
462 if (!init_sam_from_buffer(user, data.dptr, data.dsize)) {
463 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
464 SAFE_FREE(data.dptr);
467 SAFE_FREE(data.dptr);
469 /* validate the account and fill in UNIX uid and gid. sys_getpwnam()
470 is used instaed of Get_Pwnam() as we do not need to try case
472 if ((pw=sys_getpwnam(pdb_get_username(user))) == NULL) {
473 DEBUG(0,("pdb_getsampwent: getpwnam(%s) return NULL. User does not exist!\n",
474 pdb_get_username(user)));
480 pdb_set_uid(user, uid);
481 pdb_set_gid(user, gid);
483 /* 21 days from present */
484 pdb_set_pass_must_change_time(user, time(NULL)+1814400);
486 sam_user = pdb_get_username(user);
487 pstrcpy(sam_subst, pdb_get_logon_script(user));
488 standard_sub_advanced(-1, sam_user, "", gid, sam_user, sam_subst);
489 pdb_set_logon_script(user, sam_subst, True);
490 pstrcpy(sam_subst, pdb_get_profile_path(user));
491 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_username(user), sam_subst);
492 pdb_set_profile_path(user, sam_subst, True);
493 pstrcpy(sam_subst, pdb_get_homedir(user));
494 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_username(user), sam_subst);
495 pdb_set_homedir(user, sam_subst, True);
497 /* increment to next in line */
498 global_tdb_ent.key = tdb_nextkey(global_tdb_ent.passwd_tdb, global_tdb_ent.key);
503 /******************************************************************
504 Lookup a name in the SAM TDB
505 ******************************************************************/
507 BOOL pdb_getsampwnam (SAM_ACCOUNT *user, const char *sname)
509 TDB_CONTEXT *pwd_tdb;
518 const char *sam_user;
522 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
526 /* Data is stored in all lower-case */
527 unix_strlower(sname, -1, name, sizeof(name));
529 get_private_directory(tdbfile);
530 pstrcat(tdbfile, PASSDB_FILE_NAME);
533 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
535 key.dsize = strlen(keystr) + 1;
537 /* open the accounts TDB */
538 if (!(pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
539 DEBUG(0, ("pdb_getsampwnam: Unable to open TDB passwd!\n"));
544 data = tdb_fetch(pwd_tdb, key);
546 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
547 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
552 /* unpack the buffer */
553 if (!init_sam_from_buffer(user, data.dptr, data.dsize)) {
554 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
555 SAFE_FREE(data.dptr);
559 SAFE_FREE(data.dptr);
561 /* no further use for database, close it now */
564 /* validate the account and fill in UNIX uid and gid. sys_getpwnam()
565 is used instead of Get_Pwnam() as we do not need to try case
567 if ((pw=sys_getpwnam(pdb_get_username(user)))) {
570 pdb_set_uid(user, uid);
571 pdb_set_gid(user, gid);
573 /* 21 days from present */
574 pdb_set_pass_must_change_time(user, time(NULL)+1814400);
576 sam_user = pdb_get_username(user);
577 pstrcpy(sam_subst, pdb_get_logon_script(user));
578 standard_sub_advanced(-1, sam_user, "", gid, sam_user, sam_subst);
579 pdb_set_logon_script(user, sam_subst, True);
580 pstrcpy(sam_subst, pdb_get_profile_path(user));
581 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_username(user), sam_subst);
582 pdb_set_profile_path(user, sam_subst, True);
583 pstrcpy(sam_subst, pdb_get_homedir(user));
584 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_username(user), sam_subst);
585 pdb_set_homedir(user, sam_subst, True);
588 DEBUG(0,("pdb_getsampwent: getpwnam(%s) return NULL. User does not exist!\n",
589 pdb_get_username(user)));
596 /***************************************************************************
598 **************************************************************************/
600 BOOL pdb_getsampwrid (SAM_ACCOUNT *user, uint32 rid)
602 TDB_CONTEXT *pwd_tdb;
609 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
613 get_private_directory(tdbfile);
614 pstrcat (tdbfile, PASSDB_FILE_NAME);
617 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
619 key.dsize = strlen (keystr) + 1;
621 /* open the accounts TDB */
622 if (!(pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
623 DEBUG(0, ("pdb_getsampwrid: Unable to open TDB rid database!\n"));
628 data = tdb_fetch (pwd_tdb, key);
630 DEBUG(5,("pdb_getsampwrid (TDB): error fetching database.\n"));
631 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
636 fstrcpy (name, data.dptr);
637 SAFE_FREE(data.dptr);
641 return pdb_getsampwnam (user, name);
644 /***************************************************************************
646 ****************************************************************************/
648 BOOL pdb_delete_sam_account(const char *sname)
650 SAM_ACCOUNT *sam_pass = NULL;
651 TDB_CONTEXT *pwd_tdb;
658 unix_strlower(sname, -1, name, sizeof(name));
660 get_private_directory(tdbfile);
661 pstrcat (tdbfile, PASSDB_FILE_NAME);
664 if (!(pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDWR, 0600))) {
665 DEBUG(0, ("Unable to open TDB passwd!"));
669 /* set the search key */
670 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
672 key.dsize = strlen (keystr) + 1;
675 data = tdb_fetch (pwd_tdb, key);
677 DEBUG(5,("pdb_delete_sam_account (TDB): error fetching database.\n"));
678 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
683 /* unpack the buffer */
684 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pass))) {
689 if (!init_sam_from_buffer (sam_pass, data.dptr, data.dsize)) {
690 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
692 SAFE_FREE(data.dptr);
695 SAFE_FREE(data.dptr);
697 rid = pdb_get_user_rid(sam_pass);
699 pdb_free_sam (&sam_pass);
701 /* it's outaa here! 8^) */
702 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
703 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
704 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
709 /* delete also the RID key */
711 /* set the search key */
712 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
714 key.dsize = strlen (keystr) + 1;
716 /* it's outaa here! 8^) */
717 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
718 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
719 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
729 /***************************************************************************
731 ****************************************************************************/
733 static BOOL tdb_update_sam(const SAM_ACCOUNT* newpwd, BOOL override, int flag)
735 TDB_CONTEXT *pwd_tdb = NULL;
743 get_private_directory(tdbfile);
744 pstrcat (tdbfile, PASSDB_FILE_NAME);
746 /* if we don't have a RID, then FAIL */
747 if (!pdb_get_user_rid(newpwd))
748 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a RID\n",pdb_get_username(newpwd)));
749 if (!pdb_get_group_rid(newpwd))
750 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a primary group RID\n",pdb_get_username(newpwd)));
752 /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
753 if ((data.dsize=init_buffer_from_sam (&buf, newpwd)) == -1) {
754 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
760 unix_strlower(pdb_get_username(newpwd), -1, name, sizeof(name));
762 /* setup the USER index key */
763 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
765 key.dsize = strlen (keystr) + 1;
767 /* invalidate the existing TDB iterator if it is open */
768 if (global_tdb_ent.passwd_tdb) {
769 tdb_close(global_tdb_ent.passwd_tdb);
770 global_tdb_ent.passwd_tdb = NULL;
773 /* open the account TDB passwd*/
774 pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0600);
777 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd!\n"));
781 /* add the account */
782 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
783 DEBUG(0, ("Unable to modify passwd TDB!"));
784 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
790 data.dsize = sizeof(fstring);
793 /* setup the RID index key */
794 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, pdb_get_user_rid(newpwd));
796 key.dsize = strlen (keystr) + 1;
798 /* add the reference */
799 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
800 DEBUG(0, ("Unable to modify TDB passwd !"));
801 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
814 /***************************************************************************
815 Modifies an existing SAM_ACCOUNT
816 ****************************************************************************/
818 BOOL pdb_update_sam_account (const SAM_ACCOUNT *newpwd, BOOL override)
820 return (tdb_update_sam(newpwd, override, TDB_MODIFY));
823 /***************************************************************************
824 Adds an existing SAM_ACCOUNT
825 ****************************************************************************/
827 BOOL pdb_add_sam_account (const SAM_ACCOUNT *newpwd)
829 return (tdb_update_sam(newpwd, True, TDB_INSERT));
833 /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
834 void samtdb_dummy_function(void) { } /* stop some compilers complaining */
835 #endif /* WITH_TDB_SAM */