Merge of Andrew's changes in 2.2.
[jra/samba/.git] / source / rpc_server / srv_srvsvc_nt.c
1 /* 
2  *  Unix SMB/Netbios implementation.
3  *  Version 1.9.
4  *  RPC Pipe client / server routines
5  *  Copyright (C) Andrew Tridgell              1992-1997,
6  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
7  *  Copyright (C) Paul Ashton                       1997.
8  *  Copyright (C) Jeremy Allison                                        2001.
9  *  
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 /* This is the implementation of the srvsvc pipe. */
26
27 #include "includes.h"
28
29 extern int DEBUGLEVEL;
30 extern pstring global_myname;
31
32 /*******************************************************************
33  Fill in a share info level 1 structure.
34  ********************************************************************/
35
36 static void init_srv_share_info_1(SRV_SHARE_INFO_1 *sh1, int snum)
37 {
38         int len_net_name;
39         pstring net_name;
40         pstring remark;
41         uint32 type;
42
43         pstrcpy(net_name, lp_servicename(snum));
44         pstrcpy(remark, lp_comment(snum));
45         pstring_sub(remark,"%S",lp_servicename(snum));
46         len_net_name = strlen(net_name);
47
48         /* work out the share type */
49         type = STYPE_DISKTREE;
50                 
51         if (lp_print_ok(snum))
52                 type = STYPE_PRINTQ;
53         if (strequal("IPC$", net_name) || strequal("ADMIN$", net_name))
54                 type = STYPE_IPC;
55         if (net_name[len_net_name] == '$')
56                 type |= STYPE_HIDDEN;
57
58         init_srv_share_info1(&sh1->info_1, net_name, type, remark);
59         init_srv_share_info1_str(&sh1->info_1_str, net_name, remark);
60 }
61
62 /*******************************************************************
63  Fill in a share info level 2 structure.
64  ********************************************************************/
65
66 static void init_srv_share_info_2(SRV_SHARE_INFO_2 *sh2, int snum)
67 {
68         int len_net_name;
69         pstring net_name;
70         pstring remark;
71         pstring path;
72         pstring passwd;
73         uint32 type;
74
75         pstrcpy(net_name, lp_servicename(snum));
76         pstrcpy(remark, lp_comment(snum));
77         pstring_sub(remark,"%S",lp_servicename(snum));
78         pstrcpy(path, "C:");
79         pstrcat(path, lp_pathname(snum));
80         pstrcpy(passwd, "");
81         len_net_name = strlen(net_name);
82
83         /* work out the share type */
84         type = STYPE_DISKTREE;
85                 
86         if (lp_print_ok(snum))
87                 type = STYPE_PRINTQ;
88         if (strequal("IPC$", net_name) || strequal("ADMIN$", net_name))
89                 type = STYPE_IPC;
90         if (net_name[len_net_name] == '$')
91                 type |= STYPE_HIDDEN;
92
93         init_srv_share_info2(&sh2->info_2, net_name, type, remark, 0, 0xffffffff, 1, path, passwd);
94         init_srv_share_info2_str(&sh2->info_2_str, net_name, remark, path, passwd);
95 }
96
97 /*******************************************************************
98  What to do when smb.conf is updated.
99  ********************************************************************/
100
101 static void smb_conf_updated(int msg_type, pid_t src, void *buf, size_t len)
102 {
103         DEBUG(10,("smb_conf_updated: Got message saying smb.conf was updated. Reloading.\n"));
104         reload_services(False);
105 }
106
107 /*******************************************************************
108  Create the share security tdb.
109  ********************************************************************/
110
111 static TDB_CONTEXT *share_tdb; /* used for share security descriptors */
112 #define SHARE_DATABASE_VERSION 1
113
114 BOOL share_info_db_init(void)
115 {
116     static pid_t local_pid;
117     char *vstring = "INFO/version";
118  
119     if (share_tdb && local_pid == sys_getpid()) return True;
120     share_tdb = tdb_open(lock_path("share_info.tdb"), 0, 0, O_RDWR|O_CREAT, 0600);
121     if (!share_tdb) {
122         DEBUG(0,("Failed to open share info database %s (%s)\n",
123                                 lock_path("share_info.tdb"), strerror(errno) ));
124         return False;
125     }
126  
127     local_pid = sys_getpid();
128  
129     /* handle a Samba upgrade */
130     tdb_lock_bystring(share_tdb, vstring);
131     if (tdb_fetch_int(share_tdb, vstring) != SHARE_DATABASE_VERSION) {
132         tdb_traverse(share_tdb, (tdb_traverse_func)tdb_delete, NULL);
133         tdb_store_int(share_tdb, vstring, SHARE_DATABASE_VERSION);
134     }
135     tdb_unlock_bystring(share_tdb, vstring);
136
137         message_register(MSG_SMB_CONF_UPDATED, smb_conf_updated);
138  
139     return True;
140 }
141
142 /*******************************************************************
143  Fake up a Everyone, full access as a default.
144  ********************************************************************/
145
146 static SEC_DESC *get_share_security_default( TALLOC_CTX *ctx, int snum, size_t *psize)
147 {
148         extern DOM_SID global_sid_World;
149         extern struct generic_mapping file_generic_mapping;
150         SEC_ACCESS sa;
151         SEC_ACE ace;
152         SEC_ACL *psa = NULL;
153         SEC_DESC *psd = NULL;
154         uint32 def_access = GENERIC_ALL_ACCESS;
155
156         se_map_generic(&def_access, &file_generic_mapping);
157
158     init_sec_access(&sa, GENERIC_ALL_ACCESS | def_access );
159     init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0);
160
161         if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 1, &ace)) != NULL) {
162                 psd = make_sec_desc(ctx, SEC_DESC_REVISION, NULL, NULL, NULL, psa, psize);
163         }
164
165         if (!psd) {
166                 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
167                 return NULL;
168         }
169
170         return psd;
171 }
172
173 /*******************************************************************
174  Pull a security descriptor from the share tdb.
175  ********************************************************************/
176
177 static SEC_DESC *get_share_security( TALLOC_CTX *ctx, int snum, size_t *psize)
178 {
179         prs_struct ps;
180         fstring key;
181         SEC_DESC *psd = NULL;
182
183         *psize = 0;
184
185         /* Fetch security descriptor from tdb */
186  
187         slprintf(key, sizeof(key)-1, "SECDESC/%s", lp_servicename(snum));
188  
189     if (tdb_prs_fetch(share_tdb, key, &ps, ctx)!=0 ||
190         !sec_io_desc("get_share_security", &psd, &ps, 1)) {
191  
192         DEBUG(4,("get_share_security: using default secdesc for %s\n", lp_servicename(snum) ));
193  
194         return get_share_security_default(ctx, snum, psize);
195     }
196
197         if (psd)
198                 *psize = sec_desc_size(psd);
199
200         prs_mem_free(&ps);
201         return psd;
202 }
203
204 /*******************************************************************
205  Store a security descriptor in the share db.
206  ********************************************************************/
207
208 static BOOL set_share_security(TALLOC_CTX *ctx, const char *share_name, SEC_DESC *psd)
209 {
210         prs_struct ps;
211         TALLOC_CTX *mem_ctx = NULL;
212         fstring key;
213         BOOL ret = False;
214
215         mem_ctx = talloc_init();
216         if (mem_ctx == NULL)
217                 return False;
218
219         prs_init(&ps, (uint32)sec_desc_size(psd), mem_ctx, MARSHALL);
220  
221     if (!sec_io_desc("nt_printing_setsec", &psd, &ps, 1)) {
222         goto out;
223     }
224  
225         slprintf(key, sizeof(key)-1, "SECDESC/%s", share_name);
226  
227     if (tdb_prs_store(share_tdb, key, &ps)==0) {
228         ret = True;
229         DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
230     } else {
231         DEBUG(1,("set_share_security: Failed to store secdesc for %s\n", share_name ));
232     }
233
234     /* Free malloc'ed memory */
235  
236  out:
237  
238     prs_mem_free(&ps);
239     if (mem_ctx)
240         talloc_destroy(mem_ctx);
241     return ret;
242 }
243
244 /*******************************************************************
245  Delete a security descriptor.
246 ********************************************************************/
247
248 static BOOL delete_share_security(int snum)
249 {
250         TDB_DATA kbuf;
251         fstring key;
252
253         slprintf(key, sizeof(key)-1, "SECDESC/%s", lp_servicename(snum));
254         kbuf.dptr = key;
255         kbuf.dsize = strlen(key)+1;
256
257         if (tdb_delete(share_tdb, kbuf) != 0) {
258                 DEBUG(0,("delete_share_security: Failed to delete entry for share %s\n",
259                                 lp_servicename(snum) ));
260                 return False;
261         }
262
263         return True;
264 }
265
266 /*******************************************************************
267  Map any generic bits to file specific bits.
268 ********************************************************************/
269
270 void map_generic_share_sd_bits(SEC_DESC *psd)
271 {
272         extern struct generic_mapping file_generic_mapping;
273         int i;
274         SEC_ACL *ps_dacl = NULL;
275
276         if (!psd)
277                 return;
278
279         ps_dacl = psd->dacl;
280         if (!ps_dacl)
281                 return;
282
283         for (i = 0; i < ps_dacl->num_aces; i++) {
284                 SEC_ACE *psa = &ps_dacl->ace[i];
285                 uint32 orig_mask = psa->info.mask;
286
287                 se_map_generic(&psa->info.mask, &file_generic_mapping);
288                 psa->info.mask |= orig_mask;
289         }       
290 }
291
292 /*******************************************************************
293  Can this user access with share with the required permissions ?
294 ********************************************************************/
295
296 BOOL share_access_check(connection_struct *conn, int snum, uint16 vuid, uint32 desired_access)
297 {
298         uint32 granted, status;
299         TALLOC_CTX *mem_ctx = NULL;
300         SEC_DESC *psd = NULL;
301         size_t sd_size;
302         struct current_user tmp_user;
303         struct current_user *puser = NULL;
304         user_struct *vuser = get_valid_user_struct(vuid);
305         BOOL ret = True;
306
307         mem_ctx = talloc_init();
308         if (mem_ctx == NULL)
309                 return False;
310
311         psd = get_share_security(mem_ctx, snum, &sd_size);
312
313         if (!psd)
314                 goto out;
315
316         ZERO_STRUCT(tmp_user);
317         if (vuser) {
318                 tmp_user.vuid = vuid;
319                 tmp_user.uid = vuser->uid;
320                 tmp_user.gid = vuser->gid;
321                 tmp_user.ngroups = vuser->n_groups;
322                 tmp_user.groups = vuser->groups;
323                 tmp_user.nt_user_token = vuser->nt_user_token;
324         } else {
325                 tmp_user.vuid = vuid;
326                 tmp_user.uid = conn->uid;
327                 tmp_user.gid = conn->gid;
328                 tmp_user.ngroups = conn->ngroups;
329                 tmp_user.groups = conn->groups;
330                 tmp_user.nt_user_token = conn->nt_user_token;
331         }
332
333         puser = &tmp_user;
334
335         ret = se_access_check(psd, puser, desired_access, &granted, &status);
336
337   out:
338
339         talloc_destroy(mem_ctx);
340
341         return ret;
342 }
343
344 /*******************************************************************
345  Fill in a share info level 502 structure.
346  ********************************************************************/
347
348 static void init_srv_share_info_502(TALLOC_CTX *ctx, SRV_SHARE_INFO_502 *sh502, int snum)
349 {
350         int len_net_name;
351         pstring net_name;
352         pstring remark;
353         pstring path;
354         pstring passwd;
355         uint32 type;
356         SEC_DESC *sd;
357         size_t sd_size;
358
359         ZERO_STRUCTP(sh502);
360
361         pstrcpy(net_name, lp_servicename(snum));
362         pstrcpy(remark, lp_comment(snum));
363         pstring_sub(remark,"%S",lp_servicename(snum));
364         pstrcpy(path, "C:");
365         pstrcat(path, lp_pathname(snum));
366         pstrcpy(passwd, "");
367         len_net_name = strlen(net_name);
368
369         /* work out the share type */
370         type = STYPE_DISKTREE;
371                 
372         if (lp_print_ok(snum))
373                 type = STYPE_PRINTQ;
374         if (strequal("IPC$", net_name))
375                 type = STYPE_IPC;
376         if (net_name[len_net_name] == '$')
377                 type |= STYPE_HIDDEN;
378
379         sd = get_share_security(ctx, snum, &sd_size);
380
381         init_srv_share_info502(&sh502->info_502, net_name, type, remark, 0, 0xffffffff, 1, path, passwd, sd, sd_size);
382         init_srv_share_info502_str(&sh502->info_502_str, net_name, remark, path, passwd, sd, sd_size);
383 }
384
385 /***************************************************************************
386  Fill in a share info level 1005 structure.
387  ***************************************************************************/
388
389 static void init_srv_share_info_1005(SRV_SHARE_INFO_1005* sh1005, int snum)
390 {
391         sh1005->dfs_root_flag = 0;
392
393 #ifdef WITH_MSDFS
394         if(lp_host_msdfs() && lp_msdfs_root(snum))
395                 sh1005->dfs_root_flag = 3;
396 #endif
397
398 }
399
400 /*******************************************************************
401  True if it ends in '$'.
402  ********************************************************************/
403
404 static BOOL is_admin_share(int snum)
405 {
406         pstring net_name;
407
408         pstrcpy(net_name, lp_servicename(snum));
409         return (net_name[strlen(net_name)] == '$') ? True : False;
410 }
411
412 /*******************************************************************
413  Fill in a share info structure.
414  ********************************************************************/
415
416 static BOOL init_srv_share_info_ctr(TALLOC_CTX *ctx, SRV_SHARE_INFO_CTR *ctr,
417                uint32 info_level, uint32 *resume_hnd, uint32 *total_entries, BOOL all_shares)
418 {
419         int num_entries = 0;
420         int num_services = lp_numservices();
421         int snum;
422
423         DEBUG(5,("init_srv_share_info_ctr\n"));
424
425         ZERO_STRUCTPN(ctr);
426
427         ctr->info_level = ctr->switch_value = info_level;
428         *resume_hnd = 0;
429
430         /* Count the number of entries. */
431         for (snum = 0; snum < num_services; snum++) {
432                 if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_admin_share(snum)) )
433                         num_entries++;
434         }
435
436         *total_entries = num_entries;
437         ctr->num_entries2 = ctr->num_entries = num_entries;
438         ctr->ptr_share_info = ctr->ptr_entries = 1;
439
440         if (!num_entries)
441                 return True;
442
443         switch (info_level) {
444         case 1:
445         {
446                 SRV_SHARE_INFO_1 *info1;
447                 int i = 0;
448
449                 info1 = talloc(ctx, num_entries * sizeof(SRV_SHARE_INFO_1));
450
451                 for (snum = *resume_hnd; snum < num_services; snum++) {
452                         if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_admin_share(snum)) ) {
453                                 init_srv_share_info_1(&info1[i++], snum);
454                         }
455                 }
456
457                 ctr->share.info1 = info1;
458                 break;
459         }
460
461         case 2:
462         {
463                 SRV_SHARE_INFO_2 *info2;
464                 int i = 0;
465
466                 info2 = talloc(ctx, num_entries * sizeof(SRV_SHARE_INFO_2));
467
468                 for (snum = *resume_hnd; snum < num_services; snum++) {
469                         if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_admin_share(snum)) ) {
470                                 init_srv_share_info_2(&info2[i++], snum);
471                         }
472                 }
473
474                 ctr->share.info2 = info2;
475                 break;
476         }
477
478         case 502:
479         {
480                 SRV_SHARE_INFO_502 *info502;
481                 int i = 0;
482
483                 info502 = talloc(ctx, num_entries * sizeof(SRV_SHARE_INFO_502));
484
485                 for (snum = *resume_hnd; snum < num_services; snum++) {
486                         if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_admin_share(snum)) ) {
487                                 init_srv_share_info_502(ctx, &info502[i++], snum);
488                         }
489                 }
490
491                 ctr->share.info502 = info502;
492                 break;
493         }
494
495         default:
496                 DEBUG(5,("init_srv_share_info_ctr: unsupported switch value %d\n", info_level));
497                 return False;
498         }
499
500         return True;
501 }
502
503 /*******************************************************************
504  Inits a SRV_R_NET_SHARE_ENUM structure.
505 ********************************************************************/
506
507 static void init_srv_r_net_share_enum(TALLOC_CTX *ctx, SRV_R_NET_SHARE_ENUM *r_n,
508                                       uint32 info_level, uint32 resume_hnd, BOOL all)  
509 {
510         DEBUG(5,("init_srv_r_net_share_enum: %d\n", __LINE__));
511
512         if (init_srv_share_info_ctr(ctx, &r_n->ctr, info_level,
513                                     &resume_hnd, &r_n->total_entries, all)) {
514                 r_n->status = NT_STATUS_NOPROBLEMO;
515         } else {
516                 r_n->status = NT_STATUS_INVALID_INFO_CLASS;
517         }
518
519         init_enum_hnd(&r_n->enum_hnd, resume_hnd);
520 }
521
522 /*******************************************************************
523  Inits a SRV_R_NET_SHARE_GET_INFO structure.
524 ********************************************************************/
525
526 static void init_srv_r_net_share_get_info(TALLOC_CTX *ctx, SRV_R_NET_SHARE_GET_INFO *r_n,
527                                   char *share_name, uint32 info_level)
528 {
529         uint32 status = NT_STATUS_NOPROBLEMO;
530         int snum;
531
532         DEBUG(5,("init_srv_r_net_share_get_info: %d\n", __LINE__));
533
534         r_n->info.switch_value = info_level;
535
536         snum = find_service(share_name);
537
538         if (snum >= 0) {
539                 switch (info_level) {
540                 case 1:
541                         init_srv_share_info_1(&r_n->info.share.info1, snum);
542                         break;
543                 case 2:
544                         init_srv_share_info_2(&r_n->info.share.info2, snum);
545                         break;
546                 case 502:
547                         init_srv_share_info_502(ctx, &r_n->info.share.info502, snum);
548                         break;
549                 case 1005:
550                         init_srv_share_info_1005(&r_n->info.share.info1005, snum);
551                         break;
552                 default:
553                         DEBUG(5,("init_srv_net_share_get_info: unsupported switch value %d\n", info_level));
554                         status = NT_STATUS_INVALID_INFO_CLASS;
555                         break;
556                 }
557         } else {
558                 status = NT_STATUS_BAD_NETWORK_NAME;
559         }
560
561         r_n->info.ptr_share_ctr = (status == NT_STATUS_NOPROBLEMO) ? 1 : 0;
562         r_n->status = status;
563 }
564
565 /*******************************************************************
566  fill in a sess info level 1 structure.
567  ********************************************************************/
568
569 static void init_srv_sess_0_info(SESS_INFO_0 *se0, SESS_INFO_0_STR *str0, char *name)
570 {
571         init_srv_sess_info0(se0, name);
572         init_srv_sess_info0_str(str0, name);
573 }
574
575 /*******************************************************************
576  fill in a sess info level 0 structure.
577  ********************************************************************/
578
579 static void init_srv_sess_info_0(SRV_SESS_INFO_0 *ss0, uint32 *snum, uint32 *stot)
580 {
581         uint32 num_entries = 0;
582         (*stot) = 1;
583
584         if (ss0 == NULL) {
585                 (*snum) = 0;
586                 return;
587         }
588
589         DEBUG(5,("init_srv_sess_0_ss0\n"));
590
591         if (snum) {
592                 for (; (*snum) < (*stot) && num_entries < MAX_SESS_ENTRIES; (*snum)++) {
593                         init_srv_sess_0_info(&ss0->info_0[num_entries],
594                                                                  &ss0->info_0_str[num_entries], "MACHINE");
595
596                         /* move on to creating next session */
597                         /* move on to creating next sess */
598                         num_entries++;
599                 }
600
601                 ss0->num_entries_read  = num_entries;
602                 ss0->ptr_sess_info     = num_entries > 0 ? 1 : 0;
603                 ss0->num_entries_read2 = num_entries;
604                 
605                 if ((*snum) >= (*stot)) {
606                         (*snum) = 0;
607                 }
608
609         } else {
610                 ss0->num_entries_read = 0;
611                 ss0->ptr_sess_info = 0;
612                 ss0->num_entries_read2 = 0;
613         }
614 }
615
616 /*******************************************************************
617  fill in a sess info level 1 structure.
618  ********************************************************************/
619
620 static void init_srv_sess_1_info(SESS_INFO_1 *se1, SESS_INFO_1_STR *str1,
621                                 char *name, char *user,
622                                 uint32 num_opens,
623                                 uint32 open_time, uint32 idle_time,
624                                 uint32 usr_flgs)
625 {
626         init_srv_sess_info1(se1 , name, user, num_opens, open_time, idle_time, usr_flgs);
627         init_srv_sess_info1_str(str1, name, user);
628 }
629
630 /*******************************************************************
631  fill in a sess info level 1 structure.
632  ********************************************************************/
633
634 static void init_srv_sess_info_1(SRV_SESS_INFO_1 *ss1, uint32 *snum, uint32 *stot)
635 {
636         uint32 num_entries = 0;
637         (*stot) = 1;
638
639         if (ss1 == NULL) {
640                 (*snum) = 0;
641                 return;
642         }
643
644         DEBUG(5,("init_srv_sess_1_ss1\n"));
645
646         if (snum) {
647                 for (; (*snum) < (*stot) && num_entries < MAX_SESS_ENTRIES; (*snum)++) {
648                         init_srv_sess_1_info(&ss1->info_1[num_entries],
649                                                                  &ss1->info_1_str[num_entries],
650                                              "MACHINE", "dummy_user", 1, 10, 5, 0);
651
652                         /* move on to creating next session */
653                         /* move on to creating next sess */
654                         num_entries++;
655                 }
656
657                 ss1->num_entries_read  = num_entries;
658                 ss1->ptr_sess_info     = num_entries > 0 ? 1 : 0;
659                 ss1->num_entries_read2 = num_entries;
660                 
661                 if ((*snum) >= (*stot)) {
662                         (*snum) = 0;
663                 }
664
665         } else {
666                 ss1->num_entries_read = 0;
667                 ss1->ptr_sess_info = 0;
668                 ss1->num_entries_read2 = 0;
669                 
670                 (*stot) = 0;
671         }
672 }
673
674 /*******************************************************************
675  makes a SRV_R_NET_SESS_ENUM structure.
676 ********************************************************************/
677
678 static uint32 init_srv_sess_info_ctr(SRV_SESS_INFO_CTR *ctr,
679                                 int switch_value, uint32 *resume_hnd, uint32 *total_entries)
680 {
681         uint32 status = NT_STATUS_NOPROBLEMO;
682         DEBUG(5,("init_srv_sess_info_ctr: %d\n", __LINE__));
683
684         ctr->switch_value = switch_value;
685
686         switch (switch_value) {
687         case 0:
688                 init_srv_sess_info_0(&(ctr->sess.info0), resume_hnd, total_entries);
689                 ctr->ptr_sess_ctr = 1;
690                 break;
691         case 1:
692                 init_srv_sess_info_1(&(ctr->sess.info1), resume_hnd, total_entries);
693                 ctr->ptr_sess_ctr = 1;
694                 break;
695         default:
696                 DEBUG(5,("init_srv_sess_info_ctr: unsupported switch value %d\n", switch_value));
697                 (*resume_hnd) = 0;
698                 (*total_entries) = 0;
699                 ctr->ptr_sess_ctr = 0;
700                 status = NT_STATUS_INVALID_INFO_CLASS;
701                 break;
702         }
703
704         return status;
705 }
706
707 /*******************************************************************
708  makes a SRV_R_NET_SESS_ENUM structure.
709 ********************************************************************/
710
711 static void init_srv_r_net_sess_enum(SRV_R_NET_SESS_ENUM *r_n,
712                                 uint32 resume_hnd, int sess_level, int switch_value)  
713 {
714         DEBUG(5,("init_srv_r_net_sess_enum: %d\n", __LINE__));
715
716         r_n->sess_level  = sess_level;
717
718         if (sess_level == -1)
719                 r_n->status = NT_STATUS_INVALID_INFO_CLASS;
720         else
721                 r_n->status = init_srv_sess_info_ctr(r_n->ctr, switch_value, &resume_hnd, &r_n->total_entries);
722
723         if (r_n->status != NT_STATUS_NOPROBLEMO)
724                 resume_hnd = 0;
725
726         init_enum_hnd(&r_n->enum_hnd, resume_hnd);
727 }
728
729 /*******************************************************************
730  fill in a conn info level 0 structure.
731  ********************************************************************/
732
733 static void init_srv_conn_info_0(SRV_CONN_INFO_0 *ss0, uint32 *snum, uint32 *stot)
734 {
735         uint32 num_entries = 0;
736         (*stot) = 1;
737
738         if (ss0 == NULL) {
739                 (*snum) = 0;
740                 return;
741         }
742
743         DEBUG(5,("init_srv_conn_0_ss0\n"));
744
745         if (snum) {
746                 for (; (*snum) < (*stot) && num_entries < MAX_CONN_ENTRIES; (*snum)++) {
747
748                         init_srv_conn_info0(&ss0->info_0[num_entries], (*stot));
749
750                         /* move on to creating next connection */
751                         /* move on to creating next conn */
752                         num_entries++;
753                 }
754
755                 ss0->num_entries_read  = num_entries;
756                 ss0->ptr_conn_info     = num_entries > 0 ? 1 : 0;
757                 ss0->num_entries_read2 = num_entries;
758                 
759                 if ((*snum) >= (*stot)) {
760                         (*snum) = 0;
761                 }
762
763         } else {
764                 ss0->num_entries_read = 0;
765                 ss0->ptr_conn_info = 0;
766                 ss0->num_entries_read2 = 0;
767
768                 (*stot) = 0;
769         }
770 }
771
772 /*******************************************************************
773  fill in a conn info level 1 structure.
774  ********************************************************************/
775
776 static void init_srv_conn_1_info(CONN_INFO_1 *se1, CONN_INFO_1_STR *str1,
777                                 uint32 id, uint32 type,
778                                 uint32 num_opens, uint32 num_users, uint32 open_time,
779                                 char *usr_name, char *net_name)
780 {
781         init_srv_conn_info1(se1 , id, type, num_opens, num_users, open_time, usr_name, net_name);
782         init_srv_conn_info1_str(str1, usr_name, net_name);
783 }
784
785 /*******************************************************************
786  fill in a conn info level 1 structure.
787  ********************************************************************/
788
789 static void init_srv_conn_info_1(SRV_CONN_INFO_1 *ss1, uint32 *snum, uint32 *stot)
790 {
791         uint32 num_entries = 0;
792         (*stot) = 1;
793
794         if (ss1 == NULL) {
795                 (*snum) = 0;
796                 return;
797         }
798
799         DEBUG(5,("init_srv_conn_1_ss1\n"));
800
801         if (snum) {
802                 for (; (*snum) < (*stot) && num_entries < MAX_CONN_ENTRIES; (*snum)++) {
803                         init_srv_conn_1_info(&ss1->info_1[num_entries],
804                                                                  &ss1->info_1_str[num_entries],
805                                              (*stot), 0x3, 1, 1, 3,"dummy_user", "IPC$");
806
807                         /* move on to creating next connection */
808                         /* move on to creating next conn */
809                         num_entries++;
810                 }
811
812                 ss1->num_entries_read  = num_entries;
813                 ss1->ptr_conn_info     = num_entries > 0 ? 1 : 0;
814                 ss1->num_entries_read2 = num_entries;
815                 
816
817                 if ((*snum) >= (*stot)) {
818                         (*snum) = 0;
819                 }
820
821         } else {
822                 ss1->num_entries_read = 0;
823                 ss1->ptr_conn_info = 0;
824                 ss1->num_entries_read2 = 0;
825                 
826                 (*stot) = 0;
827         }
828 }
829
830 /*******************************************************************
831  makes a SRV_R_NET_CONN_ENUM structure.
832 ********************************************************************/
833
834 static uint32 init_srv_conn_info_ctr(SRV_CONN_INFO_CTR *ctr,
835                                 int switch_value, uint32 *resume_hnd, uint32 *total_entries)
836 {
837         uint32 status = NT_STATUS_NOPROBLEMO;
838         DEBUG(5,("init_srv_conn_info_ctr: %d\n", __LINE__));
839
840         ctr->switch_value = switch_value;
841
842         switch (switch_value) {
843         case 0:
844                 init_srv_conn_info_0(&ctr->conn.info0, resume_hnd, total_entries);
845                 ctr->ptr_conn_ctr = 1;
846                 break;
847         case 1:
848                 init_srv_conn_info_1(&ctr->conn.info1, resume_hnd, total_entries);
849                 ctr->ptr_conn_ctr = 1;
850                 break;
851         default:
852                 DEBUG(5,("init_srv_conn_info_ctr: unsupported switch value %d\n", switch_value));
853                 (*resume_hnd = 0);
854                 (*total_entries) = 0;
855                 ctr->ptr_conn_ctr = 0;
856                 status = NT_STATUS_INVALID_INFO_CLASS;
857                 break;
858         }
859
860         return status;
861 }
862
863 /*******************************************************************
864  makes a SRV_R_NET_CONN_ENUM structure.
865 ********************************************************************/
866
867 static void init_srv_r_net_conn_enum(SRV_R_NET_CONN_ENUM *r_n,
868                                 uint32 resume_hnd, int conn_level, int switch_value)  
869 {
870         DEBUG(5,("init_srv_r_net_conn_enum: %d\n", __LINE__));
871
872         r_n->conn_level  = conn_level;
873         if (conn_level == -1)
874                 r_n->status = NT_STATUS_INVALID_INFO_CLASS;
875         else
876                 r_n->status = init_srv_conn_info_ctr(r_n->ctr, switch_value, &resume_hnd, &r_n->total_entries);
877
878         if (r_n->status != NT_STATUS_NOPROBLEMO)
879                 resume_hnd = 0;
880
881         init_enum_hnd(&r_n->enum_hnd, resume_hnd);
882 }
883
884 /*******************************************************************
885  fill in a file info level 3 structure.
886  ********************************************************************/
887
888 static void init_srv_file_3_info(FILE_INFO_3 *fl3, FILE_INFO_3_STR *str3,
889                                 uint32 fnum, uint32 perms, uint32 num_locks,
890                                 char *path_name, char *user_name)
891 {
892         init_srv_file_info3(fl3 , fnum, perms, num_locks, path_name, user_name);
893         init_srv_file_info3_str(str3, path_name, user_name);
894 }
895
896 /*******************************************************************
897  fill in a file info level 3 structure.
898  ********************************************************************/
899
900 static void init_srv_file_info_3(SRV_FILE_INFO_3 *fl3, uint32 *fnum, uint32 *ftot)
901 {
902         uint32 num_entries = 0;
903         (*ftot) = 1;
904
905         if (fl3 == NULL) {
906                 (*fnum) = 0;
907                 return;
908         }
909
910         DEBUG(5,("init_srv_file_3_fl3\n"));
911
912         for (; (*fnum) < (*ftot) && num_entries < MAX_FILE_ENTRIES; (*fnum)++) {
913                 init_srv_file_3_info(&fl3->info_3[num_entries],
914                                          &fl3->info_3_str[num_entries],
915                                      (*fnum), 0x35, 0, "\\PIPE\\samr", "dummy user");
916
917                 /* move on to creating next file */
918                 num_entries++;
919         }
920
921         fl3->num_entries_read  = num_entries;
922         fl3->ptr_file_info     = num_entries > 0 ? 1 : 0;
923         fl3->num_entries_read2 = num_entries;
924         
925         if ((*fnum) >= (*ftot)) {
926                 (*fnum) = 0;
927         }
928 }
929
930 /*******************************************************************
931  makes a SRV_R_NET_FILE_ENUM structure.
932 ********************************************************************/
933
934 static uint32 init_srv_file_info_ctr(SRV_FILE_INFO_CTR *ctr,
935                                 int switch_value, uint32 *resume_hnd, uint32 *total_entries)  
936 {
937         uint32 status = NT_STATUS_NOPROBLEMO;
938         DEBUG(5,("init_srv_file_info_ctr: %d\n", __LINE__));
939
940         ctr->switch_value = switch_value;
941
942         switch (switch_value) {
943         case 3:
944                 init_srv_file_info_3(&ctr->file.info3, resume_hnd, total_entries);
945                 ctr->ptr_file_ctr = 1;
946                 break;
947         default:
948                 DEBUG(5,("init_srv_file_info_ctr: unsupported switch value %d\n", switch_value));
949                 (*resume_hnd = 0);
950                 (*total_entries) = 0;
951                 ctr->ptr_file_ctr = 0;
952                 status = NT_STATUS_INVALID_INFO_CLASS;
953                 break;
954         }
955
956         return status;
957 }
958
959 /*******************************************************************
960  makes a SRV_R_NET_FILE_ENUM structure.
961 ********************************************************************/
962
963 static void init_srv_r_net_file_enum(SRV_R_NET_FILE_ENUM *r_n,
964                                 uint32 resume_hnd, int file_level, int switch_value)  
965 {
966         DEBUG(5,("init_srv_r_net_file_enum: %d\n", __LINE__));
967
968         r_n->file_level  = file_level;
969         if (file_level == 0)
970                 r_n->status = NT_STATUS_INVALID_INFO_CLASS;
971         else
972                 r_n->status = init_srv_file_info_ctr(r_n->ctr, switch_value, &resume_hnd, &(r_n->total_entries));
973
974         if (r_n->status != NT_STATUS_NOPROBLEMO)
975                 resume_hnd = 0;
976
977         init_enum_hnd(&r_n->enum_hnd, resume_hnd);
978 }
979
980 /*******************************************************************
981 net server get info
982 ********************************************************************/
983
984 uint32 _srv_net_srv_get_info(pipes_struct *p, SRV_Q_NET_SRV_GET_INFO *q_u, SRV_R_NET_SRV_GET_INFO *r_u)
985 {
986         uint32 status = NT_STATUS_NOPROBLEMO;
987         SRV_INFO_CTR *ctr = (SRV_INFO_CTR *)talloc(p->mem_ctx, sizeof(SRV_INFO_CTR));
988
989         if (!ctr)
990                 return NT_STATUS_NO_MEMORY;
991
992         ZERO_STRUCTP(ctr);
993
994         DEBUG(5,("srv_net_srv_get_info: %d\n", __LINE__));
995
996         switch (q_u->switch_value) {
997         case 102:
998                 init_srv_info_102(&ctr->srv.sv102,
999                                   500, global_myname, 
1000                                                 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH),
1001                                   lp_major_announce_version(), lp_minor_announce_version(),
1002                                   lp_default_server_announce(),
1003                                   0xffffffff, /* users */
1004                                   0xf, /* disc */
1005                                   0, /* hidden */
1006                                   240, /* announce */
1007                                   3000, /* announce delta */
1008                                   100000, /* licenses */
1009                                   "c:\\"); /* user path */
1010                 break;
1011         case 101:
1012                 init_srv_info_101(&ctr->srv.sv101,
1013                                   500, global_myname,
1014                                   lp_major_announce_version(), lp_minor_announce_version(),
1015                                   lp_default_server_announce(),
1016                                   string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH));
1017                 break;
1018         case 100:
1019                 init_srv_info_100(&ctr->srv.sv100, 500, global_myname);
1020                 break;
1021         default:
1022                 status = NT_STATUS_INVALID_INFO_CLASS;
1023                 break;
1024         }
1025
1026         /* set up the net server get info structure */
1027         init_srv_r_net_srv_get_info(r_u, q_u->switch_value, ctr, status);
1028
1029         DEBUG(5,("srv_net_srv_get_info: %d\n", __LINE__));
1030
1031         return r_u->status;
1032 }
1033
1034 /*******************************************************************
1035 net file enum
1036 ********************************************************************/
1037
1038 uint32 _srv_net_file_enum(pipes_struct *p, SRV_Q_NET_FILE_ENUM *q_u, SRV_R_NET_FILE_ENUM *r_u)
1039 {
1040         r_u->ctr = (SRV_FILE_INFO_CTR *)talloc(p->mem_ctx, sizeof(SRV_FILE_INFO_CTR));
1041         if (!r_u->ctr)
1042                 return NT_STATUS_NO_MEMORY;
1043
1044         ZERO_STRUCTP(r_u->ctr);
1045
1046         DEBUG(5,("srv_net_file_enum: %d\n", __LINE__));
1047
1048         /* set up the */
1049         init_srv_r_net_file_enum(r_u,
1050                                 get_enum_hnd(&q_u->enum_hnd),
1051                                 q_u->file_level,
1052                                 q_u->ctr->switch_value);
1053
1054         DEBUG(5,("srv_net_file_enum: %d\n", __LINE__));
1055
1056         return r_u->status;
1057 }
1058
1059 /*******************************************************************
1060 net conn enum
1061 ********************************************************************/
1062
1063 uint32 _srv_net_conn_enum(pipes_struct *p, SRV_Q_NET_CONN_ENUM *q_u, SRV_R_NET_CONN_ENUM *r_u)
1064 {
1065         DEBUG(5,("srv_net_conn_enum: %d\n", __LINE__));
1066
1067         r_u->ctr = (SRV_CONN_INFO_CTR *)talloc(p->mem_ctx, sizeof(SRV_CONN_INFO_CTR));
1068         if (!r_u->ctr)
1069                 return NT_STATUS_NO_MEMORY;
1070
1071         ZERO_STRUCTP(r_u->ctr);
1072
1073         /* set up the */
1074         init_srv_r_net_conn_enum(r_u,
1075                                 get_enum_hnd(&q_u->enum_hnd),
1076                                 q_u->conn_level,
1077                                 q_u->ctr->switch_value);
1078
1079         DEBUG(5,("srv_net_conn_enum: %d\n", __LINE__));
1080
1081         return r_u->status;
1082 }
1083
1084 /*******************************************************************
1085 net sess enum
1086 ********************************************************************/
1087
1088 uint32 _srv_net_sess_enum(pipes_struct *p, SRV_Q_NET_SESS_ENUM *q_u, SRV_R_NET_SESS_ENUM *r_u)
1089 {
1090         DEBUG(5,("_srv_net_sess_enum: %d\n", __LINE__));
1091
1092         r_u->ctr = (SRV_SESS_INFO_CTR *)talloc(p->mem_ctx, sizeof(SRV_SESS_INFO_CTR));
1093         if (!r_u->ctr)
1094                 return NT_STATUS_NO_MEMORY;
1095
1096         ZERO_STRUCTP(r_u->ctr);
1097
1098         /* set up the */
1099         init_srv_r_net_sess_enum(r_u,
1100                                 get_enum_hnd(&q_u->enum_hnd),
1101                                 q_u->sess_level,
1102                                 q_u->ctr->switch_value);
1103
1104         DEBUG(5,("_srv_net_sess_enum: %d\n", __LINE__));
1105
1106         return r_u->status;
1107 }
1108
1109 /*******************************************************************
1110  Net share enum all.
1111 ********************************************************************/
1112
1113 uint32 _srv_net_share_enum_all(pipes_struct *p, SRV_Q_NET_SHARE_ENUM *q_u, SRV_R_NET_SHARE_ENUM *r_u)
1114 {
1115         DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
1116
1117         /* Create the list of shares for the response. */
1118         init_srv_r_net_share_enum(p->mem_ctx, r_u,
1119                                 q_u->ctr.info_level,
1120                                 get_enum_hnd(&q_u->enum_hnd), True);
1121
1122         DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
1123
1124         return r_u->status;
1125 }
1126
1127 /*******************************************************************
1128  Net share enum.
1129 ********************************************************************/
1130
1131 uint32 _srv_net_share_enum(pipes_struct *p, SRV_Q_NET_SHARE_ENUM *q_u, SRV_R_NET_SHARE_ENUM *r_u)
1132 {
1133         DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
1134
1135         /* Create the list of shares for the response. */
1136         init_srv_r_net_share_enum(p->mem_ctx, r_u,
1137                                 q_u->ctr.info_level,
1138                                 get_enum_hnd(&q_u->enum_hnd), False);
1139
1140         DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
1141
1142         return r_u->status;
1143 }
1144
1145 /*******************************************************************
1146  Net share get info.
1147 ********************************************************************/
1148
1149 uint32 _srv_net_share_get_info(pipes_struct *p, SRV_Q_NET_SHARE_GET_INFO *q_u, SRV_R_NET_SHARE_GET_INFO *r_u)
1150 {
1151         fstring share_name;
1152
1153         DEBUG(5,("_srv_net_share_get_info: %d\n", __LINE__));
1154
1155         /* Create the list of shares for the response. */
1156         unistr2_to_ascii(share_name, &q_u->uni_share_name, sizeof(share_name));
1157         init_srv_r_net_share_get_info(p->mem_ctx, r_u, share_name, q_u->info_level);
1158
1159         DEBUG(5,("_srv_net_share_get_info: %d\n", __LINE__));
1160
1161         return r_u->status;
1162 }
1163
1164 /*******************************************************************
1165  Check a given DOS pathname is valid for a share.
1166 ********************************************************************/
1167
1168 static char *valid_share_pathname(char *dos_pathname)
1169 {
1170         pstring saved_pathname;
1171         pstring unix_pathname;
1172         char *ptr;
1173         int ret;
1174
1175         /* Convert any '\' paths to '/' */
1176         unix_format(dos_pathname);
1177         unix_clean_name(dos_pathname);
1178
1179         /* NT is braindead - it wants a C: prefix to a pathname ! So strip it. */
1180         ptr = dos_pathname;
1181         if (strlen(dos_pathname) > 2 && ptr[1] == ':' && ptr[0] != '/')
1182                 ptr += 2;
1183
1184         /* Only abolute paths allowed. */
1185         if (*ptr != '/')
1186                 return NULL;
1187
1188         /* Can we cd to it ? */
1189
1190         /* First save our current directory. */
1191         if (getcwd(saved_pathname, sizeof(saved_pathname)) == NULL)
1192                 return False;
1193
1194         /* Convert to UNIX charset. */
1195         pstrcpy(unix_pathname, ptr);
1196         dos_to_unix(unix_pathname, True);
1197         
1198         ret = chdir(unix_pathname);
1199
1200         /* We *MUST* be able to chdir back. Abort if we can't. */
1201         if (chdir(saved_pathname) == -1)
1202                 smb_panic("valid_share_pathname: Unable to restore current directory.\n");
1203
1204         return (ret != -1) ? ptr : NULL;
1205 }
1206
1207 /*******************************************************************
1208  Net share set info. Modify share details.
1209 ********************************************************************/
1210
1211 uint32 _srv_net_share_set_info(pipes_struct *p, SRV_Q_NET_SHARE_SET_INFO *q_u, SRV_R_NET_SHARE_SET_INFO *r_u)
1212 {
1213         struct current_user user;
1214         pstring command;
1215         fstring share_name;
1216         fstring comment;
1217         pstring pathname;
1218         int type;
1219         int snum;
1220         int ret;
1221         char *ptr;
1222         SEC_DESC *psd = NULL;
1223
1224         DEBUG(5,("_srv_net_share_set_info: %d\n", __LINE__));
1225
1226         unistr2_to_ascii(share_name, &q_u->uni_share_name, sizeof(share_name));
1227
1228         r_u->switch_value = 0;
1229
1230         if (strequal(share_name,"IPC$") || strequal(share_name,"ADMIN$") || strequal(share_name,"global"))
1231                 return ERROR_ACCESS_DENIED;
1232
1233         snum = find_service(share_name);
1234
1235         /* Does this share exist ? */
1236         if (snum < 0)
1237                 return ERRnosuchshare;
1238
1239         /* No change to printer shares. */
1240         if (lp_print_ok(snum))
1241                 return ERROR_ACCESS_DENIED;
1242
1243         get_current_user(&user,p);
1244
1245         if (user.uid != 0)
1246                 return ERROR_ACCESS_DENIED;
1247
1248         switch (q_u->info_level) {
1249         case 1:
1250                 /* Not enough info in a level 1 to do anything. */
1251                 return ERROR_ACCESS_DENIED;
1252         case 2:
1253                 unistr2_to_ascii(comment, &q_u->info.share.info2.info_2_str.uni_remark, sizeof(share_name));
1254                 unistr2_to_ascii(pathname, &q_u->info.share.info2.info_2_str.uni_path, sizeof(share_name));
1255                 type = q_u->info.share.info2.info_2.type;
1256                 psd = NULL;
1257                 break;
1258         case 502:
1259                 unistr2_to_ascii(comment, &q_u->info.share.info502.info_502_str.uni_remark, sizeof(share_name));
1260                 unistr2_to_ascii(pathname, &q_u->info.share.info502.info_502_str.uni_path, sizeof(share_name));
1261                 type = q_u->info.share.info502.info_502.type;
1262                 psd = q_u->info.share.info502.info_502_str.sd;
1263                 map_generic_share_sd_bits(psd);
1264                 break;
1265         case 1005:
1266                 return ERROR_ACCESS_DENIED;
1267         case 1501:
1268                 fstrcpy(pathname, lp_pathname(snum));
1269                 fstrcpy(comment, lp_comment(snum));
1270                 psd = q_u->info.share.info1501.sdb->sec;
1271                 map_generic_share_sd_bits(psd);
1272                 type = STYPE_DISKTREE;
1273                 break;
1274         default:
1275                 DEBUG(5,("_srv_net_share_set_info: unsupported switch value %d\n", q_u->info_level));
1276                 return NT_STATUS_INVALID_INFO_CLASS;
1277         }
1278
1279         /* We can only modify disk shares. */
1280         if (type != STYPE_DISKTREE)
1281                 return ERROR_ACCESS_DENIED;
1282                 
1283         /* Check if the pathname is valid. */
1284         if (!(ptr = valid_share_pathname( pathname )))
1285                 return ERRbadpath;
1286
1287         /* Ensure share name, pathname and comment don't contain '"' characters. */
1288         string_replace(share_name, '"', ' ');
1289         string_replace(ptr, '"', ' ');
1290         string_replace(comment, '"', ' ');
1291
1292         DEBUG(10,("_srv_net_share_set_info: change share command = %s\n",
1293                 lp_change_share_cmd() ? lp_change_share_cmd() : "NULL" ));
1294
1295         /* Only call modify function if something changed. */
1296
1297         if (strcmp(ptr, lp_pathname(snum)) || strcmp(comment, lp_comment(snum)) ) {
1298                 if (!lp_change_share_cmd() || !*lp_change_share_cmd())
1299                         return ERROR_ACCESS_DENIED;
1300
1301                 slprintf(command, sizeof(command)-1, "%s \"%s\" \"%s\" \"%s\" \"%s\"",
1302                                 lp_change_share_cmd(), CONFIGFILE, share_name, ptr, comment);
1303                 dos_to_unix(command, True);  /* Convert to unix-codepage */
1304
1305                 DEBUG(10,("_srv_net_share_set_info: Running [%s]\n", command ));
1306                 if ((ret = smbrun(command, NULL)) != 0) {
1307                         DEBUG(0,("_srv_net_share_set_info: Running [%s] returned (%d)\n", command, ret ));
1308                         return ERROR_ACCESS_DENIED;
1309                 }
1310
1311                 /* Tell everyone we updated smb.conf. */
1312                 message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED, NULL, 0, False);
1313
1314         } else {
1315                 DEBUG(10,("_srv_net_share_set_info: No change to share name (%s)\n", share_name ));
1316         }
1317
1318         /* Replace SD if changed. */
1319         if (psd) {
1320                 SEC_DESC *old_sd;
1321                 size_t sd_size;
1322
1323                 old_sd = get_share_security(p->mem_ctx, snum, &sd_size);
1324
1325                 if (old_sd && !sec_desc_equal(old_sd, psd)) {
1326                         if (!set_share_security(p->mem_ctx, share_name, psd))
1327                                 DEBUG(0,("_srv_net_share_set_info: Failed to change security info in share %s.\n",
1328                                         share_name ));
1329                 }
1330         }
1331
1332         DEBUG(5,("_srv_net_share_set_info: %d\n", __LINE__));
1333
1334         return NT_STATUS_NOPROBLEMO;
1335 }
1336
1337 /*******************************************************************
1338  Net share add. Call 'add_share_command "sharename" "pathname" "comment" "read only = xxx"'
1339 ********************************************************************/
1340
1341 uint32 _srv_net_share_add(pipes_struct *p, SRV_Q_NET_SHARE_ADD *q_u, SRV_R_NET_SHARE_ADD *r_u)
1342 {
1343         struct current_user user;
1344         pstring command;
1345         fstring share_name;
1346         fstring comment;
1347         pstring pathname;
1348         int type;
1349         int snum;
1350         int ret;
1351         char *ptr;
1352         SEC_DESC *psd = NULL;
1353
1354         DEBUG(5,("_srv_net_share_add: %d\n", __LINE__));
1355
1356         r_u->switch_value = 0;
1357
1358         get_current_user(&user,p);
1359
1360         if (user.uid != 0) {
1361                 DEBUG(10,("_srv_net_share_add: uid != 0. Access denied.\n"));
1362                 return ERROR_ACCESS_DENIED;
1363         }
1364
1365         if (!lp_add_share_cmd() || !*lp_add_share_cmd()) {
1366                 DEBUG(10,("_srv_net_share_add: No add share command\n"));
1367                 return ERROR_ACCESS_DENIED;
1368         }
1369
1370         switch (q_u->info_level) {
1371         case 1:
1372                 /* Not enough info in a level 1 to do anything. */
1373                 return ERROR_ACCESS_DENIED;
1374         case 2:
1375                 unistr2_to_ascii(share_name, &q_u->info.share.info2.info_2_str.uni_netname, sizeof(share_name));
1376                 unistr2_to_ascii(comment, &q_u->info.share.info2.info_2_str.uni_remark, sizeof(share_name));
1377                 unistr2_to_ascii(pathname, &q_u->info.share.info2.info_2_str.uni_path, sizeof(share_name));
1378                 type = q_u->info.share.info2.info_2.type;
1379                 break;
1380         case 502:
1381                 unistr2_to_ascii(share_name, &q_u->info.share.info502.info_502_str.uni_netname, sizeof(share_name));
1382                 unistr2_to_ascii(comment, &q_u->info.share.info502.info_502_str.uni_remark, sizeof(share_name));
1383                 unistr2_to_ascii(pathname, &q_u->info.share.info502.info_502_str.uni_path, sizeof(share_name));
1384                 type = q_u->info.share.info502.info_502.type;
1385                 psd = q_u->info.share.info502.info_502_str.sd;
1386                 map_generic_share_sd_bits(psd);
1387                 break;
1388         case 1005:
1389                 /* DFS only level. */
1390                 return ERROR_ACCESS_DENIED;
1391         default:
1392                 DEBUG(5,("_srv_net_share_add: unsupported switch value %d\n", q_u->info_level));
1393                 return NT_STATUS_INVALID_INFO_CLASS;
1394         }
1395
1396         if (strequal(share_name,"IPC$") || strequal(share_name,"ADMIN$") || strequal(share_name,"global"))
1397                 return ERROR_ACCESS_DENIED;
1398
1399         snum = find_service(share_name);
1400
1401         /* Share already exists. */
1402         if (snum >= 0)
1403                 return ERRfilexists;
1404
1405         /* We can only add disk shares. */
1406         if (type != STYPE_DISKTREE)
1407                 return ERROR_ACCESS_DENIED;
1408                 
1409         /* Check if the pathname is valid. */
1410         if (!(ptr = valid_share_pathname( pathname )))
1411                 return ERRbadpath;
1412
1413         /* Ensure share name, pathname and comment don't contain '"' characters. */
1414         string_replace(share_name, '"', ' ');
1415         string_replace(ptr, '"', ' ');
1416         string_replace(comment, '"', ' ');
1417
1418         slprintf(command, sizeof(command)-1, "%s \"%s\" \"%s\" \"%s\" \"%s\"",
1419                         lp_add_share_cmd(), CONFIGFILE, share_name, ptr, comment);
1420         dos_to_unix(command, True);  /* Convert to unix-codepage */
1421
1422         DEBUG(10,("_srv_net_share_add: Running [%s]\n", command ));
1423         if ((ret = smbrun(command, NULL)) != 0) {
1424                 DEBUG(0,("_srv_net_share_add: Running [%s] returned (%d)\n", command, ret ));
1425                 return ERROR_ACCESS_DENIED;
1426         }
1427
1428         if (psd) {
1429                 if (!set_share_security(p->mem_ctx, share_name, psd))
1430                         DEBUG(0,("_srv_net_share_add: Failed to add security info to share %s.\n",
1431                                 share_name ));
1432         }
1433
1434         /* Tell everyone we updated smb.conf. */
1435         message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED, NULL, 0, False);
1436
1437         /*
1438          * We don't call reload_services() here, the message will
1439          * cause this to be done before the next packet is read
1440          * from the client. JRA.
1441          */
1442
1443         DEBUG(5,("_srv_net_share_add: %d\n", __LINE__));
1444
1445         return NT_STATUS_NOPROBLEMO;
1446 }
1447
1448 /*******************************************************************
1449  Net share delete. Call "delete share command" with the share name as
1450  a parameter.
1451 ********************************************************************/
1452
1453 uint32 _srv_net_share_del(pipes_struct *p, SRV_Q_NET_SHARE_DEL *q_u, SRV_R_NET_SHARE_DEL *r_u)
1454 {
1455         struct current_user user;
1456         pstring command;
1457         fstring share_name;
1458         int ret;
1459         int snum;
1460
1461         DEBUG(5,("_srv_net_share_del: %d\n", __LINE__));
1462
1463         unistr2_to_ascii(share_name, &q_u->uni_share_name, sizeof(share_name));
1464
1465         if (strequal(share_name,"IPC$") || strequal(share_name,"ADMIN$") || strequal(share_name,"global"))
1466                 return ERROR_ACCESS_DENIED;
1467
1468         snum = find_service(share_name);
1469
1470         if (snum < 0)
1471                 return ERRnosuchshare;
1472
1473         /* No change to printer shares. */
1474         if (lp_print_ok(snum))
1475                 return ERROR_ACCESS_DENIED;
1476
1477         get_current_user(&user,p);
1478
1479         if (user.uid != 0)
1480                 return ERROR_ACCESS_DENIED;
1481
1482         if (!lp_delete_share_cmd() || !*lp_delete_share_cmd())
1483                 return ERROR_ACCESS_DENIED;
1484
1485         slprintf(command, sizeof(command)-1, "%s \"%s\" \"%s\"",
1486                         lp_delete_share_cmd(), CONFIGFILE, lp_servicename(snum));
1487         dos_to_unix(command, True);  /* Convert to unix-codepage */
1488
1489         DEBUG(10,("_srv_net_share_del: Running [%s]\n", command ));
1490         if ((ret = smbrun(command, NULL)) != 0) {
1491                 DEBUG(0,("_srv_net_share_del: Running [%s] returned (%d)\n", command, ret ));
1492                 return ERROR_ACCESS_DENIED;
1493         }
1494
1495         /* Delete the SD in the database. */
1496         delete_share_security(snum);
1497
1498         /* Tell everyone we updated smb.conf. */
1499         message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED, NULL, 0, False);
1500
1501         lp_killservice(snum);
1502
1503         return NT_STATUS_NOPROBLEMO;
1504 }
1505
1506 /*******************************************************************
1507 time of day
1508 ********************************************************************/
1509
1510 uint32 _srv_net_remote_tod(pipes_struct *p, SRV_Q_NET_REMOTE_TOD *q_u, SRV_R_NET_REMOTE_TOD *r_u)
1511 {
1512         TIME_OF_DAY_INFO *tod;
1513         struct tm *t;
1514         time_t unixdate = time(NULL);
1515
1516         tod = (TIME_OF_DAY_INFO *)talloc(p->mem_ctx, sizeof(TIME_OF_DAY_INFO));
1517         if (!tod)
1518                 return NT_STATUS_NO_MEMORY;
1519
1520         ZERO_STRUCTP(tod);
1521  
1522         r_u->tod = tod;
1523         r_u->ptr_srv_tod = 0x1;
1524         r_u->status = NT_STATUS_NOPROBLEMO;
1525
1526         DEBUG(5,("_srv_net_remote_tod: %d\n", __LINE__));
1527
1528         t = gmtime(&unixdate);
1529
1530         /* set up the */
1531         init_time_of_day_info(tod,
1532                               unixdate,
1533                               0,
1534                               t->tm_hour,
1535                               t->tm_min,
1536                               t->tm_sec,
1537                               0,
1538                               TimeDiff(unixdate)/60,
1539                               10000,
1540                               t->tm_mday,
1541                               t->tm_mon + 1,
1542                               1900+t->tm_year,
1543                               t->tm_wday);
1544         
1545         DEBUG(5,("_srv_net_remote_tod: %d\n", __LINE__));
1546
1547         return r_u->status;
1548 }