server: Prevent unloading a registry hive while the key is in use.
[wine.git] / server / winstation.c
blob1c7552f06871106ee351e29c6ca8aee5cbd7e58f
1 /*
2 * Server-side window stations and desktops handling
4 * Copyright (C) 2002, 2005 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdio.h>
25 #include <stdarg.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winternl.h"
34 #include "object.h"
35 #include "handle.h"
36 #include "request.h"
37 #include "process.h"
38 #include "user.h"
39 #include "file.h"
40 #include "security.h"
42 #define DESKTOP_ALL_ACCESS 0x01ff
44 static struct list winstation_list = LIST_INIT(winstation_list);
46 static void winstation_dump( struct object *obj, int verbose );
47 static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
48 static struct object *winstation_lookup_name( struct object *obj, struct unicode_str *name,
49 unsigned int attr, struct object *root );
50 static void winstation_destroy( struct object *obj );
51 static void desktop_dump( struct object *obj, int verbose );
52 static int desktop_link_name( struct object *obj, struct object_name *name, struct object *parent );
53 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
54 static void desktop_destroy( struct object *obj );
56 static const WCHAR winstation_name[] = {'W','i','n','d','o','w','S','t','a','t','i','o','n'};
58 struct type_descr winstation_type =
60 { winstation_name, sizeof(winstation_name) }, /* name */
61 STANDARD_RIGHTS_REQUIRED | WINSTA_ALL_ACCESS, /* valid_access */
62 { /* mapping */
63 STANDARD_RIGHTS_READ | WINSTA_READSCREEN | WINSTA_ENUMERATE | WINSTA_READATTRIBUTES | WINSTA_ENUMDESKTOPS,
64 STANDARD_RIGHTS_WRITE | WINSTA_WRITEATTRIBUTES | WINSTA_CREATEDESKTOP | WINSTA_ACCESSCLIPBOARD,
65 STANDARD_RIGHTS_EXECUTE | WINSTA_EXITWINDOWS | WINSTA_ACCESSGLOBALATOMS,
66 STANDARD_RIGHTS_REQUIRED | WINSTA_ALL_ACCESS
70 static const struct object_ops winstation_ops =
72 sizeof(struct winstation), /* size */
73 &winstation_type, /* type */
74 winstation_dump, /* dump */
75 no_add_queue, /* add_queue */
76 NULL, /* remove_queue */
77 NULL, /* signaled */
78 NULL, /* satisfied */
79 no_signal, /* signal */
80 no_get_fd, /* get_fd */
81 default_map_access, /* map_access */
82 default_get_sd, /* get_sd */
83 default_set_sd, /* set_sd */
84 default_get_full_name, /* get_full_name */
85 winstation_lookup_name, /* lookup_name */
86 directory_link_name, /* link_name */
87 default_unlink_name, /* unlink_name */
88 no_open_file, /* open_file */
89 no_kernel_obj_list, /* get_kernel_obj_list */
90 winstation_close_handle, /* close_handle */
91 winstation_destroy /* destroy */
95 static const WCHAR desktop_name[] = {'D','e','s','k','t','o','p'};
97 struct type_descr desktop_type =
99 { desktop_name, sizeof(desktop_name) }, /* name */
100 STANDARD_RIGHTS_REQUIRED | DESKTOP_ALL_ACCESS, /* valid_access */
101 { /* mapping */
102 STANDARD_RIGHTS_READ | DESKTOP_ENUMERATE | DESKTOP_READOBJECTS,
103 STANDARD_RIGHTS_WRITE | DESKTOP_WRITEOBJECTS | DESKTOP_JOURNALPLAYBACK | DESKTOP_JOURNALRECORD
104 | DESKTOP_HOOKCONTROL | DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW,
105 STANDARD_RIGHTS_EXECUTE | DESKTOP_SWITCHDESKTOP,
106 STANDARD_RIGHTS_REQUIRED | DESKTOP_ALL_ACCESS
110 static const struct object_ops desktop_ops =
112 sizeof(struct desktop), /* size */
113 &desktop_type, /* type */
114 desktop_dump, /* dump */
115 no_add_queue, /* add_queue */
116 NULL, /* remove_queue */
117 NULL, /* signaled */
118 NULL, /* satisfied */
119 no_signal, /* signal */
120 no_get_fd, /* get_fd */
121 default_map_access, /* map_access */
122 default_get_sd, /* get_sd */
123 default_set_sd, /* set_sd */
124 default_get_full_name, /* get_full_name */
125 no_lookup_name, /* lookup_name */
126 desktop_link_name, /* link_name */
127 default_unlink_name, /* unlink_name */
128 no_open_file, /* open_file */
129 no_kernel_obj_list, /* get_kernel_obj_list */
130 desktop_close_handle, /* close_handle */
131 desktop_destroy /* destroy */
134 /* create a winstation object */
135 static struct winstation *create_winstation( struct object *root, const struct unicode_str *name,
136 unsigned int attr, unsigned int flags )
138 struct winstation *winstation;
140 if ((winstation = create_named_object( root, &winstation_ops, name, attr, NULL )))
142 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
144 /* initialize it if it didn't already exist */
145 winstation->flags = flags;
146 winstation->clipboard = NULL;
147 winstation->atom_table = NULL;
148 list_add_tail( &winstation_list, &winstation->entry );
149 list_init( &winstation->desktops );
150 if (!(winstation->desktop_names = create_namespace( 7 )))
152 release_object( winstation );
153 return NULL;
156 else clear_error();
158 return winstation;
161 static void winstation_dump( struct object *obj, int verbose )
163 struct winstation *winstation = (struct winstation *)obj;
165 fprintf( stderr, "Winstation flags=%x clipboard=%p atoms=%p\n",
166 winstation->flags, winstation->clipboard, winstation->atom_table );
169 static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
171 return (process->winstation != handle);
174 static struct object *winstation_lookup_name( struct object *obj, struct unicode_str *name,
175 unsigned int attr, struct object *root )
177 struct winstation *winstation = (struct winstation *)obj;
178 struct object *found;
180 assert( obj->ops == &winstation_ops );
182 if (!name) return NULL; /* open the winstation itself */
184 if (get_path_element( name->str, name->len ) < name->len) /* no backslash allowed in name */
186 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
187 return NULL;
190 if ((found = find_object( winstation->desktop_names, name, attr )))
191 name->len = 0;
193 return found;
196 static void winstation_destroy( struct object *obj )
198 struct winstation *winstation = (struct winstation *)obj;
200 list_remove( &winstation->entry );
201 if (winstation->clipboard) release_object( winstation->clipboard );
202 if (winstation->atom_table) release_object( winstation->atom_table );
203 free( winstation->desktop_names );
206 /* retrieve the process window station, checking the handle access rights */
207 struct winstation *get_process_winstation( struct process *process, unsigned int access )
209 return (struct winstation *)get_handle_obj( process, process->winstation,
210 access, &winstation_ops );
213 /* retrieve a pointer to a desktop object */
214 struct desktop *get_desktop_obj( struct process *process, obj_handle_t handle, unsigned int access )
216 return (struct desktop *)get_handle_obj( process, handle, access, &desktop_ops );
219 /* create a desktop object */
220 static struct desktop *create_desktop( const struct unicode_str *name, unsigned int attr,
221 unsigned int flags, struct winstation *winstation )
223 struct desktop *desktop;
225 if ((desktop = create_named_object( &winstation->obj, &desktop_ops, name, attr, NULL )))
227 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
229 /* initialize it if it didn't already exist */
230 desktop->flags = flags;
231 desktop->winstation = (struct winstation *)grab_object( winstation );
232 desktop->top_window = NULL;
233 desktop->msg_window = NULL;
234 desktop->global_hooks = NULL;
235 desktop->close_timeout = NULL;
236 desktop->foreground_input = NULL;
237 desktop->users = 0;
238 memset( &desktop->cursor, 0, sizeof(desktop->cursor) );
239 memset( desktop->keystate, 0, sizeof(desktop->keystate) );
240 list_add_tail( &winstation->desktops, &desktop->entry );
241 list_init( &desktop->hotkeys );
243 else clear_error();
245 return desktop;
248 static void desktop_dump( struct object *obj, int verbose )
250 struct desktop *desktop = (struct desktop *)obj;
252 fprintf( stderr, "Desktop flags=%x winstation=%p top_win=%p hooks=%p\n",
253 desktop->flags, desktop->winstation, desktop->top_window, desktop->global_hooks );
256 static int desktop_link_name( struct object *obj, struct object_name *name, struct object *parent )
258 struct winstation *winstation = (struct winstation *)parent;
260 if (parent->ops != &winstation_ops)
262 set_error( STATUS_OBJECT_TYPE_MISMATCH );
263 return 0;
265 if (get_path_element( name->name, name->len ) < name->len) /* no backslash allowed in name */
267 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
268 return 0;
270 namespace_add( winstation->desktop_names, name );
271 return 1;
274 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
276 struct thread *thread;
278 /* check if the handle is currently used by the process or one of its threads */
279 if (process->desktop == handle) return 0;
280 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
281 if (thread->desktop == handle) return 0;
282 return 1;
285 static void desktop_destroy( struct object *obj )
287 struct desktop *desktop = (struct desktop *)obj;
289 free_hotkeys( desktop, 0 );
290 if (desktop->top_window) destroy_window( desktop->top_window );
291 if (desktop->msg_window) destroy_window( desktop->msg_window );
292 if (desktop->global_hooks) release_object( desktop->global_hooks );
293 if (desktop->close_timeout) remove_timeout_user( desktop->close_timeout );
294 list_remove( &desktop->entry );
295 release_object( desktop->winstation );
298 /* retrieve the thread desktop, checking the handle access rights */
299 struct desktop *get_thread_desktop( struct thread *thread, unsigned int access )
301 return get_desktop_obj( thread->process, thread->desktop, access );
304 static void close_desktop_timeout( void *private )
306 struct desktop *desktop = private;
308 desktop->close_timeout = NULL;
309 unlink_named_object( &desktop->obj ); /* make sure no other process can open it */
310 post_desktop_message( desktop, WM_CLOSE, 0, 0 ); /* and signal the owner to quit */
313 /* add a user of the desktop and cancel the close timeout */
314 static void add_desktop_user( struct desktop *desktop )
316 desktop->users++;
317 if (desktop->close_timeout)
319 remove_timeout_user( desktop->close_timeout );
320 desktop->close_timeout = NULL;
324 /* remove a user of the desktop and start the close timeout if necessary */
325 static void remove_desktop_user( struct desktop *desktop )
327 assert( desktop->users > 0 );
328 desktop->users--;
330 /* if we have one remaining user, it has to be the manager of the desktop window */
331 if (desktop->users == 1 && get_top_window_owner( desktop ))
333 assert( !desktop->close_timeout );
334 desktop->close_timeout = add_timeout_user( -TICKS_PER_SEC, close_desktop_timeout, desktop );
338 /* set the process default desktop handle */
339 void set_process_default_desktop( struct process *process, struct desktop *desktop,
340 obj_handle_t handle )
342 struct thread *thread;
343 struct desktop *old_desktop;
345 if (process->desktop == handle) return; /* nothing to do */
347 if (!(old_desktop = get_desktop_obj( process, process->desktop, 0 ))) clear_error();
348 process->desktop = handle;
350 /* set desktop for threads that don't have one yet */
351 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
352 if (!thread->desktop) thread->desktop = handle;
354 if (!process->is_system && desktop != old_desktop)
356 add_desktop_user( desktop );
357 if (old_desktop) remove_desktop_user( old_desktop );
360 if (old_desktop) release_object( old_desktop );
363 /* connect a process to its window station */
364 void connect_process_winstation( struct process *process, struct thread *parent_thread,
365 struct process *parent_process )
367 struct winstation *winstation = NULL;
368 struct desktop *desktop = NULL;
369 obj_handle_t handle;
371 /* check for an inherited winstation handle (don't ask...) */
372 if ((handle = find_inherited_handle( process, &winstation_ops )))
374 winstation = (struct winstation *)get_handle_obj( process, handle, 0, &winstation_ops );
376 else if (parent_process->winstation)
378 handle = duplicate_handle( parent_process, parent_process->winstation,
379 process, 0, 0, DUPLICATE_SAME_ACCESS );
380 winstation = (struct winstation *)get_handle_obj( process, handle, 0, &winstation_ops );
382 if (!winstation) goto done;
383 process->winstation = handle;
385 if ((handle = find_inherited_handle( process, &desktop_ops )))
387 desktop = get_desktop_obj( process, handle, 0 );
388 if (!desktop || desktop->winstation != winstation) goto done;
390 else
392 if (parent_thread && parent_thread->desktop)
393 handle = parent_thread->desktop;
394 else if (parent_process->desktop)
395 handle = parent_process->desktop;
396 else
397 goto done;
399 desktop = get_desktop_obj( parent_process, handle, 0 );
401 if (!desktop || desktop->winstation != winstation) goto done;
403 handle = duplicate_handle( parent_process, handle, process, 0, 0, DUPLICATE_SAME_ACCESS );
405 if (handle) set_process_default_desktop( process, desktop, handle );
407 done:
408 if (desktop) release_object( desktop );
409 if (winstation) release_object( winstation );
410 clear_error();
413 /* close the desktop of a given process */
414 void close_process_desktop( struct process *process )
416 struct desktop *desktop;
418 if (process->desktop && (desktop = get_desktop_obj( process, process->desktop, 0 )))
420 remove_desktop_user( desktop );
421 release_object( desktop );
423 clear_error(); /* ignore errors */
426 /* close the desktop of a given thread */
427 void close_thread_desktop( struct thread *thread )
429 obj_handle_t handle = thread->desktop;
431 thread->desktop = 0;
432 if (handle) close_handle( thread->process, handle );
435 /* create a window station */
436 DECL_HANDLER(create_winstation)
438 struct winstation *winstation;
439 struct unicode_str name = get_req_unicode_str();
440 struct object *root = NULL;
442 reply->handle = 0;
443 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir ))) return;
445 if ((winstation = create_winstation( root, &name, req->attributes, req->flags )))
447 reply->handle = alloc_handle( current->process, winstation, req->access, req->attributes );
448 release_object( winstation );
450 if (root) release_object( root );
453 /* open a handle to a window station */
454 DECL_HANDLER(open_winstation)
456 struct unicode_str name = get_req_unicode_str();
458 reply->handle = open_object( current->process, req->rootdir, req->access,
459 &winstation_ops, &name, req->attributes );
463 /* close a window station */
464 DECL_HANDLER(close_winstation)
466 struct winstation *winstation;
468 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
469 0, &winstation_ops )))
471 if (close_handle( current->process, req->handle )) set_error( STATUS_ACCESS_DENIED );
472 release_object( winstation );
477 /* get the process current window station */
478 DECL_HANDLER(get_process_winstation)
480 reply->handle = current->process->winstation;
484 /* set the process current window station */
485 DECL_HANDLER(set_process_winstation)
487 struct winstation *winstation;
489 if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
490 0, &winstation_ops )))
492 /* FIXME: should we close the old one? */
493 current->process->winstation = req->handle;
494 release_object( winstation );
498 /* create a desktop */
499 DECL_HANDLER(create_desktop)
501 struct desktop *desktop;
502 struct winstation *winstation;
503 struct unicode_str name = get_req_unicode_str();
505 reply->handle = 0;
506 if (!name.len)
508 set_error( STATUS_INVALID_HANDLE );
509 return;
511 if ((winstation = get_process_winstation( current->process, WINSTA_CREATEDESKTOP )))
513 if ((desktop = create_desktop( &name, req->attributes, req->flags, winstation )))
515 reply->handle = alloc_handle( current->process, desktop, req->access, req->attributes );
516 release_object( desktop );
518 release_object( winstation );
522 /* open a handle to a desktop */
523 DECL_HANDLER(open_desktop)
525 struct winstation *winstation;
526 struct object *obj;
527 struct unicode_str name = get_req_unicode_str();
529 /* FIXME: check access rights */
530 if (!req->winsta)
531 winstation = get_process_winstation( current->process, 0 );
532 else
533 winstation = (struct winstation *)get_handle_obj( current->process, req->winsta, 0, &winstation_ops );
535 if (!winstation) return;
537 if ((obj = open_named_object( &winstation->obj, &desktop_ops, &name, req->attributes )))
539 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
540 release_object( obj );
543 release_object( winstation );
546 /* open a handle to current input desktop */
547 DECL_HANDLER(open_input_desktop)
549 /* FIXME: check access rights */
550 struct winstation *winstation = get_process_winstation( current->process, 0 );
551 struct desktop *desktop;
553 if (!winstation) return;
555 if (!(winstation->flags & WSF_VISIBLE))
557 set_error( STATUS_ILLEGAL_FUNCTION );
558 release_object( winstation );
559 return;
562 if ((desktop = get_desktop_obj( current->process, current->process->desktop, 0 )))
564 reply->handle = alloc_handle( current->process, desktop, req->access, req->attributes );
565 release_object( desktop );
567 release_object( winstation );
570 /* close a desktop */
571 DECL_HANDLER(close_desktop)
573 struct desktop *desktop;
575 /* make sure it is a desktop handle */
576 if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
577 0, &desktop_ops )))
579 if (close_handle( current->process, req->handle )) set_error( STATUS_DEVICE_BUSY );
580 release_object( desktop );
585 /* get the thread current desktop */
586 DECL_HANDLER(get_thread_desktop)
588 struct thread *thread;
590 if (!(thread = get_thread_from_id( req->tid ))) return;
591 reply->handle = thread->desktop;
592 release_object( thread );
596 /* set the thread current desktop */
597 DECL_HANDLER(set_thread_desktop)
599 struct desktop *old_desktop, *new_desktop;
600 struct winstation *winstation;
602 if (!(winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
603 return;
605 if (!(new_desktop = get_desktop_obj( current->process, req->handle, 0 )))
607 release_object( winstation );
608 return;
610 if (new_desktop->winstation != winstation)
612 set_error( STATUS_ACCESS_DENIED );
613 release_object( new_desktop );
614 release_object( winstation );
615 return;
618 /* check if we are changing to a new desktop */
620 if (!(old_desktop = get_desktop_obj( current->process, current->desktop, 0)))
621 clear_error(); /* ignore error */
623 /* when changing desktop, we can't have any users on the current one */
624 if (old_desktop != new_desktop && current->desktop_users > 0)
625 set_error( STATUS_DEVICE_BUSY );
626 else
627 current->desktop = req->handle; /* FIXME: should we close the old one? */
629 if (!current->process->desktop)
630 set_process_default_desktop( current->process, new_desktop, req->handle );
632 if (old_desktop != new_desktop && current->queue) detach_thread_input( current );
634 if (old_desktop) release_object( old_desktop );
635 release_object( new_desktop );
636 release_object( winstation );
640 /* get/set information about a user object (window station or desktop) */
641 DECL_HANDLER(set_user_object_info)
643 struct object *obj;
644 data_size_t len;
646 if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
648 if (obj->ops == &desktop_ops)
650 struct desktop *desktop = (struct desktop *)obj;
651 reply->is_desktop = 1;
652 reply->old_obj_flags = desktop->flags;
653 if (req->flags & SET_USER_OBJECT_SET_FLAGS) desktop->flags = req->obj_flags;
655 else if (obj->ops == &winstation_ops)
657 struct winstation *winstation = (struct winstation *)obj;
658 reply->is_desktop = 0;
659 reply->old_obj_flags = winstation->flags;
660 if (req->flags & SET_USER_OBJECT_SET_FLAGS) winstation->flags = req->obj_flags;
662 else
664 set_error( STATUS_OBJECT_TYPE_MISMATCH );
665 release_object( obj );
666 return;
668 if (obj->ops == &desktop_ops && get_reply_max_size() && (req->flags & SET_USER_OBJECT_GET_FULL_NAME))
670 struct desktop *desktop = (struct desktop *)obj;
671 data_size_t winstation_len, desktop_len;
672 const WCHAR *winstation_name = get_object_name( &desktop->winstation->obj, &winstation_len );
673 const WCHAR *desktop_name = get_object_name( obj, &desktop_len );
674 WCHAR *full_name;
676 if (!winstation_name) winstation_len = 0;
677 if (!desktop_name) desktop_len = 0;
678 len = winstation_len + desktop_len + sizeof(WCHAR);
679 if ((full_name = mem_alloc( len )))
681 memcpy( full_name, winstation_name, winstation_len );
682 full_name[winstation_len / sizeof(WCHAR)] = '\\';
683 memcpy( full_name + winstation_len / sizeof(WCHAR) + 1, desktop_name, desktop_len );
684 set_reply_data_ptr( full_name, min( len, get_reply_max_size() ));
687 else
689 const WCHAR *name = get_object_name( obj, &len );
690 if (name) set_reply_data( name, min( len, get_reply_max_size() ));
692 release_object( obj );
696 /* enumerate window stations */
697 DECL_HANDLER(enum_winstation)
699 unsigned int index = 0;
700 struct winstation *winsta;
701 const WCHAR *name;
702 data_size_t len;
704 LIST_FOR_EACH_ENTRY( winsta, &winstation_list, struct winstation, entry )
706 unsigned int access = WINSTA_ENUMERATE;
707 if (req->index > index++) continue;
708 if (!check_object_access( NULL, &winsta->obj, &access )) continue;
709 clear_error();
710 reply->next = index;
711 if ((name = get_object_name( &winsta->obj, &len )))
712 set_reply_data( name, min( len, get_reply_max_size() ));
713 return;
715 set_error( STATUS_NO_MORE_ENTRIES );
719 /* enumerate desktops */
720 DECL_HANDLER(enum_desktop)
722 struct winstation *winstation;
723 struct desktop *desktop;
724 unsigned int index = 0;
725 const WCHAR *name;
726 data_size_t len;
728 if (!(winstation = (struct winstation *)get_handle_obj( current->process, req->winstation,
729 WINSTA_ENUMDESKTOPS, &winstation_ops )))
730 return;
732 LIST_FOR_EACH_ENTRY( desktop, &winstation->desktops, struct desktop, entry )
734 unsigned int access = DESKTOP_ENUMERATE;
735 if (req->index > index++) continue;
736 if (!desktop->obj.name) continue;
737 if (!check_object_access( NULL, &desktop->obj, &access )) continue;
738 if ((name = get_object_name( &desktop->obj, &len )))
739 set_reply_data( name, min( len, get_reply_max_size() ));
740 release_object( winstation );
741 clear_error();
742 reply->next = index;
743 return;
746 release_object( winstation );
747 set_error( STATUS_NO_MORE_ENTRIES );