server: Do not dereference symlinks specified as root directory.
[wine/zf.git] / server / symlink.c
blob0b85350e1a57a9d2e815fa75e444df014dfcbf2d
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"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <sys/types.h>
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "winternl.h"
34 #include "ddk/wdm.h"
36 #include "handle.h"
37 #include "request.h"
38 #include "object.h"
39 #include "unicode.h"
41 struct symlink
43 struct object obj; /* object header */
44 WCHAR *target; /* target of the symlink */
45 data_size_t len; /* target len in bytes */
48 static void symlink_dump( struct object *obj, int verbose );
49 static struct object_type *symlink_get_type( struct object *obj );
50 static unsigned int symlink_map_access( struct object *obj, unsigned int access );
51 static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name,
52 unsigned int attr, struct object *root );
53 static void symlink_destroy( struct object *obj );
55 static const struct object_ops symlink_ops =
57 sizeof(struct symlink), /* size */
58 symlink_dump, /* dump */
59 symlink_get_type, /* get_type */
60 no_add_queue, /* add_queue */
61 NULL, /* remove_queue */
62 NULL, /* signaled */
63 NULL, /* satisfied */
64 no_signal, /* signal */
65 no_get_fd, /* get_fd */
66 symlink_map_access, /* map_access */
67 default_get_sd, /* get_sd */
68 default_set_sd, /* set_sd */
69 default_get_full_name, /* get_full_name */
70 symlink_lookup_name, /* lookup_name */
71 directory_link_name, /* link_name */
72 default_unlink_name, /* unlink_name */
73 no_open_file, /* open_file */
74 no_kernel_obj_list, /* get_kernel_obj_list */
75 no_close_handle, /* close_handle */
76 symlink_destroy /* destroy */
79 static void symlink_dump( struct object *obj, int verbose )
81 struct symlink *symlink = (struct symlink *)obj;
82 assert( obj->ops == &symlink_ops );
84 fputs( "Symlink target=\"", stderr );
85 dump_strW( symlink->target, symlink->len, stderr, "\"\"" );
86 fputs( "\"\n", stderr );
89 static struct object_type *symlink_get_type( struct object *obj )
91 static const WCHAR name[] = {'S','y','m','b','o','l','i','c','L','i','n','k'};
92 static const struct unicode_str str = { name, sizeof(name) };
93 return get_object_type( &str );
96 static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name,
97 unsigned int attr, struct object *root )
99 struct symlink *symlink = (struct symlink *)obj;
100 struct unicode_str target_str, name_left;
101 struct object *target;
103 assert( obj->ops == &symlink_ops );
105 if (!name) return NULL;
106 if (!name->len && (attr & OBJ_OPENLINK)) return NULL;
107 if (obj == root) return NULL;
109 target_str.str = symlink->target;
110 target_str.len = symlink->len;
111 if ((target = lookup_named_object( NULL, &target_str, attr, &name_left )))
113 if (name_left.len)
115 release_object( target );
116 target = NULL;
117 set_error( STATUS_OBJECT_PATH_NOT_FOUND );
120 return target;
123 static unsigned int symlink_map_access( struct object *obj, unsigned int access )
125 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYMBOLIC_LINK_QUERY;
126 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
127 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
128 if (access & GENERIC_ALL) access |= SYMBOLIC_LINK_ALL_ACCESS;
129 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
132 static void symlink_destroy( struct object *obj )
134 struct symlink *symlink = (struct symlink *)obj;
135 assert( obj->ops == &symlink_ops );
136 free( symlink->target );
139 struct object *create_symlink( struct object *root, const struct unicode_str *name,
140 unsigned int attr, const struct unicode_str *target,
141 const struct security_descriptor *sd )
143 struct symlink *symlink;
145 if (!target->len)
147 set_error( STATUS_INVALID_PARAMETER );
148 return NULL;
150 if (!(symlink = create_named_object( root, &symlink_ops, name, attr, sd ))) return NULL;
151 if (get_error() != STATUS_OBJECT_NAME_EXISTS && !(symlink->target = memdup( target->str, target->len )))
153 release_object( symlink );
154 return NULL;
156 symlink->len = target->len;
157 return &symlink->obj;
160 /* create a symlink pointing to an existing object */
161 struct object *create_obj_symlink( struct object *root, const struct unicode_str *name,
162 unsigned int attr, struct object *target,
163 const struct security_descriptor *sd )
165 struct symlink *symlink;
166 data_size_t len;
167 WCHAR *target_name;
169 if (!(target_name = target->ops->get_full_name( target, &len )))
171 set_error( STATUS_INVALID_PARAMETER );
172 return NULL;
174 if ((symlink = create_named_object( root, &symlink_ops, name, attr, sd )) &&
175 (get_error() != STATUS_OBJECT_NAME_EXISTS))
177 symlink->target = target_name;
178 symlink->len = len;
180 else free( target_name );
182 return &symlink->obj;
186 /* create a symbolic link object */
187 DECL_HANDLER(create_symlink)
189 struct object *symlink;
190 struct unicode_str name, target;
191 struct object *root;
192 const struct security_descriptor *sd;
193 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
195 if (!objattr) return;
197 target.str = get_req_data_after_objattr( objattr, &target.len );
198 target.len = (target.len / sizeof(WCHAR)) * sizeof(WCHAR);
200 if ((symlink = create_symlink( root, &name, objattr->attributes, &target, sd )))
202 reply->handle = alloc_handle( current->process, symlink, req->access, objattr->attributes );
203 release_object( symlink );
206 if (root) release_object( root );
209 /* open a symbolic link object */
210 DECL_HANDLER(open_symlink)
212 struct unicode_str name = get_req_unicode_str();
214 reply->handle = open_object( current->process, req->rootdir, req->access,
215 &symlink_ops, &name, req->attributes | OBJ_OPENLINK );
218 /* query a symbolic link object */
219 DECL_HANDLER(query_symlink)
221 struct symlink *symlink;
223 symlink = (struct symlink *)get_handle_obj( current->process, req->handle,
224 SYMBOLIC_LINK_QUERY, &symlink_ops );
225 if (!symlink) return;
227 reply->total = symlink->len;
228 if (get_reply_max_size() < symlink->len)
229 set_error( STATUS_BUFFER_TOO_SMALL );
230 else
231 set_reply_data( symlink->target, symlink->len );
232 release_object( symlink );