#include "includes.h"
static BOOL read_sid_from_file(int fd, char *sid_file)
{
fstring fline;
memset(fline, '\0', sizeof(fline));
if(read(fd, fline, sizeof(fline) -1 ) < 0) {
DEBUG(0,("unable to read file %s. Error was %s\n",
sid_file, strerror(errno) ));
return False;
}
fline[sizeof(fline)-1] = '\0';
if(!string_to_sid( &global_sam_sid, fline)) {
DEBUG(0,("unable to generate machine SID.\n"));
return False;
}
return True;
}
BOOL pdb_generate_sam_sid(void)
{
int fd;
pstring sid_file;
fstring sid_string;
SMB_STRUCT_STAT st;
BOOL overwrite_bad_sid = False;
generate_wellknown_sids();
get_private_directory(sid_file);
if (!directory_exist(sid_file, NULL)) {
if (mkdir(sid_file, 0700) != 0) {
DEBUG(0,("can't create private directory %s : %s\n",
sid_file, strerror(errno)));
return False;
}
}
pstrcat(sid_file, "/MACHINE.SID");
if((fd = sys_open(sid_file, O_RDWR | O_CREAT, 0644)) == -1) {
DEBUG(0,("unable to open or create file %s. Error was %s\n",
sid_file, strerror(errno) ));
return False;
}
if(sys_fstat( fd, &st) < 0) {
DEBUG(0,("unable to stat file %s. Error was %s\n",
sid_file, strerror(errno) ));
close(fd);
return False;
}
if(st.st_size > 0) {
if(!read_sid_from_file( fd, sid_file)) {
DEBUG(0,("unable to read file %s. Error was %s\n",
sid_file, strerror(errno) ));
close(fd);
return False;
}
if(global_sam_sid.num_auths > 0 && global_sam_sid.sub_auths[0] == 0x21) {
overwrite_bad_sid = True;
global_sam_sid.sub_auths[0] = 21;
DEBUG(5,("pdb_generate_sam_sid: Old (incorrect) sid id_auth of hex 21 \
detected - re-writing to be decimal 21 instead.\n" ));
sid_to_string(sid_string, &global_sam_sid);
if(sys_lseek(fd, (SMB_OFF_T)0, SEEK_SET) != 0) {
DEBUG(0,("unable to seek file file %s. Error was %s\n",
sid_file, strerror(errno) ));
close(fd);
return False;
}
} else {
close(fd);
return True;
}
} else {
int i;
uchar raw_sid_data[12];
DOM_SID mysid;
memset((char *)&mysid, '\0', sizeof(DOM_SID));
mysid.sid_rev_num = 1;
mysid.id_auth[5] = 5;
mysid.num_auths = 0;
mysid.sub_auths[mysid.num_auths++] = 21;
generate_random_buffer( raw_sid_data, 12, True);
for( i = 0; i < 3; i++)
mysid.sub_auths[mysid.num_auths++] = IVAL(raw_sid_data, i*4);
sid_to_string(sid_string, &mysid);
}
fstrcat(sid_string, "\n");
if(!string_to_sid( &global_sam_sid, sid_string)) {
DEBUG(0,("unable to generate machine SID.\n"));
return False;
}
if(!do_file_lock( fd, 60, F_WRLCK)) {
DEBUG(0,("unable to lock file %s. Error was %s\n",
sid_file, strerror(errno) ));
close(fd);
return False;
}
if(!overwrite_bad_sid) {
if(sys_fstat( fd, &st) < 0) {
DEBUG(0,("unable to stat file %s. Error was %s\n",
sid_file, strerror(errno) ));
close(fd);
return False;
}
if(st.st_size > 0) {
do_file_lock( fd, 60, F_UNLCK);
if(!read_sid_from_file( fd, sid_file)) {
DEBUG(0,("unable to read file %s. Error was %s\n",
sid_file, strerror(errno) ));
close(fd);
return False;
}
close(fd);
return True;
}
}
if(chmod(sid_file, 0644) < 0) {
DEBUG(0,("unable to set correct permissions on file %s. \
Error was %s\n", sid_file, strerror(errno) ));
do_file_lock( fd, 60, F_UNLCK);
close(fd);
return False;
}
if(write( fd, sid_string, strlen(sid_string)) != strlen(sid_string)) {
DEBUG(0,("unable to write file %s. Error was %s\n",
sid_file, strerror(errno) ));
do_file_lock( fd, 60, F_UNLCK);
close(fd);
return False;
}
do_file_lock( fd, 60, F_UNLCK);
close(fd);
return True;
}