08debe96b193a5d42fb5d44a8722e50a6ac755a8
[sfrench/samba-autobuild/.git] / source4 / lib / samba3 / samba3.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Copyright (C) Jelmer Vernooij                       2005
4  *  
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *  
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *  
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include "includes.h"
21 #include "lib/samba3/samba3.h"
22
23 struct smbconf_data {
24         TALLOC_CTX *ctx;
25         struct samba3 *db;
26         struct samba3_share_info *current_share;
27 };
28
29 struct samba3_share_info *samba3_find_share(struct samba3 *db, TALLOC_CTX* ctx, const char *name)
30 {
31         int i;
32         for (i = 0; i < db->share_count; i++) {
33                 if (!StrCaseCmp(db->shares[i].name, name)) 
34                         return &db->shares[i];
35         }
36
37         db->shares = talloc_realloc(ctx, db->shares, struct samba3_share_info, db->share_count+1);
38         ZERO_STRUCT(db->shares[i]);
39         db->shares[i].name = talloc_strdup(ctx, name);
40         db->share_count++;
41         
42         return &db->shares[i];
43 }
44
45 static BOOL samba3_sfunc (const char *name, void *_db)
46 {
47         struct smbconf_data *privdat = _db;
48
49         privdat->current_share = samba3_find_share(privdat->db, privdat->ctx, name);
50
51         return True;
52 }
53
54 static BOOL samba3_pfunc (const char *name, const char *value, void *_db)
55 {
56         struct smbconf_data *privdat = _db;
57         struct samba3_parameter *p;
58
59         privdat->current_share->parameters = 
60                 talloc_realloc(privdat->ctx, privdat->current_share->parameters,
61                                            struct samba3_parameter, 
62                                            privdat->current_share->parameter_count+1);
63
64         p = &privdat->current_share->parameters[privdat->current_share->parameter_count];
65         p->name = talloc_strdup(privdat->ctx, name);
66         p->value = talloc_strdup(privdat->ctx, value);
67
68         privdat->current_share->parameter_count++;
69
70         return True;
71 }
72
73 NTSTATUS samba3_read_smbconf(const char *fn, TALLOC_CTX *ctx, struct samba3 *db)
74 {
75         struct smbconf_data privdat;
76
77         privdat.ctx = ctx;
78         privdat.db = db;
79         privdat.current_share = samba3_find_share(db, ctx, "global");
80         
81         if (!pm_process( fn, samba3_sfunc, samba3_pfunc, &privdat )) {
82                 return NT_STATUS_UNSUCCESSFUL;
83         }
84
85         return NT_STATUS_OK;
86 }
87
88 NTSTATUS samba3_read(const char *smbconf, const char *libdir, TALLOC_CTX *ctx, struct samba3 **samba3)
89 {
90         struct samba3 *ret;
91         char *dbfile;
92
93         ret = talloc_zero(ctx, struct samba3);
94
95         if (smbconf) 
96                 samba3_read_smbconf(smbconf, ctx, ret);
97
98         asprintf(&dbfile, "%s/wins.dat", libdir);
99         samba3_read_winsdb(dbfile, ret, &ret->winsdb_entries, &ret->winsdb_count);
100         SAFE_FREE(dbfile);
101
102         asprintf(&dbfile, "%s/passdb.tdb", libdir);
103         samba3_read_tdbsam(dbfile, ctx, &ret->samaccounts, &ret->samaccount_count);
104         SAFE_FREE(dbfile);
105
106         asprintf(&dbfile, "%s/group_mapping.tdb", libdir);
107         samba3_read_grouptdb(dbfile, ctx, &ret->group);
108         SAFE_FREE(dbfile);
109
110         asprintf(&dbfile, "%s/winbindd_idmap.tdb", libdir);
111         samba3_read_idmap(dbfile, ctx, &ret->idmap);
112         SAFE_FREE(dbfile);
113
114         asprintf(&dbfile, "%s/account_policy.tdb", libdir);
115         samba3_read_account_policy(dbfile, ctx, &ret->policy);
116         SAFE_FREE(dbfile);
117
118         asprintf(&dbfile, "%s/registry.tdb", libdir);
119         samba3_read_regdb(dbfile, ctx, &ret->registry);
120         SAFE_FREE(dbfile);
121
122         asprintf(&dbfile, "%s/secrets.tdb", libdir);
123         samba3_read_secrets(dbfile, ctx, &ret->secrets);
124         SAFE_FREE(dbfile);
125
126         asprintf(&dbfile, "%s/share_info.tdb", libdir);
127         samba3_read_share_info(dbfile, ctx, ret);
128         SAFE_FREE(dbfile);
129
130         *samba3 = ret;
131
132         return NT_STATUS_OK;
133 }