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