Prefill in username in libnetapi ctx.
[ira/wip.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 /****************************************************************
30 ****************************************************************/
31
32 NET_API_STATUS libnetapi_init(struct libnetapi_ctx **context)
33 {
34         struct libnetapi_ctx *ctx = NULL;
35         char *krb5_cc_env = NULL;
36
37         if (stat_ctx && libnetapi_initialized) {
38                 *context = stat_ctx;
39                 return NET_API_STATUS_SUCCESS;
40         }
41
42 #ifdef DEVELOPER
43         talloc_enable_leak_report();
44 #endif
45         frame = talloc_stackframe();
46
47         ctx = talloc_zero(frame, struct libnetapi_ctx);
48         if (!ctx) {
49                 TALLOC_FREE(frame);
50                 return W_ERROR_V(WERR_NOMEM);
51         }
52
53         if (!DEBUGLEVEL) {
54                 DEBUGLEVEL = 0;
55         }
56         setup_logging("libnetapi", true);
57
58         dbf = x_stderr;
59         x_setbuf(x_stderr, NULL);
60         AllowDebugChange = false;
61
62         load_case_tables();
63
64         if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
65                 TALLOC_FREE(frame);
66                 fprintf(stderr, "lp_load failed\n");
67                 return W_ERROR_V(WERR_GENERAL_FAILURE);
68         }
69
70         AllowDebugChange = true;
71
72         init_names();
73         load_interfaces();
74         reopen_logs();
75
76         BlockSignals(True, SIGPIPE);
77
78         krb5_cc_env = getenv(KRB5_ENV_CCNAME);
79         if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
80                 ctx->krb5_cc_env = talloc_strdup(frame, "MEMORY:libnetapi");
81                 setenv(KRB5_ENV_CCNAME, ctx->krb5_cc_env, 1);
82         }
83
84         ctx->username = talloc_strdup(frame, getenv("USER"));
85         if (!ctx->username) {
86                 TALLOC_FREE(frame);
87                 fprintf(stderr, "out of memory\n");
88                 return W_ERROR_V(WERR_NOMEM);
89         }
90
91         libnetapi_initialized = true;
92
93         *context = stat_ctx = ctx;
94
95         return NET_API_STATUS_SUCCESS;
96 }
97
98 /****************************************************************
99 ****************************************************************/
100
101 NET_API_STATUS libnetapi_getctx(struct libnetapi_ctx **ctx)
102 {
103         if (stat_ctx) {
104                 *ctx = stat_ctx;
105                 return NET_API_STATUS_SUCCESS;
106         }
107
108         return libnetapi_init(ctx);
109 }
110
111 /****************************************************************
112 ****************************************************************/
113
114 NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
115 {
116
117         if (ctx->krb5_cc_env) {
118                 char *env = getenv(KRB5_ENV_CCNAME);
119                 if (env && (strequal(ctx->krb5_cc_env, env))) {
120                         unsetenv(KRB5_ENV_CCNAME);
121                 }
122         }
123
124         gfree_names();
125         gfree_loadparm();
126         gfree_case_tables();
127         gfree_charcnv();
128         gfree_interfaces();
129
130         gencache_shutdown();
131         secrets_shutdown();
132
133         TALLOC_FREE(ctx);
134         TALLOC_FREE(frame);
135
136         gfree_debugsyms();
137
138         return NET_API_STATUS_SUCCESS;
139 }
140
141 /****************************************************************
142 ****************************************************************/
143
144 NET_API_STATUS libnetapi_set_debuglevel(struct libnetapi_ctx *ctx,
145                                         const char *debuglevel)
146 {
147         AllowDebugChange = true;
148         ctx->debuglevel = talloc_strdup(ctx, debuglevel);
149         if (!debug_parse_levels(debuglevel)) {
150                 return W_ERROR_V(WERR_GENERAL_FAILURE);
151         }
152         return NET_API_STATUS_SUCCESS;
153 }
154
155 /****************************************************************
156 ****************************************************************/
157
158 NET_API_STATUS libnetapi_get_debuglevel(struct libnetapi_ctx *ctx,
159                                         char **debuglevel)
160 {
161         *debuglevel = ctx->debuglevel;
162         return NET_API_STATUS_SUCCESS;
163 }
164
165 /****************************************************************
166 ****************************************************************/
167
168 NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
169                                       const char *username)
170 {
171         TALLOC_FREE(ctx->username);
172         ctx->username = talloc_strdup(ctx, username ? username : "");
173
174         if (!ctx->username) {
175                 return W_ERROR_V(WERR_NOMEM);
176         }
177         return NET_API_STATUS_SUCCESS;
178 }
179
180 NET_API_STATUS libnetapi_set_password(struct libnetapi_ctx *ctx,
181                                       const char *password)
182 {
183         TALLOC_FREE(ctx->password);
184         ctx->password = talloc_strdup(ctx, password);
185         if (!ctx->password) {
186                 return W_ERROR_V(WERR_NOMEM);
187         }
188         return NET_API_STATUS_SUCCESS;
189 }
190
191 NET_API_STATUS libnetapi_set_workgroup(struct libnetapi_ctx *ctx,
192                                        const char *workgroup)
193 {
194         TALLOC_FREE(ctx->workgroup);
195         ctx->workgroup = talloc_strdup(ctx, workgroup);
196         if (!ctx->workgroup) {
197                 return W_ERROR_V(WERR_NOMEM);
198         }
199         return NET_API_STATUS_SUCCESS;
200 }
201
202 /****************************************************************
203 ****************************************************************/
204
205 const char *libnetapi_errstr(NET_API_STATUS status)
206 {
207         if (status & 0xc0000000) {
208                 return get_friendly_nt_error_msg(NT_STATUS(status));
209         }
210
211         return get_friendly_werror_msg(W_ERROR(status));
212 }
213
214 /****************************************************************
215 ****************************************************************/
216
217 NET_API_STATUS libnetapi_set_error_string(struct libnetapi_ctx *ctx,
218                                           const char *format, ...)
219 {
220         va_list args;
221
222         TALLOC_FREE(ctx->error_string);
223
224         va_start(args, format);
225         ctx->error_string = talloc_vasprintf(ctx, format, args);
226         va_end(args);
227
228         if (!ctx->error_string) {
229                 return W_ERROR_V(WERR_NOMEM);
230         }
231         return NET_API_STATUS_SUCCESS;
232 }
233
234 /****************************************************************
235 ****************************************************************/
236
237 const char *libnetapi_get_error_string(struct libnetapi_ctx *ctx,
238                                        NET_API_STATUS status)
239 {
240         struct libnetapi_ctx *tmp_ctx = ctx;
241
242         if (!tmp_ctx) {
243                 status = libnetapi_getctx(&tmp_ctx);
244                 if (status != 0) {
245                         return NULL;
246                 }
247         }
248
249         if (tmp_ctx->error_string) {
250                 return tmp_ctx->error_string;
251         }
252
253         return libnetapi_errstr(status);
254 }
255
256 /****************************************************************
257 ****************************************************************/
258
259 NET_API_STATUS NetApiBufferFree(void *buffer)
260 {
261         if (!buffer) {
262                 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
263         }
264
265         talloc_free(buffer);
266
267         return NET_API_STATUS_SUCCESS;
268 }