r12858: This moves the libnet_LookupPdc code to use a GetDC request to find
[kamenim/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 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
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         uint32_t resume_handle;
32         struct srvsvc_NetShareCtr0 ctr0;
33         struct srvsvc_NetShareCtr1 ctr1;
34         struct srvsvc_NetShareCtr2 ctr2;
35         struct srvsvc_NetShareCtr501 ctr501;
36         struct srvsvc_NetShareCtr502 ctr502;
37
38         c.level               = LIBNET_RPC_CONNECT_SERVER;
39         c.in.name             = r->in.server_name;
40         c.in.dcerpc_iface     = &dcerpc_table_srvsvc;
41
42         s.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s", c.in.name);
43
44         status = libnet_RpcConnect(ctx, mem_ctx, &c);
45         if (!NT_STATUS_IS_OK(status)) {
46                 r->out.error_string = talloc_asprintf(mem_ctx,
47                                                       "Connection to SRVSVC pipe of server %s "
48                                                       "failed: %s\n",
49                                                       r->in.server_name,
50                                                       nt_errstr(status));
51                 return status;
52         }
53
54         s.in.level = r->in.level;
55         switch (s.in.level) {
56         case 0:
57                 s.in.ctr.ctr0 = &ctr0;
58                 ZERO_STRUCT(ctr0);
59                 break;
60         case 1:
61                 s.in.ctr.ctr1 = &ctr1;
62                 ZERO_STRUCT(ctr1);
63                 break;
64         case 2:
65                 s.in.ctr.ctr2 = &ctr2;
66                 ZERO_STRUCT(ctr2);
67                 break;
68         case 501:
69                 s.in.ctr.ctr501 = &ctr501;
70                 ZERO_STRUCT(ctr501);
71                 break;
72         case 502:
73                 s.in.ctr.ctr502 = &ctr502;
74                 ZERO_STRUCT(ctr502);
75                 break;
76         default:
77                 r->out.error_string = talloc_asprintf(mem_ctx,
78                                                       "libnet_ListShares: Invalid info level requested: %d\n",
79                                                       s.in.level);
80                 return NT_STATUS_INVALID_PARAMETER;
81         }
82         s.in.max_buffer = ~0;
83         s.in.resume_handle = &resume_handle;
84
85
86         status = dcerpc_srvsvc_NetShareEnumAll(c.out.dcerpc_pipe, mem_ctx, &s);
87         
88         if (!NT_STATUS_IS_OK(status)) {
89                 r->out.error_string = talloc_asprintf(mem_ctx,
90                                                       "srvsvc_NetShareEnumAll on server '%s' failed"
91                                                       ": %s\n",
92                                                       r->in.server_name, nt_errstr(status));
93                 goto disconnect;
94         }
95
96         if (!W_ERROR_IS_OK(s.out.result) && !W_ERROR_EQUAL(s.out.result, WERR_MORE_DATA)) {
97                 r->out.error_string = talloc_asprintf(mem_ctx,
98                                                       "srvsvc_NetShareEnumAll on server '%s' failed: %s",
99                                                       r->in.server_name, win_errstr(s.out.result));
100                 goto disconnect;
101         }
102
103         r->out.ctr = s.out.ctr;
104
105 disconnect:
106         talloc_free(c.out.dcerpc_pipe);
107
108         return status;  
109 }
110
111
112 NTSTATUS libnet_AddShare(struct libnet_context *ctx,
113                          TALLOC_CTX *mem_ctx, struct libnet_AddShare *r)
114 {
115         NTSTATUS status;
116         struct libnet_RpcConnect c;
117         struct srvsvc_NetShareAdd s;
118
119         c.level              = LIBNET_RPC_CONNECT_SERVER;
120         c.in.name            = r->in.server_name;
121         c.in.dcerpc_iface    = &dcerpc_table_srvsvc;
122
123         status = libnet_RpcConnect(ctx, mem_ctx, &c);
124         if (!NT_STATUS_IS_OK(status)) {
125                 r->out.error_string = talloc_asprintf(mem_ctx,
126                                                       "Connection to SRVSVC pipe of server %s "
127                                                       "failed: %s\n",
128                                                       r->in.server_name, nt_errstr(status));
129                 return status;
130         }
131
132         s.in.level              = r->in.level;
133         s.in.info.info2         = &r->in.share;
134         s.in.server_unc         = talloc_asprintf(mem_ctx, "\\\\%s", r->in.server_name);
135  
136         status = dcerpc_srvsvc_NetShareAdd(c.out.dcerpc_pipe, mem_ctx,&s);      
137
138         if (!NT_STATUS_IS_OK(status)) {
139                 r->out.error_string = talloc_asprintf(mem_ctx,
140                                                       "srvsvc_NetShareAdd on server '%s' failed"
141                                                       ": %s\n",
142                                                       r->in.server_name, nt_errstr(status));
143         }
144
145         talloc_free(c.out.dcerpc_pipe);
146         
147         return status;
148 }
149
150
151 NTSTATUS libnet_DelShare(struct libnet_context *ctx,
152                          TALLOC_CTX *mem_ctx, struct libnet_DelShare *r)
153 {
154         NTSTATUS status;
155         struct libnet_RpcConnect c;
156         struct srvsvc_NetShareDel s;
157
158         c.level               = LIBNET_RPC_CONNECT_SERVER;
159         c.in.name             = r->in.server_name;
160         c.in.dcerpc_iface     = &dcerpc_table_srvsvc;
161
162         status = libnet_RpcConnect(ctx, mem_ctx, &c);
163         if (!NT_STATUS_IS_OK(status)) {
164                 r->out.error_string = talloc_asprintf(mem_ctx,
165                                                       "Connection to SRVSVC pipe of server %s "
166                                                       "failed: %s\n",
167                                                       r->in.server_name, nt_errstr(status));
168                 return status;
169         } 
170                 
171         s.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s", r->in.server_name);
172         s.in.share_name = r->in.share_name;
173
174         status = dcerpc_srvsvc_NetShareDel(c.out.dcerpc_pipe, mem_ctx, &s);
175         if (!NT_STATUS_IS_OK(status)) {
176                 r->out.error_string = talloc_asprintf(mem_ctx,
177                                                       "srvsvc_NetShareDel on server '%s' failed"
178                                                       ": %s\n",
179                                                       r->in.server_name, nt_errstr(status));
180         }
181
182         talloc_free(c.out.dcerpc_pipe);
183
184         return status;
185 }