server: Add an open_file() function to the object operations.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "file.h"
40 #include "wine/unicode.h"
41
42
43 static struct list winstation_list = LIST_INIT(winstation_list);
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 unsigned int winstation_map_access( struct object *obj, unsigned int access );
50 static void desktop_dump( struct object *obj, int verbose );
51 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
52 static void desktop_destroy( struct object *obj );
53 static unsigned int desktop_map_access( struct object *obj, unsigned int access );
54
55 static const struct object_ops winstation_ops =
56 {
57     sizeof(struct winstation),    /* size */
58     winstation_dump,              /* dump */
59     no_add_queue,                 /* add_queue */
60     NULL,                         /* remove_queue */
61     NULL,                         /* signaled */
62     NULL,                         /* satisfied */
63     no_signal,                    /* signal */
64     no_get_fd,                    /* get_fd */
65     winstation_map_access,        /* map_access */
66     no_lookup_name,               /* lookup_name */
67     no_open_file,                 /* open_file */
68     winstation_close_handle,      /* close_handle */
69     winstation_destroy            /* destroy */
70 };
71
72
73 static const struct object_ops desktop_ops =
74 {
75     sizeof(struct desktop),       /* size */
76     desktop_dump,                 /* dump */
77     no_add_queue,                 /* add_queue */
78     NULL,                         /* remove_queue */
79     NULL,                         /* signaled */
80     NULL,                         /* satisfied */
81     no_signal,                    /* signal */
82     no_get_fd,                    /* get_fd */
83     desktop_map_access,           /* map_access */
84     no_lookup_name,               /* lookup_name */
85     no_open_file,                 /* open_file */
86     desktop_close_handle,         /* close_handle */
87     desktop_destroy               /* destroy */
88 };
89
90 #define DESKTOP_ALL_ACCESS 0x01ff
91
92 /* create a winstation object */
93 static struct winstation *create_winstation( const struct unicode_str *name, unsigned int attr,
94                                              unsigned int flags )
95 {
96     struct winstation *winstation;
97
98     if (!winstation_namespace && !(winstation_namespace = create_namespace( 7 )))
99         return NULL;
100
101     if (memchrW( name->str, '\\', name->len / sizeof(WCHAR) ))  /* no backslash allowed in name */
102     {
103         set_error( STATUS_INVALID_PARAMETER );
104         return NULL;
105     }
106
107     if ((winstation = create_named_object( winstation_namespace, &winstation_ops, name, attr )))
108     {
109         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
110         {
111             /* initialize it if it didn't already exist */
112             winstation->flags = flags;
113             winstation->clipboard = NULL;
114             winstation->atom_table = NULL;
115             list_add_tail( &winstation_list, &winstation->entry );
116             list_init( &winstation->desktops );
117         }
118     }
119     return winstation;
120 }
121
122 static void winstation_dump( struct object *obj, int verbose )
123 {
124     struct winstation *winstation = (struct winstation *)obj;
125
126     fprintf( stderr, "Winstation flags=%x clipboard=%p atoms=%p ",
127              winstation->flags, winstation->clipboard, winstation->atom_table );
128     dump_object_name( &winstation->obj );
129     fputc( '\n', stderr );
130 }
131
132 static int winstation_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
133 {
134     return (process->winstation != handle);
135 }
136
137 static void winstation_destroy( struct object *obj )
138 {
139     struct winstation *winstation = (struct winstation *)obj;
140
141     list_remove( &winstation->entry );
142     if (winstation->clipboard) release_object( winstation->clipboard );
143     if (winstation->atom_table) release_object( winstation->atom_table );
144 }
145
146 static unsigned int winstation_map_access( struct object *obj, unsigned int access )
147 {
148     if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ | WINSTA_ENUMDESKTOPS | WINSTA_READATTRIBUTES |
149                                             WINSTA_ENUMERATE | WINSTA_READSCREEN;
150     if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE | WINSTA_ACCESSCLIPBOARD | WINSTA_CREATEDESKTOP |
151                                             WINSTA_WRITEATTRIBUTES;
152     if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | WINSTA_ACCESSGLOBALATOMS | WINSTA_EXITWINDOWS;
153     if (access & GENERIC_ALL)     access |= STANDARD_RIGHTS_REQUIRED | WINSTA_ALL_ACCESS;
154     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
155 }
156
157 /* retrieve the process window station, checking the handle access rights */
158 struct winstation *get_process_winstation( struct process *process, unsigned int access )
159 {
160     return (struct winstation *)get_handle_obj( process, process->winstation,
161                                                 access, &winstation_ops );
162 }
163
164 /* build the full name of a desktop object */
165 static WCHAR *build_desktop_name( const struct unicode_str *name,
166                                   struct winstation *winstation, struct unicode_str *res )
167 {
168     const WCHAR *winstation_name;
169     WCHAR *full_name;
170     data_size_t winstation_len;
171
172     if (memchrW( name->str, '\\', name->len / sizeof(WCHAR) ))
173     {
174         set_error( STATUS_INVALID_PARAMETER );
175         return NULL;
176     }
177
178     if (!(winstation_name = get_object_name( &winstation->obj, &winstation_len )))
179         winstation_len = 0;
180
181     res->len = winstation_len + name->len + sizeof(WCHAR);
182     if (!(full_name = mem_alloc( res->len ))) return NULL;
183     memcpy( full_name, winstation_name, winstation_len );
184     full_name[winstation_len / sizeof(WCHAR)] = '\\';
185     memcpy( full_name + winstation_len / sizeof(WCHAR) + 1, name->str, name->len );
186     res->str = full_name;
187     return full_name;
188 }
189
190 /* retrieve a pointer to a desktop object */
191 static inline struct desktop *get_desktop_obj( struct process *process, obj_handle_t handle,
192                                                unsigned int access )
193 {
194     return (struct desktop *)get_handle_obj( process, handle, access, &desktop_ops );
195 }
196
197 /* create a desktop object */
198 static struct desktop *create_desktop( const struct unicode_str *name, unsigned int attr,
199                                        unsigned int flags, struct winstation *winstation )
200 {
201     struct desktop *desktop;
202     struct unicode_str full_str;
203     WCHAR *full_name;
204
205     if (!(full_name = build_desktop_name( name, winstation, &full_str ))) return NULL;
206
207     if ((desktop = create_named_object( winstation_namespace, &desktop_ops, &full_str, attr )))
208     {
209         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
210         {
211             /* initialize it if it didn't already exist */
212             desktop->flags = flags;
213             desktop->winstation = (struct winstation *)grab_object( winstation );
214             desktop->top_window = NULL;
215             desktop->global_hooks = NULL;
216             desktop->close_timeout = NULL;
217             desktop->users = 0;
218             list_add_tail( &winstation->desktops, &desktop->entry );
219         }
220     }
221     free( full_name );
222     return desktop;
223 }
224
225 static void desktop_dump( struct object *obj, int verbose )
226 {
227     struct desktop *desktop = (struct desktop *)obj;
228
229     fprintf( stderr, "Desktop flags=%x winstation=%p top_win=%p hooks=%p ",
230              desktop->flags, desktop->winstation, desktop->top_window, desktop->global_hooks );
231     dump_object_name( &desktop->obj );
232     fputc( '\n', stderr );
233 }
234
235 static int desktop_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
236 {
237     struct thread *thread;
238
239     /* check if the handle is currently used by the process or one of its threads */
240     if (process->desktop == handle) return 0;
241     LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
242         if (thread->desktop == handle) return 0;
243     return 1;
244 }
245
246 static void desktop_destroy( struct object *obj )
247 {
248     struct desktop *desktop = (struct desktop *)obj;
249
250     if (desktop->top_window) destroy_window( desktop->top_window );
251     if (desktop->global_hooks) release_object( desktop->global_hooks );
252     if (desktop->close_timeout) remove_timeout_user( desktop->close_timeout );
253     list_remove( &desktop->entry );
254     release_object( desktop->winstation );
255 }
256
257 static unsigned int desktop_map_access( struct object *obj, unsigned int access )
258 {
259     if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ | DESKTOP_READOBJECTS | DESKTOP_ENUMERATE;
260     if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE | DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW |
261                                             DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK |
262                                             DESKTOP_WRITEOBJECTS;
263     if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | DESKTOP_SWITCHDESKTOP;
264     if (access & GENERIC_ALL)     access |= STANDARD_RIGHTS_REQUIRED | DESKTOP_ALL_ACCESS;
265     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
266 }
267
268 /* retrieve the thread desktop, checking the handle access rights */
269 struct desktop *get_thread_desktop( struct thread *thread, unsigned int access )
270 {
271     return get_desktop_obj( thread->process, thread->desktop, access );
272 }
273
274 /* set the process default desktop handle */
275 void set_process_default_desktop( struct process *process, struct desktop *desktop,
276                                   obj_handle_t handle )
277 {
278     struct thread *thread;
279     struct desktop *old_desktop;
280
281     if (process->desktop == handle) return;  /* nothing to do */
282
283     if (!(old_desktop = get_desktop_obj( process, process->desktop, 0 ))) clear_error();
284     process->desktop = handle;
285
286     /* set desktop for threads that don't have one yet */
287     LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
288         if (!thread->desktop) thread->desktop = handle;
289
290     desktop->users++;
291     if (desktop->close_timeout)
292     {
293         remove_timeout_user( desktop->close_timeout );
294         desktop->close_timeout = NULL;
295     }
296     if (old_desktop)
297     {
298         old_desktop->users--;
299         release_object( old_desktop );
300     }
301 }
302
303 /* connect a process to its window station */
304 void connect_process_winstation( struct process *process, struct thread *parent )
305 {
306     struct winstation *winstation = NULL;
307     struct desktop *desktop = NULL;
308     obj_handle_t handle;
309
310     /* check for an inherited winstation handle (don't ask...) */
311     if ((handle = find_inherited_handle( process, &winstation_ops )))
312     {
313         winstation = (struct winstation *)get_handle_obj( process, handle, 0, &winstation_ops );
314     }
315     else if (parent && parent->process->winstation)
316     {
317         handle = duplicate_handle( parent->process, parent->process->winstation,
318                                    process, 0, 0, DUP_HANDLE_SAME_ACCESS );
319         winstation = (struct winstation *)get_handle_obj( process, handle, 0, &winstation_ops );
320     }
321     if (!winstation) goto done;
322     process->winstation = handle;
323
324     if ((handle = find_inherited_handle( process, &desktop_ops )))
325     {
326         desktop = get_desktop_obj( process, handle, 0 );
327         if (!desktop || desktop->winstation != winstation) goto done;
328     }
329     else if (parent && parent->desktop)
330     {
331         desktop = get_desktop_obj( parent->process, parent->desktop, 0 );
332         if (!desktop || desktop->winstation != winstation) goto done;
333         handle = duplicate_handle( parent->process, parent->desktop,
334                                    process, 0, 0, DUP_HANDLE_SAME_ACCESS );
335     }
336
337     if (handle) set_process_default_desktop( process, desktop, handle );
338
339 done:
340     if (desktop) release_object( desktop );
341     if (winstation) release_object( winstation );
342     clear_error();
343 }
344
345 static void close_desktop_timeout( void *private )
346 {
347     struct desktop *desktop = private;
348
349     desktop->close_timeout = NULL;
350     unlink_named_object( &desktop->obj );  /* make sure no other process can open it */
351     close_desktop_window( desktop );  /* and signal the owner to quit */
352 }
353
354 /* close the desktop of a given process */
355 void close_process_desktop( struct process *process )
356 {
357     struct desktop *desktop;
358
359     if (process->desktop && (desktop = get_desktop_obj( process, process->desktop, 0 )))
360     {
361         assert( desktop->users > 0 );
362         desktop->users--;
363         /* if we have one remaining user, it has to be the manager of the desktop window */
364         if (desktop->users == 1 && get_top_window_owner( desktop ))
365         {
366             struct timeval when = current_time;
367             add_timeout( &when, 1000 );
368             assert( !desktop->close_timeout );
369             desktop->close_timeout = add_timeout_user( &when, close_desktop_timeout, desktop );
370         }
371         release_object( desktop );
372     }
373     clear_error();  /* ignore errors */
374 }
375
376 /* close the desktop of a given thread */
377 void close_thread_desktop( struct thread *thread )
378 {
379     obj_handle_t handle = thread->desktop;
380
381     thread->desktop = 0;
382     if (handle) close_handle( thread->process, handle );
383     clear_error();  /* ignore errors */
384 }
385
386
387 /* create a window station */
388 DECL_HANDLER(create_winstation)
389 {
390     struct winstation *winstation;
391     struct unicode_str name;
392
393     reply->handle = 0;
394     get_req_unicode_str( &name );
395     if ((winstation = create_winstation( &name, req->attributes, req->flags )))
396     {
397         reply->handle = alloc_handle( current->process, winstation, req->access, req->attributes );
398         release_object( winstation );
399     }
400 }
401
402 /* open a handle to a window station */
403 DECL_HANDLER(open_winstation)
404 {
405     struct unicode_str name;
406
407     get_req_unicode_str( &name );
408     if (winstation_namespace)
409         reply->handle = open_object( winstation_namespace, &name, &winstation_ops, req->access,
410                                      req->attributes );
411     else
412         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
413 }
414
415
416 /* close a window station */
417 DECL_HANDLER(close_winstation)
418 {
419     struct winstation *winstation;
420
421     if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
422                                                            0, &winstation_ops )))
423     {
424         if (!close_handle( current->process, req->handle )) set_error( STATUS_ACCESS_DENIED );
425         release_object( winstation );
426     }
427 }
428
429
430 /* get the process current window station */
431 DECL_HANDLER(get_process_winstation)
432 {
433     reply->handle = current->process->winstation;
434 }
435
436
437 /* set the process current window station */
438 DECL_HANDLER(set_process_winstation)
439 {
440     struct winstation *winstation;
441
442     if ((winstation = (struct winstation *)get_handle_obj( current->process, req->handle,
443                                                            0, &winstation_ops )))
444     {
445         /* FIXME: should we close the old one? */
446         current->process->winstation = req->handle;
447         release_object( winstation );
448     }
449 }
450
451 /* create a desktop */
452 DECL_HANDLER(create_desktop)
453 {
454     struct desktop *desktop;
455     struct winstation *winstation;
456     struct unicode_str name;
457
458     reply->handle = 0;
459     get_req_unicode_str( &name );
460     if ((winstation = get_process_winstation( current->process, WINSTA_CREATEDESKTOP )))
461     {
462         if ((desktop = create_desktop( &name, req->attributes, req->flags, winstation )))
463         {
464             reply->handle = alloc_handle( current->process, desktop, req->access, req->attributes );
465             release_object( desktop );
466         }
467         release_object( winstation );
468     }
469 }
470
471 /* open a handle to a desktop */
472 DECL_HANDLER(open_desktop)
473 {
474     struct winstation *winstation;
475     struct unicode_str name;
476
477     get_req_unicode_str( &name );
478     if ((winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
479     {
480         struct unicode_str full_str;
481         WCHAR *full_name;
482
483         if ((full_name = build_desktop_name( &name, winstation, &full_str )))
484         {
485             reply->handle = open_object( winstation_namespace, &full_str, &desktop_ops, req->access,
486                                          req->attributes );
487             free( full_name );
488         }
489         release_object( winstation );
490     }
491 }
492
493
494 /* close a desktop */
495 DECL_HANDLER(close_desktop)
496 {
497     struct desktop *desktop;
498
499     /* make sure it is a desktop handle */
500     if ((desktop = (struct desktop *)get_handle_obj( current->process, req->handle,
501                                                      0, &desktop_ops )))
502     {
503         if (!close_handle( current->process, req->handle )) set_error( STATUS_DEVICE_BUSY );
504         release_object( desktop );
505     }
506 }
507
508
509 /* get the thread current desktop */
510 DECL_HANDLER(get_thread_desktop)
511 {
512     struct thread *thread;
513
514     if (!(thread = get_thread_from_id( req->tid ))) return;
515     reply->handle = thread->desktop;
516     release_object( thread );
517 }
518
519
520 /* set the thread current desktop */
521 DECL_HANDLER(set_thread_desktop)
522 {
523     struct desktop *old_desktop, *new_desktop;
524     struct winstation *winstation;
525
526     if (!(winstation = get_process_winstation( current->process, 0 /* FIXME: access rights? */ )))
527         return;
528
529     if (!(new_desktop = get_desktop_obj( current->process, req->handle, 0 )))
530     {
531         release_object( winstation );
532         return;
533     }
534     if (new_desktop->winstation != winstation)
535     {
536         set_error( STATUS_ACCESS_DENIED );
537         release_object( new_desktop );
538         release_object( winstation );
539         return;
540     }
541
542     /* check if we are changing to a new desktop */
543
544     if (!(old_desktop = get_desktop_obj( current->process, current->desktop, 0)))
545         clear_error();  /* ignore error */
546
547     /* when changing desktop, we can't have any users on the current one */
548     if (old_desktop != new_desktop && current->desktop_users > 0)
549         set_error( STATUS_DEVICE_BUSY );
550     else
551         current->desktop = req->handle;  /* FIXME: should we close the old one? */
552
553     if (!current->process->desktop)
554         set_process_default_desktop( current->process, new_desktop, req->handle );
555
556     if (old_desktop != new_desktop && current->queue) detach_thread_input( current );
557
558     if (old_desktop) release_object( old_desktop );
559     release_object( new_desktop );
560     release_object( winstation );
561 }
562
563
564 /* get/set information about a user object (window station or desktop) */
565 DECL_HANDLER(set_user_object_info)
566 {
567     struct object *obj;
568
569     if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return;
570
571     if (obj->ops == &desktop_ops)
572     {
573         struct desktop *desktop = (struct desktop *)obj;
574         reply->is_desktop = 1;
575         reply->old_obj_flags = desktop->flags;
576         if (req->flags & SET_USER_OBJECT_FLAGS) desktop->flags = req->obj_flags;
577     }
578     else if (obj->ops == &winstation_ops)
579     {
580         struct winstation *winstation = (struct winstation *)obj;
581         reply->is_desktop = 0;
582         reply->old_obj_flags = winstation->flags;
583         if (req->flags & SET_USER_OBJECT_FLAGS) winstation->flags = req->obj_flags;
584     }
585     else
586     {
587         set_error( STATUS_OBJECT_TYPE_MISMATCH );
588         release_object( obj );
589         return;
590     }
591     if (get_reply_max_size())
592     {
593         data_size_t len;
594         const WCHAR *ptr, *name = get_object_name( obj, &len );
595
596         /* if there is a backslash return the part of the name after it */
597         if (name && (ptr = memchrW( name, '\\', len )))
598         {
599             len -= (ptr + 1 - name) * sizeof(WCHAR);
600             name = ptr + 1;
601         }
602         if (name) set_reply_data( name, min( len, get_reply_max_size() ));
603     }
604     release_object( obj );
605 }