Makefile :
[kai/samba.git] / source3 / pipesrvsvc.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Pipe SMB reply routines - srvsvc pipe
5    Copyright (C) Andrew Tridgell 1992-1997,
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1997.
7    Copyright (C) Paul Ashton  1997.
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24
25 #include "includes.h"
26 #include "trans2.h"
27 #include "nterr.h"
28
29 extern int DEBUGLEVEL;
30
31
32 /*******************************************************************
33 ********************************************************************/
34 static void make_srv_share_info1_str(SH_INFO_1_STR *sh1, char *net_name, char *remark)
35 {
36         if (sh1 == NULL) return;
37
38         DEBUG(5,("make_srv_share_info1_str: %s %s\n", net_name, remark));
39
40         make_unistr2(&(sh1->uni_netname), net_name, strlen(net_name)+1);
41         make_unistr2(&(sh1->uni_remark ), remark  , strlen(remark  )+1);
42 }
43
44 /*******************************************************************
45 ********************************************************************/
46 static void make_srv_share_info1(SH_INFO_1 *sh1, char *net_name, uint32 type, char *remark)
47 {
48         if (sh1 == NULL) return;
49
50         DEBUG(5,("make_srv_share_info1_str: %s %8x %s\n", net_name, type, remark));
51
52         sh1->ptr_netname = net_name != NULL ? 1 : 0;
53         sh1->type        = type;
54         sh1->ptr_remark  = remark   != NULL ? 1 : 0;
55 }
56
57 /*******************************************************************
58 fill in a share info level 1 structure.
59
60 this function breaks the rule that i'd like to be in place, namely
61 it doesn't receive its data as arguments: it has to call lp_xxxx()
62 functions itself.  yuck.
63
64 this function is identical to api_RNetShareEnum().  maybe it even
65 generates the same output!  (too much to hope for, really...)
66
67 ********************************************************************/
68 static void make_srv_share_1_ctr(SHARE_INFO_1_CTR *ctr)
69 {
70         int snum;
71         int num_entries = 0;
72         int svcs = lp_numservices();
73
74         if (ctr == NULL) return;
75
76         DEBUG(5,("make_srv_share_1_ctr\n"));
77
78         for (snum = 0; snum < svcs && num_entries < MAX_SHARE_ENTRIES; num_entries++, snum++)
79         {
80                 int len_net_name;
81                 pstring net_name;
82                 pstring remark;
83                 uint32 type;
84
85                 if (lp_browseable(snum) && lp_snum_ok(snum))
86                 {
87                         /* see ipc.c:fill_share_info() */
88
89                         pstrcpy(net_name, lp_servicename(snum));
90                         pstrcpy(remark  , lp_comment    (snum));
91                         len_net_name = strlen(net_name);
92
93                         /* work out the share type */
94                         type = STYPE_DISKTREE;
95                         
96                         if (lp_print_ok(snum))             type = STYPE_PRINTQ;
97                         if (strequal("IPC$", net_name))    type = STYPE_IPC;
98                         if (net_name[len_net_name] == '$') type |= STYPE_HIDDEN;
99
100                         make_srv_share_info1    (&(ctr->info_1    [num_entries]), net_name, type, remark);
101                         make_srv_share_info1_str(&(ctr->info_1_str[num_entries]), net_name,       remark);
102                 }
103         }
104
105         ctr->num_entries_read  = num_entries;
106         ctr->ptr_share_info    = num_entries > 0 ? 1 : 0;
107         ctr->num_entries_read2 = num_entries;
108         ctr->num_entries_read3 = num_entries;
109         ctr->padding           = 0;
110 }
111
112 /*******************************************************************
113 ********************************************************************/
114 static void make_srv_net_share_enum(SRV_R_NET_SHARE_ENUM *r_n,
115                              int share_level, int switch_value, int status)  
116 {
117         DEBUG(5,("make_srv_net_share_enum: %d\n", __LINE__));
118
119         r_n->share_level  = share_level;
120         r_n->switch_value = switch_value;
121         r_n->status       = status;
122
123         switch (switch_value)
124         {
125                 case 1:
126                 {
127                         make_srv_share_1_ctr(&(r_n->share.info1));
128                         r_n->ptr_share_info = r_n->share.info1.num_entries_read > 0 ? 1 : 0;
129                         break;
130                 }
131                 default:
132                 {
133                         DEBUG(5,("make_srv_net_share_enum: unsupported switch value %d\n",
134                                   switch_value));
135                         r_n->ptr_share_info = 0;
136                         break;
137                 }
138         }
139 }
140
141 /*******************************************************************
142 ********************************************************************/
143 static int srv_reply_net_share_enum(SRV_Q_NET_SHARE_ENUM *q_n,
144                                 char *q, char *base,
145                                 int status)
146 {
147         SRV_R_NET_SHARE_ENUM r_n;
148
149         DEBUG(5,("srv_net_share_enum: %d\n", __LINE__));
150
151         /* set up the */
152         make_srv_net_share_enum(&r_n, q_n->share_level, q_n->switch_value, status);
153
154         /* store the response in the SMB stream */
155         q = srv_io_r_net_share_enum(False, &r_n, q, base, 4, 0);
156
157         DEBUG(5,("srv_srv_pwset: %d\n", __LINE__));
158
159         /* return length of SMB data stored */
160         return PTR_DIFF(q, base);
161 }
162
163 /*******************************************************************
164 ********************************************************************/
165 static void api_srv_net_share_info( char *param, char *data,
166                                     char **rdata, int *rdata_len )
167 {
168         SRV_Q_NET_SHARE_ENUM q_n;
169
170         /* grab the net share enum */
171         srv_io_q_net_share_enum(True, &q_n, data + 0x18, data, 4, 0);
172
173         /* XXXX push the reply buffer size up a bit, and hope it's sufficient */
174         /* for the current maximum limit of 32 share entries */
175         *rdata_len = 4096;
176         *rdata = REALLOC(*rdata, *rdata_len);
177
178         /* construct reply.  always indicate success */
179         *rdata_len = srv_reply_net_share_enum(&q_n, *rdata + 0x18, *rdata, 0x0);
180 }
181
182
183 /*******************************************************************
184 receives a srvsvc pipe and responds.
185 ********************************************************************/
186 BOOL api_srvsvcTNP(int cnum,int uid, char *param,char *data,
187                      int mdrcnt,int mprcnt,
188                      char **rdata,char **rparam,
189                      int *rdata_len,int *rparam_len)
190 {
191         RPC_HDR hdr;
192
193         if (data == NULL)
194         {
195                 DEBUG(2,("api_srvsvcTNP: NULL data received\n"));
196                 return False;
197         }
198
199         smb_io_rpc_hdr(True, &hdr, data, data, 4, 0);
200
201         if (hdr.pkt_type == RPC_BIND) /* RPC BIND */
202         {
203                 DEBUG(4,("srvsvc rpc bind %x\n",hdr.pkt_type));
204                 LsarpcTNP1(data,rdata,rdata_len);
205                 return True;
206         }
207
208         DEBUG(4,("srvsvc TransactNamedPipe op %x\n",hdr.opnum));
209
210         switch (hdr.opnum)
211         {
212                 case NETSHAREENUM:
213                 {
214                         api_srv_net_share_info( param, data, rdata, rdata_len);
215                         create_rpc_reply(hdr.call_id, *rdata, *rdata_len);
216                         break;
217                 }
218
219                 case NETSERVERGETINFO:
220                 {
221                         extern pstring myname;
222                         char *q;
223                         char *servername;
224                         uint32 level;
225                         UNISTR2 uni_str;
226
227                         q = data + 0x18;
228
229                         servername = q + 16;
230                         q = skip_unicode_string(servername,1);
231                         if (strlen(unistr(servername)) % 2 == 0)
232                         q += 2;
233                         level = IVAL(q, 0); q += 4;
234
235                         /* ignore the rest for the moment */
236                         initrpcreply(data, *rdata);
237                         q = *rdata + 0x18;
238
239                         SIVAL(q, 0, 101); q += 4; /* switch value */
240                         SIVAL(q, 0, 2); q += 4; /* bufptr */
241                         SIVAL(q, 0, 0x1f4); q += 4; /* platform id */
242                         SIVAL(q, 0, 2); q += 4; /* bufptr for name */
243                         SIVAL(q, 0, 5); q += 4; /* major version */
244                         SIVAL(q, 0, 4); q += 4; /* minor version == 5.4 */
245                         SIVAL(q, 0, 0x4100B); q += 4; /* type */
246                         SIVAL(q, 0, 2); q += 4; /* comment */
247
248                         get_myname(myname,NULL);
249
250                         make_unistr2(&uni_str, myname, strlen(myname));
251                         q = smb_io_unistr2(False, &uni_str, q, *rdata, 4, 0);
252
253                         make_unistr2(&uni_str, lp_serverstring(), strlen(lp_serverstring()));
254                         q = smb_io_unistr2(False, &uni_str, q, *rdata, 4, 0);
255
256                         q = align_offset(q, *rdata, 4);
257
258                         endrpcreply(data, *rdata, q-*rdata, 0, rdata_len);
259                         break;
260                 }
261
262                 default:
263                 {
264                         DEBUG(4, ("srvsvc, unknown code: %lx\n", hdr.opnum));
265                         break;
266                 }
267         }
268
269         return(True);
270 }
271