| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 | Password and authentication handling
|
|---|
| 4 | Copyright (C) Jeremy Allison 1996-2002
|
|---|
| 5 | Copyright (C) Andrew Tridgell 2002
|
|---|
| 6 | Copyright (C) Gerald (Jerry) Carter 2000
|
|---|
| 7 | Copyright (C) Stefan (metze) Metzmacher 2002
|
|---|
| 8 |
|
|---|
| 9 | This program is free software; you can redistribute it and/or modify
|
|---|
| 10 | it under the terms of the GNU General Public License as published by
|
|---|
| 11 | the Free Software Foundation; either version 2 of the License, or
|
|---|
| 12 | (at your option) any later version.
|
|---|
| 13 |
|
|---|
| 14 | This program is distributed in the hope that it will be useful,
|
|---|
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 17 | GNU General Public License for more details.
|
|---|
| 18 |
|
|---|
| 19 | You should have received a copy of the GNU General Public License
|
|---|
| 20 | along with this program; if not, write to the Free Software
|
|---|
| 21 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|---|
| 22 | */
|
|---|
| 23 |
|
|---|
| 24 | #include "includes.h"
|
|---|
| 25 |
|
|---|
| 26 | /* NOTE! the global_sam_sid is the SID of our local SAM. This is only
|
|---|
| 27 | equal to the domain SID when we are a DC, otherwise its our
|
|---|
| 28 | workstation SID */
|
|---|
| 29 | static DOM_SID *global_sam_sid=NULL;
|
|---|
| 30 |
|
|---|
| 31 | #undef DBGC_CLASS
|
|---|
| 32 | #define DBGC_CLASS DBGC_PASSDB
|
|---|
| 33 |
|
|---|
| 34 | /****************************************************************************
|
|---|
| 35 | Read a SID from a file. This is for compatibility with the old MACHINE.SID
|
|---|
| 36 | style of SID storage
|
|---|
| 37 | ****************************************************************************/
|
|---|
| 38 |
|
|---|
| 39 | static BOOL read_sid_from_file(const char *fname, DOM_SID *sid)
|
|---|
| 40 | {
|
|---|
| 41 | char **lines;
|
|---|
| 42 | int numlines;
|
|---|
| 43 | BOOL ret;
|
|---|
| 44 |
|
|---|
| 45 | lines = file_lines_load(fname, &numlines,0);
|
|---|
| 46 |
|
|---|
| 47 | if (!lines || numlines < 1) {
|
|---|
| 48 | if (lines) file_lines_free(lines);
|
|---|
| 49 | return False;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | ret = string_to_sid(sid, lines[0]);
|
|---|
| 53 | file_lines_free(lines);
|
|---|
| 54 | return ret;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /*
|
|---|
| 58 | generate a random sid - used to build our own sid if we don't have one
|
|---|
| 59 | */
|
|---|
| 60 | static void generate_random_sid(DOM_SID *sid)
|
|---|
| 61 | {
|
|---|
| 62 | int i;
|
|---|
| 63 | uchar raw_sid_data[12];
|
|---|
| 64 |
|
|---|
| 65 | memset((char *)sid, '\0', sizeof(*sid));
|
|---|
| 66 | sid->sid_rev_num = 1;
|
|---|
| 67 | sid->id_auth[5] = 5;
|
|---|
| 68 | sid->num_auths = 0;
|
|---|
| 69 | sid->sub_auths[sid->num_auths++] = 21;
|
|---|
| 70 |
|
|---|
| 71 | generate_random_buffer(raw_sid_data, 12);
|
|---|
| 72 | for (i = 0; i < 3; i++)
|
|---|
| 73 | sid->sub_auths[sid->num_auths++] = IVAL(raw_sid_data, i*4);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /****************************************************************************
|
|---|
| 77 | Generate the global machine sid.
|
|---|
| 78 | ****************************************************************************/
|
|---|
| 79 |
|
|---|
| 80 | static DOM_SID *pdb_generate_sam_sid(void)
|
|---|
| 81 | {
|
|---|
| 82 | DOM_SID domain_sid;
|
|---|
| 83 | char *fname = NULL;
|
|---|
| 84 | DOM_SID *sam_sid;
|
|---|
| 85 |
|
|---|
| 86 | if(!(sam_sid=SMB_MALLOC_P(DOM_SID)))
|
|---|
| 87 | return NULL;
|
|---|
| 88 |
|
|---|
| 89 | if ( IS_DC ) {
|
|---|
| 90 | if (secrets_fetch_domain_sid(lp_workgroup(), &domain_sid)) {
|
|---|
| 91 | sid_copy(sam_sid, &domain_sid);
|
|---|
| 92 | return sam_sid;
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | if (secrets_fetch_domain_sid(global_myname(), sam_sid)) {
|
|---|
| 97 |
|
|---|
| 98 | /* We got our sid. If not a pdc/bdc, we're done. */
|
|---|
| 99 | if ( !IS_DC )
|
|---|
| 100 | return sam_sid;
|
|---|
| 101 |
|
|---|
| 102 | if (!secrets_fetch_domain_sid(lp_workgroup(), &domain_sid)) {
|
|---|
| 103 |
|
|---|
| 104 | /* No domain sid and we're a pdc/bdc. Store it */
|
|---|
| 105 |
|
|---|
| 106 | if (!secrets_store_domain_sid(lp_workgroup(), sam_sid)) {
|
|---|
| 107 | DEBUG(0,("pdb_generate_sam_sid: Can't store domain SID as a pdc/bdc.\n"));
|
|---|
| 108 | SAFE_FREE(sam_sid);
|
|---|
| 109 | return NULL;
|
|---|
| 110 | }
|
|---|
| 111 | return sam_sid;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | if (!sid_equal(&domain_sid, sam_sid)) {
|
|---|
| 115 |
|
|---|
| 116 | /* Domain name sid doesn't match global sam sid. Re-store domain sid as 'local' sid. */
|
|---|
| 117 |
|
|---|
| 118 | DEBUG(0,("pdb_generate_sam_sid: Mismatched SIDs as a pdc/bdc.\n"));
|
|---|
| 119 | if (!secrets_store_domain_sid(global_myname(), &domain_sid)) {
|
|---|
| 120 | DEBUG(0,("pdb_generate_sam_sid: Can't re-store domain SID for local sid as PDC/BDC.\n"));
|
|---|
| 121 | SAFE_FREE(sam_sid);
|
|---|
| 122 | return NULL;
|
|---|
| 123 | }
|
|---|
| 124 | return sam_sid;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | return sam_sid;
|
|---|
| 128 |
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /* check for an old MACHINE.SID file for backwards compatibility */
|
|---|
| 132 | asprintf(&fname, "%s/MACHINE.SID", lp_private_dir());
|
|---|
| 133 |
|
|---|
| 134 | if (read_sid_from_file(fname, sam_sid)) {
|
|---|
| 135 | /* remember it for future reference and unlink the old MACHINE.SID */
|
|---|
| 136 | if (!secrets_store_domain_sid(global_myname(), sam_sid)) {
|
|---|
| 137 | DEBUG(0,("pdb_generate_sam_sid: Failed to store SID from file.\n"));
|
|---|
| 138 | SAFE_FREE(fname);
|
|---|
| 139 | SAFE_FREE(sam_sid);
|
|---|
| 140 | return NULL;
|
|---|
| 141 | }
|
|---|
| 142 | unlink(fname);
|
|---|
| 143 | if ( !IS_DC ) {
|
|---|
| 144 | if (!secrets_store_domain_sid(lp_workgroup(), sam_sid)) {
|
|---|
| 145 | DEBUG(0,("pdb_generate_sam_sid: Failed to store domain SID from file.\n"));
|
|---|
| 146 | SAFE_FREE(fname);
|
|---|
| 147 | SAFE_FREE(sam_sid);
|
|---|
| 148 | return NULL;
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /* Stored the old sid from MACHINE.SID successfully.*/
|
|---|
| 153 | SAFE_FREE(fname);
|
|---|
| 154 | return sam_sid;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | SAFE_FREE(fname);
|
|---|
| 158 |
|
|---|
| 159 | /* we don't have the SID in secrets.tdb, we will need to
|
|---|
| 160 | generate one and save it */
|
|---|
| 161 | generate_random_sid(sam_sid);
|
|---|
| 162 |
|
|---|
| 163 | if (!secrets_store_domain_sid(global_myname(), sam_sid)) {
|
|---|
| 164 | DEBUG(0,("pdb_generate_sam_sid: Failed to store generated machine SID.\n"));
|
|---|
| 165 | SAFE_FREE(sam_sid);
|
|---|
| 166 | return NULL;
|
|---|
| 167 | }
|
|---|
| 168 | if ( IS_DC ) {
|
|---|
| 169 | if (!secrets_store_domain_sid(lp_workgroup(), sam_sid)) {
|
|---|
| 170 | DEBUG(0,("pdb_generate_sam_sid: Failed to store generated domain SID.\n"));
|
|---|
| 171 | SAFE_FREE(sam_sid);
|
|---|
| 172 | return NULL;
|
|---|
| 173 | }
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | return sam_sid;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /* return our global_sam_sid */
|
|---|
| 180 | DOM_SID *get_global_sam_sid(void)
|
|---|
| 181 | {
|
|---|
| 182 | if (global_sam_sid != NULL)
|
|---|
| 183 | return global_sam_sid;
|
|---|
| 184 |
|
|---|
| 185 | /* memory for global_sam_sid is allocated in
|
|---|
| 186 | pdb_generate_sam_sid() as needed */
|
|---|
| 187 |
|
|---|
| 188 | if (!(global_sam_sid = pdb_generate_sam_sid())) {
|
|---|
| 189 | smb_panic("Could not generate a machine SID\n");
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | return global_sam_sid;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /**
|
|---|
| 196 | * Force get_global_sam_sid to requery the backends
|
|---|
| 197 | */
|
|---|
| 198 | void reset_global_sam_sid(void)
|
|---|
| 199 | {
|
|---|
| 200 | SAFE_FREE(global_sam_sid);
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /*****************************************************************
|
|---|
| 204 | Check if the SID is our domain SID (S-1-5-21-x-y-z).
|
|---|
| 205 | *****************************************************************/
|
|---|
| 206 |
|
|---|
| 207 | BOOL sid_check_is_domain(const DOM_SID *sid)
|
|---|
| 208 | {
|
|---|
| 209 | return sid_equal(sid, get_global_sam_sid());
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | /*****************************************************************
|
|---|
| 213 | Check if the SID is our domain SID (S-1-5-21-x-y-z).
|
|---|
| 214 | *****************************************************************/
|
|---|
| 215 |
|
|---|
| 216 | BOOL sid_check_is_in_our_domain(const DOM_SID *sid)
|
|---|
| 217 | {
|
|---|
| 218 | DOM_SID dom_sid;
|
|---|
| 219 | uint32 rid;
|
|---|
| 220 |
|
|---|
| 221 | sid_copy(&dom_sid, sid);
|
|---|
| 222 | sid_split_rid(&dom_sid, &rid);
|
|---|
| 223 |
|
|---|
| 224 | return sid_equal(&dom_sid, get_global_sam_sid());
|
|---|
| 225 | }
|
|---|