2 * Unix SMB/Netbios implementation.
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.
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.
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.
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.
25 /* This is the implementation of the srvsvc pipe. */
29 extern int DEBUGLEVEL;
30 extern pstring global_myname;
32 /*******************************************************************
33 Fill in a share info level 1 structure.
34 ********************************************************************/
36 static void init_srv_share_info_1(SRV_SHARE_INFO_1 *sh1, int snum)
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);
48 /* work out the share type */
49 type = STYPE_DISKTREE;
51 if (lp_print_ok(snum))
53 if (strequal("IPC", lp_fstype(snum)))
55 if (net_name[len_net_name] == '$')
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);
62 /*******************************************************************
63 Fill in a share info level 2 structure.
64 ********************************************************************/
66 static void init_srv_share_info_2(SRV_SHARE_INFO_2 *sh2, int snum)
75 pstrcpy(net_name, lp_servicename(snum));
76 pstrcpy(remark, lp_comment(snum));
77 pstring_sub(remark,"%S",lp_servicename(snum));
79 pstrcat(path, lp_pathname(snum));
81 len_net_name = strlen(net_name);
83 /* work out the share type */
84 type = STYPE_DISKTREE;
86 if (lp_print_ok(snum))
88 if (strequal("IPC", lp_fstype(snum)))
90 if (net_name[len_net_name] == '$')
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);
97 /*******************************************************************
98 What to do when smb.conf is updated.
99 ********************************************************************/
101 static void smb_conf_updated(int msg_type, pid_t src, void *buf, size_t len)
103 DEBUG(10,("smb_conf_updated: Got message saying smb.conf was updated. Reloading.\n"));
104 reload_services(False);
107 /*******************************************************************
108 Create the share security tdb.
109 ********************************************************************/
111 static TDB_CONTEXT *share_tdb; /* used for share security descriptors */
112 #define SHARE_DATABASE_VERSION 1
114 BOOL share_info_db_init(void)
116 static pid_t local_pid;
117 char *vstring = "INFO/version";
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);
122 DEBUG(0,("Failed to open share info database %s (%s)\n",
123 lock_path("share_info.tdb"), strerror(errno) ));
127 local_pid = sys_getpid();
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);
135 tdb_unlock_bystring(share_tdb, vstring);
137 message_register(MSG_SMB_CONF_UPDATED, smb_conf_updated);
142 /*******************************************************************
143 Fake up a Everyone, full access as a default.
144 ********************************************************************/
146 static SEC_DESC *get_share_security_default( TALLOC_CTX *ctx, int snum, size_t *psize)
148 extern DOM_SID global_sid_World;
149 extern struct generic_mapping file_generic_mapping;
153 SEC_DESC *psd = NULL;
154 uint32 def_access = GENERIC_ALL_ACCESS;
156 se_map_generic(&def_access, &file_generic_mapping);
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);
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);
166 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
173 /*******************************************************************
174 Pull a security descriptor from the share tdb.
175 ********************************************************************/
177 static SEC_DESC *get_share_security( TALLOC_CTX *ctx, int snum, size_t *psize)
181 SEC_DESC *psd = NULL;
185 /* Fetch security descriptor from tdb */
187 slprintf(key, sizeof(key)-1, "SECDESC/%s", lp_servicename(snum));
189 if (tdb_prs_fetch(share_tdb, key, &ps, ctx)!=0 ||
190 !sec_io_desc("get_share_security", &psd, &ps, 1)) {
192 DEBUG(4,("get_share_security: using default secdesc for %s\n", lp_servicename(snum) ));
194 return get_share_security_default(ctx, snum, psize);
198 *psize = sec_desc_size(psd);
204 /*******************************************************************
205 Store a security descriptor in the share db.
206 ********************************************************************/
208 static BOOL set_share_security(TALLOC_CTX *ctx, const char *share_name, SEC_DESC *psd)
211 TALLOC_CTX *mem_ctx = NULL;
215 mem_ctx = talloc_init();
219 prs_init(&ps, (uint32)sec_desc_size(psd), mem_ctx, MARSHALL);
221 if (!sec_io_desc("nt_printing_setsec", &psd, &ps, 1)) {
225 slprintf(key, sizeof(key)-1, "SECDESC/%s", share_name);
227 if (tdb_prs_store(share_tdb, key, &ps)==0) {
229 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
231 DEBUG(1,("set_share_security: Failed to store secdesc for %s\n", share_name ));
234 /* Free malloc'ed memory */
240 talloc_destroy(mem_ctx);
244 /*******************************************************************
245 Delete a security descriptor.
246 ********************************************************************/
248 static BOOL delete_share_security(int snum)
253 slprintf(key, sizeof(key)-1, "SECDESC/%s", lp_servicename(snum));
255 kbuf.dsize = strlen(key)+1;
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) ));
266 /*******************************************************************
267 Map any generic bits to file specific bits.
268 ********************************************************************/
270 void map_generic_share_sd_bits(SEC_DESC *psd)
272 extern struct generic_mapping file_generic_mapping;
274 SEC_ACL *ps_dacl = NULL;
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;
287 se_map_generic(&psa->info.mask, &file_generic_mapping);
288 psa->info.mask |= orig_mask;
292 /*******************************************************************
293 Can this user access with share with the required permissions ?
294 ********************************************************************/
296 BOOL share_access_check(int snum, uint16 vuid, uint32 desired_access)
298 uint32 granted, status;
299 TALLOC_CTX *mem_ctx = NULL;
300 SEC_DESC *psd = NULL;
302 struct current_user tmp_user;
303 struct current_user *puser = NULL;
304 user_struct *vuser = get_valid_user_struct(vuid);
307 mem_ctx = talloc_init();
311 psd = get_share_security(mem_ctx, snum, &sd_size);
317 ZERO_STRUCT(tmp_user);
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;
327 ret = se_access_check(psd, puser, desired_access, &granted, &status);
331 talloc_destroy(mem_ctx);
336 /*******************************************************************
337 Fill in a share info level 502 structure.
338 ********************************************************************/
340 static void init_srv_share_info_502(TALLOC_CTX *ctx, SRV_SHARE_INFO_502 *sh502, int snum)
353 pstrcpy(net_name, lp_servicename(snum));
354 pstrcpy(remark, lp_comment(snum));
355 pstring_sub(remark,"%S",lp_servicename(snum));
357 pstrcat(path, lp_pathname(snum));
359 len_net_name = strlen(net_name);
361 /* work out the share type */
362 type = STYPE_DISKTREE;
364 if (lp_print_ok(snum))
366 if (strequal("IPC$", net_name))
368 if (net_name[len_net_name] == '$')
369 type |= STYPE_HIDDEN;
371 sd = get_share_security(ctx, snum, &sd_size);
373 init_srv_share_info502(&sh502->info_502, net_name, type, remark, 0, 0xffffffff, 1, path, passwd, sd, sd_size);
374 init_srv_share_info502_str(&sh502->info_502_str, net_name, remark, path, passwd, sd, sd_size);
377 /***************************************************************************
378 Fill in a share info level 1005 structure.
379 ***************************************************************************/
381 static void init_srv_share_info_1005(SRV_SHARE_INFO_1005* sh1005, int snum)
383 sh1005->dfs_root_flag = 0;
386 if(lp_host_msdfs() && lp_msdfs_root(snum))
387 sh1005->dfs_root_flag = 3;
392 /*******************************************************************
393 True if it ends in '$'.
394 ********************************************************************/
396 static BOOL is_admin_share(int snum)
400 pstrcpy(net_name, lp_servicename(snum));
401 return (net_name[strlen(net_name)] == '$') ? True : False;
404 /*******************************************************************
405 Fill in a share info structure.
406 ********************************************************************/
408 static BOOL init_srv_share_info_ctr(TALLOC_CTX *ctx, SRV_SHARE_INFO_CTR *ctr,
409 uint32 info_level, uint32 *resume_hnd, uint32 *total_entries, BOOL all_shares)
412 int num_services = lp_numservices();
415 DEBUG(5,("init_srv_share_info_ctr\n"));
419 ctr->info_level = ctr->switch_value = info_level;
422 /* Count the number of entries. */
423 for (snum = 0; snum < num_services; snum++) {
424 if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_admin_share(snum)) )
428 *total_entries = num_entries;
429 ctr->num_entries2 = ctr->num_entries = num_entries;
430 ctr->ptr_share_info = ctr->ptr_entries = 1;
435 switch (info_level) {
438 SRV_SHARE_INFO_1 *info1;
441 info1 = talloc(ctx, num_entries * sizeof(SRV_SHARE_INFO_1));
443 for (snum = *resume_hnd; snum < num_services; snum++) {
444 if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_admin_share(snum)) ) {
445 init_srv_share_info_1(&info1[i++], snum);
449 ctr->share.info1 = info1;
455 SRV_SHARE_INFO_2 *info2;
458 info2 = talloc(ctx, num_entries * sizeof(SRV_SHARE_INFO_2));
460 for (snum = *resume_hnd; snum < num_services; snum++) {
461 if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_admin_share(snum)) ) {
462 init_srv_share_info_2(&info2[i++], snum);
466 ctr->share.info2 = info2;
472 SRV_SHARE_INFO_502 *info502;
475 info502 = talloc(ctx, num_entries * sizeof(SRV_SHARE_INFO_502));
477 for (snum = *resume_hnd; snum < num_services; snum++) {
478 if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_admin_share(snum)) ) {
479 init_srv_share_info_502(ctx, &info502[i++], snum);
483 ctr->share.info502 = info502;
488 DEBUG(5,("init_srv_share_info_ctr: unsupported switch value %d\n", info_level));
495 /*******************************************************************
496 Inits a SRV_R_NET_SHARE_ENUM structure.
497 ********************************************************************/
499 static void init_srv_r_net_share_enum(TALLOC_CTX *ctx, SRV_R_NET_SHARE_ENUM *r_n,
500 uint32 info_level, uint32 resume_hnd, BOOL all)
502 DEBUG(5,("init_srv_r_net_share_enum: %d\n", __LINE__));
504 if (init_srv_share_info_ctr(ctx, &r_n->ctr, info_level,
505 &resume_hnd, &r_n->total_entries, all)) {
506 r_n->status = NT_STATUS_NOPROBLEMO;
508 r_n->status = NT_STATUS_INVALID_INFO_CLASS;
511 init_enum_hnd(&r_n->enum_hnd, resume_hnd);
514 /*******************************************************************
515 Inits a SRV_R_NET_SHARE_GET_INFO structure.
516 ********************************************************************/
518 static void init_srv_r_net_share_get_info(TALLOC_CTX *ctx, SRV_R_NET_SHARE_GET_INFO *r_n,
519 char *share_name, uint32 info_level)
521 uint32 status = NT_STATUS_NOPROBLEMO;
524 DEBUG(5,("init_srv_r_net_share_get_info: %d\n", __LINE__));
526 r_n->info.switch_value = info_level;
528 snum = find_service(share_name);
531 switch (info_level) {
533 init_srv_share_info_1(&r_n->info.share.info1, snum);
536 init_srv_share_info_2(&r_n->info.share.info2, snum);
539 init_srv_share_info_502(ctx, &r_n->info.share.info502, snum);
542 init_srv_share_info_1005(&r_n->info.share.info1005, snum);
545 DEBUG(5,("init_srv_net_share_get_info: unsupported switch value %d\n", info_level));
546 status = NT_STATUS_INVALID_INFO_CLASS;
550 status = NT_STATUS_BAD_NETWORK_NAME;
553 r_n->info.ptr_share_ctr = (status == NT_STATUS_NOPROBLEMO) ? 1 : 0;
554 r_n->status = status;
557 /*******************************************************************
558 fill in a sess info level 1 structure.
559 ********************************************************************/
561 static void init_srv_sess_0_info(SESS_INFO_0 *se0, SESS_INFO_0_STR *str0, char *name)
563 init_srv_sess_info0(se0, name);
564 init_srv_sess_info0_str(str0, name);
567 /*******************************************************************
568 fill in a sess info level 0 structure.
569 ********************************************************************/
571 static void init_srv_sess_info_0(SRV_SESS_INFO_0 *ss0, uint32 *snum, uint32 *stot)
573 uint32 num_entries = 0;
581 DEBUG(5,("init_srv_sess_0_ss0\n"));
584 for (; (*snum) < (*stot) && num_entries < MAX_SESS_ENTRIES; (*snum)++) {
585 init_srv_sess_0_info(&ss0->info_0[num_entries],
586 &ss0->info_0_str[num_entries], "MACHINE");
588 /* move on to creating next session */
589 /* move on to creating next sess */
593 ss0->num_entries_read = num_entries;
594 ss0->ptr_sess_info = num_entries > 0 ? 1 : 0;
595 ss0->num_entries_read2 = num_entries;
597 if ((*snum) >= (*stot)) {
602 ss0->num_entries_read = 0;
603 ss0->ptr_sess_info = 0;
604 ss0->num_entries_read2 = 0;
608 /*******************************************************************
609 fill in a sess info level 1 structure.
610 ********************************************************************/
612 static void init_srv_sess_1_info(SESS_INFO_1 *se1, SESS_INFO_1_STR *str1,
613 char *name, char *user,
615 uint32 open_time, uint32 idle_time,
618 init_srv_sess_info1(se1 , name, user, num_opens, open_time, idle_time, usr_flgs);
619 init_srv_sess_info1_str(str1, name, user);
622 /*******************************************************************
623 fill in a sess info level 1 structure.
624 ********************************************************************/
626 static void init_srv_sess_info_1(SRV_SESS_INFO_1 *ss1, uint32 *snum, uint32 *stot)
628 uint32 num_entries = 0;
636 DEBUG(5,("init_srv_sess_1_ss1\n"));
639 for (; (*snum) < (*stot) && num_entries < MAX_SESS_ENTRIES; (*snum)++) {
640 init_srv_sess_1_info(&ss1->info_1[num_entries],
641 &ss1->info_1_str[num_entries],
642 "MACHINE", "dummy_user", 1, 10, 5, 0);
644 /* move on to creating next session */
645 /* move on to creating next sess */
649 ss1->num_entries_read = num_entries;
650 ss1->ptr_sess_info = num_entries > 0 ? 1 : 0;
651 ss1->num_entries_read2 = num_entries;
653 if ((*snum) >= (*stot)) {
658 ss1->num_entries_read = 0;
659 ss1->ptr_sess_info = 0;
660 ss1->num_entries_read2 = 0;
666 /*******************************************************************
667 makes a SRV_R_NET_SESS_ENUM structure.
668 ********************************************************************/
670 static uint32 init_srv_sess_info_ctr(SRV_SESS_INFO_CTR *ctr,
671 int switch_value, uint32 *resume_hnd, uint32 *total_entries)
673 uint32 status = NT_STATUS_NOPROBLEMO;
674 DEBUG(5,("init_srv_sess_info_ctr: %d\n", __LINE__));
676 ctr->switch_value = switch_value;
678 switch (switch_value) {
680 init_srv_sess_info_0(&(ctr->sess.info0), resume_hnd, total_entries);
681 ctr->ptr_sess_ctr = 1;
684 init_srv_sess_info_1(&(ctr->sess.info1), resume_hnd, total_entries);
685 ctr->ptr_sess_ctr = 1;
688 DEBUG(5,("init_srv_sess_info_ctr: unsupported switch value %d\n", switch_value));
690 (*total_entries) = 0;
691 ctr->ptr_sess_ctr = 0;
692 status = NT_STATUS_INVALID_INFO_CLASS;
699 /*******************************************************************
700 makes a SRV_R_NET_SESS_ENUM structure.
701 ********************************************************************/
703 static void init_srv_r_net_sess_enum(SRV_R_NET_SESS_ENUM *r_n,
704 uint32 resume_hnd, int sess_level, int switch_value)
706 DEBUG(5,("init_srv_r_net_sess_enum: %d\n", __LINE__));
708 r_n->sess_level = sess_level;
710 if (sess_level == -1)
711 r_n->status = NT_STATUS_INVALID_INFO_CLASS;
713 r_n->status = init_srv_sess_info_ctr(r_n->ctr, switch_value, &resume_hnd, &r_n->total_entries);
715 if (r_n->status != NT_STATUS_NOPROBLEMO)
718 init_enum_hnd(&r_n->enum_hnd, resume_hnd);
721 /*******************************************************************
722 fill in a conn info level 0 structure.
723 ********************************************************************/
725 static void init_srv_conn_info_0(SRV_CONN_INFO_0 *ss0, uint32 *snum, uint32 *stot)
727 uint32 num_entries = 0;
735 DEBUG(5,("init_srv_conn_0_ss0\n"));
738 for (; (*snum) < (*stot) && num_entries < MAX_CONN_ENTRIES; (*snum)++) {
740 init_srv_conn_info0(&ss0->info_0[num_entries], (*stot));
742 /* move on to creating next connection */
743 /* move on to creating next conn */
747 ss0->num_entries_read = num_entries;
748 ss0->ptr_conn_info = num_entries > 0 ? 1 : 0;
749 ss0->num_entries_read2 = num_entries;
751 if ((*snum) >= (*stot)) {
756 ss0->num_entries_read = 0;
757 ss0->ptr_conn_info = 0;
758 ss0->num_entries_read2 = 0;
764 /*******************************************************************
765 fill in a conn info level 1 structure.
766 ********************************************************************/
768 static void init_srv_conn_1_info(CONN_INFO_1 *se1, CONN_INFO_1_STR *str1,
769 uint32 id, uint32 type,
770 uint32 num_opens, uint32 num_users, uint32 open_time,
771 char *usr_name, char *net_name)
773 init_srv_conn_info1(se1 , id, type, num_opens, num_users, open_time, usr_name, net_name);
774 init_srv_conn_info1_str(str1, usr_name, net_name);
777 /*******************************************************************
778 fill in a conn info level 1 structure.
779 ********************************************************************/
781 static void init_srv_conn_info_1(SRV_CONN_INFO_1 *ss1, uint32 *snum, uint32 *stot)
783 uint32 num_entries = 0;
791 DEBUG(5,("init_srv_conn_1_ss1\n"));
794 for (; (*snum) < (*stot) && num_entries < MAX_CONN_ENTRIES; (*snum)++) {
795 init_srv_conn_1_info(&ss1->info_1[num_entries],
796 &ss1->info_1_str[num_entries],
797 (*stot), 0x3, 1, 1, 3,"dummy_user", "IPC$");
799 /* move on to creating next connection */
800 /* move on to creating next conn */
804 ss1->num_entries_read = num_entries;
805 ss1->ptr_conn_info = num_entries > 0 ? 1 : 0;
806 ss1->num_entries_read2 = num_entries;
809 if ((*snum) >= (*stot)) {
814 ss1->num_entries_read = 0;
815 ss1->ptr_conn_info = 0;
816 ss1->num_entries_read2 = 0;
822 /*******************************************************************
823 makes a SRV_R_NET_CONN_ENUM structure.
824 ********************************************************************/
826 static uint32 init_srv_conn_info_ctr(SRV_CONN_INFO_CTR *ctr,
827 int switch_value, uint32 *resume_hnd, uint32 *total_entries)
829 uint32 status = NT_STATUS_NOPROBLEMO;
830 DEBUG(5,("init_srv_conn_info_ctr: %d\n", __LINE__));
832 ctr->switch_value = switch_value;
834 switch (switch_value) {
836 init_srv_conn_info_0(&ctr->conn.info0, resume_hnd, total_entries);
837 ctr->ptr_conn_ctr = 1;
840 init_srv_conn_info_1(&ctr->conn.info1, resume_hnd, total_entries);
841 ctr->ptr_conn_ctr = 1;
844 DEBUG(5,("init_srv_conn_info_ctr: unsupported switch value %d\n", switch_value));
846 (*total_entries) = 0;
847 ctr->ptr_conn_ctr = 0;
848 status = NT_STATUS_INVALID_INFO_CLASS;
855 /*******************************************************************
856 makes a SRV_R_NET_CONN_ENUM structure.
857 ********************************************************************/
859 static void init_srv_r_net_conn_enum(SRV_R_NET_CONN_ENUM *r_n,
860 uint32 resume_hnd, int conn_level, int switch_value)
862 DEBUG(5,("init_srv_r_net_conn_enum: %d\n", __LINE__));
864 r_n->conn_level = conn_level;
865 if (conn_level == -1)
866 r_n->status = NT_STATUS_INVALID_INFO_CLASS;
868 r_n->status = init_srv_conn_info_ctr(r_n->ctr, switch_value, &resume_hnd, &r_n->total_entries);
870 if (r_n->status != NT_STATUS_NOPROBLEMO)
873 init_enum_hnd(&r_n->enum_hnd, resume_hnd);
876 /*******************************************************************
877 fill in a file info level 3 structure.
878 ********************************************************************/
880 static void init_srv_file_3_info(FILE_INFO_3 *fl3, FILE_INFO_3_STR *str3,
881 uint32 fnum, uint32 perms, uint32 num_locks,
882 char *path_name, char *user_name)
884 init_srv_file_info3(fl3 , fnum, perms, num_locks, path_name, user_name);
885 init_srv_file_info3_str(str3, path_name, user_name);
888 /*******************************************************************
889 fill in a file info level 3 structure.
890 ********************************************************************/
892 static void init_srv_file_info_3(SRV_FILE_INFO_3 *fl3, uint32 *fnum, uint32 *ftot)
894 uint32 num_entries = 0;
902 DEBUG(5,("init_srv_file_3_fl3\n"));
904 for (; (*fnum) < (*ftot) && num_entries < MAX_FILE_ENTRIES; (*fnum)++) {
905 init_srv_file_3_info(&fl3->info_3[num_entries],
906 &fl3->info_3_str[num_entries],
907 (*fnum), 0x35, 0, "\\PIPE\\samr", "dummy user");
909 /* move on to creating next file */
913 fl3->num_entries_read = num_entries;
914 fl3->ptr_file_info = num_entries > 0 ? 1 : 0;
915 fl3->num_entries_read2 = num_entries;
917 if ((*fnum) >= (*ftot)) {
922 /*******************************************************************
923 makes a SRV_R_NET_FILE_ENUM structure.
924 ********************************************************************/
926 static uint32 init_srv_file_info_ctr(SRV_FILE_INFO_CTR *ctr,
927 int switch_value, uint32 *resume_hnd, uint32 *total_entries)
929 uint32 status = NT_STATUS_NOPROBLEMO;
930 DEBUG(5,("init_srv_file_info_ctr: %d\n", __LINE__));
932 ctr->switch_value = switch_value;
934 switch (switch_value) {
936 init_srv_file_info_3(&ctr->file.info3, resume_hnd, total_entries);
937 ctr->ptr_file_ctr = 1;
940 DEBUG(5,("init_srv_file_info_ctr: unsupported switch value %d\n", switch_value));
942 (*total_entries) = 0;
943 ctr->ptr_file_ctr = 0;
944 status = NT_STATUS_INVALID_INFO_CLASS;
951 /*******************************************************************
952 makes a SRV_R_NET_FILE_ENUM structure.
953 ********************************************************************/
955 static void init_srv_r_net_file_enum(SRV_R_NET_FILE_ENUM *r_n,
956 uint32 resume_hnd, int file_level, int switch_value)
958 DEBUG(5,("init_srv_r_net_file_enum: %d\n", __LINE__));
960 r_n->file_level = file_level;
962 r_n->status = NT_STATUS_INVALID_INFO_CLASS;
964 r_n->status = init_srv_file_info_ctr(r_n->ctr, switch_value, &resume_hnd, &(r_n->total_entries));
966 if (r_n->status != NT_STATUS_NOPROBLEMO)
969 init_enum_hnd(&r_n->enum_hnd, resume_hnd);
972 /*******************************************************************
974 ********************************************************************/
976 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)
978 uint32 status = NT_STATUS_NOPROBLEMO;
979 SRV_INFO_CTR *ctr = (SRV_INFO_CTR *)talloc(p->mem_ctx, sizeof(SRV_INFO_CTR));
982 return NT_STATUS_NO_MEMORY;
986 DEBUG(5,("srv_net_srv_get_info: %d\n", __LINE__));
988 switch (q_u->switch_value) {
990 init_srv_info_102(&ctr->srv.sv102,
992 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH),
993 lp_major_announce_version(), lp_minor_announce_version(),
994 lp_default_server_announce(),
995 0xffffffff, /* users */
999 3000, /* announce delta */
1000 100000, /* licenses */
1001 "c:\\"); /* user path */
1004 init_srv_info_101(&ctr->srv.sv101,
1006 lp_major_announce_version(), lp_minor_announce_version(),
1007 lp_default_server_announce(),
1008 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH));
1011 init_srv_info_100(&ctr->srv.sv100, 500, global_myname);
1014 status = NT_STATUS_INVALID_INFO_CLASS;
1018 /* set up the net server get info structure */
1019 init_srv_r_net_srv_get_info(r_u, q_u->switch_value, ctr, status);
1021 DEBUG(5,("srv_net_srv_get_info: %d\n", __LINE__));
1026 /*******************************************************************
1028 ********************************************************************/
1030 uint32 _srv_net_file_enum(pipes_struct *p, SRV_Q_NET_FILE_ENUM *q_u, SRV_R_NET_FILE_ENUM *r_u)
1032 r_u->ctr = (SRV_FILE_INFO_CTR *)talloc(p->mem_ctx, sizeof(SRV_FILE_INFO_CTR));
1034 return NT_STATUS_NO_MEMORY;
1036 ZERO_STRUCTP(r_u->ctr);
1038 DEBUG(5,("srv_net_file_enum: %d\n", __LINE__));
1041 init_srv_r_net_file_enum(r_u,
1042 get_enum_hnd(&q_u->enum_hnd),
1044 q_u->ctr->switch_value);
1046 DEBUG(5,("srv_net_file_enum: %d\n", __LINE__));
1051 /*******************************************************************
1053 ********************************************************************/
1055 uint32 _srv_net_conn_enum(pipes_struct *p, SRV_Q_NET_CONN_ENUM *q_u, SRV_R_NET_CONN_ENUM *r_u)
1057 DEBUG(5,("srv_net_conn_enum: %d\n", __LINE__));
1059 r_u->ctr = (SRV_CONN_INFO_CTR *)talloc(p->mem_ctx, sizeof(SRV_CONN_INFO_CTR));
1061 return NT_STATUS_NO_MEMORY;
1063 ZERO_STRUCTP(r_u->ctr);
1066 init_srv_r_net_conn_enum(r_u,
1067 get_enum_hnd(&q_u->enum_hnd),
1069 q_u->ctr->switch_value);
1071 DEBUG(5,("srv_net_conn_enum: %d\n", __LINE__));
1076 /*******************************************************************
1078 ********************************************************************/
1080 uint32 _srv_net_sess_enum(pipes_struct *p, SRV_Q_NET_SESS_ENUM *q_u, SRV_R_NET_SESS_ENUM *r_u)
1082 DEBUG(5,("_srv_net_sess_enum: %d\n", __LINE__));
1084 r_u->ctr = (SRV_SESS_INFO_CTR *)talloc(p->mem_ctx, sizeof(SRV_SESS_INFO_CTR));
1086 return NT_STATUS_NO_MEMORY;
1088 ZERO_STRUCTP(r_u->ctr);
1091 init_srv_r_net_sess_enum(r_u,
1092 get_enum_hnd(&q_u->enum_hnd),
1094 q_u->ctr->switch_value);
1096 DEBUG(5,("_srv_net_sess_enum: %d\n", __LINE__));
1101 /*******************************************************************
1103 ********************************************************************/
1105 uint32 _srv_net_share_enum_all(pipes_struct *p, SRV_Q_NET_SHARE_ENUM *q_u, SRV_R_NET_SHARE_ENUM *r_u)
1107 DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
1109 /* Create the list of shares for the response. */
1110 init_srv_r_net_share_enum(p->mem_ctx, r_u,
1111 q_u->ctr.info_level,
1112 get_enum_hnd(&q_u->enum_hnd), True);
1114 DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
1119 /*******************************************************************
1121 ********************************************************************/
1123 uint32 _srv_net_share_enum(pipes_struct *p, SRV_Q_NET_SHARE_ENUM *q_u, SRV_R_NET_SHARE_ENUM *r_u)
1125 DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
1127 /* Create the list of shares for the response. */
1128 init_srv_r_net_share_enum(p->mem_ctx, r_u,
1129 q_u->ctr.info_level,
1130 get_enum_hnd(&q_u->enum_hnd), False);
1132 DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
1137 /*******************************************************************
1139 ********************************************************************/
1141 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)
1145 DEBUG(5,("_srv_net_share_get_info: %d\n", __LINE__));
1147 /* Create the list of shares for the response. */
1148 unistr2_to_ascii(share_name, &q_u->uni_share_name, sizeof(share_name));
1149 init_srv_r_net_share_get_info(p->mem_ctx, r_u, share_name, q_u->info_level);
1151 DEBUG(5,("_srv_net_share_get_info: %d\n", __LINE__));
1156 /*******************************************************************
1157 Check a given DOS pathname is valid for a share.
1158 ********************************************************************/
1160 static char *valid_share_pathname(char *dos_pathname)
1162 pstring saved_pathname;
1163 pstring unix_pathname;
1167 /* Convert any '\' paths to '/' */
1168 unix_format(dos_pathname);
1169 unix_clean_name(dos_pathname);
1171 /* NT is braindead - it wants a C: prefix to a pathname ! So strip it. */
1173 if (strlen(dos_pathname) > 2 && ptr[1] == ':' && ptr[0] != '/')
1176 /* Only abolute paths allowed. */
1180 /* Can we cd to it ? */
1182 /* First save our current directory. */
1183 if (getcwd(saved_pathname, sizeof(saved_pathname)) == NULL)
1186 /* Convert to UNIX charset. */
1187 pstrcpy(unix_pathname, ptr);
1188 dos_to_unix(unix_pathname, True);
1190 ret = chdir(unix_pathname);
1192 /* We *MUST* be able to chdir back. Abort if we can't. */
1193 if (chdir(saved_pathname) == -1)
1194 smb_panic("valid_share_pathname: Unable to restore current directory.\n");
1196 return (ret != -1) ? ptr : NULL;
1199 /*******************************************************************
1200 Net share set info. Modify share details.
1201 ********************************************************************/
1203 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)
1205 struct current_user user;
1214 SEC_DESC *psd = NULL;
1216 DEBUG(5,("_srv_net_share_set_info: %d\n", __LINE__));
1218 unistr2_to_ascii(share_name, &q_u->uni_share_name, sizeof(share_name));
1220 r_u->switch_value = 0;
1222 if (strequal(share_name,"IPC$") || strequal(share_name,"ADMIN$"))
1223 return ERROR_ACCESS_DENIED;
1225 snum = find_service(share_name);
1227 /* Does this share exist ? */
1229 return ERRnosuchshare;
1231 /* No change to printer shares. */
1232 if (lp_print_ok(snum))
1233 return ERROR_ACCESS_DENIED;
1235 get_current_user(&user,p);
1238 return ERROR_ACCESS_DENIED;
1240 switch (q_u->info_level) {
1242 /* Not enough info in a level 1 to do anything. */
1243 return ERROR_ACCESS_DENIED;
1245 unistr2_to_ascii(comment, &q_u->info.share.info2.info_2_str.uni_remark, sizeof(share_name));
1246 unistr2_to_ascii(pathname, &q_u->info.share.info2.info_2_str.uni_path, sizeof(share_name));
1247 type = q_u->info.share.info2.info_2.type;
1251 unistr2_to_ascii(comment, &q_u->info.share.info502.info_502_str.uni_remark, sizeof(share_name));
1252 unistr2_to_ascii(pathname, &q_u->info.share.info502.info_502_str.uni_path, sizeof(share_name));
1253 type = q_u->info.share.info502.info_502.type;
1254 psd = q_u->info.share.info502.info_502_str.sd;
1255 map_generic_share_sd_bits(psd);
1258 return ERROR_ACCESS_DENIED;
1260 fstrcpy(pathname, lp_pathname(snum));
1261 fstrcpy(comment, lp_comment(snum));
1262 psd = q_u->info.share.info1501.sdb->sec;
1263 map_generic_share_sd_bits(psd);
1264 type = STYPE_DISKTREE;
1267 DEBUG(5,("_srv_net_share_set_info: unsupported switch value %d\n", q_u->info_level));
1268 return NT_STATUS_INVALID_INFO_CLASS;
1271 /* We can only modify disk shares. */
1272 if (type != STYPE_DISKTREE)
1273 return ERROR_ACCESS_DENIED;
1275 /* Check if the pathname is valid. */
1276 if (!(ptr = valid_share_pathname( pathname )))
1279 /* Ensure share name, pathname and comment don't contain '"' characters. */
1280 string_replace(share_name, '"', ' ');
1281 string_replace(ptr, '"', ' ');
1282 string_replace(comment, '"', ' ');
1284 DEBUG(10,("_srv_net_share_set_info: change share command = %s\n",
1285 lp_change_share_cmd() ? lp_change_share_cmd() : "NULL" ));
1287 /* Only call modify function if something changed. */
1289 if (strcmp(ptr, lp_pathname(snum)) || strcmp(comment, lp_comment(snum)) ) {
1290 if (!lp_change_share_cmd() || !*lp_change_share_cmd())
1291 return ERROR_ACCESS_DENIED;
1293 slprintf(command, sizeof(command)-1, "%s \"%s\" \"%s\" \"%s\"",
1294 lp_change_share_cmd(), share_name, ptr, comment);
1295 dos_to_unix(command, True); /* Convert to unix-codepage */
1297 DEBUG(10,("_srv_net_share_set_info: Running [%s]\n", command ));
1298 if ((ret = smbrun(command, NULL, False)) != 0) {
1299 DEBUG(0,("_srv_net_share_set_info: Running [%s] returned (%d)\n", command, ret ));
1300 return ERROR_ACCESS_DENIED;
1303 /* Tell everyone we updated smb.conf. */
1304 message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED, NULL, 0, False);
1307 DEBUG(10,("_srv_net_share_set_info: No change to share name (%s)\n", share_name ));
1310 /* Replace SD if changed. */
1315 old_sd = get_share_security(p->mem_ctx, snum, &sd_size);
1317 if (old_sd && !sec_desc_equal(old_sd, psd)) {
1318 if (!set_share_security(p->mem_ctx, share_name, psd))
1319 DEBUG(0,("_srv_net_share_set_info: Failed to change security info in share %s.\n",
1324 DEBUG(5,("_srv_net_share_set_info: %d\n", __LINE__));
1326 return NT_STATUS_NOPROBLEMO;
1329 /*******************************************************************
1330 Net share add. Call 'add_share_command "sharename" "pathname" "comment" "read only = xxx"'
1331 ********************************************************************/
1333 uint32 _srv_net_share_add(pipes_struct *p, SRV_Q_NET_SHARE_ADD *q_u, SRV_R_NET_SHARE_ADD *r_u)
1335 struct current_user user;
1344 SEC_DESC *psd = NULL;
1346 DEBUG(5,("_srv_net_share_add: %d\n", __LINE__));
1348 r_u->switch_value = 0;
1350 get_current_user(&user,p);
1352 if (user.uid != 0) {
1353 DEBUG(10,("_srv_net_share_add: uid != 0. Access denied.\n"));
1354 return ERROR_ACCESS_DENIED;
1357 if (!lp_add_share_cmd() || !*lp_add_share_cmd()) {
1358 DEBUG(10,("_srv_net_share_add: No add share command\n"));
1359 return ERROR_ACCESS_DENIED;
1362 switch (q_u->info_level) {
1364 /* Not enough info in a level 1 to do anything. */
1365 return ERROR_ACCESS_DENIED;
1367 unistr2_to_ascii(share_name, &q_u->info.share.info2.info_2_str.uni_netname, sizeof(share_name));
1368 unistr2_to_ascii(comment, &q_u->info.share.info2.info_2_str.uni_remark, sizeof(share_name));
1369 unistr2_to_ascii(pathname, &q_u->info.share.info2.info_2_str.uni_path, sizeof(share_name));
1370 type = q_u->info.share.info2.info_2.type;
1373 unistr2_to_ascii(share_name, &q_u->info.share.info502.info_502_str.uni_netname, sizeof(share_name));
1374 unistr2_to_ascii(comment, &q_u->info.share.info502.info_502_str.uni_remark, sizeof(share_name));
1375 unistr2_to_ascii(pathname, &q_u->info.share.info502.info_502_str.uni_path, sizeof(share_name));
1376 type = q_u->info.share.info502.info_502.type;
1377 psd = q_u->info.share.info502.info_502_str.sd;
1378 map_generic_share_sd_bits(psd);
1381 /* DFS only level. */
1382 return ERROR_ACCESS_DENIED;
1384 DEBUG(5,("_srv_net_share_add: unsupported switch value %d\n", q_u->info_level));
1385 return NT_STATUS_INVALID_INFO_CLASS;
1388 snum = find_service(share_name);
1390 /* Share already exists. */
1392 return ERRfilexists;
1394 /* We can only add disk shares. */
1395 if (type != STYPE_DISKTREE)
1396 return ERROR_ACCESS_DENIED;
1398 /* Check if the pathname is valid. */
1399 if (!(ptr = valid_share_pathname( pathname )))
1402 /* Ensure share name, pathname and comment don't contain '"' characters. */
1403 string_replace(share_name, '"', ' ');
1404 string_replace(ptr, '"', ' ');
1405 string_replace(comment, '"', ' ');
1407 slprintf(command, sizeof(command)-1, "%s \"%s\" \"%s\" \"%s\"",
1408 lp_add_share_cmd(), share_name, ptr, comment);
1409 dos_to_unix(command, True); /* Convert to unix-codepage */
1411 DEBUG(10,("_srv_net_share_add: Running [%s]\n", command ));
1412 if ((ret = smbrun(command, NULL, False)) != 0) {
1413 DEBUG(0,("_srv_net_share_add: Running [%s] returned (%d)\n", command, ret ));
1414 return ERROR_ACCESS_DENIED;
1418 if (!set_share_security(p->mem_ctx, share_name, psd))
1419 DEBUG(0,("_srv_net_share_add: Failed to add security info to share %s.\n",
1423 /* Tell everyone we updated smb.conf. */
1424 message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED, NULL, 0, False);
1427 * We don't call reload_services() here, the message will
1428 * cause this to be done before the next packet is read
1429 * from the client. JRA.
1432 DEBUG(5,("_srv_net_share_add: %d\n", __LINE__));
1434 return NT_STATUS_NOPROBLEMO;
1437 /*******************************************************************
1438 Net share delete. Call "delete share command" with the share name as
1440 ********************************************************************/
1442 uint32 _srv_net_share_del(pipes_struct *p, SRV_Q_NET_SHARE_DEL *q_u, SRV_R_NET_SHARE_DEL *r_u)
1444 struct current_user user;
1450 DEBUG(5,("_srv_net_share_del: %d\n", __LINE__));
1452 unistr2_to_ascii(share_name, &q_u->uni_share_name, sizeof(share_name));
1454 if (strequal(share_name,"IPC$") || strequal(share_name,"ADMIN$"))
1455 return ERROR_ACCESS_DENIED;
1457 snum = find_service(share_name);
1460 return ERRnosuchshare;
1462 /* No change to printer shares. */
1463 if (lp_print_ok(snum))
1464 return ERROR_ACCESS_DENIED;
1466 get_current_user(&user,p);
1469 return ERROR_ACCESS_DENIED;
1471 if (!lp_delete_share_cmd() || !*lp_delete_share_cmd())
1472 return ERROR_ACCESS_DENIED;
1474 slprintf(command, sizeof(command)-1, "%s \"%s\"", lp_delete_share_cmd(), lp_servicename(snum));
1475 dos_to_unix(command, True); /* Convert to unix-codepage */
1477 DEBUG(10,("_srv_net_share_del: Running [%s]\n", command ));
1478 if ((ret = smbrun(command, NULL, False)) != 0) {
1479 DEBUG(0,("_srv_net_share_del: Running [%s] returned (%d)\n", command, ret ));
1480 return ERROR_ACCESS_DENIED;
1483 /* Delete the SD in the database. */
1484 delete_share_security(snum);
1486 /* Tell everyone we updated smb.conf. */
1487 message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED, NULL, 0, False);
1489 lp_killservice(snum);
1491 return NT_STATUS_NOPROBLEMO;
1494 /*******************************************************************
1496 ********************************************************************/
1498 uint32 _srv_net_remote_tod(pipes_struct *p, SRV_Q_NET_REMOTE_TOD *q_u, SRV_R_NET_REMOTE_TOD *r_u)
1500 TIME_OF_DAY_INFO *tod;
1502 time_t unixdate = time(NULL);
1504 tod = (TIME_OF_DAY_INFO *)talloc(p->mem_ctx, sizeof(TIME_OF_DAY_INFO));
1506 return NT_STATUS_NO_MEMORY;
1511 r_u->ptr_srv_tod = 0x1;
1512 r_u->status = NT_STATUS_NOPROBLEMO;
1514 DEBUG(5,("_srv_net_remote_tod: %d\n", __LINE__));
1516 t = gmtime(&unixdate);
1519 init_time_of_day_info(tod,
1526 TimeDiff(unixdate)/60,
1533 DEBUG(5,("_srv_net_remote_tod: %d\n", __LINE__));