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