r6225: get rid of warnings from my compiler about nested externs
[samba.git] / source / rpc_server / srv_srvsvc_nt.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell              1992-1997,
5  *  Copyright (C) Jeremy Allison                                        2001.
6  *  Copyright (C) Nigel Williams                                        2001.
7  *  
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *  
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *  
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 /* This is the implementation of the srvsvc pipe. */
24
25 #include "includes.h"
26
27 extern DOM_SID global_sid_World;
28 extern struct generic_mapping file_generic_mapping;
29
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_RPC_SRV
32
33 /*******************************************************************
34  Utility function to get the 'type' of a share from an snum.
35  ********************************************************************/
36 static uint32 get_share_type(int snum) 
37 {
38         char *net_name = lp_servicename(snum);
39         int len_net_name = strlen(net_name);
40         
41         /* work out the share type */
42         uint32 type = STYPE_DISKTREE;
43
44         if (lp_print_ok(snum))
45                 type = STYPE_PRINTQ;
46         if (strequal(lp_fstype(snum), "IPC"))
47                 type = STYPE_IPC;
48         if (net_name[len_net_name] == '$')
49                 type |= STYPE_HIDDEN;
50
51         return type;
52 }
53         
54 /*******************************************************************
55  Fill in a share info level 0 structure.
56  ********************************************************************/
57
58 static void init_srv_share_info_0(pipes_struct *p, SRV_SHARE_INFO_0 *sh0, int snum)
59 {
60         pstring net_name;
61
62         pstrcpy(net_name, lp_servicename(snum));
63
64         init_srv_share_info0(&sh0->info_0, net_name);
65         init_srv_share_info0_str(&sh0->info_0_str, net_name);
66 }
67
68 /*******************************************************************
69  Fill in a share info level 1 structure.
70  ********************************************************************/
71
72 static void init_srv_share_info_1(pipes_struct *p, SRV_SHARE_INFO_1 *sh1, int snum)
73 {
74         pstring remark;
75
76         char *net_name = lp_servicename(snum);
77         pstrcpy(remark, lp_comment(snum));
78         standard_sub_conn(p->conn, remark,sizeof(remark));
79
80         init_srv_share_info1(&sh1->info_1, net_name, get_share_type(snum), remark);
81         init_srv_share_info1_str(&sh1->info_1_str, net_name, remark);
82 }
83
84 /*******************************************************************
85  Fill in a share info level 2 structure.
86  ********************************************************************/
87
88 static void init_srv_share_info_2(pipes_struct *p, SRV_SHARE_INFO_2 *sh2, int snum)
89 {
90         pstring remark;
91         pstring path;
92         pstring passwd;
93
94         char *net_name = lp_servicename(snum);
95         pstrcpy(remark, lp_comment(snum));
96         standard_sub_conn(p->conn, remark,sizeof(remark));
97         pstrcpy(path, "C:");
98         pstrcat(path, lp_pathname(snum));
99
100         /*
101          * Change / to \\ so that win2k will see it as a valid path.  This was added to
102          * enable use of browsing in win2k add share dialog.
103          */ 
104
105         string_replace(path, '/', '\\');
106
107         pstrcpy(passwd, "");
108
109         init_srv_share_info2(&sh2->info_2, net_name, get_share_type(snum), remark, 0, 0xffffffff, 1, path, passwd);
110         init_srv_share_info2_str(&sh2->info_2_str, net_name, remark, path, passwd);
111 }
112
113 /*******************************************************************
114  What to do when smb.conf is updated.
115  ********************************************************************/
116
117 static void smb_conf_updated(int msg_type, pid_t src, void *buf, size_t len)
118 {
119         DEBUG(10,("smb_conf_updated: Got message saying smb.conf was updated. Reloading.\n"));
120         reload_services(False);
121 }
122
123 /*******************************************************************
124  Create the share security tdb.
125  ********************************************************************/
126
127 static TDB_CONTEXT *share_tdb; /* used for share security descriptors */
128 #define SHARE_DATABASE_VERSION_V1 1
129 #define SHARE_DATABASE_VERSION_V2 2 /* version id in little endian. */
130
131 BOOL share_info_db_init(void)
132 {
133         static pid_t local_pid;
134         const char *vstring = "INFO/version";
135         int32 vers_id;
136  
137         if (share_tdb && local_pid == sys_getpid())
138                 return True;
139         share_tdb = tdb_open_log(lock_path("share_info.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
140         if (!share_tdb) {
141                 DEBUG(0,("Failed to open share info database %s (%s)\n",
142                         lock_path("share_info.tdb"), strerror(errno) ));
143                 return False;
144         }
145  
146         local_pid = sys_getpid();
147  
148         /* handle a Samba upgrade */
149         tdb_lock_bystring(share_tdb, vstring, 0);
150
151         /* Cope with byte-reversed older versions of the db. */
152         vers_id = tdb_fetch_int32(share_tdb, vstring);
153         if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) {
154                 /* Written on a bigendian machine with old fetch_int code. Save as le. */
155                 tdb_store_int32(share_tdb, vstring, SHARE_DATABASE_VERSION_V2);
156                 vers_id = SHARE_DATABASE_VERSION_V2;
157         }
158
159         if (vers_id != SHARE_DATABASE_VERSION_V2) {
160                 tdb_traverse(share_tdb, tdb_traverse_delete_fn, NULL);
161                 tdb_store_int32(share_tdb, vstring, SHARE_DATABASE_VERSION_V2);
162         }
163         tdb_unlock_bystring(share_tdb, vstring);
164
165         message_register(MSG_SMB_CONF_UPDATED, smb_conf_updated);
166  
167         return True;
168 }
169
170 /*******************************************************************
171  Fake up a Everyone, full access as a default.
172  ********************************************************************/
173
174 static SEC_DESC *get_share_security_default( TALLOC_CTX *ctx, int snum, size_t *psize)
175 {
176         SEC_ACCESS sa;
177         SEC_ACE ace;
178         SEC_ACL *psa = NULL;
179         SEC_DESC *psd = NULL;
180         uint32 def_access = GENERIC_ALL_ACCESS;
181
182         se_map_generic(&def_access, &file_generic_mapping);
183
184         init_sec_access(&sa, GENERIC_ALL_ACCESS | def_access );
185         init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0);
186
187         if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 1, &ace)) != NULL) {
188                 psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, psa, psize);
189         }
190
191         if (!psd) {
192                 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
193                 return NULL;
194         }
195
196         return psd;
197 }
198
199 /*******************************************************************
200  Pull a security descriptor from the share tdb.
201  ********************************************************************/
202
203 static SEC_DESC *get_share_security( TALLOC_CTX *ctx, int snum, size_t *psize)
204 {
205         prs_struct ps;
206         fstring key;
207         SEC_DESC *psd = NULL;
208
209         *psize = 0;
210
211         /* Fetch security descriptor from tdb */
212  
213         slprintf(key, sizeof(key)-1, "SECDESC/%s", lp_servicename(snum));
214  
215         if (tdb_prs_fetch(share_tdb, key, &ps, ctx)!=0 ||
216                 !sec_io_desc("get_share_security", &psd, &ps, 1)) {
217  
218                 DEBUG(4,("get_share_security: using default secdesc for %s\n", lp_servicename(snum) ));
219  
220                 return get_share_security_default(ctx, snum, psize);
221         }
222
223         if (psd)
224                 *psize = sec_desc_size(psd);
225
226         prs_mem_free(&ps);
227         return psd;
228 }
229
230 /*******************************************************************
231  Store a security descriptor in the share db.
232  ********************************************************************/
233
234 static BOOL set_share_security(TALLOC_CTX *ctx, const char *share_name, SEC_DESC *psd)
235 {
236         prs_struct ps;
237         TALLOC_CTX *mem_ctx = NULL;
238         fstring key;
239         BOOL ret = False;
240
241         mem_ctx = talloc_init("set_share_security");
242         if (mem_ctx == NULL)
243                 return False;
244
245         prs_init(&ps, (uint32)sec_desc_size(psd), mem_ctx, MARSHALL);
246  
247         if (!sec_io_desc("share_security", &psd, &ps, 1))
248                 goto out;
249  
250         slprintf(key, sizeof(key)-1, "SECDESC/%s", share_name);
251  
252         if (tdb_prs_store(share_tdb, key, &ps)==0) {
253                 ret = True;
254                 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
255         } else {
256                 DEBUG(1,("set_share_security: Failed to store secdesc for %s\n", share_name ));
257         } 
258
259         /* Free malloc'ed memory */
260  
261 out:
262  
263         prs_mem_free(&ps);
264         if (mem_ctx)
265                 talloc_destroy(mem_ctx);
266         return ret;
267 }
268
269 /*******************************************************************
270  Delete a security descriptor.
271 ********************************************************************/
272
273 static BOOL delete_share_security(int snum)
274 {
275         TDB_DATA kbuf;
276         fstring key;
277
278         slprintf(key, sizeof(key)-1, "SECDESC/%s", lp_servicename(snum));
279         kbuf.dptr = key;
280         kbuf.dsize = strlen(key)+1;
281
282         if (tdb_delete(share_tdb, kbuf) != 0) {
283                 DEBUG(0,("delete_share_security: Failed to delete entry for share %s\n",
284                                 lp_servicename(snum) ));
285                 return False;
286         }
287
288         return True;
289 }
290
291 /*******************************************************************
292  Map any generic bits to file specific bits.
293 ********************************************************************/
294
295 void map_generic_share_sd_bits(SEC_DESC *psd)
296 {
297         int i;
298         SEC_ACL *ps_dacl = NULL;
299
300         if (!psd)
301                 return;
302
303         ps_dacl = psd->dacl;
304         if (!ps_dacl)
305                 return;
306
307         for (i = 0; i < ps_dacl->num_aces; i++) {
308                 SEC_ACE *psa = &ps_dacl->ace[i];
309                 uint32 orig_mask = psa->info.mask;
310
311                 se_map_generic(&psa->info.mask, &file_generic_mapping);
312                 psa->info.mask |= orig_mask;
313         }       
314 }
315
316 /*******************************************************************
317  Can this user access with share with the required permissions ?
318 ********************************************************************/
319
320 BOOL share_access_check(connection_struct *conn, int snum, user_struct *vuser, uint32 desired_access)
321 {
322         uint32 granted;
323         NTSTATUS status;
324         TALLOC_CTX *mem_ctx = NULL;
325         SEC_DESC *psd = NULL;
326         size_t sd_size;
327         NT_USER_TOKEN *token = NULL;
328         BOOL ret = True;
329
330         mem_ctx = talloc_init("share_access_check");
331         if (mem_ctx == NULL)
332                 return False;
333
334         psd = get_share_security(mem_ctx, snum, &sd_size);
335
336         if (!psd)
337                 goto out;
338
339         if (conn->nt_user_token)
340                 token = conn->nt_user_token;
341         else 
342                 token = vuser->nt_user_token;
343
344         ret = se_access_check(psd, token, desired_access, &granted, &status);
345
346 out:
347
348         talloc_destroy(mem_ctx);
349
350         return ret;
351 }
352
353 /*******************************************************************
354  Fill in a share info level 501 structure.
355 ********************************************************************/
356
357 static void init_srv_share_info_501(pipes_struct *p, SRV_SHARE_INFO_501 *sh501, int snum)
358 {
359         pstring remark;
360
361         const char *net_name = lp_servicename(snum);
362         pstrcpy(remark, lp_comment(snum));
363         standard_sub_conn(p->conn, remark, sizeof(remark));
364
365         init_srv_share_info501(&sh501->info_501, net_name, get_share_type(snum), remark, (lp_csc_policy(snum) << 4));
366         init_srv_share_info501_str(&sh501->info_501_str, net_name, remark);
367 }
368
369 /*******************************************************************
370  Fill in a share info level 502 structure.
371  ********************************************************************/
372
373 static void init_srv_share_info_502(pipes_struct *p, SRV_SHARE_INFO_502 *sh502, int snum)
374 {
375         pstring net_name;
376         pstring remark;
377         pstring path;
378         pstring passwd;
379         SEC_DESC *sd;
380         size_t sd_size;
381         TALLOC_CTX *ctx = p->mem_ctx;
382
383
384         ZERO_STRUCTP(sh502);
385
386         pstrcpy(net_name, lp_servicename(snum));
387         pstrcpy(remark, lp_comment(snum));
388         standard_sub_conn(p->conn, remark,sizeof(remark));
389         pstrcpy(path, "C:");
390         pstrcat(path, lp_pathname(snum));
391
392         /*
393          * Change / to \\ so that win2k will see it as a valid path.  This was added to
394          * enable use of browsing in win2k add share dialog.
395          */ 
396
397         string_replace(path, '/', '\\');
398
399         pstrcpy(passwd, "");
400
401         sd = get_share_security(ctx, snum, &sd_size);
402
403         init_srv_share_info502(&sh502->info_502, net_name, get_share_type(snum), remark, 0, 0xffffffff, 1, path, passwd, sd, sd_size);
404         init_srv_share_info502_str(&sh502->info_502_str, net_name, remark, path, passwd, sd, sd_size);
405 }
406
407 /***************************************************************************
408  Fill in a share info level 1004 structure.
409  ***************************************************************************/
410
411 static void init_srv_share_info_1004(pipes_struct *p, SRV_SHARE_INFO_1004* sh1004, int snum)
412 {
413         pstring remark;
414
415         pstrcpy(remark, lp_comment(snum));
416         standard_sub_conn(p->conn, remark, sizeof(remark));
417
418         ZERO_STRUCTP(sh1004);
419   
420         init_srv_share_info1004(&sh1004->info_1004, remark);
421         init_srv_share_info1004_str(&sh1004->info_1004_str, remark);
422 }
423
424 /***************************************************************************
425  Fill in a share info level 1005 structure.
426  ***************************************************************************/
427
428 static void init_srv_share_info_1005(pipes_struct *p, SRV_SHARE_INFO_1005* sh1005, int snum)
429 {
430         sh1005->share_info_flags = 0;
431
432         if(lp_host_msdfs() && lp_msdfs_root(snum))
433                 sh1005->share_info_flags |= 
434                         SHARE_1005_IN_DFS | SHARE_1005_DFS_ROOT;
435         sh1005->share_info_flags |= 
436                 lp_csc_policy(snum) << SHARE_1005_CSC_POLICY_SHIFT;
437 }
438 /***************************************************************************
439  Fill in a share info level 1006 structure.
440  ***************************************************************************/
441
442 static void init_srv_share_info_1006(pipes_struct *p, SRV_SHARE_INFO_1006* sh1006, int snum)
443 {
444         sh1006->max_uses = -1;
445 }
446
447 /***************************************************************************
448  Fill in a share info level 1007 structure.
449  ***************************************************************************/
450
451 static void init_srv_share_info_1007(pipes_struct *p, SRV_SHARE_INFO_1007* sh1007, int snum)
452 {
453         pstring alternate_directory_name = "";
454         uint32 flags = 0;
455
456         ZERO_STRUCTP(sh1007);
457   
458         init_srv_share_info1007(&sh1007->info_1007, flags, alternate_directory_name);
459         init_srv_share_info1007_str(&sh1007->info_1007_str, alternate_directory_name);
460 }
461
462 /*******************************************************************
463  Fill in a share info level 1501 structure.
464  ********************************************************************/
465
466 static void init_srv_share_info_1501(pipes_struct *p, SRV_SHARE_INFO_1501 *sh1501, int snum)
467 {
468         SEC_DESC *sd;
469         size_t sd_size;
470         TALLOC_CTX *ctx = p->mem_ctx;
471
472         ZERO_STRUCTP(sh1501);
473
474         sd = get_share_security(ctx, snum, &sd_size);
475
476         sh1501->sdb = make_sec_desc_buf(p->mem_ctx, sd_size, sd);
477 }
478
479 /*******************************************************************
480  True if it ends in '$'.
481  ********************************************************************/
482
483 static BOOL is_hidden_share(int snum)
484 {
485         const char *net_name = lp_servicename(snum);
486
487         return (net_name[strlen(net_name) - 1] == '$') ? True : False;
488 }
489
490 /*******************************************************************
491  Fill in a share info structure.
492  ********************************************************************/
493
494 static BOOL init_srv_share_info_ctr(pipes_struct *p, SRV_SHARE_INFO_CTR *ctr,
495                uint32 info_level, uint32 *resume_hnd, uint32 *total_entries, BOOL all_shares)
496 {
497         int num_entries = 0;
498         int num_services = lp_numservices();
499         int snum;
500         TALLOC_CTX *ctx = p->mem_ctx;
501
502         DEBUG(5,("init_srv_share_info_ctr\n"));
503
504         ZERO_STRUCTPN(ctr);
505
506         ctr->info_level = ctr->switch_value = info_level;
507         *resume_hnd = 0;
508
509         /* Count the number of entries. */
510         for (snum = 0; snum < num_services; snum++) {
511                 if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) )
512                         num_entries++;
513         }
514
515         *total_entries = num_entries;
516         ctr->num_entries2 = ctr->num_entries = num_entries;
517         ctr->ptr_share_info = ctr->ptr_entries = 1;
518
519         if (!num_entries)
520                 return True;
521
522         switch (info_level) {
523         case 0:
524         {
525                 SRV_SHARE_INFO_0 *info0 = TALLOC_ARRAY(ctx, SRV_SHARE_INFO_0, num_entries);
526                 int i = 0;
527
528                 if (!info0) {
529                         return False;
530                 }
531
532                 for (snum = *resume_hnd; snum < num_services; snum++) {
533                         if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) ) {
534                                 init_srv_share_info_0(p, &info0[i++], snum);
535                         }
536                 }
537
538                 ctr->share.info0 = info0;
539                 break;
540
541         }
542
543         case 1:
544         {
545                 SRV_SHARE_INFO_1 *info1 = TALLOC_ARRAY(ctx, SRV_SHARE_INFO_1, num_entries);
546                 int i = 0;
547
548                 if (!info1) {
549                         return False;
550                 }
551
552                 for (snum = *resume_hnd; snum < num_services; snum++) {
553                         if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) ) {
554                                 init_srv_share_info_1(p, &info1[i++], snum);
555                         }
556                 }
557
558                 ctr->share.info1 = info1;
559                 break;
560         }
561
562         case 2:
563         {
564                 SRV_SHARE_INFO_2 *info2 = TALLOC_ARRAY(ctx, SRV_SHARE_INFO_2, num_entries);
565                 int i = 0;
566
567                 if (!info2) {
568                         return False;
569                 }
570
571                 for (snum = *resume_hnd; snum < num_services; snum++) {
572                         if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) ) {
573                                 init_srv_share_info_2(p, &info2[i++], snum);
574                         }
575                 }
576
577                 ctr->share.info2 = info2;
578                 break;
579         }
580
581         case 501:
582         {
583                 SRV_SHARE_INFO_501 *info501 = TALLOC_ARRAY(ctx, SRV_SHARE_INFO_501, num_entries);
584                 int i = 0;
585         
586                 if (!info501) {
587                         return False;
588                 }
589
590                 for (snum = *resume_hnd; snum < num_services; snum++) {
591                         if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) ) {
592                                 init_srv_share_info_501(p, &info501[i++], snum);
593                         }
594                 }
595         
596                 ctr->share.info501 = info501;
597                 break;
598         }
599
600         case 502:
601         {
602                 SRV_SHARE_INFO_502 *info502 = TALLOC_ARRAY(ctx, SRV_SHARE_INFO_502, num_entries);
603                 int i = 0;
604
605                 if (!info502) {
606                         return False;
607                 }
608
609                 for (snum = *resume_hnd; snum < num_services; snum++) {
610                         if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) ) {
611                                 init_srv_share_info_502(p, &info502[i++], snum);
612                         }
613                 }
614
615                 ctr->share.info502 = info502;
616                 break;
617         }
618
619         /* here for completeness but not currently used with enum (1004 - 1501)*/
620         
621         case 1004:
622         {
623                 SRV_SHARE_INFO_1004 *info1004 = TALLOC_ARRAY(ctx, SRV_SHARE_INFO_1004, num_entries);
624                 int i = 0;
625
626                 if (!info1004) {
627                         return False;
628                 }
629
630                 for (snum = *resume_hnd; snum < num_services; snum++) {
631                         if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) ) {
632                                 init_srv_share_info_1004(p, &info1004[i++], snum);
633                         }
634                 }
635
636                 ctr->share.info1004 = info1004;
637                 break;
638         }
639
640         case 1005:
641         {
642                 SRV_SHARE_INFO_1005 *info1005 = TALLOC_ARRAY(ctx, SRV_SHARE_INFO_1005, num_entries);
643                 int i = 0;
644
645                 if (!info1005) {
646                         return False;
647                 }
648
649                 for (snum = *resume_hnd; snum < num_services; snum++) {
650                         if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) ) {
651                                 init_srv_share_info_1005(p, &info1005[i++], snum);
652                         }
653                 }
654
655                 ctr->share.info1005 = info1005;
656                 break;
657         }
658
659         case 1006:
660         {
661                 SRV_SHARE_INFO_1006 *info1006 = TALLOC_ARRAY(ctx, SRV_SHARE_INFO_1006, num_entries);
662                 int i = 0;
663
664                 if (!info1006) {
665                         return False;
666                 }
667
668                 for (snum = *resume_hnd; snum < num_services; snum++) {
669                         if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) ) {
670                                 init_srv_share_info_1006(p, &info1006[i++], snum);
671                         }
672                 }
673
674                 ctr->share.info1006 = info1006;
675                 break;
676         }
677
678         case 1007:
679         {
680                 SRV_SHARE_INFO_1007 *info1007 = TALLOC_ARRAY(ctx, SRV_SHARE_INFO_1007, num_entries);
681                 int i = 0;
682
683                 if (!info1007) {
684                         return False;
685                 }
686
687                 for (snum = *resume_hnd; snum < num_services; snum++) {
688                         if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) ) {
689                                 init_srv_share_info_1007(p, &info1007[i++], snum);
690                         }
691                 }
692
693                 ctr->share.info1007 = info1007;
694                 break;
695         }
696
697         case 1501:
698         {
699                 SRV_SHARE_INFO_1501 *info1501 = TALLOC_ARRAY(ctx, SRV_SHARE_INFO_1501, num_entries);
700                 int i = 0;
701
702                 if (!info1501) {
703                         return False;
704                 }
705
706                 for (snum = *resume_hnd; snum < num_services; snum++) {
707                         if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) ) {
708                                 init_srv_share_info_1501(p, &info1501[i++], snum);
709                         }
710                 }
711
712                 ctr->share.info1501 = info1501;
713                 break;
714         }
715         default:
716                 DEBUG(5,("init_srv_share_info_ctr: unsupported switch value %d\n", info_level));
717                 return False;
718         }
719
720         return True;
721 }
722
723 /*******************************************************************
724  Inits a SRV_R_NET_SHARE_ENUM structure.
725 ********************************************************************/
726
727 static void init_srv_r_net_share_enum(pipes_struct *p, SRV_R_NET_SHARE_ENUM *r_n,
728                                       uint32 info_level, uint32 resume_hnd, BOOL all)  
729 {
730         DEBUG(5,("init_srv_r_net_share_enum: %d\n", __LINE__));
731
732         if (init_srv_share_info_ctr(p, &r_n->ctr, info_level,
733                                     &resume_hnd, &r_n->total_entries, all)) {
734                 r_n->status = WERR_OK;
735         } else {
736                 r_n->status = WERR_UNKNOWN_LEVEL;
737         }
738
739         init_enum_hnd(&r_n->enum_hnd, resume_hnd);
740 }
741
742 /*******************************************************************
743  Inits a SRV_R_NET_SHARE_GET_INFO structure.
744 ********************************************************************/
745
746 static void init_srv_r_net_share_get_info(pipes_struct *p, SRV_R_NET_SHARE_GET_INFO *r_n,
747                                   char *share_name, uint32 info_level)
748 {
749         WERROR status = WERR_OK;
750         int snum;
751
752         DEBUG(5,("init_srv_r_net_share_get_info: %d\n", __LINE__));
753
754         r_n->info.switch_value = info_level;
755
756         snum = find_service(share_name);
757
758         if (snum >= 0) {
759                 switch (info_level) {
760                 case 0:
761                         init_srv_share_info_0(p, &r_n->info.share.info0, snum);
762                         break;
763                 case 1:
764                         init_srv_share_info_1(p, &r_n->info.share.info1, snum);
765                         break;
766                 case 2:
767                         init_srv_share_info_2(p, &r_n->info.share.info2, snum);
768                         break;
769                 case 501:
770                         init_srv_share_info_501(p, &r_n->info.share.info501, snum);
771                         break;
772                 case 502:
773                         init_srv_share_info_502(p, &r_n->info.share.info502, snum);
774                         break;
775
776                         /* here for completeness */
777                 case 1004:
778                         init_srv_share_info_1004(p, &r_n->info.share.info1004, snum);
779                         break;
780                 case 1005:
781                         init_srv_share_info_1005(p, &r_n->info.share.info1005, snum);
782                         break;
783
784                         /* here for completeness 1006 - 1501 */
785                 case 1006:
786                         init_srv_share_info_1006(p, &r_n->info.share.info1006, snum);
787                         break;
788                 case 1007:
789                         init_srv_share_info_1007(p, &r_n->info.share.info1007, snum);
790                         break;
791                 case 1501:
792                         init_srv_share_info_1501(p, &r_n->info.share.info1501, snum);
793                         break;
794                 default:
795                         DEBUG(5,("init_srv_net_share_get_info: unsupported switch value %d\n", info_level));
796                         status = WERR_UNKNOWN_LEVEL;
797                         break;
798                 }
799         } else {
800                 status = WERR_INVALID_NAME;
801         }
802
803         r_n->info.ptr_share_ctr = W_ERROR_IS_OK(status) ? 1 : 0;
804         r_n->status = status;
805 }
806
807 /*******************************************************************
808  fill in a sess info level 1 structure.
809  ********************************************************************/
810
811 static void init_srv_sess_0_info(SESS_INFO_0 *se0, SESS_INFO_0_STR *str0, char *name)
812 {
813         init_srv_sess_info0(se0, name);
814         init_srv_sess_info0_str(str0, name);
815 }
816
817 /*******************************************************************
818  fill in a sess info level 0 structure.
819  ********************************************************************/
820
821 static void init_srv_sess_info_0(SRV_SESS_INFO_0 *ss0, uint32 *snum, uint32 *stot)
822 {
823         struct sessionid *session_list;
824         uint32 num_entries = 0;
825         (*stot) = list_sessions(&session_list);
826
827         if (ss0 == NULL) {
828                 (*snum) = 0;
829                 SAFE_FREE(session_list);
830                 return;
831         }
832
833         DEBUG(5,("init_srv_sess_0_ss0\n"));
834
835         if (snum) {
836                 for (; (*snum) < (*stot) && num_entries < MAX_SESS_ENTRIES; (*snum)++) {
837                         init_srv_sess_0_info(&ss0->info_0[num_entries],
838                                                                  &ss0->info_0_str[num_entries], session_list[(*snum)].remote_machine);
839
840                         /* move on to creating next session */
841                         /* move on to creating next sess */
842                         num_entries++;
843                 }
844
845                 ss0->num_entries_read  = num_entries;
846                 ss0->ptr_sess_info     = num_entries > 0 ? 1 : 0;
847                 ss0->num_entries_read2 = num_entries;
848                 
849                 if ((*snum) >= (*stot)) {
850                         (*snum) = 0;
851                 }
852
853         } else {
854                 ss0->num_entries_read = 0;
855                 ss0->ptr_sess_info = 0;
856                 ss0->num_entries_read2 = 0;
857         }
858         SAFE_FREE(session_list);
859 }
860
861 /*******************************************************************
862  fill in a sess info level 1 structure.
863  ********************************************************************/
864
865 static void init_srv_sess_1_info(SESS_INFO_1 *se1, SESS_INFO_1_STR *str1,
866                                 char *name, char *user,
867                                 uint32 num_opens,
868                                 uint32 open_time, uint32 idle_time,
869                                 uint32 usr_flgs)
870 {
871         init_srv_sess_info1(se1 , name, user, num_opens, open_time, idle_time, usr_flgs);
872         init_srv_sess_info1_str(str1, name, user);
873 }
874
875 /*******************************************************************
876  fill in a sess info level 1 structure.
877  ********************************************************************/
878
879 static void init_srv_sess_info_1(SRV_SESS_INFO_1 *ss1, uint32 *snum, uint32 *stot)
880 {
881         struct sessionid *session_list;
882         uint32 num_entries = 0;
883         (*stot) = list_sessions(&session_list);
884
885         if (ss1 == NULL) {
886                 (*snum) = 0;
887                 SAFE_FREE(session_list);
888                 return;
889         }
890
891         DEBUG(5,("init_srv_sess_1_ss1\n"));
892
893         if (snum) {
894                 for (; (*snum) < (*stot) && num_entries < MAX_SESS_ENTRIES; (*snum)++) {
895                         init_srv_sess_1_info(&ss1->info_1[num_entries],
896                                              &ss1->info_1_str[num_entries],
897                                             session_list[*snum].remote_machine,
898                                              session_list[*snum].username,
899                                              1, 10, 5, 0);
900
901                         /* move on to creating next session */
902                         /* move on to creating next sess */
903                         num_entries++;
904                 }
905
906                 ss1->num_entries_read  = num_entries;
907                 ss1->ptr_sess_info     = num_entries > 0 ? 1 : 0;
908                 ss1->num_entries_read2 = num_entries;
909                 
910                 if ((*snum) >= (*stot)) {
911                         (*snum) = 0;
912                 }
913
914         } else {
915                 ss1->num_entries_read = 0;
916                 ss1->ptr_sess_info = 0;
917                 ss1->num_entries_read2 = 0;
918                 
919                 (*stot) = 0;
920         }
921 }
922
923 /*******************************************************************
924  makes a SRV_R_NET_SESS_ENUM structure.
925 ********************************************************************/
926
927 static WERROR init_srv_sess_info_ctr(SRV_SESS_INFO_CTR *ctr,
928                                 int switch_value, uint32 *resume_hnd, uint32 *total_entries)
929 {
930         WERROR status = WERR_OK;
931         DEBUG(5,("init_srv_sess_info_ctr: %d\n", __LINE__));
932
933         ctr->switch_value = switch_value;
934
935         switch (switch_value) {
936         case 0:
937                 init_srv_sess_info_0(&(ctr->sess.info0), resume_hnd, total_entries);
938                 ctr->ptr_sess_ctr = 1;
939                 break;
940         case 1:
941                 init_srv_sess_info_1(&(ctr->sess.info1), resume_hnd, total_entries);
942                 ctr->ptr_sess_ctr = 1;
943                 break;
944         default:
945                 DEBUG(5,("init_srv_sess_info_ctr: unsupported switch value %d\n", switch_value));
946                 (*resume_hnd) = 0;
947                 (*total_entries) = 0;
948                 ctr->ptr_sess_ctr = 0;
949                 status = WERR_UNKNOWN_LEVEL;
950                 break;
951         }
952
953         return status;
954 }
955
956 /*******************************************************************
957  makes a SRV_R_NET_SESS_ENUM structure.
958 ********************************************************************/
959
960 static void init_srv_r_net_sess_enum(SRV_R_NET_SESS_ENUM *r_n,
961                                 uint32 resume_hnd, int sess_level, int switch_value)  
962 {
963         DEBUG(5,("init_srv_r_net_sess_enum: %d\n", __LINE__));
964
965         r_n->sess_level  = sess_level;
966
967         if (sess_level == -1)
968                 r_n->status = WERR_UNKNOWN_LEVEL;
969         else
970                 r_n->status = init_srv_sess_info_ctr(r_n->ctr, switch_value, &resume_hnd, &r_n->total_entries);
971
972         if (!W_ERROR_IS_OK(r_n->status))
973                 resume_hnd = 0;
974
975         init_enum_hnd(&r_n->enum_hnd, resume_hnd);
976 }
977
978 /*******************************************************************
979  fill in a conn info level 0 structure.
980  ********************************************************************/
981
982 static void init_srv_conn_info_0(SRV_CONN_INFO_0 *ss0, uint32 *snum, uint32 *stot)
983 {
984         uint32 num_entries = 0;
985         (*stot) = 1;
986
987         if (ss0 == NULL) {
988                 (*snum) = 0;
989                 return;
990         }
991
992         DEBUG(5,("init_srv_conn_0_ss0\n"));
993
994         if (snum) {
995                 for (; (*snum) < (*stot) && num_entries < MAX_CONN_ENTRIES; (*snum)++) {
996
997                         init_srv_conn_info0(&ss0->info_0[num_entries], (*stot));
998
999                         /* move on to creating next connection */
1000                         /* move on to creating next conn */
1001                         num_entries++;
1002                 }
1003
1004                 ss0->num_entries_read  = num_entries;
1005                 ss0->ptr_conn_info     = num_entries > 0 ? 1 : 0;
1006                 ss0->num_entries_read2 = num_entries;
1007                 
1008                 if ((*snum) >= (*stot)) {
1009                         (*snum) = 0;
1010                 }
1011
1012         } else {
1013                 ss0->num_entries_read = 0;
1014                 ss0->ptr_conn_info = 0;
1015                 ss0->num_entries_read2 = 0;
1016
1017                 (*stot) = 0;
1018         }
1019 }
1020
1021 /*******************************************************************
1022  fill in a conn info level 1 structure.
1023  ********************************************************************/
1024
1025 static void init_srv_conn_1_info(CONN_INFO_1 *se1, CONN_INFO_1_STR *str1,
1026                                 uint32 id, uint32 type,
1027                                 uint32 num_opens, uint32 num_users, uint32 open_time,
1028                                 const char *usr_name, const char *net_name)
1029 {
1030         init_srv_conn_info1(se1 , id, type, num_opens, num_users, open_time, usr_name, net_name);
1031         init_srv_conn_info1_str(str1, usr_name, net_name);
1032 }
1033
1034 /*******************************************************************
1035  fill in a conn info level 1 structure.
1036  ********************************************************************/
1037
1038 static void init_srv_conn_info_1(SRV_CONN_INFO_1 *ss1, uint32 *snum, uint32 *stot)
1039 {
1040         uint32 num_entries = 0;
1041         (*stot) = 1;
1042
1043         if (ss1 == NULL) {
1044                 (*snum) = 0;
1045                 return;
1046         }
1047
1048         DEBUG(5,("init_srv_conn_1_ss1\n"));
1049
1050         if (snum) {
1051                 for (; (*snum) < (*stot) && num_entries < MAX_CONN_ENTRIES; (*snum)++) {
1052                         init_srv_conn_1_info(&ss1->info_1[num_entries],
1053                                                                  &ss1->info_1_str[num_entries],
1054                                              (*stot), 0x3, 1, 1, 3,"dummy_user", "IPC$");
1055
1056                         /* move on to creating next connection */
1057                         /* move on to creating next conn */
1058                         num_entries++;
1059                 }
1060
1061                 ss1->num_entries_read  = num_entries;
1062                 ss1->ptr_conn_info     = num_entries > 0 ? 1 : 0;
1063                 ss1->num_entries_read2 = num_entries;
1064                 
1065
1066                 if ((*snum) >= (*stot)) {
1067                         (*snum) = 0;
1068                 }
1069
1070         } else {
1071                 ss1->num_entries_read = 0;
1072                 ss1->ptr_conn_info = 0;
1073                 ss1->num_entries_read2 = 0;
1074                 
1075                 (*stot) = 0;
1076         }
1077 }
1078
1079 /*******************************************************************
1080  makes a SRV_R_NET_CONN_ENUM structure.
1081 ********************************************************************/
1082
1083 static WERROR init_srv_conn_info_ctr(SRV_CONN_INFO_CTR *ctr,
1084                                 int switch_value, uint32 *resume_hnd, uint32 *total_entries)
1085 {
1086         WERROR status = WERR_OK;
1087         DEBUG(5,("init_srv_conn_info_ctr: %d\n", __LINE__));
1088
1089         ctr->switch_value = switch_value;
1090
1091         switch (switch_value) {
1092         case 0:
1093                 init_srv_conn_info_0(&ctr->conn.info0, resume_hnd, total_entries);
1094                 ctr->ptr_conn_ctr = 1;
1095                 break;
1096         case 1:
1097                 init_srv_conn_info_1(&ctr->conn.info1, resume_hnd, total_entries);
1098                 ctr->ptr_conn_ctr = 1;
1099                 break;
1100         default:
1101                 DEBUG(5,("init_srv_conn_info_ctr: unsupported switch value %d\n", switch_value));
1102                 (*resume_hnd = 0);
1103                 (*total_entries) = 0;
1104                 ctr->ptr_conn_ctr = 0;
1105                 status = WERR_UNKNOWN_LEVEL;
1106                 break;
1107         }
1108
1109         return status;
1110 }
1111
1112 /*******************************************************************
1113  makes a SRV_R_NET_CONN_ENUM structure.
1114 ********************************************************************/
1115
1116 static void init_srv_r_net_conn_enum(SRV_R_NET_CONN_ENUM *r_n,
1117                                 uint32 resume_hnd, int conn_level, int switch_value)  
1118 {
1119         DEBUG(5,("init_srv_r_net_conn_enum: %d\n", __LINE__));
1120
1121         r_n->conn_level  = conn_level;
1122         if (conn_level == -1)
1123                 r_n->status = WERR_UNKNOWN_LEVEL;
1124         else
1125                 r_n->status = init_srv_conn_info_ctr(r_n->ctr, switch_value, &resume_hnd, &r_n->total_entries);
1126
1127         if (!W_ERROR_IS_OK(r_n->status))
1128                 resume_hnd = 0;
1129
1130         init_enum_hnd(&r_n->enum_hnd, resume_hnd);
1131 }
1132
1133 /*******************************************************************
1134  makes a SRV_R_NET_FILE_ENUM structure.
1135 ********************************************************************/
1136
1137 static WERROR init_srv_file_info_ctr(pipes_struct *p, SRV_FILE_INFO_CTR *ctr,
1138                                      int switch_value, uint32 *resume_hnd, 
1139                                      uint32 *total_entries)  
1140 {
1141         WERROR status = WERR_OK;
1142         TALLOC_CTX *ctx = p->mem_ctx;
1143         DEBUG(5,("init_srv_file_info_ctr: %d\n", __LINE__));
1144         *total_entries = 1; /* dummy entries only, for */
1145
1146         ctr->switch_value = switch_value;
1147         ctr->num_entries = *total_entries - *resume_hnd;
1148         ctr->num_entries2 = ctr->num_entries;
1149
1150         switch (switch_value) {
1151         case 3: {
1152                 int i;
1153                 if (*total_entries > 0) {
1154                         ctr->ptr_entries = 1;
1155                         ctr->file.info3 = TALLOC_ARRAY(ctx, SRV_FILE_INFO_3, ctr->num_entries);
1156                 }
1157                 for (i=0 ;i<ctr->num_entries;i++) {
1158                         init_srv_file_info3(&ctr->file.info3[i].info_3, i+*resume_hnd, 0x35, 0, "\\PIPE\\samr", "dummy user");
1159                         init_srv_file_info3_str(&ctr->file.info3[i].info_3_str,  "\\PIPE\\samr", "dummy user");
1160                         
1161                 }
1162                 ctr->ptr_file_info = 1;
1163                 *resume_hnd = 0;
1164                 break;
1165         }
1166         default:
1167                 DEBUG(5,("init_srv_file_info_ctr: unsupported switch value %d\n", switch_value));
1168                 (*resume_hnd = 0);
1169                 (*total_entries) = 0;
1170                 ctr->ptr_entries = 0;
1171                 status = WERR_UNKNOWN_LEVEL;
1172                 break;
1173         }
1174
1175         return status;
1176 }
1177
1178 /*******************************************************************
1179  makes a SRV_R_NET_FILE_ENUM structure.
1180 ********************************************************************/
1181
1182 static void init_srv_r_net_file_enum(pipes_struct *p, SRV_R_NET_FILE_ENUM *r_n,
1183                                 uint32 resume_hnd, int file_level, int switch_value)  
1184 {
1185         DEBUG(5,("init_srv_r_net_file_enum: %d\n", __LINE__));
1186
1187         r_n->file_level  = file_level;
1188         if (file_level == 0)
1189                 r_n->status = WERR_UNKNOWN_LEVEL;
1190         else
1191                 r_n->status = init_srv_file_info_ctr(p, &r_n->ctr, switch_value, &resume_hnd, &(r_n->total_entries));
1192
1193         if (!W_ERROR_IS_OK(r_n->status))
1194                 resume_hnd = 0;
1195
1196         init_enum_hnd(&r_n->enum_hnd, resume_hnd);
1197 }
1198
1199 /*******************************************************************
1200 net server get info
1201 ********************************************************************/
1202
1203 WERROR _srv_net_srv_get_info(pipes_struct *p, SRV_Q_NET_SRV_GET_INFO *q_u, SRV_R_NET_SRV_GET_INFO *r_u)
1204 {
1205         WERROR status = WERR_OK;
1206         SRV_INFO_CTR *ctr = TALLOC_P(p->mem_ctx, SRV_INFO_CTR);
1207
1208         if (!ctr)
1209                 return WERR_NOMEM;
1210
1211         ZERO_STRUCTP(ctr);
1212
1213         DEBUG(5,("srv_net_srv_get_info: %d\n", __LINE__));
1214
1215         if (!pipe_access_check(p)) {
1216                 DEBUG(3, ("access denied to srv_net_srv_get_info\n"));
1217                 return WERR_ACCESS_DENIED;
1218         }
1219
1220         switch (q_u->switch_value) {
1221
1222                 /* Technically level 102 should only be available to
1223                    Administrators but there isn't anything super-secret
1224                    here, as most of it is made up. */
1225
1226         case 102:
1227                 init_srv_info_102(&ctr->srv.sv102,
1228                                   500, global_myname(), 
1229                                   string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH),
1230                                   lp_major_announce_version(), lp_minor_announce_version(),
1231                                   lp_default_server_announce(),
1232                                   0xffffffff, /* users */
1233                                   0xf, /* disc */
1234                                   0, /* hidden */
1235                                   240, /* announce */
1236                                   3000, /* announce delta */
1237                                   100000, /* licenses */
1238                                   "c:\\"); /* user path */
1239                 break;
1240         case 101:
1241                 init_srv_info_101(&ctr->srv.sv101,
1242                                   500, global_myname(),
1243                                   lp_major_announce_version(), lp_minor_announce_version(),
1244                                   lp_default_server_announce(),
1245                                   string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH));
1246                 break;
1247         case 100:
1248                 init_srv_info_100(&ctr->srv.sv100, 500, global_myname());
1249                 break;
1250         default:
1251                 status = WERR_UNKNOWN_LEVEL;
1252                 break;
1253         }
1254
1255         /* set up the net server get info structure */
1256         init_srv_r_net_srv_get_info(r_u, q_u->switch_value, ctr, status);
1257
1258         DEBUG(5,("srv_net_srv_get_info: %d\n", __LINE__));
1259
1260         return r_u->status;
1261 }
1262
1263 /*******************************************************************
1264 net server set info
1265 ********************************************************************/
1266
1267 WERROR _srv_net_srv_set_info(pipes_struct *p, SRV_Q_NET_SRV_SET_INFO *q_u, SRV_R_NET_SRV_SET_INFO *r_u)
1268 {
1269         WERROR status = WERR_OK;
1270
1271         DEBUG(5,("srv_net_srv_set_info: %d\n", __LINE__));
1272
1273         /* Set up the net server set info structure. */
1274
1275         init_srv_r_net_srv_set_info(r_u, 0x0, status);
1276
1277         DEBUG(5,("srv_net_srv_set_info: %d\n", __LINE__));
1278
1279         return r_u->status;
1280 }
1281
1282 /*******************************************************************
1283 net file enum
1284 ********************************************************************/
1285
1286 WERROR _srv_net_file_enum(pipes_struct *p, SRV_Q_NET_FILE_ENUM *q_u, SRV_R_NET_FILE_ENUM *r_u)
1287 {
1288         DEBUG(5,("srv_net_file_enum: %d\n", __LINE__));
1289
1290         /* set up the */
1291         init_srv_r_net_file_enum(p, r_u,
1292                                 get_enum_hnd(&q_u->enum_hnd),
1293                                 q_u->file_level,
1294                                 q_u->ctr.switch_value);
1295
1296         DEBUG(5,("srv_net_file_enum: %d\n", __LINE__));
1297
1298         return r_u->status;
1299 }
1300
1301 /*******************************************************************
1302 net conn enum
1303 ********************************************************************/
1304
1305 WERROR _srv_net_conn_enum(pipes_struct *p, SRV_Q_NET_CONN_ENUM *q_u, SRV_R_NET_CONN_ENUM *r_u)
1306 {
1307         DEBUG(5,("srv_net_conn_enum: %d\n", __LINE__));
1308
1309         r_u->ctr = TALLOC_P(p->mem_ctx, SRV_CONN_INFO_CTR);
1310         if (!r_u->ctr)
1311                 return WERR_NOMEM;
1312
1313         ZERO_STRUCTP(r_u->ctr);
1314
1315         /* set up the */
1316         init_srv_r_net_conn_enum(r_u,
1317                                 get_enum_hnd(&q_u->enum_hnd),
1318                                 q_u->conn_level,
1319                                 q_u->ctr->switch_value);
1320
1321         DEBUG(5,("srv_net_conn_enum: %d\n", __LINE__));
1322
1323         return r_u->status;
1324 }
1325
1326 /*******************************************************************
1327 net sess enum
1328 ********************************************************************/
1329
1330 WERROR _srv_net_sess_enum(pipes_struct *p, SRV_Q_NET_SESS_ENUM *q_u, SRV_R_NET_SESS_ENUM *r_u)
1331 {
1332         DEBUG(5,("_srv_net_sess_enum: %d\n", __LINE__));
1333
1334         r_u->ctr = TALLOC_P(p->mem_ctx, SRV_SESS_INFO_CTR);
1335         if (!r_u->ctr)
1336                 return WERR_NOMEM;
1337
1338         ZERO_STRUCTP(r_u->ctr);
1339
1340         /* set up the */
1341         init_srv_r_net_sess_enum(r_u,
1342                                 get_enum_hnd(&q_u->enum_hnd),
1343                                 q_u->sess_level,
1344                                 q_u->ctr->switch_value);
1345
1346         DEBUG(5,("_srv_net_sess_enum: %d\n", __LINE__));
1347
1348         return r_u->status;
1349 }
1350
1351 /*******************************************************************
1352 net sess del
1353 ********************************************************************/
1354
1355 WERROR _srv_net_sess_del(pipes_struct *p, SRV_Q_NET_SESS_DEL *q_u, SRV_R_NET_SESS_DEL *r_u)
1356 {
1357         struct sessionid *session_list;
1358         struct current_user user;
1359         int num_sessions, snum, ret;
1360         fstring username;
1361         fstring machine;
1362         BOOL not_root = False;
1363
1364         rpcstr_pull_unistr2_fstring(username, &q_u->uni_user_name);
1365         rpcstr_pull_unistr2_fstring(machine, &q_u->uni_cli_name);
1366
1367         /* strip leading backslashes if any */
1368         while (machine[0] == '\\') {
1369                 memmove(machine, &machine[1], strlen(machine));
1370         }
1371
1372         num_sessions = list_sessions(&session_list);
1373
1374         DEBUG(5,("_srv_net_sess_del: %d\n", __LINE__));
1375
1376         r_u->status = WERR_ACCESS_DENIED;
1377
1378         get_current_user(&user, p);
1379
1380         /* fail out now if you are not root or not a domain admin */
1381
1382         if ((user.uid != sec_initial_uid()) && 
1383                 ( ! nt_token_check_domain_rid(p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS))) {
1384
1385                 goto done;
1386         }
1387
1388         for (snum = 0; snum < num_sessions; snum++) {
1389
1390                 if ((strequal(session_list[snum].username, username) || username[0] == '\0' ) &&
1391                     strequal(session_list[snum].remote_machine, machine)) {
1392                 
1393                         if (user.uid != sec_initial_uid()) {
1394                                 not_root = True;
1395                                 become_root();
1396                         }
1397
1398                         if ((ret = message_send_pid(session_list[snum].pid, MSG_SHUTDOWN, NULL, 0, False))) 
1399                                 r_u->status = WERR_OK;
1400
1401                         if (not_root) 
1402                                 unbecome_root();
1403                 }
1404         }
1405
1406         DEBUG(5,("_srv_net_sess_del: %d\n", __LINE__));
1407
1408
1409 done:
1410         SAFE_FREE(session_list);
1411
1412         return r_u->status;
1413 }
1414
1415 /*******************************************************************
1416  Net share enum all.
1417 ********************************************************************/
1418
1419 WERROR _srv_net_share_enum_all(pipes_struct *p, SRV_Q_NET_SHARE_ENUM *q_u, SRV_R_NET_SHARE_ENUM *r_u)
1420 {
1421         DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
1422
1423         if (!pipe_access_check(p)) {
1424                 DEBUG(3, ("access denied to srv_net_share_enum_all\n"));
1425                 return WERR_ACCESS_DENIED;
1426         }
1427
1428         /* Create the list of shares for the response. */
1429         init_srv_r_net_share_enum(p, r_u,
1430                                 q_u->ctr.info_level,
1431                                 get_enum_hnd(&q_u->enum_hnd), True);
1432
1433         DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
1434
1435         return r_u->status;
1436 }
1437
1438 /*******************************************************************
1439  Net share enum.
1440 ********************************************************************/
1441
1442 WERROR _srv_net_share_enum(pipes_struct *p, SRV_Q_NET_SHARE_ENUM *q_u, SRV_R_NET_SHARE_ENUM *r_u)
1443 {
1444         DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
1445
1446         if (!pipe_access_check(p)) {
1447                 DEBUG(3, ("access denied to srv_net_share_enum\n"));
1448                 return WERR_ACCESS_DENIED;
1449         }
1450
1451         /* Create the list of shares for the response. */
1452         init_srv_r_net_share_enum(p, r_u,
1453                                   q_u->ctr.info_level,
1454                                   get_enum_hnd(&q_u->enum_hnd), False);
1455
1456         DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
1457
1458         return r_u->status;
1459 }
1460
1461 /*******************************************************************
1462  Net share get info.
1463 ********************************************************************/
1464
1465 WERROR _srv_net_share_get_info(pipes_struct *p, SRV_Q_NET_SHARE_GET_INFO *q_u, SRV_R_NET_SHARE_GET_INFO *r_u)
1466 {
1467         fstring share_name;
1468
1469         DEBUG(5,("_srv_net_share_get_info: %d\n", __LINE__));
1470
1471         /* Create the list of shares for the response. */
1472         unistr2_to_ascii(share_name, &q_u->uni_share_name, sizeof(share_name));
1473         init_srv_r_net_share_get_info(p, r_u, share_name, q_u->info_level);
1474
1475         DEBUG(5,("_srv_net_share_get_info: %d\n", __LINE__));
1476
1477         return r_u->status;
1478 }
1479
1480 /*******************************************************************
1481  Check a given DOS pathname is valid for a share.
1482 ********************************************************************/
1483
1484 static char *valid_share_pathname(char *dos_pathname)
1485 {
1486         char *ptr;
1487
1488         /* Convert any '\' paths to '/' */
1489         unix_format(dos_pathname);
1490         unix_clean_name(dos_pathname);
1491
1492         /* NT is braindead - it wants a C: prefix to a pathname ! So strip it. */
1493         ptr = dos_pathname;
1494         if (strlen(dos_pathname) > 2 && ptr[1] == ':' && ptr[0] != '/')
1495                 ptr += 2;
1496
1497         /* Only abolute paths allowed. */
1498         if (*ptr != '/')
1499                 return NULL;
1500
1501         return ptr;
1502 }
1503
1504 /*******************************************************************
1505  Net share set info. Modify share details.
1506 ********************************************************************/
1507
1508 WERROR _srv_net_share_set_info(pipes_struct *p, SRV_Q_NET_SHARE_SET_INFO *q_u, SRV_R_NET_SHARE_SET_INFO *r_u)
1509 {
1510         struct current_user user;
1511         pstring command;
1512         fstring share_name;
1513         fstring comment;
1514         pstring pathname;
1515         int type;
1516         int snum;
1517         int ret;
1518         char *path;
1519         SEC_DESC *psd = NULL;
1520         SE_PRIV se_diskop = SE_DISK_OPERATOR;
1521         BOOL is_disk_op = False;
1522
1523         DEBUG(5,("_srv_net_share_set_info: %d\n", __LINE__));
1524
1525         unistr2_to_ascii(share_name, &q_u->uni_share_name, sizeof(share_name));
1526
1527         r_u->parm_error = 0;
1528
1529         if (strequal(share_name,"IPC$") || strequal(share_name,"ADMIN$") || strequal(share_name,"global"))
1530                 return WERR_ACCESS_DENIED;
1531
1532         snum = find_service(share_name);
1533
1534         /* Does this share exist ? */
1535         if (snum < 0)
1536                 return WERR_INVALID_NAME;
1537
1538         /* No change to printer shares. */
1539         if (lp_print_ok(snum))
1540                 return WERR_ACCESS_DENIED;
1541
1542         get_current_user(&user,p);
1543
1544         is_disk_op = user_has_privileges( p->pipe_user.nt_user_token, &se_diskop );
1545         
1546         /* fail out now if you are not root and not a disk op */
1547         
1548         if ( user.uid != sec_initial_uid() && !is_disk_op )
1549                 return WERR_ACCESS_DENIED;
1550
1551         switch (q_u->info_level) {
1552         case 1:
1553                 pstrcpy(pathname, lp_pathname(snum));
1554                 unistr2_to_ascii(comment, &q_u->info.share.info2.info_2_str.uni_remark, sizeof(comment));
1555                 type = q_u->info.share.info2.info_2.type;
1556                 psd = NULL;
1557                 break;
1558         case 2:
1559                 unistr2_to_ascii(comment, &q_u->info.share.info2.info_2_str.uni_remark, sizeof(comment));
1560                 unistr2_to_ascii(pathname, &q_u->info.share.info2.info_2_str.uni_path, sizeof(pathname));
1561                 type = q_u->info.share.info2.info_2.type;
1562                 psd = NULL;
1563                 break;
1564 #if 0
1565                 /* not supported on set but here for completeness */
1566         case 501:
1567                 unistr2_to_ascii(comment, &q_u->info.share.info501.info_501_str.uni_remark, sizeof(comment));
1568                 type = q_u->info.share.info501.info_501.type;
1569                 psd = NULL;
1570                 break;
1571 #endif
1572         case 502:
1573                 unistr2_to_ascii(comment, &q_u->info.share.info502.info_502_str.uni_remark, sizeof(comment));
1574                 unistr2_to_ascii(pathname, &q_u->info.share.info502.info_502_str.uni_path, sizeof(pathname));
1575                 type = q_u->info.share.info502.info_502.type;
1576                 psd = q_u->info.share.info502.info_502_str.sd;
1577                 map_generic_share_sd_bits(psd);
1578                 break;
1579         case 1004:
1580                 pstrcpy(pathname, lp_pathname(snum));
1581                 unistr2_to_ascii(comment, &q_u->info.share.info1004.info_1004_str.uni_remark, sizeof(comment));
1582                 type = STYPE_DISKTREE;
1583                 break;
1584         case 1005:
1585                 /* XP re-sets the csc policy even if it wasn't changed by the
1586                    user, so we must compare it to see if it's what is set in
1587                    smb.conf, so that we can contine other ops like setting
1588                    ACLs on a share */
1589                 if (((q_u->info.share.info1005.share_info_flags &
1590                       SHARE_1005_CSC_POLICY_MASK) >>
1591                      SHARE_1005_CSC_POLICY_SHIFT) == lp_csc_policy(snum))
1592                         return WERR_OK;
1593                 else {
1594                         DEBUG(3, ("_srv_net_share_set_info: client is trying to change csc policy from the network; must be done with smb.conf\n"));
1595                         return WERR_ACCESS_DENIED;
1596                 }
1597                 break;
1598         case 1006:
1599         case 1007:
1600                 return WERR_ACCESS_DENIED;
1601                 break;
1602         case 1501:
1603                 pstrcpy(pathname, lp_pathname(snum));
1604                 fstrcpy(comment, lp_comment(snum));
1605                 psd = q_u->info.share.info1501.sdb->sec;
1606                 map_generic_share_sd_bits(psd);
1607                 type = STYPE_DISKTREE;
1608                 break;
1609         default:
1610                 DEBUG(5,("_srv_net_share_set_info: unsupported switch value %d\n", q_u->info_level));
1611                 return WERR_UNKNOWN_LEVEL;
1612         }
1613
1614         /* We can only modify disk shares. */
1615         if (type != STYPE_DISKTREE)
1616                 return WERR_ACCESS_DENIED;
1617                 
1618         /* Check if the pathname is valid. */
1619         if (!(path = valid_share_pathname( pathname )))
1620                 return WERR_OBJECT_PATH_INVALID;
1621
1622         /* Ensure share name, pathname and comment don't contain '"' characters. */
1623         string_replace(share_name, '"', ' ');
1624         string_replace(path, '"', ' ');
1625         string_replace(comment, '"', ' ');
1626
1627         DEBUG(10,("_srv_net_share_set_info: change share command = %s\n",
1628                 lp_change_share_cmd() ? lp_change_share_cmd() : "NULL" ));
1629
1630         /* Only call modify function if something changed. */
1631         
1632         if (strcmp(path, lp_pathname(snum)) || strcmp(comment, lp_comment(snum)) ) 
1633         {
1634                 if (!lp_change_share_cmd() || !*lp_change_share_cmd()) 
1635                         return WERR_ACCESS_DENIED;
1636
1637                 slprintf(command, sizeof(command)-1, "%s \"%s\" \"%s\" \"%s\" \"%s\"",
1638                                 lp_change_share_cmd(), dyn_CONFIGFILE, share_name, path, comment);
1639
1640                 DEBUG(10,("_srv_net_share_set_info: Running [%s]\n", command ));
1641                                 
1642                 /********* BEGIN SeDiskOperatorPrivilege BLOCK *********/
1643         
1644                 if ( is_disk_op )
1645                         become_root();
1646                         
1647                 if ( (ret = smbrun(command, NULL)) == 0 ) {
1648                         /* Tell everyone we updated smb.conf. */
1649                         message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED, NULL, 0, False, NULL);
1650                 }
1651                 
1652                 if ( is_disk_op )
1653                         unbecome_root();
1654                         
1655                 /********* END SeDiskOperatorPrivilege BLOCK *********/
1656
1657                 DEBUG(3,("_srv_net_share_set_info: Running [%s] returned (%d)\n", command, ret ));              
1658         
1659                 if ( ret != 0 )
1660                         return WERR_ACCESS_DENIED;
1661         } else {
1662                 DEBUG(10,("_srv_net_share_set_info: No change to share name (%s)\n", share_name ));
1663         }
1664
1665         /* Replace SD if changed. */
1666         if (psd) {
1667                 SEC_DESC *old_sd;
1668                 size_t sd_size;
1669
1670                 old_sd = get_share_security(p->mem_ctx, snum, &sd_size);
1671
1672                 if (old_sd && !sec_desc_equal(old_sd, psd)) {
1673                         if (!set_share_security(p->mem_ctx, share_name, psd))
1674                                 DEBUG(0,("_srv_net_share_set_info: Failed to change security info in share %s.\n",
1675                                         share_name ));
1676                 }
1677         }
1678                         
1679         DEBUG(5,("_srv_net_share_set_info: %d\n", __LINE__));
1680
1681         return WERR_OK;
1682 }
1683
1684 /*******************************************************************
1685  Net share add. Call 'add_share_command "sharename" "pathname" "comment" "read only = xxx"'
1686 ********************************************************************/
1687
1688 WERROR _srv_net_share_add(pipes_struct *p, SRV_Q_NET_SHARE_ADD *q_u, SRV_R_NET_SHARE_ADD *r_u)
1689 {
1690         struct current_user user;
1691         pstring command;
1692         fstring share_name;
1693         fstring comment;
1694         pstring pathname;
1695         int type;
1696         int snum;
1697         int ret;
1698         char *path;
1699         SEC_DESC *psd = NULL;
1700         SE_PRIV se_diskop = SE_DISK_OPERATOR;
1701         BOOL is_disk_op;
1702
1703         DEBUG(5,("_srv_net_share_add: %d\n", __LINE__));
1704
1705         r_u->parm_error = 0;
1706
1707         get_current_user(&user,p);
1708
1709         is_disk_op = user_has_privileges( p->pipe_user.nt_user_token, &se_diskop );
1710
1711         if (user.uid != sec_initial_uid()  && !is_disk_op ) 
1712                 return WERR_ACCESS_DENIED;
1713
1714         if (!lp_add_share_cmd() || !*lp_add_share_cmd()) {
1715                 DEBUG(10,("_srv_net_share_add: No add share command\n"));
1716                 return WERR_ACCESS_DENIED;
1717         }
1718         
1719         switch (q_u->info_level) {
1720         case 0:
1721                 /* No path. Not enough info in a level 0 to do anything. */
1722                 return WERR_ACCESS_DENIED;
1723         case 1:
1724                 /* Not enough info in a level 1 to do anything. */
1725                 return WERR_ACCESS_DENIED;
1726         case 2:
1727                 unistr2_to_ascii(share_name, &q_u->info.share.info2.info_2_str.uni_netname, sizeof(share_name));
1728                 unistr2_to_ascii(comment, &q_u->info.share.info2.info_2_str.uni_remark, sizeof(share_name));
1729                 unistr2_to_ascii(pathname, &q_u->info.share.info2.info_2_str.uni_path, sizeof(share_name));
1730                 type = q_u->info.share.info2.info_2.type;
1731                 break;
1732         case 501:
1733                 /* No path. Not enough info in a level 501 to do anything. */
1734                 return WERR_ACCESS_DENIED;
1735         case 502:
1736                 unistr2_to_ascii(share_name, &q_u->info.share.info502.info_502_str.uni_netname, sizeof(share_name));
1737                 unistr2_to_ascii(comment, &q_u->info.share.info502.info_502_str.uni_remark, sizeof(share_name));
1738                 unistr2_to_ascii(pathname, &q_u->info.share.info502.info_502_str.uni_path, sizeof(share_name));
1739                 type = q_u->info.share.info502.info_502.type;
1740                 psd = q_u->info.share.info502.info_502_str.sd;
1741                 map_generic_share_sd_bits(psd);
1742                 break;
1743
1744                 /* none of the following contain share names.  NetShareAdd does not have a separate parameter for the share name */ 
1745
1746         case 1004:
1747         case 1005:
1748         case 1006:
1749         case 1007:
1750                 return WERR_ACCESS_DENIED;
1751                 break;
1752         case 1501:
1753                 /* DFS only level. */
1754                 return WERR_ACCESS_DENIED;
1755         default:
1756                 DEBUG(5,("_srv_net_share_add: unsupported switch value %d\n", q_u->info_level));
1757                 return WERR_UNKNOWN_LEVEL;
1758         }
1759
1760         if (strequal(share_name,"IPC$") || strequal(share_name,"ADMIN$") || strequal(share_name,"global"))
1761                 return WERR_ACCESS_DENIED;
1762
1763         snum = find_service(share_name);
1764
1765         /* Share already exists. */
1766         if (snum >= 0)
1767                 return WERR_ALREADY_EXISTS;
1768
1769         /* We can only add disk shares. */
1770         if (type != STYPE_DISKTREE)
1771                 return WERR_ACCESS_DENIED;
1772                 
1773         /* Check if the pathname is valid. */
1774         if (!(path = valid_share_pathname( pathname )))
1775                 return WERR_OBJECT_PATH_INVALID;
1776
1777         /* Ensure share name, pathname and comment don't contain '"' characters. */
1778         string_replace(share_name, '"', ' ');
1779         string_replace(path, '"', ' ');
1780         string_replace(comment, '"', ' ');
1781
1782         slprintf(command, sizeof(command)-1, "%s \"%s\" \"%s\" \"%s\" \"%s\"",
1783                         lp_add_share_cmd(), dyn_CONFIGFILE, share_name, path, comment);
1784                         
1785         DEBUG(10,("_srv_net_share_add: Running [%s]\n", command ));
1786         
1787         /********* BEGIN SeDiskOperatorPrivilege BLOCK *********/
1788         
1789         if ( is_disk_op )
1790                 become_root();
1791
1792         if ( (ret = smbrun(command, NULL)) == 0 ) {
1793                 /* Tell everyone we updated smb.conf. */
1794                 message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED, NULL, 0, False, NULL);
1795         }
1796
1797         if ( is_disk_op )
1798                 unbecome_root();
1799                 
1800         /********* END SeDiskOperatorPrivilege BLOCK *********/
1801
1802         DEBUG(3,("_srv_net_share_add: Running [%s] returned (%d)\n", command, ret ));
1803
1804         if ( ret != 0 )
1805                 return WERR_ACCESS_DENIED;
1806
1807         if (psd) {
1808                 if (!set_share_security(p->mem_ctx, share_name, psd)) {
1809                         DEBUG(0,("_srv_net_share_add: Failed to add security info to share %s.\n", share_name ));
1810                 }
1811         }
1812
1813         /*
1814          * We don't call reload_services() here, the message will
1815          * cause this to be done before the next packet is read
1816          * from the client. JRA.
1817          */
1818
1819         DEBUG(5,("_srv_net_share_add: %d\n", __LINE__));
1820
1821         return WERR_OK;
1822 }
1823
1824 /*******************************************************************
1825  Net share delete. Call "delete share command" with the share name as
1826  a parameter.
1827 ********************************************************************/
1828
1829 WERROR _srv_net_share_del(pipes_struct *p, SRV_Q_NET_SHARE_DEL *q_u, SRV_R_NET_SHARE_DEL *r_u)
1830 {
1831         struct current_user user;
1832         pstring command;
1833         fstring share_name;
1834         int ret;
1835         int snum;
1836         SE_PRIV se_diskop = SE_DISK_OPERATOR;
1837         BOOL is_disk_op;
1838
1839         DEBUG(5,("_srv_net_share_del: %d\n", __LINE__));
1840
1841         unistr2_to_ascii(share_name, &q_u->uni_share_name, sizeof(share_name));
1842
1843         if (strequal(share_name,"IPC$") || strequal(share_name,"ADMIN$") || strequal(share_name,"global"))
1844                 return WERR_ACCESS_DENIED;
1845
1846         snum = find_service(share_name);
1847
1848         if (snum < 0)
1849                 return WERR_NO_SUCH_SHARE;
1850
1851         /* No change to printer shares. */
1852         if (lp_print_ok(snum))
1853                 return WERR_ACCESS_DENIED;
1854
1855         get_current_user(&user,p);
1856
1857         is_disk_op = user_has_privileges( p->pipe_user.nt_user_token, &se_diskop );
1858
1859         if (user.uid != sec_initial_uid()  && !is_disk_op ) 
1860                 return WERR_ACCESS_DENIED;
1861
1862         if (!lp_delete_share_cmd() || !*lp_delete_share_cmd())
1863                 return WERR_ACCESS_DENIED;
1864                 
1865         slprintf(command, sizeof(command)-1, "%s \"%s\" \"%s\"",
1866                         lp_delete_share_cmd(), dyn_CONFIGFILE, lp_servicename(snum));
1867
1868         DEBUG(10,("_srv_net_share_del: Running [%s]\n", command ));
1869
1870         /********* BEGIN SeDiskOperatorPrivilege BLOCK *********/
1871         
1872         if ( is_disk_op )
1873                 become_root();
1874
1875         if ( (ret = smbrun(command, NULL)) == 0 ) {
1876                 /* Tell everyone we updated smb.conf. */
1877                 message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED, NULL, 0, False, NULL);
1878         }
1879
1880         if ( is_disk_op )
1881                 unbecome_root();
1882                 
1883         /********* END SeDiskOperatorPrivilege BLOCK *********/
1884
1885         DEBUG(3,("_srv_net_share_del: Running [%s] returned (%d)\n", command, ret ));
1886
1887         if ( ret != 0 )
1888                 return WERR_ACCESS_DENIED;
1889
1890         /* Delete the SD in the database. */
1891         delete_share_security(snum);
1892
1893         lp_killservice(snum);
1894
1895         return WERR_OK;
1896 }
1897
1898 WERROR _srv_net_share_del_sticky(pipes_struct *p, SRV_Q_NET_SHARE_DEL *q_u, SRV_R_NET_SHARE_DEL *r_u)
1899 {
1900         DEBUG(5,("_srv_net_share_del_stick: %d\n", __LINE__));
1901
1902         return _srv_net_share_del(p, q_u, r_u);
1903 }
1904
1905 /*******************************************************************
1906 time of day
1907 ********************************************************************/
1908
1909 WERROR _srv_net_remote_tod(pipes_struct *p, SRV_Q_NET_REMOTE_TOD *q_u, SRV_R_NET_REMOTE_TOD *r_u)
1910 {
1911         TIME_OF_DAY_INFO *tod;
1912         struct tm *t;
1913         time_t unixdate = time(NULL);
1914
1915         tod = TALLOC_P(p->mem_ctx, TIME_OF_DAY_INFO);
1916         if (!tod)
1917                 return WERR_NOMEM;
1918
1919         ZERO_STRUCTP(tod);
1920  
1921         r_u->tod = tod;
1922         r_u->ptr_srv_tod = 0x1;
1923         r_u->status = WERR_OK;
1924
1925         DEBUG(5,("_srv_net_remote_tod: %d\n", __LINE__));
1926
1927         t = gmtime(&unixdate);
1928
1929         /* set up the */
1930         init_time_of_day_info(tod,
1931                               unixdate,
1932                               0,
1933                               t->tm_hour,
1934                               t->tm_min,
1935                               t->tm_sec,
1936                               0,
1937                               TimeDiff(unixdate)/60,
1938                               10000,
1939                               t->tm_mday,
1940                               t->tm_mon + 1,
1941                               1900+t->tm_year,
1942                               t->tm_wday);
1943         
1944         DEBUG(5,("_srv_net_remote_tod: %d\n", __LINE__));
1945
1946         return r_u->status;
1947 }
1948
1949 /***********************************************************************************
1950  Win9x NT tools get security descriptor.
1951 ***********************************************************************************/
1952
1953 WERROR _srv_net_file_query_secdesc(pipes_struct *p, SRV_Q_NET_FILE_QUERY_SECDESC *q_u,
1954                         SRV_R_NET_FILE_QUERY_SECDESC *r_u)
1955 {
1956         SEC_DESC *psd = NULL;
1957         size_t sd_size;
1958         DATA_BLOB null_pw;
1959         pstring filename;
1960         pstring qualname;
1961         files_struct *fsp = NULL;
1962         SMB_STRUCT_STAT st;
1963         BOOL bad_path;
1964         int access_mode;
1965         int action;
1966         NTSTATUS nt_status;
1967         struct current_user user;
1968         connection_struct *conn = NULL;
1969         BOOL became_user = False; 
1970
1971         ZERO_STRUCT(st);
1972
1973         r_u->status = WERR_OK;
1974
1975         unistr2_to_ascii(qualname, &q_u->uni_qual_name, sizeof(qualname));
1976
1977         /* Null password is ok - we are already an authenticated user... */
1978         null_pw = data_blob(NULL, 0);
1979
1980         get_current_user(&user, p);
1981
1982         become_root();
1983         conn = make_connection(qualname, null_pw, "A:", user.vuid, &nt_status);
1984         unbecome_root();
1985
1986         if (conn == NULL) {
1987                 DEBUG(3,("_srv_net_file_query_secdesc: Unable to connect to %s\n", qualname));
1988                 r_u->status = ntstatus_to_werror(nt_status);
1989                 goto error_exit;
1990         }
1991
1992         if (!become_user(conn, conn->vuid)) {
1993                 DEBUG(0,("_srv_net_file_query_secdesc: Can't become connected user!\n"));
1994                 r_u->status = WERR_ACCESS_DENIED;
1995                 goto error_exit;
1996         }
1997         became_user = True;
1998
1999         unistr2_to_ascii(filename, &q_u->uni_file_name, sizeof(filename));
2000         unix_convert(filename, conn, NULL, &bad_path, &st);
2001         if (bad_path) {
2002                 DEBUG(3,("_srv_net_file_query_secdesc: bad pathname %s\n", filename));
2003                 r_u->status = WERR_ACCESS_DENIED;
2004                 goto error_exit;
2005         }
2006
2007         if (!check_name(filename,conn)) {
2008                 DEBUG(3,("_srv_net_file_query_secdesc: can't access %s\n", filename));
2009                 r_u->status = WERR_ACCESS_DENIED;
2010                 goto error_exit;
2011         }
2012
2013         fsp = open_file_shared(conn, filename, &st, SET_DENY_MODE(DENY_NONE)|SET_OPEN_MODE(DOS_OPEN_RDONLY),
2014                                 (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN), FILE_ATTRIBUTE_NORMAL, INTERNAL_OPEN_ONLY,
2015                                 &access_mode, &action);
2016
2017         if (!fsp) {
2018                 /* Perhaps it is a directory */
2019                 if (errno == EISDIR)
2020                         fsp = open_directory(conn, filename, &st,FILE_READ_ATTRIBUTES,0,
2021                                         (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN), &action);
2022
2023                 if (!fsp) {
2024                         DEBUG(3,("_srv_net_file_query_secdesc: Unable to open file %s\n", filename));
2025                         r_u->status = WERR_ACCESS_DENIED;
2026                         goto error_exit;
2027                 }
2028         }
2029
2030         sd_size = SMB_VFS_GET_NT_ACL(fsp, fsp->fsp_name, (OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION), &psd);
2031
2032         if (sd_size == 0) {
2033                 DEBUG(3,("_srv_net_file_query_secdesc: Unable to get NT ACL for file %s\n", filename));
2034                 r_u->status = WERR_ACCESS_DENIED;
2035                 goto error_exit;
2036         }
2037
2038         r_u->ptr_response = 1;
2039         r_u->size_response = sd_size;
2040         r_u->ptr_secdesc = 1;
2041         r_u->size_secdesc = sd_size;
2042         r_u->sec_desc = psd;
2043
2044         psd->dacl->revision = (uint16) NT4_ACL_REVISION;
2045
2046         close_file(fsp, True);
2047         unbecome_user();
2048         close_cnum(conn, user.vuid);
2049         return r_u->status;
2050
2051 error_exit:
2052
2053         if(fsp) {
2054                 close_file(fsp, True);
2055         }
2056
2057         if (became_user)
2058                 unbecome_user();
2059
2060         if (conn) 
2061                 close_cnum(conn, user.vuid);
2062
2063         return r_u->status;
2064 }
2065
2066 /***********************************************************************************
2067  Win9x NT tools set security descriptor.
2068 ***********************************************************************************/
2069
2070 WERROR _srv_net_file_set_secdesc(pipes_struct *p, SRV_Q_NET_FILE_SET_SECDESC *q_u,
2071                                                                         SRV_R_NET_FILE_SET_SECDESC *r_u)
2072 {
2073         BOOL ret;
2074         pstring filename;
2075         pstring qualname;
2076         DATA_BLOB null_pw;
2077         files_struct *fsp = NULL;
2078         SMB_STRUCT_STAT st;
2079         BOOL bad_path;
2080         int access_mode;
2081         int action;
2082         NTSTATUS nt_status;
2083         struct current_user user;
2084         connection_struct *conn = NULL;
2085         BOOL became_user = False;
2086
2087         ZERO_STRUCT(st);
2088
2089         r_u->status = WERR_OK;
2090
2091         unistr2_to_ascii(qualname, &q_u->uni_qual_name, sizeof(qualname));
2092
2093         /* Null password is ok - we are already an authenticated user... */
2094         null_pw = data_blob(NULL, 0);
2095
2096         get_current_user(&user, p);
2097
2098         become_root();
2099         conn = make_connection(qualname, null_pw, "A:", user.vuid, &nt_status);
2100         unbecome_root();
2101
2102         if (conn == NULL) {
2103                 DEBUG(3,("_srv_net_file_set_secdesc: Unable to connect to %s\n", qualname));
2104                 r_u->status = ntstatus_to_werror(nt_status);
2105                 goto error_exit;
2106         }
2107
2108         if (!become_user(conn, conn->vuid)) {
2109                 DEBUG(0,("_srv_net_file_set_secdesc: Can't become connected user!\n"));
2110                 r_u->status = WERR_ACCESS_DENIED;
2111                 goto error_exit;
2112         }
2113         became_user = True;
2114
2115         unistr2_to_ascii(filename, &q_u->uni_file_name, sizeof(filename));
2116         unix_convert(filename, conn, NULL, &bad_path, &st);
2117         if (bad_path) {
2118                 DEBUG(3,("_srv_net_file_set_secdesc: bad pathname %s\n", filename));
2119                 r_u->status = WERR_ACCESS_DENIED;
2120                 goto error_exit;
2121         }
2122
2123         if (!check_name(filename,conn)) {
2124                 DEBUG(3,("_srv_net_file_set_secdesc: can't access %s\n", filename));
2125                 r_u->status = WERR_ACCESS_DENIED;
2126                 goto error_exit;
2127         }
2128
2129
2130         fsp = open_file_shared(conn, filename, &st, SET_DENY_MODE(DENY_NONE)|SET_OPEN_MODE(DOS_OPEN_RDWR),
2131                         (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN), FILE_ATTRIBUTE_NORMAL, INTERNAL_OPEN_ONLY,
2132                         &access_mode, &action);
2133
2134         if (!fsp) {
2135                 /* Perhaps it is a directory */
2136                 if (errno == EISDIR)
2137                         fsp = open_directory(conn, filename, &st,FILE_READ_ATTRIBUTES,0,
2138                                                 (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN), &action);
2139
2140                 if (!fsp) {
2141                         DEBUG(3,("_srv_net_file_set_secdesc: Unable to open file %s\n", filename));
2142                         r_u->status = WERR_ACCESS_DENIED;
2143                         goto error_exit;
2144                 }
2145         }
2146
2147         ret = SMB_VFS_SET_NT_ACL(fsp, fsp->fsp_name, q_u->sec_info, q_u->sec_desc);
2148
2149         if (ret == False) {
2150                 DEBUG(3,("_srv_net_file_set_secdesc: Unable to set NT ACL on file %s\n", filename));
2151                 r_u->status = WERR_ACCESS_DENIED;
2152                 goto error_exit;
2153         }
2154
2155         close_file(fsp, True);
2156         unbecome_user();
2157         close_cnum(conn, user.vuid);
2158         return r_u->status;
2159
2160 error_exit:
2161
2162         if(fsp) {
2163                 close_file(fsp, True);
2164         }
2165
2166         if (became_user)
2167                 unbecome_user();
2168
2169         if (conn) 
2170                 close_cnum(conn, user.vuid);
2171
2172         return r_u->status;
2173 }
2174
2175 /***********************************************************************************
2176  It may be that we want to limit users to creating shares on certain areas of the UNIX file area.
2177  We could define areas by mapping Windows style disks to points on the UNIX directory hierarchy.
2178  These disks would the disks listed by this function.
2179  Users could then create shares relative to these disks.  Watch out for moving these disks around.
2180  "Nigel Williams" <nigel@veritas.com>.
2181 ***********************************************************************************/
2182
2183 static const char *server_disks[] = {"C:"};
2184
2185 static uint32 get_server_disk_count(void)
2186 {
2187         return sizeof(server_disks)/sizeof(server_disks[0]);
2188 }
2189
2190 static uint32 init_server_disk_enum(uint32 *resume)
2191 {
2192         uint32 server_disk_count = get_server_disk_count();
2193
2194         /*resume can be an offset into the list for now*/
2195
2196         if(*resume & 0x80000000)
2197                 *resume = 0;
2198
2199         if(*resume > server_disk_count)
2200                 *resume = server_disk_count;
2201
2202         return server_disk_count - *resume;
2203 }
2204
2205 static const char *next_server_disk_enum(uint32 *resume)
2206 {
2207         const char *disk;
2208
2209         if(init_server_disk_enum(resume) == 0)
2210                 return NULL;
2211
2212         disk = server_disks[*resume];
2213
2214         (*resume)++;
2215
2216         DEBUG(10, ("next_server_disk_enum: reporting disk %s. resume handle %d.\n", disk, *resume));
2217
2218         return disk;
2219 }
2220
2221 WERROR _srv_net_disk_enum(pipes_struct *p, SRV_Q_NET_DISK_ENUM *q_u, SRV_R_NET_DISK_ENUM *r_u)
2222 {
2223         uint32 i;
2224         const char *disk_name;
2225         TALLOC_CTX *ctx = p->mem_ctx;
2226         uint32 resume=get_enum_hnd(&q_u->enum_hnd);
2227
2228         r_u->status=WERR_OK;
2229
2230         r_u->total_entries = init_server_disk_enum(&resume);
2231
2232         r_u->disk_enum_ctr.unknown = 0; 
2233
2234         if(!(r_u->disk_enum_ctr.disk_info =  TALLOC_ARRAY(ctx, DISK_INFO, MAX_SERVER_DISK_ENTRIES))) {
2235                 return WERR_NOMEM;
2236         }
2237
2238         r_u->disk_enum_ctr.disk_info_ptr = r_u->disk_enum_ctr.disk_info ? 1 : 0;
2239
2240         /*allow one DISK_INFO for null terminator*/
2241
2242         for(i = 0; i < MAX_SERVER_DISK_ENTRIES -1 && (disk_name = next_server_disk_enum(&resume)); i++) {
2243
2244                 r_u->disk_enum_ctr.entries_read++;
2245
2246                 /*copy disk name into a unicode string*/
2247
2248                 init_unistr3(&r_u->disk_enum_ctr.disk_info[i].disk_name, disk_name);    
2249         }
2250
2251         /* add a terminating null string.  Is this there if there is more data to come? */
2252
2253         r_u->disk_enum_ctr.entries_read++;
2254
2255         init_unistr3(&r_u->disk_enum_ctr.disk_info[i].disk_name, "");
2256
2257         init_enum_hnd(&r_u->enum_hnd, resume);
2258
2259         return r_u->status;
2260 }
2261
2262 WERROR _srv_net_name_validate(pipes_struct *p, SRV_Q_NET_NAME_VALIDATE *q_u, SRV_R_NET_NAME_VALIDATE *r_u)
2263 {
2264         int snum;
2265         fstring share_name;
2266
2267         r_u->status=WERR_OK;
2268
2269         switch(q_u->type) {
2270
2271         case 0x9:
2272
2273                 /*check if share name is ok*/
2274                 /*also check if we already have a share with this name*/
2275
2276                 unistr2_to_ascii(share_name, &q_u->uni_name, sizeof(share_name));
2277                 snum = find_service(share_name);
2278
2279                 /* Share already exists. */
2280                 if (snum >= 0)
2281                         r_u->status = WERR_ALREADY_EXISTS;
2282                 break;
2283
2284         default:
2285                 /*unsupported type*/
2286                 r_u->status = WERR_UNKNOWN_LEVEL;
2287                 break;
2288         }
2289
2290         return r_u->status;
2291 }