s4:remove many invocations of "samdb_msg_add_string"
[kai/samba.git] / source4 / libnet / libnet_share.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Copyright (C) GrĂ©gory LEOCADIE <gleocadie@idealx.com>
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 "libnet/libnet.h"
22 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
23
24
25 NTSTATUS libnet_ListShares(struct libnet_context *ctx, 
26                            TALLOC_CTX *mem_ctx, struct libnet_ListShares *r)
27 {
28         NTSTATUS status;
29         struct libnet_RpcConnect c;
30         struct srvsvc_NetShareEnumAll s;
31         struct srvsvc_NetShareInfoCtr info_ctr;
32         uint32_t resume_handle = 0;
33         uint32_t totalentries = 0;
34         struct srvsvc_NetShareCtr0 ctr0;
35         struct srvsvc_NetShareCtr1 ctr1;
36         struct srvsvc_NetShareCtr2 ctr2;
37         struct srvsvc_NetShareCtr501 ctr501;
38         struct srvsvc_NetShareCtr502 ctr502;
39
40         ZERO_STRUCT(c);
41
42         c.level               = LIBNET_RPC_CONNECT_SERVER;
43         c.in.name             = r->in.server_name;
44         c.in.dcerpc_iface     = &ndr_table_srvsvc;
45
46         s.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s", c.in.name);
47
48         status = libnet_RpcConnect(ctx, mem_ctx, &c);
49         if (!NT_STATUS_IS_OK(status)) {
50                 r->out.error_string = talloc_asprintf(mem_ctx,
51                                                       "Connection to SRVSVC pipe of server %s "
52                                                       "failed: %s",
53                                                       r->in.server_name,
54                                                       nt_errstr(status));
55                 return status;
56         }
57
58         info_ctr.level = r->in.level;
59         switch (info_ctr.level) {
60         case 0:
61                 info_ctr.ctr.ctr0 = &ctr0;
62                 ZERO_STRUCT(ctr0);
63                 break;
64         case 1:
65                 info_ctr.ctr.ctr1 = &ctr1;
66                 ZERO_STRUCT(ctr1);
67                 break;
68         case 2:
69                 info_ctr.ctr.ctr2 = &ctr2;
70                 ZERO_STRUCT(ctr2);
71                 break;
72         case 501:
73                 info_ctr.ctr.ctr501 = &ctr501;
74                 ZERO_STRUCT(ctr501);
75                 break;
76         case 502:
77                 info_ctr.ctr.ctr502 = &ctr502;
78                 ZERO_STRUCT(ctr502);
79                 break;
80         default:
81                 r->out.error_string = talloc_asprintf(mem_ctx,
82                                                       "libnet_ListShares: Invalid info level requested: %d",
83                                                       info_ctr.level);
84                 return NT_STATUS_INVALID_PARAMETER;
85         }
86         s.in.max_buffer = ~0;
87         s.in.resume_handle = &resume_handle;
88         s.in.info_ctr = &info_ctr;
89         s.out.info_ctr = &info_ctr;
90         s.out.totalentries = &totalentries;
91
92         status = dcerpc_srvsvc_NetShareEnumAll_r(c.out.dcerpc_pipe->binding_handle, mem_ctx, &s);
93         
94         if (!NT_STATUS_IS_OK(status)) {
95                 r->out.error_string = talloc_asprintf(mem_ctx,
96                                                       "srvsvc_NetShareEnumAll on server '%s' failed"
97                                                       ": %s",
98                                                       r->in.server_name, nt_errstr(status));
99                 goto disconnect;
100         }
101
102         if (!W_ERROR_IS_OK(s.out.result) && !W_ERROR_EQUAL(s.out.result, WERR_MORE_DATA)) {
103                 r->out.error_string = talloc_asprintf(mem_ctx,
104                                                       "srvsvc_NetShareEnumAll on server '%s' failed: %s",
105                                                       r->in.server_name, win_errstr(s.out.result));
106                 goto disconnect;
107         }
108
109         r->out.ctr = s.out.info_ctr->ctr;
110
111 disconnect:
112         talloc_free(c.out.dcerpc_pipe);
113
114         return status;  
115 }
116
117
118 NTSTATUS libnet_AddShare(struct libnet_context *ctx,
119                          TALLOC_CTX *mem_ctx, struct libnet_AddShare *r)
120 {
121         NTSTATUS status;
122         struct libnet_RpcConnect c;
123         struct srvsvc_NetShareAdd s;
124         union srvsvc_NetShareInfo info;
125
126         ZERO_STRUCT(c);
127
128         c.level              = LIBNET_RPC_CONNECT_SERVER;
129         c.in.name            = r->in.server_name;
130         c.in.dcerpc_iface    = &ndr_table_srvsvc;
131
132         status = libnet_RpcConnect(ctx, mem_ctx, &c);
133         if (!NT_STATUS_IS_OK(status)) {
134                 r->out.error_string = talloc_asprintf(mem_ctx,
135                                                       "Connection to SRVSVC pipe of server %s "
136                                                       "failed: %s",
137                                                       r->in.server_name, nt_errstr(status));
138                 return status;
139         }
140
141         info.info2              = &r->in.share;
142
143         s.in.level              = 2;
144         s.in.info               = &info;
145         s.in.server_unc         = talloc_asprintf(mem_ctx, "\\\\%s", r->in.server_name);
146  
147         status = dcerpc_srvsvc_NetShareAdd_r(c.out.dcerpc_pipe->binding_handle, mem_ctx, &s);
148
149         if (!NT_STATUS_IS_OK(status)) {
150                 r->out.error_string = talloc_asprintf(mem_ctx,
151                                                       "srvsvc_NetShareAdd '%s' on server '%s' failed"
152                                                       ": %s",
153                                                       r->in.share.name, r->in.server_name, 
154                                                       nt_errstr(status));
155         } else if (!W_ERROR_IS_OK(s.out.result)) {
156                 r->out.error_string = talloc_asprintf(mem_ctx,
157                                                       "srvsvc_NetShareAdd '%s' on server '%s' failed"
158                                                       ": %s",
159                                                       r->in.share.name, r->in.server_name, 
160                                                       win_errstr(s.out.result));
161                 status = werror_to_ntstatus(s.out.result);
162         }
163
164         talloc_free(c.out.dcerpc_pipe);
165         
166         return status;
167 }
168
169
170 NTSTATUS libnet_DelShare(struct libnet_context *ctx,
171                          TALLOC_CTX *mem_ctx, struct libnet_DelShare *r)
172 {
173         NTSTATUS status;
174         struct libnet_RpcConnect c;
175         struct srvsvc_NetShareDel s;
176
177         ZERO_STRUCT(c);
178
179         c.level               = LIBNET_RPC_CONNECT_SERVER;
180         c.in.name             = r->in.server_name;
181         c.in.dcerpc_iface     = &ndr_table_srvsvc;
182
183         status = libnet_RpcConnect(ctx, mem_ctx, &c);
184         if (!NT_STATUS_IS_OK(status)) {
185                 r->out.error_string = talloc_asprintf(mem_ctx,
186                                                       "Connection to SRVSVC pipe of server %s "
187                                                       "failed: %s",
188                                                       r->in.server_name, nt_errstr(status));
189                 return status;
190         } 
191                 
192         s.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s", r->in.server_name);
193         s.in.share_name = r->in.share_name;
194
195         status = dcerpc_srvsvc_NetShareDel_r(c.out.dcerpc_pipe->binding_handle, mem_ctx, &s);
196         if (!NT_STATUS_IS_OK(status)) {
197                 r->out.error_string = talloc_asprintf(mem_ctx,
198                                                       "srvsvc_NetShareDel '%s' on server '%s' failed"
199                                                       ": %s",
200                                                       r->in.share_name, r->in.server_name, 
201                                                       nt_errstr(status));
202         } else if (!W_ERROR_IS_OK(s.out.result)) {
203                 r->out.error_string = talloc_asprintf(mem_ctx,
204                                                       "srvsvc_NetShareDel '%s' on server '%s' failed"
205                                                       ": %s",
206                                                       r->in.share_name, r->in.server_name, 
207                                                       win_errstr(s.out.result));
208                 status = werror_to_ntstatus(s.out.result);
209         }
210
211         talloc_free(c.out.dcerpc_pipe);
212
213         return status;
214 }