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$", net_name) || strequal("ADMIN$", net_name))
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$", net_name) || strequal("ADMIN$", net_name))
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(connection_struct *conn, 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);
316 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;
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;
335 ret = se_access_check(psd, puser, desired_access, &granted, &status);
339 talloc_destroy(mem_ctx);
344 /*******************************************************************
345 Fill in a share info level 502 structure.
346 ********************************************************************/
348 static void init_srv_share_info_502(TALLOC_CTX *ctx, SRV_SHARE_INFO_502 *sh502, int snum)
361 pstrcpy(net_name, lp_servicename(snum));
362 pstrcpy(remark, lp_comment(snum));
363 pstring_sub(remark,"%S",lp_servicename(snum));
365 pstrcat(path, lp_pathname(snum));
367 len_net_name = strlen(net_name);
369 /* work out the share type */
370 type = STYPE_DISKTREE;
372 if (lp_print_ok(snum))
374 if (strequal("IPC$", net_name))
376 if (net_name[len_net_name] == '$')
377 type |= STYPE_HIDDEN;
379 sd = get_share_security(ctx, snum, &sd_size);
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);
385 /***************************************************************************
386 Fill in a share info level 1005 structure.
387 ***************************************************************************/
389 static void init_srv_share_info_1005(SRV_SHARE_INFO_1005* sh1005, int snum)
391 sh1005->dfs_root_flag = 0;
394 if(lp_host_msdfs() && lp_msdfs_root(snum))
395 sh1005->dfs_root_flag = 3;
400 /*******************************************************************
401 True if it ends in '$'.
402 ********************************************************************/
404 static BOOL is_admin_share(int snum)
408 pstrcpy(net_name, lp_servicename(snum));
409 return (net_name[strlen(net_name)] == '$') ? True : False;
412 /*******************************************************************
413 Fill in a share info structure.
414 ********************************************************************/
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)
420 int num_services = lp_numservices();
423 DEBUG(5,("init_srv_share_info_ctr\n"));
427 ctr->info_level = ctr->switch_value = info_level;
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)) )
436 *total_entries = num_entries;
437 ctr->num_entries2 = ctr->num_entries = num_entries;
438 ctr->ptr_share_info = ctr->ptr_entries = 1;
443 switch (info_level) {
446 SRV_SHARE_INFO_1 *info1;
449 info1 = talloc(ctx, num_entries * sizeof(SRV_SHARE_INFO_1));
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);
457 ctr->share.info1 = info1;
463 SRV_SHARE_INFO_2 *info2;
466 info2 = talloc(ctx, num_entries * sizeof(SRV_SHARE_INFO_2));
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);
474 ctr->share.info2 = info2;
480 SRV_SHARE_INFO_502 *info502;
483 info502 = talloc(ctx, num_entries * sizeof(SRV_SHARE_INFO_502));
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);
491 ctr->share.info502 = info502;
496 DEBUG(5,("init_srv_share_info_ctr: unsupported switch value %d\n", info_level));
503 /*******************************************************************
504 Inits a SRV_R_NET_SHARE_ENUM structure.
505 ********************************************************************/
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)
510 DEBUG(5,("init_srv_r_net_share_enum: %d\n", __LINE__));
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;
516 r_n->status = NT_STATUS_INVALID_INFO_CLASS;
519 init_enum_hnd(&r_n->enum_hnd, resume_hnd);
522 /*******************************************************************
523 Inits a SRV_R_NET_SHARE_GET_INFO structure.
524 ********************************************************************/
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)
529 uint32 status = NT_STATUS_NOPROBLEMO;
532 DEBUG(5,("init_srv_r_net_share_get_info: %d\n", __LINE__));
534 r_n->info.switch_value = info_level;
536 snum = find_service(share_name);
539 switch (info_level) {
541 init_srv_share_info_1(&r_n->info.share.info1, snum);
544 init_srv_share_info_2(&r_n->info.share.info2, snum);
547 init_srv_share_info_502(ctx, &r_n->info.share.info502, snum);
550 init_srv_share_info_1005(&r_n->info.share.info1005, snum);
553 DEBUG(5,("init_srv_net_share_get_info: unsupported switch value %d\n", info_level));
554 status = NT_STATUS_INVALID_INFO_CLASS;
558 status = NT_STATUS_BAD_NETWORK_NAME;
561 r_n->info.ptr_share_ctr = (status == NT_STATUS_NOPROBLEMO) ? 1 : 0;
562 r_n->status = status;
565 /*******************************************************************
566 fill in a sess info level 1 structure.
567 ********************************************************************/
569 static void init_srv_sess_0_info(SESS_INFO_0 *se0, SESS_INFO_0_STR *str0, char *name)
571 init_srv_sess_info0(se0, name);
572 init_srv_sess_info0_str(str0, name);
575 /*******************************************************************
576 fill in a sess info level 0 structure.
577 ********************************************************************/
579 static void init_srv_sess_info_0(SRV_SESS_INFO_0 *ss0, uint32 *snum, uint32 *stot)
581 uint32 num_entries = 0;
589 DEBUG(5,("init_srv_sess_0_ss0\n"));
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");
596 /* move on to creating next session */
597 /* move on to creating next sess */
601 ss0->num_entries_read = num_entries;
602 ss0->ptr_sess_info = num_entries > 0 ? 1 : 0;
603 ss0->num_entries_read2 = num_entries;
605 if ((*snum) >= (*stot)) {
610 ss0->num_entries_read = 0;
611 ss0->ptr_sess_info = 0;
612 ss0->num_entries_read2 = 0;
616 /*******************************************************************
617 fill in a sess info level 1 structure.
618 ********************************************************************/
620 static void init_srv_sess_1_info(SESS_INFO_1 *se1, SESS_INFO_1_STR *str1,
621 char *name, char *user,
623 uint32 open_time, uint32 idle_time,
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);
630 /*******************************************************************
631 fill in a sess info level 1 structure.
632 ********************************************************************/
634 static void init_srv_sess_info_1(SRV_SESS_INFO_1 *ss1, uint32 *snum, uint32 *stot)
636 uint32 num_entries = 0;
644 DEBUG(5,("init_srv_sess_1_ss1\n"));
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);
652 /* move on to creating next session */
653 /* move on to creating next sess */
657 ss1->num_entries_read = num_entries;
658 ss1->ptr_sess_info = num_entries > 0 ? 1 : 0;
659 ss1->num_entries_read2 = num_entries;
661 if ((*snum) >= (*stot)) {
666 ss1->num_entries_read = 0;
667 ss1->ptr_sess_info = 0;
668 ss1->num_entries_read2 = 0;
674 /*******************************************************************
675 makes a SRV_R_NET_SESS_ENUM structure.
676 ********************************************************************/
678 static uint32 init_srv_sess_info_ctr(SRV_SESS_INFO_CTR *ctr,
679 int switch_value, uint32 *resume_hnd, uint32 *total_entries)
681 uint32 status = NT_STATUS_NOPROBLEMO;
682 DEBUG(5,("init_srv_sess_info_ctr: %d\n", __LINE__));
684 ctr->switch_value = switch_value;
686 switch (switch_value) {
688 init_srv_sess_info_0(&(ctr->sess.info0), resume_hnd, total_entries);
689 ctr->ptr_sess_ctr = 1;
692 init_srv_sess_info_1(&(ctr->sess.info1), resume_hnd, total_entries);
693 ctr->ptr_sess_ctr = 1;
696 DEBUG(5,("init_srv_sess_info_ctr: unsupported switch value %d\n", switch_value));
698 (*total_entries) = 0;
699 ctr->ptr_sess_ctr = 0;
700 status = NT_STATUS_INVALID_INFO_CLASS;
707 /*******************************************************************
708 makes a SRV_R_NET_SESS_ENUM structure.
709 ********************************************************************/
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)
714 DEBUG(5,("init_srv_r_net_sess_enum: %d\n", __LINE__));
716 r_n->sess_level = sess_level;
718 if (sess_level == -1)
719 r_n->status = NT_STATUS_INVALID_INFO_CLASS;
721 r_n->status = init_srv_sess_info_ctr(r_n->ctr, switch_value, &resume_hnd, &r_n->total_entries);
723 if (r_n->status != NT_STATUS_NOPROBLEMO)
726 init_enum_hnd(&r_n->enum_hnd, resume_hnd);
729 /*******************************************************************
730 fill in a conn info level 0 structure.
731 ********************************************************************/
733 static void init_srv_conn_info_0(SRV_CONN_INFO_0 *ss0, uint32 *snum, uint32 *stot)
735 uint32 num_entries = 0;
743 DEBUG(5,("init_srv_conn_0_ss0\n"));
746 for (; (*snum) < (*stot) && num_entries < MAX_CONN_ENTRIES; (*snum)++) {
748 init_srv_conn_info0(&ss0->info_0[num_entries], (*stot));
750 /* move on to creating next connection */
751 /* move on to creating next conn */
755 ss0->num_entries_read = num_entries;
756 ss0->ptr_conn_info = num_entries > 0 ? 1 : 0;
757 ss0->num_entries_read2 = num_entries;
759 if ((*snum) >= (*stot)) {
764 ss0->num_entries_read = 0;
765 ss0->ptr_conn_info = 0;
766 ss0->num_entries_read2 = 0;
772 /*******************************************************************
773 fill in a conn info level 1 structure.
774 ********************************************************************/
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)
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);
785 /*******************************************************************
786 fill in a conn info level 1 structure.
787 ********************************************************************/
789 static void init_srv_conn_info_1(SRV_CONN_INFO_1 *ss1, uint32 *snum, uint32 *stot)
791 uint32 num_entries = 0;
799 DEBUG(5,("init_srv_conn_1_ss1\n"));
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$");
807 /* move on to creating next connection */
808 /* move on to creating next conn */
812 ss1->num_entries_read = num_entries;
813 ss1->ptr_conn_info = num_entries > 0 ? 1 : 0;
814 ss1->num_entries_read2 = num_entries;
817 if ((*snum) >= (*stot)) {
822 ss1->num_entries_read = 0;
823 ss1->ptr_conn_info = 0;
824 ss1->num_entries_read2 = 0;
830 /*******************************************************************
831 makes a SRV_R_NET_CONN_ENUM structure.
832 ********************************************************************/
834 static uint32 init_srv_conn_info_ctr(SRV_CONN_INFO_CTR *ctr,
835 int switch_value, uint32 *resume_hnd, uint32 *total_entries)
837 uint32 status = NT_STATUS_NOPROBLEMO;
838 DEBUG(5,("init_srv_conn_info_ctr: %d\n", __LINE__));
840 ctr->switch_value = switch_value;
842 switch (switch_value) {
844 init_srv_conn_info_0(&ctr->conn.info0, resume_hnd, total_entries);
845 ctr->ptr_conn_ctr = 1;
848 init_srv_conn_info_1(&ctr->conn.info1, resume_hnd, total_entries);
849 ctr->ptr_conn_ctr = 1;
852 DEBUG(5,("init_srv_conn_info_ctr: unsupported switch value %d\n", switch_value));
854 (*total_entries) = 0;
855 ctr->ptr_conn_ctr = 0;
856 status = NT_STATUS_INVALID_INFO_CLASS;
863 /*******************************************************************
864 makes a SRV_R_NET_CONN_ENUM structure.
865 ********************************************************************/
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)
870 DEBUG(5,("init_srv_r_net_conn_enum: %d\n", __LINE__));
872 r_n->conn_level = conn_level;
873 if (conn_level == -1)
874 r_n->status = NT_STATUS_INVALID_INFO_CLASS;
876 r_n->status = init_srv_conn_info_ctr(r_n->ctr, switch_value, &resume_hnd, &r_n->total_entries);
878 if (r_n->status != NT_STATUS_NOPROBLEMO)
881 init_enum_hnd(&r_n->enum_hnd, resume_hnd);
884 /*******************************************************************
885 fill in a file info level 3 structure.
886 ********************************************************************/
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)
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);
896 /*******************************************************************
897 fill in a file info level 3 structure.
898 ********************************************************************/
900 static void init_srv_file_info_3(SRV_FILE_INFO_3 *fl3, uint32 *fnum, uint32 *ftot)
902 uint32 num_entries = 0;
910 DEBUG(5,("init_srv_file_3_fl3\n"));
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");
917 /* move on to creating next file */
921 fl3->num_entries_read = num_entries;
922 fl3->ptr_file_info = num_entries > 0 ? 1 : 0;
923 fl3->num_entries_read2 = num_entries;
925 if ((*fnum) >= (*ftot)) {
930 /*******************************************************************
931 makes a SRV_R_NET_FILE_ENUM structure.
932 ********************************************************************/
934 static uint32 init_srv_file_info_ctr(SRV_FILE_INFO_CTR *ctr,
935 int switch_value, uint32 *resume_hnd, uint32 *total_entries)
937 uint32 status = NT_STATUS_NOPROBLEMO;
938 DEBUG(5,("init_srv_file_info_ctr: %d\n", __LINE__));
940 ctr->switch_value = switch_value;
942 switch (switch_value) {
944 init_srv_file_info_3(&ctr->file.info3, resume_hnd, total_entries);
945 ctr->ptr_file_ctr = 1;
948 DEBUG(5,("init_srv_file_info_ctr: unsupported switch value %d\n", switch_value));
950 (*total_entries) = 0;
951 ctr->ptr_file_ctr = 0;
952 status = NT_STATUS_INVALID_INFO_CLASS;
959 /*******************************************************************
960 makes a SRV_R_NET_FILE_ENUM structure.
961 ********************************************************************/
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)
966 DEBUG(5,("init_srv_r_net_file_enum: %d\n", __LINE__));
968 r_n->file_level = file_level;
970 r_n->status = NT_STATUS_INVALID_INFO_CLASS;
972 r_n->status = init_srv_file_info_ctr(r_n->ctr, switch_value, &resume_hnd, &(r_n->total_entries));
974 if (r_n->status != NT_STATUS_NOPROBLEMO)
977 init_enum_hnd(&r_n->enum_hnd, resume_hnd);
980 /*******************************************************************
982 ********************************************************************/
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)
986 uint32 status = NT_STATUS_NOPROBLEMO;
987 SRV_INFO_CTR *ctr = (SRV_INFO_CTR *)talloc(p->mem_ctx, sizeof(SRV_INFO_CTR));
990 return NT_STATUS_NO_MEMORY;
994 DEBUG(5,("srv_net_srv_get_info: %d\n", __LINE__));
996 switch (q_u->switch_value) {
998 init_srv_info_102(&ctr->srv.sv102,
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 */
1007 3000, /* announce delta */
1008 100000, /* licenses */
1009 "c:\\"); /* user path */
1012 init_srv_info_101(&ctr->srv.sv101,
1014 lp_major_announce_version(), lp_minor_announce_version(),
1015 lp_default_server_announce(),
1016 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH));
1019 init_srv_info_100(&ctr->srv.sv100, 500, global_myname);
1022 status = NT_STATUS_INVALID_INFO_CLASS;
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);
1029 DEBUG(5,("srv_net_srv_get_info: %d\n", __LINE__));
1034 /*******************************************************************
1036 ********************************************************************/
1038 uint32 _srv_net_file_enum(pipes_struct *p, SRV_Q_NET_FILE_ENUM *q_u, SRV_R_NET_FILE_ENUM *r_u)
1040 r_u->ctr = (SRV_FILE_INFO_CTR *)talloc(p->mem_ctx, sizeof(SRV_FILE_INFO_CTR));
1042 return NT_STATUS_NO_MEMORY;
1044 ZERO_STRUCTP(r_u->ctr);
1046 DEBUG(5,("srv_net_file_enum: %d\n", __LINE__));
1049 init_srv_r_net_file_enum(r_u,
1050 get_enum_hnd(&q_u->enum_hnd),
1052 q_u->ctr->switch_value);
1054 DEBUG(5,("srv_net_file_enum: %d\n", __LINE__));
1059 /*******************************************************************
1061 ********************************************************************/
1063 uint32 _srv_net_conn_enum(pipes_struct *p, SRV_Q_NET_CONN_ENUM *q_u, SRV_R_NET_CONN_ENUM *r_u)
1065 DEBUG(5,("srv_net_conn_enum: %d\n", __LINE__));
1067 r_u->ctr = (SRV_CONN_INFO_CTR *)talloc(p->mem_ctx, sizeof(SRV_CONN_INFO_CTR));
1069 return NT_STATUS_NO_MEMORY;
1071 ZERO_STRUCTP(r_u->ctr);
1074 init_srv_r_net_conn_enum(r_u,
1075 get_enum_hnd(&q_u->enum_hnd),
1077 q_u->ctr->switch_value);
1079 DEBUG(5,("srv_net_conn_enum: %d\n", __LINE__));
1084 /*******************************************************************
1086 ********************************************************************/
1088 uint32 _srv_net_sess_enum(pipes_struct *p, SRV_Q_NET_SESS_ENUM *q_u, SRV_R_NET_SESS_ENUM *r_u)
1090 DEBUG(5,("_srv_net_sess_enum: %d\n", __LINE__));
1092 r_u->ctr = (SRV_SESS_INFO_CTR *)talloc(p->mem_ctx, sizeof(SRV_SESS_INFO_CTR));
1094 return NT_STATUS_NO_MEMORY;
1096 ZERO_STRUCTP(r_u->ctr);
1099 init_srv_r_net_sess_enum(r_u,
1100 get_enum_hnd(&q_u->enum_hnd),
1102 q_u->ctr->switch_value);
1104 DEBUG(5,("_srv_net_sess_enum: %d\n", __LINE__));
1109 /*******************************************************************
1111 ********************************************************************/
1113 uint32 _srv_net_share_enum_all(pipes_struct *p, SRV_Q_NET_SHARE_ENUM *q_u, SRV_R_NET_SHARE_ENUM *r_u)
1115 DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
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);
1122 DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
1127 /*******************************************************************
1129 ********************************************************************/
1131 uint32 _srv_net_share_enum(pipes_struct *p, SRV_Q_NET_SHARE_ENUM *q_u, SRV_R_NET_SHARE_ENUM *r_u)
1133 DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
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);
1140 DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
1145 /*******************************************************************
1147 ********************************************************************/
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)
1153 DEBUG(5,("_srv_net_share_get_info: %d\n", __LINE__));
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);
1159 DEBUG(5,("_srv_net_share_get_info: %d\n", __LINE__));
1164 /*******************************************************************
1165 Check a given DOS pathname is valid for a share.
1166 ********************************************************************/
1168 static char *valid_share_pathname(char *dos_pathname)
1170 pstring saved_pathname;
1171 pstring unix_pathname;
1175 /* Convert any '\' paths to '/' */
1176 unix_format(dos_pathname);
1177 unix_clean_name(dos_pathname);
1179 /* NT is braindead - it wants a C: prefix to a pathname ! So strip it. */
1181 if (strlen(dos_pathname) > 2 && ptr[1] == ':' && ptr[0] != '/')
1184 /* Only abolute paths allowed. */
1188 /* Can we cd to it ? */
1190 /* First save our current directory. */
1191 if (getcwd(saved_pathname, sizeof(saved_pathname)) == NULL)
1194 /* Convert to UNIX charset. */
1195 pstrcpy(unix_pathname, ptr);
1196 dos_to_unix(unix_pathname, True);
1198 ret = chdir(unix_pathname);
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");
1204 return (ret != -1) ? ptr : NULL;
1207 /*******************************************************************
1208 Net share set info. Modify share details.
1209 ********************************************************************/
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)
1213 struct current_user user;
1222 SEC_DESC *psd = NULL;
1224 DEBUG(5,("_srv_net_share_set_info: %d\n", __LINE__));
1226 unistr2_to_ascii(share_name, &q_u->uni_share_name, sizeof(share_name));
1228 r_u->switch_value = 0;
1230 if (strequal(share_name,"IPC$") || strequal(share_name,"ADMIN$") || strequal(share_name,"global"))
1231 return ERROR_ACCESS_DENIED;
1233 snum = find_service(share_name);
1235 /* Does this share exist ? */
1237 return ERRnosuchshare;
1239 /* No change to printer shares. */
1240 if (lp_print_ok(snum))
1241 return ERROR_ACCESS_DENIED;
1243 get_current_user(&user,p);
1246 return ERROR_ACCESS_DENIED;
1248 switch (q_u->info_level) {
1250 /* Not enough info in a level 1 to do anything. */
1251 return ERROR_ACCESS_DENIED;
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;
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);
1266 return ERROR_ACCESS_DENIED;
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;
1275 DEBUG(5,("_srv_net_share_set_info: unsupported switch value %d\n", q_u->info_level));
1276 return NT_STATUS_INVALID_INFO_CLASS;
1279 /* We can only modify disk shares. */
1280 if (type != STYPE_DISKTREE)
1281 return ERROR_ACCESS_DENIED;
1283 /* Check if the pathname is valid. */
1284 if (!(ptr = valid_share_pathname( pathname )))
1287 /* Ensure share name, pathname and comment don't contain '"' characters. */
1288 string_replace(share_name, '"', ' ');
1289 string_replace(ptr, '"', ' ');
1290 string_replace(comment, '"', ' ');
1292 DEBUG(10,("_srv_net_share_set_info: change share command = %s\n",
1293 lp_change_share_cmd() ? lp_change_share_cmd() : "NULL" ));
1295 /* Only call modify function if something changed. */
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;
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 */
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;
1311 /* Tell everyone we updated smb.conf. */
1312 message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED, NULL, 0, False);
1315 DEBUG(10,("_srv_net_share_set_info: No change to share name (%s)\n", share_name ));
1318 /* Replace SD if changed. */
1323 old_sd = get_share_security(p->mem_ctx, snum, &sd_size);
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",
1332 DEBUG(5,("_srv_net_share_set_info: %d\n", __LINE__));
1334 return NT_STATUS_NOPROBLEMO;
1337 /*******************************************************************
1338 Net share add. Call 'add_share_command "sharename" "pathname" "comment" "read only = xxx"'
1339 ********************************************************************/
1341 uint32 _srv_net_share_add(pipes_struct *p, SRV_Q_NET_SHARE_ADD *q_u, SRV_R_NET_SHARE_ADD *r_u)
1343 struct current_user user;
1352 SEC_DESC *psd = NULL;
1354 DEBUG(5,("_srv_net_share_add: %d\n", __LINE__));
1356 r_u->switch_value = 0;
1358 get_current_user(&user,p);
1360 if (user.uid != 0) {
1361 DEBUG(10,("_srv_net_share_add: uid != 0. Access denied.\n"));
1362 return ERROR_ACCESS_DENIED;
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;
1370 switch (q_u->info_level) {
1372 /* Not enough info in a level 1 to do anything. */
1373 return ERROR_ACCESS_DENIED;
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;
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);
1389 /* DFS only level. */
1390 return ERROR_ACCESS_DENIED;
1392 DEBUG(5,("_srv_net_share_add: unsupported switch value %d\n", q_u->info_level));
1393 return NT_STATUS_INVALID_INFO_CLASS;
1396 if (strequal(share_name,"IPC$") || strequal(share_name,"ADMIN$") || strequal(share_name,"global"))
1397 return ERROR_ACCESS_DENIED;
1399 snum = find_service(share_name);
1401 /* Share already exists. */
1403 return ERRfilexists;
1405 /* We can only add disk shares. */
1406 if (type != STYPE_DISKTREE)
1407 return ERROR_ACCESS_DENIED;
1409 /* Check if the pathname is valid. */
1410 if (!(ptr = valid_share_pathname( pathname )))
1413 /* Ensure share name, pathname and comment don't contain '"' characters. */
1414 string_replace(share_name, '"', ' ');
1415 string_replace(ptr, '"', ' ');
1416 string_replace(comment, '"', ' ');
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 */
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;
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",
1434 /* Tell everyone we updated smb.conf. */
1435 message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED, NULL, 0, False);
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.
1443 DEBUG(5,("_srv_net_share_add: %d\n", __LINE__));
1445 return NT_STATUS_NOPROBLEMO;
1448 /*******************************************************************
1449 Net share delete. Call "delete share command" with the share name as
1451 ********************************************************************/
1453 uint32 _srv_net_share_del(pipes_struct *p, SRV_Q_NET_SHARE_DEL *q_u, SRV_R_NET_SHARE_DEL *r_u)
1455 struct current_user user;
1461 DEBUG(5,("_srv_net_share_del: %d\n", __LINE__));
1463 unistr2_to_ascii(share_name, &q_u->uni_share_name, sizeof(share_name));
1465 if (strequal(share_name,"IPC$") || strequal(share_name,"ADMIN$") || strequal(share_name,"global"))
1466 return ERROR_ACCESS_DENIED;
1468 snum = find_service(share_name);
1471 return ERRnosuchshare;
1473 /* No change to printer shares. */
1474 if (lp_print_ok(snum))
1475 return ERROR_ACCESS_DENIED;
1477 get_current_user(&user,p);
1480 return ERROR_ACCESS_DENIED;
1482 if (!lp_delete_share_cmd() || !*lp_delete_share_cmd())
1483 return ERROR_ACCESS_DENIED;
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 */
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;
1495 /* Delete the SD in the database. */
1496 delete_share_security(snum);
1498 /* Tell everyone we updated smb.conf. */
1499 message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED, NULL, 0, False);
1501 lp_killservice(snum);
1503 return NT_STATUS_NOPROBLEMO;
1506 /*******************************************************************
1508 ********************************************************************/
1510 uint32 _srv_net_remote_tod(pipes_struct *p, SRV_Q_NET_REMOTE_TOD *q_u, SRV_R_NET_REMOTE_TOD *r_u)
1512 TIME_OF_DAY_INFO *tod;
1514 time_t unixdate = time(NULL);
1516 tod = (TIME_OF_DAY_INFO *)talloc(p->mem_ctx, sizeof(TIME_OF_DAY_INFO));
1518 return NT_STATUS_NO_MEMORY;
1523 r_u->ptr_srv_tod = 0x1;
1524 r_u->status = NT_STATUS_NOPROBLEMO;
1526 DEBUG(5,("_srv_net_remote_tod: %d\n", __LINE__));
1528 t = gmtime(&unixdate);
1531 init_time_of_day_info(tod,
1538 TimeDiff(unixdate)/60,
1545 DEBUG(5,("_srv_net_remote_tod: %d\n", __LINE__));