Do not use a variable format string
[ira/wip.git] / source3 / passdb / machine_sid.c
index e1f7dec2a9bfa46222aa6c090e2327a5ab7d6000..c7c3cc474b9dcb213036191fa1b716e0ceb2d1d4 100644 (file)
@@ -8,7 +8,7 @@
       
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -17,8 +17,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
@@ -35,21 +34,22 @@ static DOM_SID *global_sam_sid=NULL;
  Read a SID from a file. This is for compatibility with the old MACHINE.SID
  style of SID storage
 ****************************************************************************/
-static BOOL read_sid_from_file(const char *fname, DOM_SID *sid)
+
+static bool read_sid_from_file(const char *fname, DOM_SID *sid)
 {
        char **lines;
        int numlines;
-       BOOL ret;
+       bool ret;
 
-       lines = file_lines_load(fname, &numlines);
+       lines = file_lines_load(fname, &numlines,0, NULL);
        
        if (!lines || numlines < 1) {
-               if (lines) file_lines_free(lines);
+               if (lines) TALLOC_FREE(lines);
                return False;
        }
        
        ret = string_to_sid(sid, lines[0]);
-       file_lines_free(lines);
+       TALLOC_FREE(lines);
        return ret;
 }
 
@@ -67,7 +67,7 @@ static void generate_random_sid(DOM_SID *sid)
        sid->num_auths = 0;
        sid->sub_auths[sid->num_auths++] = 21;
 
-       generate_random_buffer(raw_sid_data, 12, True);
+       generate_random_buffer(raw_sid_data, 12);
        for (i = 0; i < 3; i++)
                sid->sub_auths[sid->num_auths++] = IVAL(raw_sid_data, i*4);
 }
@@ -76,119 +76,173 @@ static void generate_random_sid(DOM_SID *sid)
  Generate the global machine sid.
 ****************************************************************************/
 
