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