Use lp_config_backend_is_registry() instead of lp_include_registry_globals().
[samba.git] / source / lib / netapi / serverinfo.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  NetApi Server 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
22 #include "lib/netapi/netapi.h"
23 #include "libnet/libnet.h"
24
25 static WERROR NetServerGetInfoLocal_1005(struct libnetapi_ctx *ctx,
26                                          uint8_t **buffer)
27 {
28         struct srvsvc_NetSrvInfo1005 info1005;
29
30         info1005.comment = lp_serverstring();
31         *buffer = (uint8_t *)talloc_memdup(ctx, &info1005, sizeof(info1005));
32         if (!*buffer) {
33                 return WERR_NOMEM;
34         }
35
36         return WERR_OK;
37 }
38
39 static WERROR NetServerGetInfoLocal(struct libnetapi_ctx *ctx,
40                                     const char *server_name,
41                                     uint32_t level,
42                                     uint8_t **buffer)
43 {
44         switch (level) {
45                 case 1005:
46                         return NetServerGetInfoLocal_1005(ctx, buffer);
47                 default:
48                         return WERR_UNKNOWN_LEVEL;
49         }
50
51         return WERR_UNKNOWN_LEVEL;
52 }
53
54 static WERROR NetServerGetInfoRemote(struct libnetapi_ctx *ctx,
55                                      const char *server_name,
56                                      uint32_t level,
57                                      uint8_t **buffer)
58 {
59         struct cli_state *cli = NULL;
60         struct rpc_pipe_client *pipe_cli = NULL;
61         NTSTATUS status;
62         WERROR werr;
63         union srvsvc_NetSrvInfo info;
64
65         status = cli_full_connection(&cli, NULL, server_name,
66                                      NULL, 0,
67                                      "IPC$", "IPC",
68                                      ctx->username,
69                                      ctx->workgroup,
70                                      ctx->password,
71                                      0, Undefined, NULL);
72
73         if (!NT_STATUS_IS_OK(status)) {
74                 werr = ntstatus_to_werror(status);
75                 goto done;
76         }
77
78         pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC,
79                                             &status);
80         if (!pipe_cli) {
81                 werr = ntstatus_to_werror(status);
82                 goto done;
83         };
84
85         status = rpccli_srvsvc_NetSrvGetInfo(pipe_cli, ctx,
86                                              server_name,
87                                              level,
88                                              &info,
89                                              &werr);
90         if (!NT_STATUS_IS_OK(status)) {
91                 werr = ntstatus_to_werror(status);
92                 goto done;
93         }
94
95         *buffer = (uint8_t *)&info;
96
97  done:
98         if (cli) {
99                 cli_shutdown(cli);
100         }
101
102         return werr;
103 }
104
105 static WERROR libnetapi_NetServerGetInfo(struct libnetapi_ctx *ctx,
106                                          const char *server_name,
107                                          uint32_t level,
108                                          uint8_t **buffer)
109 {
110         if (!server_name || is_myname_or_ipaddr(server_name)) {
111                 return NetServerGetInfoLocal(ctx,
112                                              server_name,
113                                              level,
114                                              buffer);
115         }
116
117         return NetServerGetInfoRemote(ctx,
118                                       server_name,
119                                       level,
120                                       buffer);
121
122 }
123
124 NET_API_STATUS NetServerGetInfo(const char *server_name,
125                                 uint32_t level,
126                                 uint8_t **buffer)
127 {
128         struct libnetapi_ctx *ctx = NULL;
129         NET_API_STATUS status;
130         WERROR werr;
131
132         status = libnetapi_getctx(&ctx);
133         if (status != 0) {
134                 return status;
135         }
136
137         werr = libnetapi_NetServerGetInfo(ctx,
138                                           server_name,
139                                           level,
140                                           buffer);
141         if (!W_ERROR_IS_OK(werr)) {
142                 return W_ERROR_V(werr);
143         }
144
145         return 0;
146 }
147
148 static WERROR NetServerSetInfoLocal_1005(struct libnetapi_ctx *ctx,
149                                          uint8_t *buffer,
150                                          uint32_t *parm_error)
151 {
152         WERROR werr;
153         struct libnet_conf_ctx *conf_ctx;
154         TALLOC_CTX *mem_ctx;
155
156         struct srvsvc_NetSrvInfo1005 *info1005;
157
158         if (!buffer) {
159                 *parm_error = 1005; /* sure here ? */
160                 return WERR_INVALID_PARAM;
161         }
162
163         info1005 = (struct srvsvc_NetSrvInfo1005 *)buffer;
164
165         if (!info1005->comment) {
166                 *parm_error = 1005;
167                 return WERR_INVALID_PARAM;
168         }
169
170         if (!lp_config_backend_is_registry()) {
171                 return WERR_NOT_SUPPORTED;
172         }
173
174         mem_ctx = talloc_stackframe();
175         werr = libnet_conf_open(mem_ctx, &conf_ctx);
176         if (!W_ERROR_IS_OK(werr)) {
177                 goto done;
178         }
179
180         werr = libnet_conf_set_global_parameter(conf_ctx,
181                                                 "server string",
182                                                 info1005->comment);
183
184 done:
185         libnet_conf_close(conf_ctx);
186         TALLOC_FREE(mem_ctx);
187         return werr;
188 }
189
190 static WERROR NetServerSetInfoLocal(struct libnetapi_ctx *ctx,
191                                     const char *server_name,
192                                     uint32_t level,
193                                     uint8_t *buffer,
194                                     uint32_t *parm_error)
195 {
196         switch (level) {
197                 case 1005:
198                         return NetServerSetInfoLocal_1005(ctx, buffer, parm_error);
199                 default:
200                         return WERR_UNKNOWN_LEVEL;
201         }
202
203         return WERR_UNKNOWN_LEVEL;
204 }
205
206 static WERROR NetServerSetInfoRemote(struct libnetapi_ctx *ctx,
207                                      const char *server_name,
208                                      uint32_t level,
209                                      uint8_t *buffer,
210                                      uint32_t *parm_error)
211 {
212         struct cli_state *cli = NULL;
213         struct rpc_pipe_client *pipe_cli = NULL;
214         NTSTATUS status;
215         WERROR werr;
216         union srvsvc_NetSrvInfo info;
217
218         status = cli_full_connection(&cli, NULL, server_name,
219                                      NULL, 0,
220                                      "IPC$", "IPC",
221                                      ctx->username,
222                                      ctx->workgroup,
223                                      ctx->password,
224                                      0, Undefined, NULL);
225
226         if (!NT_STATUS_IS_OK(status)) {
227                 werr = ntstatus_to_werror(status);
228                 goto done;
229         }
230
231         pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC,
232                                             &status);
233         if (!pipe_cli) {
234                 werr = ntstatus_to_werror(status);
235                 goto done;
236         };
237
238         switch (level) {
239                 case 1005:
240                         info.info1005 = (struct srvsvc_NetSrvInfo1005 *)buffer;
241                         break;
242                 default:
243                         werr = WERR_NOT_SUPPORTED;
244                         goto done;
245         }
246
247         status = rpccli_srvsvc_NetSrvSetInfo(pipe_cli, ctx,
248                                              server_name,
249                                              level,
250                                              info,
251                                              parm_error,
252                                              &werr);
253         if (!NT_STATUS_IS_OK(status)) {
254                 werr = ntstatus_to_werror(status);
255                 goto done;
256         }
257
258  done:
259         if (cli) {
260                 cli_shutdown(cli);
261         }
262
263         return werr;
264 }
265
266 static WERROR libnetapi_NetServerSetInfo(struct libnetapi_ctx *ctx,
267                                          const char *server_name,
268                                          uint32_t level,
269                                          uint8_t *buffer,
270                                          uint32_t *parm_error)
271 {
272         if (!server_name || is_myname_or_ipaddr(server_name)) {
273                 return NetServerSetInfoLocal(ctx,
274                                              server_name,
275                                              level,
276                                              buffer,
277                                              parm_error);
278         }
279
280         return NetServerSetInfoRemote(ctx,
281                                       server_name,
282                                       level,
283                                       buffer,
284                                       parm_error);
285 }
286
287
288 NET_API_STATUS NetServerSetInfo(const char *server_name,
289                                 uint32_t level,
290                                 uint8_t *buffer,
291                                 uint32_t *parm_error)
292 {
293         struct libnetapi_ctx *ctx = NULL;
294         NET_API_STATUS status;
295         WERROR werr;
296
297         status = libnetapi_getctx(&ctx);
298         if (status != 0) {
299                 return status;
300         }
301
302         werr = libnetapi_NetServerSetInfo(ctx,
303                                           server_name,
304                                           level,
305                                           buffer,
306                                           parm_error);
307         if (!W_ERROR_IS_OK(werr)) {
308                 return W_ERROR_V(werr);
309         }
310
311         return 0;
312 }