b9bb6d736215eea398558a0d7651868b8650a33d
[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 samba3_domainsecrets *samba3_find_domainsecrets(struct samba3 *db, const char *name)
24 {
25         int i;
26         
27         for (i = 0; i < db->secrets.domain_count; i++) {
28                 if (!strcasecmp_m(db->secrets.domains[i].name, name)) 
29                         return &db->secrets.domains[i];
30         }
31
32         return NULL;
33 }
34
35 NTSTATUS samba3_read_passdb_backends(TALLOC_CTX *ctx, const char *libdir, struct samba3 *samba3)
36 {
37         char *dbfile;
38         NTSTATUS status = NT_STATUS_OK;
39         int i;
40         const char **backends = param_get_string_list(samba3->configuration, NULL, "passdb backends", NULL);
41
42         /* Default to smbpasswd */
43         if (backends == NULL) 
44                 backends = str_list_make(ctx, "smbpasswd", LIST_SEP);
45         else
46                 backends = str_list_copy(ctx, backends);
47
48         for (i = 0; backends[i]; i++) {
49                 if (!strncmp(backends[i], "tdbsam", strlen("tdbsam"))) {
50                         const char *p = strchr(backends[i], ':');
51                         if (p && p[1]) {
52                                 dbfile = talloc_strdup(ctx, p+1);
53                         } else {
54                                 dbfile = talloc_asprintf(ctx, "%s/passdb.tdb", libdir);
55                         }
56                         samba3_read_tdbsam(dbfile, ctx, &samba3->samaccounts, &samba3->samaccount_count);
57                         talloc_free(dbfile);
58                 } else if (!strncmp(backends[i], "smbpasswd", strlen("smbpasswd"))) {
59                         const char *p = strchr(backends[i], ':');
60                         if (p && p[1]) {
61                                 dbfile = talloc_strdup(ctx, p+1);
62                         } else if ((p = param_get_string(samba3->configuration, NULL, "smb passwd file"))) {
63                                 dbfile = talloc_strdup(ctx, p);
64                         } else {
65                                 dbfile = talloc_strdup(ctx, "/etc/samba/smbpasswd");
66                         }
67
68                         samba3_read_smbpasswd(dbfile, ctx, &samba3->samaccounts, &samba3->samaccount_count);
69                         talloc_free(dbfile);
70                 } else if (!strncmp(backends[i], "ldapsam", strlen("ldapsam"))) {
71                         /* Will use samba3sam mapping module */                 
72                 } else {
73                         DEBUG(0, ("Upgrade from %s database not supported", backends[i]));
74                         status = NT_STATUS_NOT_SUPPORTED;
75                         continue;
76                 }
77         }
78
79         talloc_free(backends);
80
81         return status;
82 }
83
84 NTSTATUS samba3_read(const char *libdir, const char *smbconf, TALLOC_CTX *ctx, struct samba3 **samba3)
85 {
86         struct samba3 *ret;
87         char *dbfile = NULL;
88
89         ret = talloc_zero(ctx, struct samba3);
90
91         if (smbconf != NULL) {
92                 ret->configuration = param_init(ret);
93                 if (param_read(ret->configuration, smbconf) == -1) {
94                         talloc_free(ret);
95                         return NT_STATUS_UNSUCCESSFUL;
96                 }
97         }
98
99         dbfile = talloc_asprintf(ctx, "%s/account_policy.tdb", libdir);
100         samba3_read_account_policy(dbfile, ctx, &ret->policy);
101         talloc_free(dbfile);
102
103         dbfile = talloc_asprintf(ctx, "%s/registry.tdb", libdir);
104         samba3_read_regdb(dbfile, ctx, &ret->registry);
105         talloc_free(dbfile);
106
107         dbfile = talloc_asprintf(ctx, "%s/secrets.tdb", libdir);
108         samba3_read_secrets(dbfile, ctx, &ret->secrets);
109         talloc_free(dbfile);
110
111         dbfile = talloc_asprintf(ctx, "%s/share_info.tdb", libdir);
112         samba3_read_share_info(dbfile, ctx, ret);
113         talloc_free(dbfile);
114
115         dbfile = talloc_asprintf(ctx, "%s/winbindd_idmap.tdb", libdir);
116         samba3_read_idmap(dbfile, ctx, &ret->idmap);
117         talloc_free(dbfile);
118
119         dbfile = talloc_asprintf(ctx, "%s/wins.dat", libdir);
120         samba3_read_winsdb(dbfile, ret, &ret->winsdb_entries, &ret->winsdb_count);
121         talloc_free(dbfile);
122
123         samba3_read_passdb_backends(ctx, libdir, ret);
124
125         dbfile = talloc_asprintf(ctx, "%s/group_mapping.tdb", libdir);
126         samba3_read_grouptdb(dbfile, ctx, &ret->group);
127         talloc_free(dbfile);
128
129         *samba3 = ret;
130
131         return NT_STATUS_OK;
132 }