r6962: Severely simplified share functions. Removed call levels as we don't
[sfrench/samba-autobuild/.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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "libnet/libnet.h"
23 #include "librpc/gen_ndr/ndr_srvsvc.h"
24
25
26 NTSTATUS libnet_ListShares(struct libnet_context *ctx, 
27                            TALLOC_CTX *mem_ctx, struct libnet_ListShares *r)
28 {
29         NTSTATUS status;
30         union libnet_rpc_connect c;
31         struct srvsvc_NetShareEnumAll s;
32         uint32_t resume_handle;
33         struct srvsvc_NetShareCtr0 ctr0;
34
35         c.standard.level                        = LIBNET_RPC_CONNECT_STANDARD;
36         c.standard.in.server_name               = r->in.server_name;
37         c.standard.in.dcerpc_iface_name         = DCERPC_SRVSVC_NAME;
38         c.standard.in.dcerpc_iface_uuid         = DCERPC_SRVSVC_UUID;
39         c.standard.in.dcerpc_iface_version      = DCERPC_SRVSVC_VERSION;
40
41         status = libnet_rpc_connect(ctx, mem_ctx, &c);
42         if (!NT_STATUS_IS_OK(status)) {
43                 r->out.error_string = talloc_asprintf(mem_ctx,
44                                                       "Connection to SRVSVC pipe of server %s "
45                                                       "failed: %s\n",
46                                                       r->in.server_name,
47                                                       nt_errstr(status));
48                 return status;
49         }
50
51         s.in.level = r->in.level;
52         s.in.ctr.ctr0 = &ctr0;
53         s.in.max_buffer = ~0;
54         s.in.resume_handle = &resume_handle;
55
56         ZERO_STRUCT(ctr0);
57
58         status = dcerpc_srvsvc_NetShareEnumAll(c.standard.out.dcerpc_pipe, mem_ctx, &s);
59         
60         if (!NT_STATUS_IS_OK(status)) {
61                 r->out.error_string = talloc_asprintf(mem_ctx,
62                                                       "srvsvc_NetShareEnumAll on server '%s' failed"
63                                                       ": %s\n",
64                                                       r->in.server_name, nt_errstr(status));
65                 goto disconnect;
66         }
67
68         if (!W_ERROR_IS_OK(s.out.result) && !W_ERROR_EQUAL(s.out.result, WERR_MORE_DATA)) {
69                 goto disconnect;
70         }
71
72         r->out.ctr = s.out.ctr;
73
74 disconnect:
75         talloc_free(c.standard.out.dcerpc_pipe);
76
77         return status;  
78 }
79
80
81 NTSTATUS libnet_AddShare(struct libnet_context *ctx,
82                          TALLOC_CTX *mem_ctx, struct libnet_AddShare *r)
83 {
84         NTSTATUS status;
85         union libnet_rpc_connect c;
86         struct srvsvc_NetShareAdd s;
87
88         c.standard.level                        = LIBNET_RPC_CONNECT_STANDARD;
89         c.standard.in.server_name               = r->in.server_name;
90         c.standard.in.dcerpc_iface_name         = DCERPC_SRVSVC_NAME;
91         c.standard.in.dcerpc_iface_uuid         = DCERPC_SRVSVC_UUID;
92         c.standard.in.dcerpc_iface_version      = DCERPC_SRVSVC_VERSION;
93
94         status = libnet_rpc_connect(ctx, mem_ctx, &c);
95         if (!NT_STATUS_IS_OK(status)) {
96                 r->out.error_string = talloc_asprintf(mem_ctx,
97                                                       "Connection to SRVSVC pipe of server %s "
98                                                       "failed: %s\n",
99                                                       r->in.server_name, nt_errstr(status));
100                 return status;
101         }
102
103         s.in.level              = r->in.level;
104         s.in.info.info2         = &r->in.share;
105         s.in.server_unc         = talloc_asprintf(mem_ctx, "\\\\%s", r->in.server_name);
106  
107         status = dcerpc_srvsvc_NetShareAdd(c.standard.out.dcerpc_pipe, mem_ctx,&s);     
108
109         if (!NT_STATUS_IS_OK(status)) {
110                 r->out.error_string = talloc_asprintf(mem_ctx,
111                                                       "srvsvc_NetShareAdd on server '%s' failed"
112                                                       ": %s\n",
113                                                       r->in.server_name, nt_errstr(status));
114         }
115
116 disconnect:
117         talloc_free(c.standard.out.dcerpc_pipe);
118         
119         return status;
120 }
121
122
123 NTSTATUS libnet_DelShare(struct libnet_context *ctx,
124                          TALLOC_CTX *mem_ctx, struct libnet_DelShare *r)
125 {
126         NTSTATUS status;
127         union libnet_rpc_connect c;
128         struct srvsvc_NetShareDel s;
129
130         c.standard.level                        = LIBNET_RPC_CONNECT_STANDARD;
131         c.standard.in.server_name               = r->in.server_name;
132         c.standard.in.dcerpc_iface_name         = DCERPC_SRVSVC_NAME;
133         c.standard.in.dcerpc_iface_uuid         = DCERPC_SRVSVC_UUID;
134         c.standard.in.dcerpc_iface_version      = DCERPC_SRVSVC_VERSION;
135
136         status = libnet_rpc_connect(ctx, mem_ctx, &c);
137         if (!NT_STATUS_IS_OK(status)) {
138                 r->out.error_string = talloc_asprintf(mem_ctx,
139                                                       "Connection to SRVSVC pipe of server %s "
140                                                       "failed: %s\n",
141                                                       r->in.server_name, nt_errstr(status));
142                 return status;
143         } 
144                 
145         s.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s", r->in.server_name);
146         s.in.share_name = r->in.share_name;
147
148         status = dcerpc_srvsvc_NetShareDel(c.standard.out.dcerpc_pipe, mem_ctx, &s);
149         if (!NT_STATUS_IS_OK(status)) {
150                 r->out.error_string = talloc_asprintf(mem_ctx,
151                                                       "srvsvc_NetShareDel on server '%s' failed"
152                                                       ": %s\n",
153                                                       r->in.server_name, nt_errstr(status));
154         }
155
156 disconnect:
157         talloc_free(c.standard.out.dcerpc_pipe);
158
159         return status;
160 }