Use full string based debug_parse_levels in libnetapi.
[metze/samba/wip.git] / source3 / lib / netapi / netapi.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  NetApi Support
4  *  Copyright (C) Guenther Deschner 2007
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 static bool libnetapi_initialized = false;
26
27 NET_API_STATUS libnetapi_init(struct libnetapi_ctx **context)
28 {
29         struct libnetapi_ctx *ctx = NULL;
30         TALLOC_CTX *frame = NULL;
31
32         if (libnetapi_initialized) {
33                 return W_ERROR_V(WERR_OK);
34         }
35
36         frame = talloc_stackframe();
37
38         ctx = talloc_zero(frame, struct libnetapi_ctx);
39         if (!ctx) {
40                 TALLOC_FREE(frame);
41                 return W_ERROR_V(WERR_NOMEM);
42         }
43
44         DEBUGLEVEL = 0;
45         setup_logging("libnetapi", true);
46
47         dbf = x_stderr;
48         x_setbuf(x_stderr, NULL);
49         AllowDebugChange = false;
50
51         load_case_tables();
52
53         if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
54                 TALLOC_FREE(frame);
55                 return W_ERROR_V(WERR_GENERAL_FAILURE);
56         }
57
58         AllowDebugChange = true;
59
60         init_names();
61         load_interfaces();
62         reopen_logs();
63
64         BlockSignals(True, SIGPIPE);
65
66         libnetapi_initialized = true;
67
68         *context = ctx;
69
70         return W_ERROR_V(WERR_OK);
71 }
72
73 NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
74 {
75         TALLOC_FREE(ctx);
76         return W_ERROR_V(WERR_OK);
77 }
78
79 NET_API_STATUS libnetapi_set_debuglevel(struct libnetapi_ctx *ctx,
80                                         const char *debuglevel)
81 {
82         AllowDebugChange = true;
83         ctx->debuglevel = debuglevel;
84         if (!debug_parse_levels(debuglevel)) {
85                 return W_ERROR_V(WERR_GENERAL_FAILURE);
86         }
87         return W_ERROR_V(WERR_OK);
88 }
89
90 NET_API_STATUS libnetapi_get_debuglevel(struct libnetapi_ctx *ctx,
91                                         const char **debuglevel)
92 {
93         *debuglevel = ctx->debuglevel;
94         return W_ERROR_V(WERR_OK);
95 }
96
97 NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
98                                       const char *username)
99 {
100         TALLOC_FREE(ctx->username);
101         ctx->username = talloc_strdup(ctx, username);
102         if (!ctx->username) {
103                 return W_ERROR_V(WERR_NOMEM);
104         }
105         return W_ERROR_V(WERR_OK);
106 }
107
108 NET_API_STATUS libnetapi_set_password(struct libnetapi_ctx *ctx,
109                                       const char *password)
110 {
111         TALLOC_FREE(ctx->password);
112         ctx->password = talloc_strdup(ctx, password);
113         if (!ctx->password) {
114                 return W_ERROR_V(WERR_NOMEM);
115         }
116         return W_ERROR_V(WERR_OK);
117 }
118
119 NET_API_STATUS libnetapi_set_workgroup(struct libnetapi_ctx *ctx,
120                                        const char *workgroup)
121 {
122         TALLOC_FREE(ctx->workgroup);
123         ctx->workgroup = talloc_strdup(ctx, workgroup);
124         if (!ctx->workgroup) {
125                 return W_ERROR_V(WERR_NOMEM);
126         }
127         return W_ERROR_V(WERR_OK);
128 }