combase: Check that process ID matches before searching the MTA for a stub manager.
[wine.git] / server / symlink.c
blob27d48e2f994a10316148db647e450c71476c570b
1 /*
2 * Server-side symbolic link object management
4 * Copyright (C) 2005 Vitaliy Margolen
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
22 #include "config.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <sys/types.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "winternl.h"
33 #include "ddk/wdm.h"
35 #include "handle.h"
36 #include "request.h"
37 #include "object.h"
38 #include "unicode.h"
40 static const WCHAR symlink_name[] = {'S','y','m','b','o','l','i','c','L','i','n','k'};
42 struct type_descr symlink_type =
44 { symlink_name, sizeof(symlink_name) }, /* name */
45 SYMBOLIC_LINK_ALL_ACCESS, /* valid_access */
46 { /* mapping */
47 STANDARD_RIGHTS_READ | SYMBOLIC_LINK_QUERY,
48 STANDARD_RIGHTS_WRITE,
49 STANDARD_RIGHTS_EXECUTE | SYMBOLIC_LINK_QUERY,
50 SYMBOLIC_LINK_ALL_ACCESS
54 struct symlink
56 struct object obj; /* object header */
57 WCHAR *target; /* target of the symlink */
58 data_size_t len; /* target len in bytes */
61 static void symlink_dump( struct object *obj, int verbose );
62 static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name,
63 unsigned int attr, struct object *root );
64 static void symlink_destroy( struct object *obj );
66 static const struct object_ops symlink_ops =
68 sizeof(struct symlink), /* size */
69 &symlink_type, /* type */
70 symlink_dump, /* dump */
71 no_add_queue, /* add_queue */
72 NULL, /* remove_queue */
73 NULL, /* signaled */
74 NULL, /* satisfied */
75 no_signal, /* signal */
76 no_get_fd, /* get_fd */
77 default_map_access, /* map_access */
78 default_get_sd, /* get_sd */
79 default_set_sd, /* set_sd */
80 default_get_full_name, /* get_full_name */
81 symlink_lookup_name, /* lookup_name */
82 directory_link_name, /* link_name */
83 default_unlink_name, /* unlink_name */
84 no_open_file, /* open_file */
85 no_kernel_obj_list, /* get_kernel_obj_list */
86 no_close_handle, /* close_handle */
87 symlink_destroy /* destroy */
90 static void symlink_dump( struct object *obj, int verbose )
92 struct symlink *symlink = (struct symlink *)obj;
93 assert( obj->ops == &symlink_ops );
95 fputs( "Symlink target=\"", stderr );
96 dump_strW( symlink->target, symlink->len, stderr, "\"\"" );
97 fputs( "\"\n", stderr );
100 static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name,
101 unsigned int attr, struct object *root )
103 struct symlink *symlink = (struct symlink *)obj;
104 struct unicode_str target_str, name_left;
105 struct object *target;
107 assert( obj->ops == &symlink_ops );
109 if (!name) return NULL;
110 if (!name->len && (attr & OBJ_OPENLINK)) return NULL;
111 if (obj == root) return NULL;
113 if (!symlink->len) return get_root_directory();
115 target_str.str = symlink->target;
116 target_str.len = symlink->len;
117 if ((target = lookup_named_object( NULL, &target_str, attr, &name_left )))
119 if (name_left.len)
121 release_object( target );
122 target = NULL;
123 set_error( STATUS_OBJECT_PATH_NOT_FOUND );
126 return target;
129 static void symlink_destroy( struct object *obj )
131 struct symlink *symlink = (struct symlink *)obj;
132 assert( obj->ops == &symlink_ops );
133 free( symlink->target );
136 struct object *create_root_symlink( struct object *root, const struct unicode_str *name,
137 unsigned int attr, const struct security_descriptor *sd )
139 struct symlink *symlink;
141 if (!(symlink = create_named_object( root, &symlink_ops, name, attr, sd ))) return NULL;
142 symlink->target = NULL;
143 symlink->len = 0;
144 return &symlink->obj;
147 struct object *create_symlink( struct object *root, const struct unicode_str *name,
148 unsigned int attr, const struct unicode_str *target,
149 const struct security_descriptor *sd )
151 struct symlink *symlink;
153 if (!target->len)
155 set_error( STATUS_INVALID_PARAMETER );
156 return NULL;
158 if (!(symlink = create_named_object( root, &symlink_ops, name, attr, sd ))) return NULL;
159 if (get_error() != STATUS_OBJECT_NAME_EXISTS && !(symlink->target = memdup( target->str, target->len )))
161 release_object( symlink );
162 return NULL;
164 symlink->len = target->len;
165 return &symlink->obj;
168 /* create a symlink pointing to an existing object */
169 struct object *create_obj_symlink( struct object *root, const struct unicode_str *name,
170 unsigned int attr, struct object *target,
171 const struct security_descriptor *sd )
173 struct symlink *symlink;
174 data_size_t len;
175 WCHAR *target_name;
177 if (!(target_name = target->ops->get_full_name( target, &len )))
179 set_error( STATUS_INVALID_PARAMETER );
180 return NULL;
182 if ((symlink = create_named_object( root, &symlink_ops, name, attr, sd )) &&
183 (get_error() != STATUS_OBJECT_NAME_EXISTS))
185 symlink->target = target_name;
186 symlink->len = len;
188 else free( target_name );
190 return &symlink->obj;
194 /* create a symbolic link object */
195 DECL_HANDLER(create_symlink)
197 struct object *symlink;
198 struct unicode_str name, target;
199 struct object *root;
200 const struct security_descriptor *sd;
201 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
203 if (!objattr) return;
205 target.str = get_req_data_after_objattr( objattr, &target.len );
206 target.len = (target.len / sizeof(WCHAR)) * sizeof(WCHAR);
208 if ((symlink = create_symlink( root, &name, objattr->attributes, &target, sd )))
210 reply->handle = alloc_handle( current->process, symlink, req->access, objattr->attributes );
211 release_object( symlink );
214 if (root) release_object( root );
217 /* open a symbolic link object */
218 DECL_HANDLER(open_symlink)
220 struct unicode_str name = get_req_unicode_str();
222 reply->handle = open_object( current->process, req->rootdir, req->access,
223 &symlink_ops, &name, req->attributes | OBJ_OPENLINK );
226 /* query a symbolic link object */
227 DECL_HANDLER(query_symlink)
229 struct symlink *symlink;
231 symlink = (struct symlink *)get_handle_obj( current->process, req->handle,
232 SYMBOLIC_LINK_QUERY, &symlink_ops );
233 if (!symlink) return;
235 reply->total = symlink->len;
236 if (get_reply_max_size() < symlink->len)
237 set_error( STATUS_BUFFER_TOO_SMALL );
238 else
239 set_reply_data( symlink->target, symlink->len );
240 release_object( symlink );