r23795: more v2->v3 conversion
[metze/samba/wip.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 3 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/tdb.h"
27 #include "lib/util/util_tdb.h"
28 #include "lib/samba3/samba3.h"
29 #include "system/filesys.h"
30
31 #define SHARE_DATABASE_VERSION_V1 1
32 #define SHARE_DATABASE_VERSION_V2 2 /* version id in little endian. */
33
34 NTSTATUS samba3_read_share_info(const char *fn, TALLOC_CTX *ctx, struct samba3 *db)
35 {
36         int32_t vers_id;
37         TDB_CONTEXT *tdb;
38         TDB_DATA kbuf, vbuf;
39         DATA_BLOB blob;
40  
41         tdb = tdb_open(fn, 0, TDB_DEFAULT, O_RDONLY, 0600);
42         if (!tdb) {
43                 DEBUG(0,("Failed to open share info database %s (%s)\n",
44                         fn, strerror(errno) ));
45                 return NT_STATUS_UNSUCCESSFUL;
46         }
47  
48         /* Cope with byte-reversed older versions of the db. */
49         vers_id = tdb_fetch_int32(tdb, "INFO/version");
50         if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) {
51                 /* Written on a bigendian machine with old fetch_int code. Save as le. */
52                 vers_id = SHARE_DATABASE_VERSION_V2;
53         }
54
55         if (vers_id != SHARE_DATABASE_VERSION_V2) {
56                 return NT_STATUS_UNSUCCESSFUL;
57         }
58
59         for (kbuf = tdb_firstkey(tdb); kbuf.dptr; kbuf = tdb_nextkey(tdb, kbuf)) 
60         {
61                 struct ndr_pull *pull;
62                 struct samba3_share_info *share;
63                 char *name;
64                 
65                 if (strncmp((char *)kbuf.dptr, "SECDESC/", strlen("SECDESC/")) != 0)
66                         continue;
67
68                 name = talloc_strndup(ctx, (char *)kbuf.dptr+strlen("SECDESC/"), kbuf.dsize-strlen("SECDESC/"));
69
70                 db->shares = talloc_realloc(db, db->shares, struct samba3_share_info, db->share_count+1);
71                 share = &db->shares[db->share_count];
72                 db->share_count++;
73
74                 share->name = talloc_strdup(db, name);
75
76                 vbuf = tdb_fetch(tdb, kbuf);
77                 blob.data = (uint8_t *)vbuf.dptr;
78                 blob.length = vbuf.dsize;
79
80                 pull = ndr_pull_init_blob(&blob, ctx);
81
82                 ndr_pull_security_descriptor(pull, NDR_SCALARS|NDR_BUFFERS, &share->secdesc);
83
84                 talloc_free(pull);
85         }
86
87         tdb_close(tdb);
88
89         return NT_STATUS_OK;
90 }