Close registry in libnetapi_free().
[metze/samba-autobuild/.git] / source3 / lib / netapi / netapi.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  NetApi Support
4  *  Copyright (C) Guenther Deschner 2007-2008
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "lib/netapi/netapi.h"
22
23 extern bool AllowDebugChange;
24
25 struct libnetapi_ctx *stat_ctx = NULL;
26 TALLOC_CTX *frame = NULL;
27 static bool libnetapi_initialized = false;
28
29 NET_API_STATUS libnetapi_init(struct libnetapi_ctx **context)
30 {
31         struct libnetapi_ctx *ctx = NULL;
32
33         if (stat_ctx && libnetapi_initialized) {
34                 *context = stat_ctx;
35                 return NET_API_STATUS_SUCCESS;
36         }
37
38         frame = talloc_stackframe();
39
40         ctx = talloc_zero(frame, struct libnetapi_ctx);
41         if (!ctx) {
42                 TALLOC_FREE(frame);
43                 return W_ERROR_V(WERR_NOMEM);
44         }
45
46         DEBUGLEVEL = 0;
47         setup_logging("libnetapi", true);
48
49         dbf = x_stderr;
50         x_setbuf(x_stderr, NULL);
51         AllowDebugChange = false;
52
53         load_case_tables();
54
55         if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
56                 TALLOC_FREE(frame);
57                 return W_ERROR_V(WERR_GENERAL_FAILURE);
58         }
59
60         AllowDebugChange = true;
61
62         init_names();
63         load_interfaces();
64         reopen_logs();
65
66         BlockSignals(True, SIGPIPE);
67
68         libnetapi_initialized = true;
69
70         *context = stat_ctx = ctx;
71
72         return NET_API_STATUS_SUCCESS;
73 }
74
75 NET_API_STATUS libnetapi_getctx(struct libnetapi_ctx **ctx)
76 {
77         if (stat_ctx) {
78                 *ctx = stat_ctx;
79                 return NET_API_STATUS_SUCCESS;
80         }
81
82         return libnetapi_init(ctx);
83 }
84
85 NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
86 {
87         gfree_names();
88         gfree_loadparm();
89         gfree_case_tables();
90         gfree_charcnv();
91         gfree_interfaces();
92
93         gencache_shutdown();
94         secrets_shutdown();
95         regdb_close();
96
97         TALLOC_FREE(ctx);
98         TALLOC_FREE(frame);
99
100         gfree_debugsyms();
101
102         return NET_API_STATUS_SUCCESS;
103 }
104
105 NET_API_STATUS libnetapi_set_debuglevel(struct libnetapi_ctx *ctx,
106                                         const char *debuglevel)
107 {
108         AllowDebugChange = true;
109         ctx->debuglevel = debuglevel;
110         if (!debug_parse_levels(debuglevel)) {
111                 return W_ERROR_V(WERR_GENERAL_FAILURE);
112         }
113         return NET_API_STATUS_SUCCESS;
114 }
115
116 NET_API_STATUS libnetapi_get_debuglevel(struct libnetapi_ctx *ctx,
117                                         const char **debuglevel)
118 {
119         *debuglevel = ctx->debuglevel;
120         return NET_API_STATUS_SUCCESS;
121 }
122
123 NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
124                                       const char *username)
125 {
126         TALLOC_FREE(ctx->username);
127         ctx->username = talloc_strdup(ctx, username);
128         if (!ctx->username) {
129                 return W_ERROR_V(WERR_NOMEM);
130         }
131         return NET_API_STATUS_SUCCESS;
132 }
133
134 NET_API_STATUS libnetapi_set_password(struct libnetapi_ctx *ctx,
135                                       const char *password)
136 {
137         TALLOC_FREE(ctx->password);
138         ctx->password = talloc_strdup(ctx, password);
139         if (!ctx->password) {
140                 return W_ERROR_V(WERR_NOMEM);
141         }
142         return NET_API_STATUS_SUCCESS;
143 }
144
145 NET_API_STATUS libnetapi_set_workgroup(struct libnetapi_ctx *ctx,
146                                        const char *workgroup)
147 {
148         TALLOC_FREE(ctx->workgroup);
149         ctx->workgroup = talloc_strdup(ctx, workgroup);
150         if (!ctx->workgroup) {
151                 return W_ERROR_V(WERR_NOMEM);
152         }
153         return NET_API_STATUS_SUCCESS;
154 }
155
156 const char *libnetapi_errstr(struct libnetapi_ctx *ctx,
157                              NET_API_STATUS status)
158 {
159         if (status & 0xc0000000) {
160                 return get_friendly_nt_error_msg(NT_STATUS(status));
161         }
162
163         return get_friendly_werror_msg(W_ERROR(status));
164 }