r9712: Bunch of small fixes
[sfrench/samba-autobuild/.git] / source4 / lib / samba3 / share_info.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Share Info parsing
4  *  Copyright (C) Andrew Tridgell              1992-1997,
5  *  Copyright (C) Jeremy Allison                                        2001.
6  *  Copyright (C) Nigel Williams                                        2001.
7  *  Copyright (C) Jelmer Vernooij                                       2005.
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 #include "librpc/gen_ndr/ndr_security.h"
26 #include "lib/tdb/include/tdbutil.h"
27 #include "lib/samba3/samba3.h"
28 #include "system/filesys.h"
29
30 #define SHARE_DATABASE_VERSION_V1 1
31 #define SHARE_DATABASE_VERSION_V2 2 /* version id in little endian. */
32
33 NTSTATUS samba3_read_share_info(const char *fn, TALLOC_CTX *ctx, struct samba3 *db)
34 {
35         int32_t vers_id;
36         TDB_CONTEXT *tdb;
37         TDB_DATA kbuf, vbuf;
38         DATA_BLOB blob;
39  
40         tdb = tdb_open(fn, 0, TDB_DEFAULT, O_RDONLY, 0600);
41         if (!tdb) {
42                 DEBUG(0,("Failed to open share info database %s (%s)\n",
43                         fn, strerror(errno) ));
44                 return NT_STATUS_UNSUCCESSFUL;
45         }
46  
47         /* Cope with byte-reversed older versions of the db. */
48         vers_id = tdb_fetch_int32(tdb, "INFO/version");
49         if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) {
50                 /* Written on a bigendian machine with old fetch_int code. Save as le. */
51                 vers_id = SHARE_DATABASE_VERSION_V2;
52         }
53
54         if (vers_id != SHARE_DATABASE_VERSION_V2) {
55                 return NT_STATUS_UNSUCCESSFUL;
56         }
57
58         for (kbuf = tdb_firstkey(tdb); kbuf.dptr; kbuf = tdb_nextkey(tdb, kbuf)) 
59         {
60                 struct ndr_pull *pull;
61                 struct samba3_share_info *share;
62                 char *name;
63                 
64                 if (strncmp(kbuf.dptr, "SECDESC/", strlen("SECDESC/")) != 0)
65                         continue;
66
67                 name = talloc_strndup(ctx, kbuf.dptr+strlen("SECDESC/"), kbuf.dsize-strlen("SECDESC/"));
68
69                 share = samba3_find_add_share(db, ctx, name);
70
71                 vbuf = tdb_fetch(tdb, kbuf);
72                 blob.data = (uint8_t *)vbuf.dptr;
73                 blob.length = vbuf.dsize;
74
75                 pull = ndr_pull_init_blob(&blob, ctx);
76
77                 ndr_pull_security_descriptor(pull, NDR_SCALARS|NDR_BUFFERS, &share->secdesc);
78
79                 talloc_free(pull);
80         }
81
82         tdb_close(tdb);
83
84         return NT_STATUS_OK;
85 }