Restructure inner workings of libnetapi a bit.
[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 "librpc/gen_ndr/libnetapi.h"
23 #include "lib/netapi/netapi.h"
24 #include "lib/netapi/libnetapi.h"
25 #include "libnet/libnet.h"
26
27 /****************************************************************
28 ****************************************************************/
29
30 static WERROR NetServerGetInfo_l_1005(struct libnetapi_ctx *ctx,
31                                       uint8_t **buffer)
32 {
33         struct srvsvc_NetSrvInfo1005 info1005;
34
35         info1005.comment = lp_serverstring();
36         *buffer = (uint8_t *)talloc_memdup(ctx, &info1005, sizeof(info1005));
37         if (!*buffer) {
38                 return WERR_NOMEM;
39         }
40
41         return WERR_OK;
42 }
43
44 /****************************************************************
45 ****************************************************************/
46
47 WERROR NetServerGetInfo_l(struct libnetapi_ctx *ctx,
48                           struct NetServerGetInfo *r)
49 {
50         switch (r->in.level) {
51                 case 1005:
52                         return NetServerGetInfo_l_1005(ctx, r->out.buffer);
53                 default:
54                         return WERR_UNKNOWN_LEVEL;
55         }
56
57         return WERR_UNKNOWN_LEVEL;
58 }
59
60 /****************************************************************
61 ****************************************************************/
62
63 WERROR NetServerGetInfo_r(struct libnetapi_ctx *ctx,
64                           struct NetServerGetInfo *r)
65 {
66         struct cli_state *cli = NULL;
67         struct rpc_pipe_client *pipe_cli = NULL;
68         NTSTATUS status;
69         WERROR werr;
70         union srvsvc_NetSrvInfo info;
71
72         status = cli_full_connection(&cli, NULL, r->in.server_name,
73                                      NULL, 0,
74                                      "IPC$", "IPC",
75                                      ctx->username,
76                                      ctx->workgroup,
77                                      ctx->password,
78                                      0, Undefined, NULL);
79
80         if (!NT_STATUS_IS_OK(status)) {
81                 werr = ntstatus_to_werror(status);
82                 goto done;
83         }
84
85         pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC,
86                                             &status);
87         if (!pipe_cli) {
88                 werr = ntstatus_to_werror(status);
89                 goto done;
90         };
91
92         status = rpccli_srvsvc_NetSrvGetInfo(pipe_cli, ctx,
93                                              r->in.server_name,
94                                              r->in.level,
95                                              &info,
96                                              &werr);
97         if (!NT_STATUS_IS_OK(status)) {
98                 werr = ntstatus_to_werror(status);
99                 goto done;
100         }
101
102         *r->out.buffer = talloc_memdup(ctx, &info, sizeof(info));
103         if (!*r->out.buffer) {
104                 werr = WERR_NOMEM;
105                 goto done;
106         }
107
108  done:
109         if (cli) {
110                 cli_shutdown(cli);
111         }
112
113         return werr;
114 }
115
116 /****************************************************************
117 ****************************************************************/
118
119 static WERROR NetServerSetInfo_l_1005(struct libnetapi_ctx *ctx,
120                                       struct NetServerSetInfo *r)
121 {
122         WERROR werr;
123         struct smbconf_ctx *conf_ctx;
124         struct srvsvc_NetSrvInfo1005 *info1005;
125
126         if (!r->in.buffer) {
127                 *r->out.parm_error = 1005; /* sure here ? */
128                 return WERR_INVALID_PARAM;
129         }
130
131         info1005 = (struct srvsvc_NetSrvInfo1005 *)r->in.buffer;
132
133         if (!info1005->comment) {
134                 *r->out.parm_error = 1005;
135                 return WERR_INVALID_PARAM;
136         }
137
138         if (!lp_config_backend_is_registry()) {
139                 libnetapi_set_error_string(ctx,
140                         "Configuration manipulation requested but not "
141                         "supported by backend");
142                 return WERR_NOT_SUPPORTED;
143         }
144
145         werr = smbconf_init_reg(ctx, &conf_ctx, NULL);
146         if (!W_ERROR_IS_OK(werr)) {
147                 goto done;
148         }
149
150         werr = smbconf_set_global_parameter(conf_ctx, "server string",
151                                             info1005->comment);
152
153  done:
154         smbconf_shutdown(conf_ctx);
155         return werr;
156 }
157
158 /****************************************************************
159 ****************************************************************/
160
161 WERROR NetServerSetInfo_l(struct libnetapi_ctx *ctx,
162                           struct NetServerSetInfo *r)
163 {
164         switch (r->in.level) {
165                 case 1005:
166                         return NetServerSetInfo_l_1005(ctx, r);
167                 default:
168                         return WERR_UNKNOWN_LEVEL;
169         }
170
171         return WERR_UNKNOWN_LEVEL;
172 }
173
174 /****************************************************************
175 ****************************************************************/
176
177 WERROR NetServerSetInfo_r(struct libnetapi_ctx *ctx,
178                           struct NetServerSetInfo *r)
179 {
180         struct cli_state *cli = NULL;
181         struct rpc_pipe_client *pipe_cli = NULL;
182         NTSTATUS status;
183         WERROR werr;
184         union srvsvc_NetSrvInfo info;
185
186         status = cli_full_connection(&cli, NULL, r->in.server_name,
187                                      NULL, 0,
188                                      "IPC$", "IPC",
189                                      ctx->username,
190                                      ctx->workgroup,
191                                      ctx->password,
192                                      0, Undefined, NULL);
193
194         if (!NT_STATUS_IS_OK(status)) {
195                 werr = ntstatus_to_werror(status);
196                 goto done;
197         }
198
199         pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC,
200                                             &status);
201         if (!pipe_cli) {
202                 werr = ntstatus_to_werror(status);
203                 goto done;
204         };
205
206         switch (r->in.level) {
207                 case 1005:
208                         info.info1005 = (struct srvsvc_NetSrvInfo1005 *)r->in.buffer;
209                         break;
210                 default:
211                         werr = WERR_NOT_SUPPORTED;
212                         goto done;
213         }
214
215         status = rpccli_srvsvc_NetSrvSetInfo(pipe_cli, ctx,
216                                              r->in.server_name,
217                                              r->in.level,
218                                              &info,
219                                              r->out.parm_error,
220                                              &werr);
221         if (!NT_STATUS_IS_OK(status)) {
222                 werr = ntstatus_to_werror(status);
223                 goto done;
224         }
225
226  done:
227         if (cli) {
228                 cli_shutdown(cli);
229         }
230
231         return werr;
232 }