Merge branch 'v3-2-test' of ssh://jra@git.samba.org/data/git/samba into v3-2-test
[ira/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 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         struct srvsvc_NetSrvInfo1005 *info1005;
153
154         if (!buffer) {
155                 *parm_error = 1005; /* sure here ? */
156                 return WERR_INVALID_PARAM;
157         }
158
159         info1005 = (struct srvsvc_NetSrvInfo1005 *)buffer;
160
161         if (!info1005->comment) {
162                 *parm_error = 1005;
163                 return WERR_INVALID_PARAM;
164         }
165
166         if (!lp_include_registry_globals()) {
167                 return WERR_NOT_SUPPORTED;
168         }
169
170         return libnet_conf_set_global_parameter("server string",
171                                                 info1005->comment);
172 }
173
174 static WERROR NetServerSetInfoLocal(struct libnetapi_ctx *ctx,
175                                     const char *server_name,
176                                     uint32_t level,
177                                     uint8_t *buffer,
178                                     uint32_t *parm_error)
179 {
180         switch (level) {
181                 case 1005:
182                         return NetServerSetInfoLocal_1005(ctx, buffer, parm_error);
183                 default:
184                         return WERR_UNKNOWN_LEVEL;
185         }
186
187         return WERR_UNKNOWN_LEVEL;
188 }
189
190 static WERROR NetServerSetInfoRemote(struct libnetapi_ctx *ctx,
191                                      const char *server_name,
192                                      uint32_t level,
193                                      uint8_t *buffer,
194                                      uint32_t *parm_error)
195 {
196         struct cli_state *cli = NULL;
197         struct rpc_pipe_client *pipe_cli = NULL;
198         NTSTATUS status;
199         WERROR werr;
200         union srvsvc_NetSrvInfo info;
201
202         status = cli_full_connection(&cli, NULL, server_name,
203                                      NULL, 0,
204                                      "IPC$", "IPC",
205                                      ctx->username,
206                                      ctx->workgroup,
207                                      ctx->password,
208                                      0, Undefined, NULL);
209
210         if (!NT_STATUS_IS_OK(status)) {
211                 werr = ntstatus_to_werror(status);
212                 goto done;
213         }
214
215         pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC,
216                                             &status);
217         if (!pipe_cli) {
218                 werr = ntstatus_to_werror(status);
219                 goto done;
220         };
221
222         switch (level) {
223                 case 1005:
224                         info.info1005 = (struct srvsvc_NetSrvInfo1005 *)buffer;
225                         break;
226                 default:
227                         werr = WERR_NOT_SUPPORTED;
228                         goto done;
229         }
230
231         status = rpccli_srvsvc_NetSrvSetInfo(pipe_cli, ctx,
232                                              server_name,
233                                              level,
234                                              info,
235                                              parm_error,
236                                              &werr);
237         if (!NT_STATUS_IS_OK(status)) {
238                 werr = ntstatus_to_werror(status);
239                 goto done;
240         }
241
242  done:
243         if (cli) {
244                 cli_shutdown(cli);
245         }
246
247         return werr;
248 }
249
250 static WERROR libnetapi_NetServerSetInfo(struct libnetapi_ctx *ctx,
251                                          const char *server_name,
252                                          uint32_t level,
253                                          uint8_t *buffer,
254                                          uint32_t *parm_error)
255 {
256         if (!server_name || is_myname_or_ipaddr(server_name)) {
257                 return NetServerSetInfoLocal(ctx,
258                                              server_name,
259                                              level,
260                                              buffer,
261                                              parm_error);
262         }
263
264         return NetServerSetInfoRemote(ctx,
265                                       server_name,
266                                       level,
267                                       buffer,
268                                       parm_error);
269 }
270
271
272 NET_API_STATUS NetServerSetInfo(const char *server_name,
273                                 uint32_t level,
274                                 uint8_t *buffer,
275                                 uint32_t *parm_error)
276 {
277         struct libnetapi_ctx *ctx = NULL;
278         NET_API_STATUS status;
279         WERROR werr;
280
281         status = libnetapi_getctx(&ctx);
282         if (status != 0) {
283                 return status;
284         }
285
286         werr = libnetapi_NetServerSetInfo(ctx,
287                                           server_name,
288                                           level,
289                                           buffer,
290                                           parm_error);
291         if (!W_ERROR_IS_OK(werr)) {
292                 return W_ERROR_V(werr);
293         }
294
295         return 0;
296 }