r9797: - let us parse replication packets with linked attributes fine,
[samba.git] / source / 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_domainsecrets *samba3_find_domainsecrets(struct samba3 *db, const char *name)
30 {
31         int i;
32         
33         for (i = 0; i < db->secrets.domain_count; i++) {
34                 if (!strcasecmp_m(db->secrets.domains[i].name, name)) 
35                         return &db->secrets.domains[i];
36         }
37
38         return NULL;
39 }
40
41 struct samba3_share_info *samba3_find_share(struct samba3 *db, const char *name)
42 {
43         int i;
44         for (i = 0; i < db->share_count; i++) {
45                 if (!strcasecmp_m(db->shares[i].name, name)) 
46                         return &db->shares[i];
47         }
48
49         return NULL;
50 }
51
52
53 struct samba3_share_info *samba3_find_add_share(struct samba3 *db, TALLOC_CTX* ctx, const char *name)
54 {
55         struct samba3_share_info *share = samba3_find_share(db, name);
56
57         if (share)
58                 return share;
59
60         db->shares = talloc_realloc(ctx, db->shares, struct samba3_share_info, db->share_count+1);
61         ZERO_STRUCT(db->shares[db->share_count]);
62         db->shares[db->share_count].name = talloc_strdup(ctx, name);
63         db->share_count++;
64         
65         return &db->shares[db->share_count-1];
66 }
67
68 const char *samba3_get_param(struct samba3 *samba3, const char *section, const char *param)
69 {
70         int i;
71         struct samba3_share_info *share = samba3_find_share(samba3, section);
72
73         if (share == NULL)
74                 return NULL;
75
76         for (i = 0; i < share->parameter_count; i++) {
77                 if (!strcasecmp_m(share->parameters[i].name, param))
78                         return share->parameters[i].value;
79         }
80
81         return NULL;
82 }
83
84
85 static BOOL samba3_sfunc (const char *name, void *_db)
86 {
87         struct smbconf_data *privdat = _db;
88
89         privdat->current_share = samba3_find_add_share(privdat->db, privdat->ctx, name);
90
91         return True;
92 }
93
94 static BOOL samba3_pfunc (const char *name, const char *value, void *_db)
95 {
96         struct smbconf_data *privdat = _db;
97         struct samba3_parameter *p;
98
99         privdat->current_share->parameters = 
100                 talloc_realloc(privdat->ctx, privdat->current_share->parameters,
101                                            struct samba3_parameter, 
102                                            privdat->current_share->parameter_count+1);
103
104         p = &privdat->current_share->parameters[privdat->current_share->parameter_count];
105         p->name = talloc_strdup(privdat->ctx, name);
106         p->value = talloc_strdup(privdat->ctx, value);
107
108         privdat->current_share->parameter_count++;
109
110         return True;
111 }
112
113 NTSTATUS samba3_read_smbconf(const char *fn, TALLOC_CTX *ctx, struct samba3 *db)
114 {
115         struct smbconf_data privdat;
116
117         privdat.ctx = ctx;
118         privdat.db = db;
119         privdat.current_share = samba3_find_add_share(db, ctx, "global");
120         
121         if (!pm_process( fn, samba3_sfunc, samba3_pfunc, &privdat )) {
122                 return NT_STATUS_UNSUCCESSFUL;
123         }
124
125         return NT_STATUS_OK;
126 }
127
128 NTSTATUS samba3_read(const char *smbconf, const char *libdir, TALLOC_CTX *ctx, struct samba3 **samba3)
129 {
130         struct samba3 *ret;
131         char *dbfile = NULL;
132
133         ret = talloc_zero(ctx, struct samba3);
134
135         if (smbconf) 
136                 samba3_read_smbconf(smbconf, ctx, ret);
137
138         dbfile = talloc_asprintf(ctx, "%s/account_policy.tdb", libdir);
139         samba3_read_account_policy(dbfile, ctx, &ret->policy);
140         talloc_free(dbfile);
141
142         dbfile = talloc_asprintf(ctx, "%s/registry.tdb", libdir);
143         samba3_read_regdb(dbfile, ctx, &ret->registry);
144         talloc_free(dbfile);
145
146         dbfile = talloc_asprintf(ctx, "%s/secrets.tdb", libdir);
147         samba3_read_secrets(dbfile, ctx, &ret->secrets);
148         talloc_free(dbfile);
149
150         dbfile = talloc_asprintf(ctx, "%s/share_info.tdb", libdir);
151         samba3_read_share_info(dbfile, ctx, ret);
152         talloc_free(dbfile);
153
154         dbfile = talloc_asprintf(ctx, "%s/winbindd_idmap.tdb", libdir);
155         samba3_read_idmap(dbfile, ctx, &ret->idmap);
156         talloc_free(dbfile);
157
158         dbfile = talloc_asprintf(ctx, "%s/wins.dat", libdir);
159         samba3_read_winsdb(dbfile, ret, &ret->winsdb_entries, &ret->winsdb_count);
160         talloc_free(dbfile);
161
162         dbfile = talloc_asprintf(ctx, "%s/passdb.tdb", libdir);
163         samba3_read_tdbsam(dbfile, ctx, &ret->samaccounts, &ret->samaccount_count);
164         talloc_free(dbfile);
165
166         dbfile = talloc_asprintf(ctx, "%s/group_mapping.tdb", libdir);
167         samba3_read_grouptdb(dbfile, ctx, &ret->group);
168         talloc_free(dbfile);
169
170         *samba3 = ret;
171
172         return NT_STATUS_OK;
173 }