Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header.
[samba.git] / source3 / rpc_server / srv_reg_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) Hewlett-Packard Company           1999.
9  *  Copyright (C) Jeremy Allison                                        2001.
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *  
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *  
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 /* Implementation of registry functions. */
27
28 #include "includes.h"
29
30 struct reg_info
31 {
32     /* for use by \PIPE\winreg */
33     fstring name; /* name of registry key */
34 };
35
36 static void free_reg_info(void *ptr)
37 {
38         struct reg_info *info = (struct reg_info *)ptr;
39
40         SAFE_FREE(info);
41 }
42
43 /*******************************************************************
44  reg_reply_unknown_1
45  ********************************************************************/
46
47 NTSTATUS _reg_close(pipes_struct *p, REG_Q_CLOSE *q_u, REG_R_CLOSE *r_u)
48 {
49         /* set up the REG unknown_1 response */
50         ZERO_STRUCT(r_u->pol);
51
52         /* close the policy handle */
53         if (!close_policy_hnd(p, &q_u->pol))
54                 return NT_STATUS_OBJECT_NAME_INVALID;
55
56         return NT_STATUS_OK;
57 }
58
59 /*******************************************************************
60  reg_reply_open
61  ********************************************************************/
62
63 NTSTATUS _reg_open(pipes_struct *p, REG_Q_OPEN_HKLM *q_u, REG_R_OPEN_HKLM *r_u)
64 {
65         if (!create_policy_hnd(p, &r_u->pol, free_reg_info, NULL))
66                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
67
68         return NT_STATUS_OK;
69 }
70
71 /*******************************************************************
72  reg_reply_open_entry
73  ********************************************************************/
74
75 NTSTATUS _reg_open_entry(pipes_struct *p, REG_Q_OPEN_ENTRY *q_u, REG_R_OPEN_ENTRY *r_u)
76 {
77         POLICY_HND pol;
78         fstring name;
79         struct reg_info *info = NULL;
80
81         DEBUG(5,("reg_open_entry: %d\n", __LINE__));
82
83         if (!find_policy_by_hnd(p, &q_u->pol, NULL))
84                 return NT_STATUS_INVALID_HANDLE;
85
86         rpcstr_pull(name,q_u->uni_name.buffer,sizeof(name),q_u->uni_name.uni_str_len*2,0);
87
88         DEBUG(5,("reg_open_entry: %s\n", name));
89
90         /* lkcl XXXX do a check on the name, here */
91         if (!strequal(name, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions") &&
92             !strequal(name, "System\\CurrentControlSet\\services\\Netlogon\\parameters\\"))
93                         return NT_STATUS_ACCESS_DENIED;
94
95         if ((info = (struct reg_info *)malloc(sizeof(struct reg_info))) == NULL)
96                 return NT_STATUS_NO_MEMORY;
97
98         ZERO_STRUCTP(info);
99         fstrcpy(info->name, name);
100
101         if (!create_policy_hnd(p, &pol, free_reg_info, (void *)info))
102                 return NT_STATUS_TOO_MANY_SECRETS; /* ha ha very droll */
103
104         init_reg_r_open_entry(r_u, &pol, NT_STATUS_OK);
105
106         DEBUG(5,("reg_open_entry: %d\n", __LINE__));
107
108         return r_u->status;
109 }
110
111 /*******************************************************************
112  reg_reply_info
113  ********************************************************************/
114
115 NTSTATUS _reg_info(pipes_struct *p, REG_Q_INFO *q_u, REG_R_INFO *r_u)
116 {
117         NTSTATUS status = NT_STATUS_OK;
118         char *key = NULL;
119         uint32 type=0x1; /* key type: REG_SZ */
120
121         UNISTR2 *uni_key = NULL;
122         BUFFER2 *buf = NULL;
123         fstring name;
124
125         DEBUG(5,("_reg_info: %d\n", __LINE__));
126
127         if (find_policy_by_hnd(p, &q_u->pol, NULL) == -1)
128                 return NT_STATUS_INVALID_HANDLE;
129
130         rpcstr_pull(name, q_u->uni_type.buffer, sizeof(name), q_u->uni_type.uni_str_len*2, 0);
131
132         DEBUG(5,("reg_info: checking key: %s\n", name));
133
134         uni_key = (UNISTR2 *)talloc_zero(p->mem_ctx, sizeof(UNISTR2));
135         buf = (BUFFER2 *)talloc_zero(p->mem_ctx, sizeof(BUFFER2));
136
137         if (!uni_key || !buf)
138                 return NT_STATUS_NO_MEMORY;
139
140         if ( strequal(name, "RefusePasswordChange") ) {
141                 type=0xF770;
142                 status = NT_STATUS_NO_SUCH_FILE;
143                 init_unistr2(uni_key, "", 0);
144                 init_buffer2(buf, (uint8*) uni_key->buffer, uni_key->uni_str_len*2);
145                 
146                 buf->buf_max_len=4;
147
148                 goto out;
149         }
150
151         switch (lp_server_role()) {
152         case ROLE_DOMAIN_PDC:
153         case ROLE_DOMAIN_BDC:
154                 key = "LanmanNT";
155                 break;
156         case ROLE_STANDALONE:
157                 key = "ServerNT";
158                 break;
159         case ROLE_DOMAIN_MEMBER:
160                 key = "WinNT";
161                 break;
162         }
163
164         /* This makes the server look like a member server to clients */
165         /* which tells clients that we have our own local user and    */
166         /* group databases and helps with ACL support.                */
167
168         init_unistr2(uni_key, key, strlen(key)+1);
169         init_buffer2(buf, (uint8*)uni_key->buffer, uni_key->uni_str_len*2);
170   
171  out:
172         init_reg_r_info(q_u->ptr_buf, r_u, buf, type, status);
173
174         DEBUG(5,("reg_open_entry: %d\n", __LINE__));
175
176         return status;
177 }
178
179 /*******************************************************************
180  reg_shutdwon
181  ********************************************************************/
182
183 #define SHUTDOWN_R_STRING "-r"
184 #define SHUTDOWN_F_STRING "-f"
185
186
187 NTSTATUS _reg_shutdown(pipes_struct *p, REG_Q_SHUTDOWN *q_u, REG_R_SHUTDOWN *r_u)
188 {
189         NTSTATUS status = NT_STATUS_OK;
190         pstring shutdown_script;
191         UNISTR2 unimsg = q_u->uni_msg;
192         pstring message;
193         pstring chkmsg;
194         fstring timeout;
195         fstring r;
196         fstring f;
197         
198         /* message */
199         rpcstr_pull (message, unimsg.buffer, sizeof(message), unimsg.uni_str_len*2,0);
200                 /* security check */
201         alpha_strcpy (chkmsg, message, NULL, sizeof(message));
202         /* timeout */
203         snprintf(timeout, sizeof(timeout), "%d", q_u->timeout);
204         /* reboot */
205         snprintf(r, sizeof(r), (q_u->flags & 0x100)?SHUTDOWN_R_STRING:"");
206         /* force */
207         snprintf(f, sizeof(f), (q_u->flags & 0x001)?SHUTDOWN_F_STRING:"");
208
209         pstrcpy(shutdown_script, lp_shutdown_script());
210
211         if(*shutdown_script) {
212                 int shutdown_ret;
213                 all_string_sub(shutdown_script, "%m", chkmsg, sizeof(shutdown_script));
214                 all_string_sub(shutdown_script, "%t", timeout, sizeof(shutdown_script));
215                 all_string_sub(shutdown_script, "%r", r, sizeof(shutdown_script));
216                 all_string_sub(shutdown_script, "%f", f, sizeof(shutdown_script));
217                 shutdown_ret = smbrun(shutdown_script,NULL);
218                 DEBUG(3,("_reg_shutdown: Running the command `%s' gave %d\n",shutdown_script,shutdown_ret));
219         }
220
221         return status;
222 }
223
224 NTSTATUS _reg_abort_shutdown(pipes_struct *p, REG_Q_ABORT_SHUTDOWN *q_u, REG_R_ABORT_SHUTDOWN *r_u)
225 {
226         NTSTATUS status = NT_STATUS_OK;
227         pstring abort_shutdown_script;
228
229         pstrcpy(abort_shutdown_script, lp_abort_shutdown_script());
230
231         if(*abort_shutdown_script) {
232                 int abort_shutdown_ret;
233                 abort_shutdown_ret = smbrun(abort_shutdown_script,NULL);
234                 DEBUG(3,("_reg_abort_shutdown: Running the command `%s' gave %d\n",abort_shutdown_script,abort_shutdown_ret));
235         }
236
237         return status;
238 }