passdb/machine_sid.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    Password and authentication handling
00004    Copyright (C) Jeremy Allison                 1996-2002
00005    Copyright (C) Andrew Tridgell                2002
00006    Copyright (C) Gerald (Jerry) Carter          2000
00007    Copyright (C) Stefan (metze) Metzmacher      2002
00008       
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 2 of the License, or
00012    (at your option) any later version.
00013    
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018    
00019    You should have received a copy of the GNU General Public License
00020    along with this program; if not, write to the Free Software
00021    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00022 */
00023 
00024 #include "includes.h"
00025 
00026 /* NOTE! the global_sam_sid is the SID of our local SAM. This is only
00027    equal to the domain SID when we are a DC, otherwise its our
00028    workstation SID */
00029 static DOM_SID *global_sam_sid=NULL;
00030 
00031 #undef DBGC_CLASS
00032 #define DBGC_CLASS DBGC_PASSDB
00033 
00034 /****************************************************************************
00035  Read a SID from a file. This is for compatibility with the old MACHINE.SID
00036  style of SID storage
00037 ****************************************************************************/
00038 
00039 static BOOL read_sid_from_file(const char *fname, DOM_SID *sid)
00040 {
00041         char **lines;
00042         int numlines;
00043         BOOL ret;
00044 
00045         lines = file_lines_load(fname, &numlines,0);
00046         
00047         if (!lines || numlines < 1) {
00048                 if (lines) file_lines_free(lines);
00049                 return False;
00050         }
00051         
00052         ret = string_to_sid(sid, lines[0]);
00053         file_lines_free(lines);
00054         return ret;
00055 }
00056 
00057 /*
00058   generate a random sid - used to build our own sid if we don't have one
00059 */
00060 static void generate_random_sid(DOM_SID *sid)
00061 {
00062         int i;
00063         uchar raw_sid_data[12];
00064 
00065         memset((char *)sid, '\0', sizeof(*sid));
00066         sid->sid_rev_num = 1;
00067         sid->id_auth[5] = 5;
00068         sid->num_auths = 0;
00069         sid->sub_auths[sid->num_auths++] = 21;
00070 
00071         generate_random_buffer(raw_sid_data, 12);
00072         for (i = 0; i < 3; i++)
00073                 sid->sub_auths[sid->num_auths++] = IVAL(raw_sid_data, i*4);
00074 }
00075 
00076 /****************************************************************************
00077  Generate the global machine sid.
00078 ****************************************************************************/
00079 
00080 static DOM_SID *pdb_generate_sam_sid(void)
00081 {
00082         DOM_SID domain_sid;
00083         char *fname = NULL;
00084         DOM_SID *sam_sid;
00085         
00086         if(!(sam_sid=SMB_MALLOC_P(DOM_SID)))
00087                 return NULL;
00088 
00089         if ( IS_DC ) {
00090                 if (secrets_fetch_domain_sid(lp_workgroup(), &domain_sid)) {
00091                         sid_copy(sam_sid, &domain_sid);
00092                         return sam_sid;
00093                 }
00094         }
00095 
00096         if (secrets_fetch_domain_sid(global_myname(), sam_sid)) {
00097 
00098                 /* We got our sid. If not a pdc/bdc, we're done. */
00099                 if ( !IS_DC )
00100                         return sam_sid;
00101 
00102                 if (!secrets_fetch_domain_sid(lp_workgroup(), &domain_sid)) {
00103 
00104                         /* No domain sid and we're a pdc/bdc. Store it */
00105 
00106                         if (!secrets_store_domain_sid(lp_workgroup(), sam_sid)) {
00107                                 DEBUG(0,("pdb_generate_sam_sid: Can't store domain SID as a pdc/bdc.\n"));
00108                                 SAFE_FREE(sam_sid);
00109                                 return NULL;
00110                         }
00111                         return sam_sid;
00112                 }
00113 
00114                 if (!sid_equal(&domain_sid, sam_sid)) {
00115 
00116                         /* Domain name sid doesn't match global sam sid. Re-store domain sid as 'local' sid. */
00117 
00118                         DEBUG(0,("pdb_generate_sam_sid: Mismatched SIDs as a pdc/bdc.\n"));
00119                         if (!secrets_store_domain_sid(global_myname(), &domain_sid)) {
00120                                 DEBUG(0,("pdb_generate_sam_sid: Can't re-store domain SID for local sid as PDC/BDC.\n"));
00121                                 SAFE_FREE(sam_sid);
00122                                 return NULL;
00123                         }
00124                         return sam_sid;
00125                 }
00126 
00127                 return sam_sid;
00128                 
00129         }
00130 
00131         /* check for an old MACHINE.SID file for backwards compatibility */
00132         asprintf(&fname, "%s/MACHINE.SID", lp_private_dir());
00133 
00134         if (read_sid_from_file(fname, sam_sid)) {
00135                 /* remember it for future reference and unlink the old MACHINE.SID */
00136                 if (!secrets_store_domain_sid(global_myname(), sam_sid)) {
00137                         DEBUG(0,("pdb_generate_sam_sid: Failed to store SID from file.\n"));
00138                         SAFE_FREE(fname);
00139                         SAFE_FREE(sam_sid);
00140                         return NULL;
00141                 }
00142                 unlink(fname);
00143                 if ( !IS_DC ) {
00144                         if (!secrets_store_domain_sid(lp_workgroup(), sam_sid)) {
00145                                 DEBUG(0,("pdb_generate_sam_sid: Failed to store domain SID from file.\n"));
00146                                 SAFE_FREE(fname);
00147                                 SAFE_FREE(sam_sid);
00148                                 return NULL;
00149                         }
00150                 }
00151 
00152                 /* Stored the old sid from MACHINE.SID successfully.*/
00153                 SAFE_FREE(fname);
00154                 return sam_sid;
00155         }
00156 
00157         SAFE_FREE(fname);
00158 
00159         /* we don't have the SID in secrets.tdb, we will need to
00160            generate one and save it */
00161         generate_random_sid(sam_sid);
00162 
00163         if (!secrets_store_domain_sid(global_myname(), sam_sid)) {
00164                 DEBUG(0,("pdb_generate_sam_sid: Failed to store generated machine SID.\n"));
00165                 SAFE_FREE(sam_sid);
00166                 return NULL;
00167         }
00168         if ( IS_DC ) {
00169                 if (!secrets_store_domain_sid(lp_workgroup(), sam_sid)) {
00170                         DEBUG(0,("pdb_generate_sam_sid: Failed to store generated domain SID.\n"));
00171                         SAFE_FREE(sam_sid);
00172                         return NULL;
00173                 }
00174         }
00175 
00176         return sam_sid;
00177 }   
00178 
00179 /* return our global_sam_sid */
00180 DOM_SID *get_global_sam_sid(void)
00181 {
00182         if (global_sam_sid != NULL)
00183                 return global_sam_sid;
00184         
00185         /* memory for global_sam_sid is allocated in 
00186            pdb_generate_sam_sid() as needed */
00187 
00188         if (!(global_sam_sid = pdb_generate_sam_sid())) {
00189                 smb_panic("Could not generate a machine SID\n");
00190         }
00191 
00192         return global_sam_sid;
00193 }
00194 
00195 /** 
00196  * Force get_global_sam_sid to requery the backends 
00197  */
00198 void reset_global_sam_sid(void) 
00199 {
00200         SAFE_FREE(global_sam_sid);
00201 }
00202 
00203 /*****************************************************************
00204  Check if the SID is our domain SID (S-1-5-21-x-y-z).
00205 *****************************************************************/  
00206 
00207 BOOL sid_check_is_domain(const DOM_SID *sid)
00208 {
00209         return sid_equal(sid, get_global_sam_sid());
00210 }
00211 
00212 /*****************************************************************
00213  Check if the SID is our domain SID (S-1-5-21-x-y-z).
00214 *****************************************************************/  
00215 
00216 BOOL sid_check_is_in_our_domain(const DOM_SID *sid)
00217 {
00218         DOM_SID dom_sid;
00219         uint32 rid;
00220 
00221         sid_copy(&dom_sid, sid);
00222         sid_split_rid(&dom_sid, &rid);
00223         
00224         return sid_equal(&dom_sid, get_global_sam_sid());
00225 }

Sambaに対してSat Aug 29 21:23:12 2009に生成されました。  doxygen 1.4.7