server: Fixed console access rights handling.
[wine] / server / winstation.c
1 /*
2  * Server-side window stations and desktops handling
3  *
4  * Copyright (C) 2002, 2005 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdio.h>
25 #include <stdarg.h>
26
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"
33
34 #include "object.h"
35 #include "handle.h"
36 #include "request.h"
37 #include "process.h"
38 #include "user.h"
39 #include "wine/unicode.h"
40
41
42 static struct list winstation_list = LIST_INIT(winstation_list);
43 static struct winstation *interactive_winstation;
44 static struct namespace *winstation_namespace;
45
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 void winstation_destroy( struct object *obj );
49 static void desktop_dump( struct object *obj, int verbose );
50 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
51 static void desktop_destroy( struct object *obj );
52
53 static const struct object_ops winstation_ops =
54 {
55     sizeof(struct winstation),    /* size */
56     winstation_dump,              /* dump */
57     no_add_queue,                 /* add_queue */
58     NULL,                         /* remove_queue */
59     NULL,                         /* signaled */
60     NULL,                         /* satisfied */
61     no_signal,                    /* signal */
62     no_get_fd,                    /* get_fd */
63     no_map_access,                /* map_access */
64     no_lookup_name,               /* lookup_name */
65     winstation_close_handle,      /* close_handle */
66     winstation_destroy            /* destroy */
67 };
68
69
70 static const struct object_ops desktop_ops =
71 {
72     sizeof(struct desktop),       /* size */
73     desktop_dump,                 /* dump */
74     no_add_queue,                 /* add_queue */
75     NULL,                         /* remove_queue */
76     NULL,                         /* signaled */
77     NULL,                         /* satisfied */
78     no_signal,                    /* signal */
79     no_get_fd,                    /* get_fd */
80     no_map_access,                /* map_access */
81     no_lookup_name,               /* lookup_name */
82     desktop_close_handle,         /* close_handle */
83     desktop_destroy               /* destroy */
84 };
85
86 #define DESKTOP_ALL_ACCESS 0x01ff
87
88 /* create a winstation object */
89 static struct winstation *create_winstation( const struct unicode_str *name, unsigned int attr,
90                                              unsigned int flags )
91 {
92     struct winstation *winstation;
93
94     if (!winstation_namespace && !(winstation_namespace = create_namespace( 7 )))
95         return NULL;
96
97     if (memchrW( name->str, '\\', name->len / sizeof(WCHAR) ))  /* no backslash allowed in name */
98     {
99         set_error( STATUS_INVALID_PARAMETER );
100         return NULL;
101     }
102
103     if ((winstation = create_named_object( winstation_namespace, &winstation_ops, name, attr )))
104     {
105         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
106         {
107             /* initialize it if it didn't already exist */
108             winstation->flags = flags;
109             winstation->clipboard = NULL;
110             winstation->atom_table = NULL;
111             list_add_tail( &winstation_list, &winstation->entry );
112             list_init( &winstation->desktops );
113         }
114     }
115     return winstation;
116 }
117
118 static void winstation_dump( struct object *obj, int verbose )
119 {
120     struct winstation *winstation = (struct winstation *)obj;
121
122     fprintf( stderr, "Winstation flags=%x clipboard=%p atoms=%p ",
123              winstation->flags, winstation->clipboard, winstation->atom_table );
124     dump_object_name( &winstation->obj );
125     fputc( '\n', stderr );
126 }
127
128 static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
129 {
130     return (process->winstation != handle);
131 }
132
133 static void winstation_destroy( struct object *obj )
134 {
135     struct winstation *winstation = (struct winstation *)obj;
136
137     if (winstation == interactive_winstation) interactive_winstation = NULL;
138     list_remove( &winstation->entry );
139     if (winstation->clipboard) release_object( winstation->clipboard );
140     if (winstation->atom_table) release_object( winstation->atom_table );
141 }
142
143 /* retrieve the process window station, checking the handle access rights */
144 struct winstation *get_process_winstation( struct process *process, unsigned int access )
145 {
146     return (struct winstation *)get_handle_obj( process, process->winstation,
147                                                 access, &winstation_ops );
148 }
149
150 /* build the full name of a desktop object */
151 static WCHAR *build_desktop_name( const struct unicode_str *name,
152                                   struct winstation *winstation, struct unicode_str *res )
153 {
154     const WCHAR *winstation_name;
155     WCHAR *full_name;
156     size_t winstation_len;
157
158     if (memchrW( name->str, '\\', name->len / sizeof(WCHAR) ))
159     {
160         set_error( STATUS_INVALID_PARAMETER );
161         return NULL;
162     }
163
164     if (!(winstation_name = get_object_name( &winstation->obj, &winstation_len )))
165         winstation_len = 0;
166
167     res->len = winstation_len + name->len + sizeof(WCHAR);
168     if (!(full_name = mem_alloc( res->len ))) return NULL;
169     memcpy( full_name, winstation_name, winstation_len );
170     full_name[winstation_len / sizeof(WCHAR)] = '\\';
171     memcpy( full_name + winstation_len / sizeof(WCHAR) + 1, name->str, name->len );
172     res->str = full_name;
173     return full_name;
174 }
175
176 /* retrieve a pointer to a desktop object */
177 inline static struct desktop *get_desktop_obj( struct process *process, obj_handle_t handle,
178                                                unsigned int access )
179 {
180     return (struct desktop *)get_handle_obj( process, handle, access, &desktop_ops );
181 }
182
183 /* create a desktop object */
184 static struct desktop *create_desktop( const struct unicode_str *name, unsigned int attr,
185                                        unsigned int flags, struct winstation *winstation )
186 {
187     struct desktop *desktop;
188     struct unicode_str full_str;
189     WCHAR *full_name;
190
191     if (!(full_name = build_desktop_name( name, winstation, &full_str ))) return NULL;
192
193     if ((desktop = create_named_object( winstation_namespace, &desktop_ops, &full_str, attr )))
194     {
195         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
196         {
197             /* initialize it if it didn't already exist */
198             desktop->flags = flags;
199             desktop->winstation = (struct winstation *)grab_object( winstation );
200             desktop->top_window = NULL;
201             desktop->global_hooks = NULL;
202             list_add_tail( &winstation->desktops, &desktop->entry );
203         }
204     }
205     free( full_name );
206     return desktop;
207 }
208
209 static void desktop_dump( struct object *obj, int verbose )
210 {
211     struct desktop *desktop = (struct desktop *)obj;
212
213     fprintf( stderr, "Desktop flags=%x winstation=%p top_win=%p hooks=%p ",
214              desktop->flags, desktop->winstation, desktop->top_window, desktop->global_hooks );
215     dump_object_name( &desktop->obj );
216     fputc( '\n', stderr );
217 }
218
219 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
220 {
221     struct thread *thread;
222
223     /* check if the handle is currently used by the process or one of its threads */
224     if (process->desktop == handle) return 0;
225     LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
226         if (thread->desktop == handle) return 0;
227     return 1;
228 }
229
230 static void desktop_destroy( struct object *obj )
231 {
232     struct desktop *desktop = (struct desktop *)obj;
233
234     if (desktop->top_window) destroy_window( desktop->top_window );
235     if (desktop->global_hooks) release_object( desktop->global_hooks );
236     list_remove( &desktop->entry );
237     release_object( desktop->winstation );
238 }
239
240 /* retrieve the thread desktop, checking the handle access rights */
241 struct desktop *get_thread_desktop( struct thread *thread, unsigned int access )
242 {
243     return get_desktop_obj( thread->process, thread->desktop, access );
244 }
245
246 /* connect a process to its window station */
247 void connect_process_winstation( struct process *process, const struct unicode_str *name )
248 {
249     struct winstation *winstation;
250
251     if (process->winstation) return;  /* already has one */
252
253     /* check for an inherited winstation handle (don't ask...) */
254     if ((process->winstation = find_inherited_handle( process, &winstation_ops )) != 0) return;
255
256     if (name)
257     {
258         winstation = create_winstation( name, OBJ_CASE_INSENSITIVE | OBJ_OPENIF, 0 );
259     }
260     else
261     {
262         if (!interactive_winstation)
263         {
264             static const WCHAR winsta0W[] = {'W','i','n','S','t','a','0'};
265             static const struct unicode_str winsta0 = { winsta0W, sizeof(winsta0W) };
266             interactive_winstation = create_winstation( &winsta0, OBJ_CASE_INSENSITIVE | OBJ_OPENIF, 0 );
267             winstation = interactive_winstation;
268         }
269         else winstation = (struct winstation *)grab_object( interactive_winstation );
270     }
271     if (winstation)
272     {
273         process->winstation = alloc_handle( process, winstation, WINSTA_ALL_ACCESS, 0 );
274         release_object( winstation );
275     }
276     clear_error();  /* ignore errors */
277 }
278
279 /* connect a process to its main desktop */
280 void connect_process_desktop( struct process *process, const struct unicode_str *name )
281 {
282     struct desktop *desktop;
283     struct winstation *winstation;
284
285     if (process->desktop) return;  /* already has one */
286
287     if ((winstation = get_process_winstation( process, WINSTA_CREATEDESKTOP )))
288     {
289         static const WCHAR defaultW[] = {'D','e','f','a','u','l','t'};
290         static const struct unicode_str default_str = { defaultW, sizeof(defaultW) };
291
292         if (!name) name = &default_str;
293         if ((desktop = create_desktop( name, OBJ_CASE_INSENSITIVE | OBJ_OPENIF, 0, winstation )))
294         {
295             process->desktop = alloc_handle( process, desktop, DESKTOP_ALL_ACCESS, 0 );
296             release_object( desktop );
297         }
298         release_object( winstation );
299     }
300     clear_error();  /* ignore errors */
301 }
302
303 /* close the desktop of a given thread */
304 void close_thread_desktop( struct thread *thread )
305 {
306     obj_handle_t handle = thread->desktop;
307
308     thread->desktop = 0;
309     if (handle) close_handle( thread->process, handle, NULL );
310     clear_error();  /* ignore errors */
311 }
312
313
314 /* create a window station */
315 DECL_HANDLER(create_winstation)
316 {
317     struct winstation *winstation;
318     struct unicode_str name;
319
320     reply->handle = 0;
321     get_req_unicode_str( &name );
322     if ((winstation = create_winstation( &name, req->attributes, req->flags )))
323     {
324         reply->handle = alloc_handle( current->process, winstation, req->access, req->attributes );
325         release_object( winstation );
326     }
327 }
328
329 /* open a handle to a window station */
330 DECL_HANDLER(open_winstation)
331 {
332     struct unicode_str name;
333
334     get_req_unicode_str( &name );
335     if (winstation_namespace)
336         reply->handle = open_object( winstation_namespace, &name, &winstation_ops, req->access,
337                                      req->attributes );
338     else
339         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
340 }
341
342
343 /* close a window station */
344 DECL_HANDLER(close_winstation)
345 {
346     struct winstation *winstation;
347
348     if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
349                                                            0, &winstation_ops )))
350     {
351         if (!close_handle( current->process, req->handle, NULL )) set_error( STATUS_ACCESS_DENIED );
352         release_object( winstation );
353     }
354 }
355
356
357 /* get the process current window station */
358 DECL_HANDLER(get_process_winstation)
359 {
360     reply->handle = current->process->winstation;
361 }
362
363
364 /* set the process current window station */
365 DECL_HANDLER(set_process_winstation)
366 {
367     struct winstation *winstation;
368
369     if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
370                                                            0, &winstation_ops )))
371     {
372         /* FIXME: should we close the old one? */
373         current->process->winstation = req->handle;
374         release_object( winstation );
375     }
376 }
377
378 /* create a desktop */
379 DECL_HANDLER(create_desktop)
380 {
381     struct desktop *desktop;
382     struct winstation *winstation;
383     struct unicode_str name;
384
385     reply->handle = 0;
386     get_req_unicode_str( &name );
387     if ((winstation = get_process_winstation( current->process, WINSTA_CREATEDESKTOP )))
388     {
389         if ((desktop = create_desktop( &name, req->attributes, req->flags, winstation )))
390         {
391             reply->handle = alloc_handle( current->process, desktop, req->access, req->attributes );
392             release_object( desktop );
393         }
394         release_object( winstation );
395     }
396 }
397
398 /* open a handle to a desktop */
399 DECL_HANDLER(open_desktop)
400 {
401     struct winstation *winstation;
402     struct unicode_str name;
403
404     get_req_unicode_str( &name );
405     if ((winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
406     {
407         struct unicode_str full_str;
408         WCHAR *full_name;
409
410         if ((full_name = build_desktop_name( &name, winstation, &full_str )))
411         {
412             reply->handle = open_object( winstation_namespace, &full_str, &desktop_ops, req->access,
413                                          req->attributes );
414             free( full_name );
415         }
416         release_object( winstation );
417     }
418 }
419
420
421 /* close a desktop */
422 DECL_HANDLER(close_desktop)
423 {
424     struct desktop *desktop;
425
426     /* make sure it is a desktop handle */
427     if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
428                                                      0, &desktop_ops )))
429     {
430         if (!close_handle( current->process, req->handle, NULL )) set_error( STATUS_DEVICE_BUSY );
431         release_object( desktop );
432     }
433 }
434
435
436 /* get the thread current desktop */
437 DECL_HANDLER(get_thread_desktop)
438 {
439     struct thread *thread;
440
441     if (!(thread = get_thread_from_id( req->tid ))) return;
442     reply->handle = thread->desktop;
443     release_object( thread );
444 }
445
446
447 /* set the thread current desktop */
448 DECL_HANDLER(set_thread_desktop)
449 {
450     struct desktop *old_desktop, *new_desktop;
451     struct winstation *winstation;
452
453     if (!(winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
454         return;
455
456     if (!(new_desktop = get_desktop_obj( current->process, req->handle, 0 )))
457     {
458         release_object( winstation );
459         return;
460     }
461     if (new_desktop->winstation != winstation)
462     {
463         set_error( STATUS_ACCESS_DENIED );
464         release_object( new_desktop );
465         release_object( winstation );
466         return;
467     }
468
469     /* check if we are changing to a new desktop */
470
471     if (!(old_desktop = get_desktop_obj( current->process, current->desktop, 0)))
472         clear_error();  /* ignore error */
473
474     /* when changing desktop, we can't have any users on the current one */
475     if (old_desktop != new_desktop && current->desktop_users > 0)
476         set_error( STATUS_DEVICE_BUSY );
477     else
478         current->desktop = req->handle;  /* FIXME: should we close the old one? */
479
480     if (old_desktop != new_desktop) detach_thread_input( current );
481
482     if (old_desktop) release_object( old_desktop );
483     release_object( new_desktop );
484     release_object( winstation );
485 }
486
487
488 /* get/set information about a user object (window station or desktop) */
489 DECL_HANDLER(set_user_object_info)
490 {
491     struct object *obj;
492
493     if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
494
495     if (obj->ops == &desktop_ops)
496     {
497         struct desktop *desktop = (struct desktop *)obj;
498         reply->is_desktop = 1;
499         reply->old_obj_flags = desktop->flags;
500         if (req->flags & SET_USER_OBJECT_FLAGS) desktop->flags = req->obj_flags;
501     }
502     else if (obj->ops == &winstation_ops)
503     {
504         struct winstation *winstation = (struct winstation *)obj;
505         reply->is_desktop = 0;
506         reply->old_obj_flags = winstation->flags;
507         if (req->flags & SET_USER_OBJECT_FLAGS) winstation->flags = req->obj_flags;
508     }
509     else
510     {
511         set_error( STATUS_OBJECT_TYPE_MISMATCH );
512         release_object( obj );
513         return;
514     }
515     if (get_reply_max_size())
516     {
517         size_t len;
518         const WCHAR *ptr, *name = get_object_name( obj, &len );
519
520         /* if there is a backslash return the part of the name after it */
521         if (name && (ptr = memchrW( name, '\\', len )))
522         {
523             len -= (ptr + 1 - name) * sizeof(WCHAR);
524             name = ptr + 1;
525         }
526         if (name) set_reply_data( name, min( len, get_reply_max_size() ));
527     }
528     release_object( obj );
529 }