s3: Make use of ZERO_STRUCTP
[samba.git] / source3 / passdb / machine_sid.c
index 8fafcbbbd4678b9cf707a7fae1abad04184d6bb1..d70e7c4dc4fcf7252edbe7bc53b1bcb6f0b44abe 100644 (file)
@@ -5,17 +5,17 @@
    Copyright (C) Andrew Tridgell               2002
    Copyright (C) Gerald (Jerry) Carter         2000
    Copyright (C) Stefan (metze) Metzmacher     2002
-      
+
    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 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    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, see <http://www.gnu.org/licenses/>.
 */
@@ -41,15 +41,15 @@ static bool read_sid_from_file(const char *fname, DOM_SID *sid)
        int numlines;
        bool ret;
 
-       lines = file_lines_load(fname, &numlines,0);
-       
+       lines = file_lines_load(fname, &numlines,0, NULL);
+
        if (!lines || numlines < 1) {
-               if (lines) file_lines_free(lines);
+               TALLOC_FREE(lines);
                return False;
        }
-       
+
        ret = string_to_sid(sid, lines[0]);
-       file_lines_free(lines);
+       TALLOC_FREE(lines);
        return ret;
 }
 
@@ -61,7 +61,8 @@ static void generate_random_sid(DOM_SID *sid)
        int i;
        uchar raw_sid_data[12];
 
-       memset((char *)sid, '\0', sizeof(*sid));
+       ZERO_STRUCTP(sid);
+
        sid->sid_rev_num = 1;
        sid->id_auth[5] = 5;
        sid->num_auths = 0;
@@ -81,7 +82,7 @@ static DOM_SID *pdb_generate_sam_sid(void)
        DOM_SID domain_sid;
        char *fname = NULL;
        DOM_SID *sam_sid;
-       
+
        if(!(sam_sid=SMB_MALLOC_P(DOM_SID)))
                return NULL;
 
@@ -124,7 +125,6 @@ static DOM_SID *pdb_generate_sam_sid(void)
                }
 
                return sam_sid;
-               
        }
 
        /* check for an old MACHINE.SID file for backwards compatibility */
@@ -181,16 +181,38 @@ static DOM_SID *pdb_generate_sam_sid(void)
 /* 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");
+       }
+
        return global_sam_sid;
 }