-static BOOL pdb_generate_sam_sid(void)
+static DOM_SID *pdb_generate_sam_sid(void)
 {
+       DOM_SID domain_sid;
        char *fname = NULL;
-       extern pstring global_myname;
-       extern fstring global_myworkgroup;
-       BOOL is_dc = False;
-
-       if(global_sam_sid==NULL)
-               if(!(global_sam_sid=(DOM_SID *)malloc(sizeof(DOM_SID))))
-                       return False;
-                       
-       generate_wellknown_sids();
-
-       switch (lp_server_role()) {
-       case ROLE_DOMAIN_PDC:
-       case ROLE_DOMAIN_BDC:
-               is_dc = True;
-               break;
-       default:
-               is_dc = False;
-               break;
+       DOM_SID *sam_sid;
+       
+       if(!(sam_sid=SMB_MALLOC_P(DOM_SID)))
+               return NULL;
+
+       if ( IS_DC ) {
+               if (secrets_fetch_domain_sid(lp_workgroup(), &domain_sid)) {
+                       sid_copy(sam_sid, &domain_sid);
+                       return sam_sid;
+               }
        }
 
-       if (secrets_fetch_domain_sid(global_myname, global_sam_sid)) {
-               DOM_SID domain_sid;
+       if (secrets_fetch_domain_sid(global_myname(), sam_sid)) {
 
                /* We got our sid. If not a pdc/bdc, we're done. */
-               if (!is_dc)
-                       return True;
+               if ( !IS_DC )
+                       return sam_sid;
 
-               if (!secrets_fetch_domain_sid(global_myworkgroup, &domain_sid)) {
+               if (!secrets_fetch_domain_sid(lp_workgroup(), &domain_sid)) {
 
                        /* No domain sid and we're a pdc/bdc. Store it */
 
-                       if (!secrets_store_domain_sid(global_myworkgroup, global_sam_sid)) {
+                       if (!secrets_store_domain_sid(lp_workgroup(), sam_sid)) {
                                DEBUG(0,("pdb_generate_sam_sid: Can't store domain SID as a pdc/bdc.\n"));
-                               return False;
+                               SAFE_FREE(sam_sid);
+                               return NULL;
                        }
-                       return True;
+                       return sam_sid;
                }
 
-               if (!sid_equal(&domain_sid, global_sam_sid)) {
+               if (!sid_equal(&domain_sid, sam_sid)) {
 
-                       /* Domain name sid doesn't match global sam sid. Re-store global sam sid as domain sid. */
+                       /* Domain name sid doesn't match global sam sid. Re-store domain sid as 'local' sid. */
 
                        DEBUG(0,("pdb_generate_sam_sid: Mismatched SIDs as a pdc/bdc.\n"));
-                       if (!secrets_store_domain_sid(global_myworkgroup, global_sam_sid)) {
-                               DEBUG(0,("pdb_generate_sam_sid: Can't re-store domain SID as a pdc/bdc.\n"));
-                               return False;
+                       if (!secrets_store_domain_sid(global_myname(), &domain_sid)) {
+                               DEBUG(0,("pdb_generate_sam_sid: Can't re-store domain SID for local sid as PDC/BDC.\n"));
+                               SAFE_FREE(sam_sid);
+                               return NULL;
                        }
-                       return True;
+                       return sam_sid;
                }
 
-               return True;
+               return sam_sid;
                
        }
 
        /* check for an old MACHINE.SID file for backwards compatibility */
-       asprintf(&fname, "%s/MACHINE.SID", lp_private_dir());
+       if (asprintf(&fname, "%s/MACHINE.SID", lp_private_dir()) == -1) {
+               SAFE_FREE(sam_sid);
+               return NULL;
+       }
 
-       if (read_sid_from_file(fname, global_sam_sid)) {
+       if (read_sid_from_file(fname, sam_sid)) {
                /* remember it for future reference and unlink the old MACHINE.SID */
-               if (!secrets_store_domain_sid(global_myname, global_sam_sid)) {
+               if (!secrets_store_domain_sid(global_myname(), sam_sid)) {
                        DEBUG(0,("pdb_generate_sam_sid: Failed to store SID from file.\n"));
                        SAFE_FREE(fname);
-                       return False;
+                       SAFE_FREE(sam_sid);
+                       return NULL;
                }
                unlink(fname);
-               if (is_dc) {
-                       if (!secrets_store_domain_sid(global_myworkgroup, global_sam_sid)) {
+               if ( !IS_DC ) {
+                       if (!secrets_store_domain_sid(lp_workgroup(), sam_sid)) {
                                DEBUG(0,("pdb_generate_sam_sid: Failed to store domain SID from file.\n"));
                                SAFE_FREE(fname);
-                               return False;
+                               SAFE_FREE(sam_sid);
+                               return NULL;
                        }
                }
 
                /* Stored the old sid from MACHINE.SID successfully.*/
                SAFE_FREE(fname);
-               return True;
+               return sam_sid;
        }
 
        SAFE_FREE(fname);
 
        /* we don't have the SID in secrets.tdb, we will need to
            generate one and save it */
-       generate_random_sid(global_sam_sid);
+       generate_random_sid(sam_sid);
 
-       if (!secrets_store_domain_sid(global_myname, global_sam_sid)) {
+       if (!secrets_store_domain_sid(global_myname(), sam_sid)) {
                DEBUG(0,("pdb_generate_sam_sid: Failed to store generated machine SID.\n"));
-               return False;
+               SAFE_FREE(sam_sid);
+               return NULL;
        }
-       if (is_dc) {
-               if (!secrets_store_domain_sid(global_myworkgroup, global_sam_sid)) {
+       if ( IS_DC ) {
+               if (!secrets_store_domain_sid(lp_workgroup(), sam_sid)) {
                        DEBUG(0,("pdb_generate_sam_sid: Failed to store generated domain SID.\n"));
-                       return False;
+                       SAFE_FREE(sam_sid);
+                       return NULL;
                }
        }
 
-       return True;
+       return sam_sid;
 }   
 
 /* return our global_sam_sid */
 DOM_SID *get_global_sam_sid(void)
 {
+       struct db_context *db;
+
        if (global_sam_sid != NULL)
                return global_sam_sid;
        
-       /* memory for global_sam_sid is allocated in 
-          pdb_generate_sam_sid() as needed */
+       /*
+        * memory for global_sam_sid is allocated in
+        * pdb_generate_sam_sid() as needed
+        *
+        * Note: this is garded by a transaction
+        *       to prevent races on startup which
+        *       can happen with some dbwrap backends
+        */
+
+       db = secrets_db_ctx();
+       if (!db) {
+               smb_panic("could not open secrets db");
+       }
+
+       if (db->transaction_start(db) != 0) {
+               smb_panic("could not start transaction on secrets db");
+       }
+
+       if (!(global_sam_sid = pdb_generate_sam_sid())) {
+               db->transaction_cancel(db);
+               smb_panic("could not generate a machine SID");
+       }
+
+       if (db->transaction_commit(db) != 0) {
+               smb_panic("could not start commit secrets db");
+       }
 
-       if (!pdb_generate_sam_sid())
-               global_sam_sid=NULL;    
-       
        return global_sam_sid;
 }
 
+/** 
+ * Force get_global_sam_sid to requery the backends 
+ */
+void reset_global_sam_sid(void) 
+{
+       SAFE_FREE(global_sam_sid);
+}
+
+/*****************************************************************
+ Check if the SID is our domain SID (S-1-5-21-x-y-z).
+*****************************************************************/  
+
+bool sid_check_is_domain(const DOM_SID *sid)
+{
+       return sid_equal(sid, get_global_sam_sid());
+}
+
+/*****************************************************************
+ Check if the SID is our domain SID (S-1-5-21-x-y-z).
+*****************************************************************/  
+
+bool sid_check_is_in_our_domain(const DOM_SID *sid)
+{
+       DOM_SID dom_sid;
+       uint32 rid;
+
+       sid_copy(&dom_sid, sid);
+       sid_split_rid(&dom_sid, &rid);
+       return sid_check_is_domain(&dom_sid);
+}