Removed version number from file header.
[ira/wip.git] / source3 / rpc_server / srv_reg_nt.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell              1992-1997,
5  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6  *  Copyright (C) Paul Ashton                       1997.
7  *  Copyright (C) Hewlett-Packard Company           1999.
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 /* Implementation of registry functions. */
26
27 #include "includes.h"
28
29 struct reg_info {
30         /* for use by \PIPE\winreg */
31         fstring name; /* name of registry key */
32 };
33
34 static void free_reg_info(void *ptr)
35 {
36         struct reg_info *info = (struct reg_info *)ptr;
37
38         SAFE_FREE(info);
39 }
40
41 /*******************************************************************
42  reg_reply_unknown_1
43  ********************************************************************/
44
45 NTSTATUS _reg_close(pipes_struct *p, REG_Q_CLOSE *q_u, REG_R_CLOSE *r_u)
46 {
47         /* set up the REG unknown_1 response */
48         ZERO_STRUCT(r_u->pol);
49
50         /* close the policy handle */
51         if (!close_policy_hnd(p, &q_u->pol))
52                 return NT_STATUS_OBJECT_NAME_INVALID;
53
54         return NT_STATUS_OK;
55 }
56
57 /*******************************************************************
58  reg_reply_open
59  ********************************************************************/
60
61 NTSTATUS _reg_open(pipes_struct *p, REG_Q_OPEN_HKLM *q_u, REG_R_OPEN_HKLM *r_u)
62 {
63         if (!create_policy_hnd(p, &r_u->pol, free_reg_info, NULL))
64                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
65
66         return NT_STATUS_OK;
67 }
68
69 /*******************************************************************
70  reg_reply_open_entry
71  ********************************************************************/
72
73 NTSTATUS _reg_open_entry(pipes_struct *p, REG_Q_OPEN_ENTRY *q_u, REG_R_OPEN_ENTRY *r_u)
74 {
75         POLICY_HND pol;
76         fstring name;
77         struct reg_info *info = NULL;
78
79         DEBUG(5,("reg_open_entry: %d\n", __LINE__));
80
81         if (!find_policy_by_hnd(p, &q_u->pol, NULL))
82                 return NT_STATUS_INVALID_HANDLE;
83
84         rpcstr_pull(name,q_u->uni_name.buffer,sizeof(name),q_u->uni_name.uni_str_len*2,0);
85
86         DEBUG(5,("reg_open_entry: %s\n", name));
87
88         /* lkcl XXXX do a check on the name, here */
89         if (!strequal(name, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions") &&
90             !strequal(name, "System\\CurrentControlSet\\services\\Netlogon\\parameters\\"))
91                         return NT_STATUS_ACCESS_DENIED;
92
93         if ((info = (struct reg_info *)malloc(sizeof(struct reg_info))) == NULL)
94                 return NT_STATUS_NO_MEMORY;
95
96         ZERO_STRUCTP(info);
97         fstrcpy(info->name, name);
98
99         if (!create_policy_hnd(p, &pol, free_reg_info, (void *)info))
100                 return NT_STATUS_TOO_MANY_SECRETS; /* ha ha very droll */
101
102         init_reg_r_open_entry(r_u, &pol, NT_STATUS_OK);
103
104         DEBUG(5,("reg_open_entry: %d\n", __LINE__));
105
106         return r_u->status;
107 }
108
109 /*******************************************************************
110  reg_reply_info
111  ********************************************************************/
112
113 NTSTATUS _reg_info(pipes_struct *p, REG_Q_INFO *q_u, REG_R_INFO *r_u)
114 {
115         NTSTATUS status = NT_STATUS_OK;
116         char *key = NULL;
117         uint32 type=0x1; /* key type: REG_SZ */
118
119         UNISTR2 *uni_key = NULL;
120         BUFFER2 *buf = NULL;
121         fstring name;
122
123         DEBUG(5,("_reg_info: %d\n", __LINE__));
124
125         if (!find_policy_by_hnd(p, &q_u->pol, NULL))
126                 return NT_STATUS_INVALID_HANDLE;
127
128         rpcstr_pull(name, q_u->uni_type.buffer, sizeof(name), q_u->uni_type.uni_str_len*2, 0);
129
130         DEBUG(5,("reg_info: checking key: %s\n", name));
131
132         uni_key = (UNISTR2 *)talloc_zero(p->mem_ctx, sizeof(UNISTR2));
133         buf = (BUFFER2 *)talloc_zero(p->mem_ctx, sizeof(BUFFER2));
134
135         if (!uni_key || !buf)
136                 return NT_STATUS_NO_MEMORY;
137
138         if ( strequal(name, "RefusePasswordChange") ) {
139                 type=0xF770;
140                 status = NT_STATUS_NO_SUCH_FILE;
141                 init_unistr2(uni_key, "", 0);
142                 init_buffer2(buf, (uint8*) uni_key->buffer, uni_key->uni_str_len*2);
143                 
144                 buf->buf_max_len=4;
145
146                 goto out;
147         }
148
149         switch (lp_server_role()) {
150         case ROLE_DOMAIN_PDC:
151         case ROLE_DOMAIN_BDC:
152                 key = "LanmanNT";
153                 break;
154         case ROLE_STANDALONE:
155                 key = "ServerNT";
156                 break;
157         case ROLE_DOMAIN_MEMBER:
158                 key = "WinNT";
159                 break;
160         }
161
162         /* This makes the server look like a member server to clients */
163         /* which tells clients that we have our own local user and    */
164         /* group databases and helps with ACL support.                */
165
166         init_unistr2(uni_key, key, strlen(key)+1);
167         init_buffer2(buf, (uint8*)uni_key->buffer, uni_key->uni_str_len*2);
168   
169  out:
170         init_reg_r_info(q_u->ptr_buf, r_u, buf, type, status);
171
172         DEBUG(5,("reg_open_entry: %d\n", __LINE__));
173
174         return status;
175 }
176
177 /*******************************************************************
178  reg_shutdwon
179  ********************************************************************/
180
181 #define SHUTDOWN_R_STRING "-r"
182 #define SHUTDOWN_F_STRING "-f"
183
184
185 NTSTATUS _reg_shutdown(pipes_struct *p, REG_Q_SHUTDOWN *q_u, REG_R_SHUTDOWN *r_u)
186 {
187         NTSTATUS status = NT_STATUS_OK;
188         pstring shutdown_script;
189         UNISTR2 unimsg = q_u->uni_msg;
190         pstring message;
191         pstring chkmsg;
192         fstring timeout;
193         fstring r;
194         fstring f;
195         
196         /* message */
197         rpcstr_pull (message, unimsg.buffer, sizeof(message), unimsg.uni_str_len*2,0);
198                 /* security check */
199         alpha_strcpy (chkmsg, message, NULL, sizeof(message));
200         /* timeout */
201         snprintf(timeout, sizeof(timeout), "%d", q_u->timeout);
202         /* reboot */
203         snprintf(r, sizeof(r), (q_u->flags & REG_REBOOT_ON_SHUTDOWN)?SHUTDOWN_R_STRING:"");
204         /* force */
205         snprintf(f, sizeof(f), (q_u->flags & REG_FORCE_SHUTDOWN)?SHUTDOWN_F_STRING:"");
206
207         pstrcpy(shutdown_script, lp_shutdown_script());
208
209         if(*shutdown_script) {
210                 int shutdown_ret;
211                 all_string_sub(shutdown_script, "%m", chkmsg, sizeof(shutdown_script));
212                 all_string_sub(shutdown_script, "%t", timeout, sizeof(shutdown_script));
213                 all_string_sub(shutdown_script, "%r", r, sizeof(shutdown_script));
214                 all_string_sub(shutdown_script, "%f", f, sizeof(shutdown_script));
215                 shutdown_ret = smbrun(shutdown_script,NULL);
216                 DEBUG(3,("_reg_shutdown: Running the command `%s' gave %d\n",shutdown_script,shutdown_ret));
217         }
218
219         return status;
220 }
221
222 NTSTATUS _reg_abort_shutdown(pipes_struct *p, REG_Q_ABORT_SHUTDOWN *q_u, REG_R_ABORT_SHUTDOWN *r_u)
223 {
224         NTSTATUS status = NT_STATUS_OK;
225         pstring abort_shutdown_script;
226
227         pstrcpy(abort_shutdown_script, lp_abort_shutdown_script());
228
229         if(*abort_shutdown_script) {
230                 int abort_shutdown_ret;
231                 abort_shutdown_ret = smbrun(abort_shutdown_script,NULL);
232                 DEBUG(3,("_reg_abort_shutdown: Running the command `%s' gave %d\n",abort_shutdown_script,abort_shutdown_ret));
233         }
234
235         return status;
236 }