Vitaliy Margolen | 80444df | 2005-11-30 19:22:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Server-side symbolic link object management |
| 3 | * |
| 4 | * Copyright (C) 2005 Vitaliy Margolen |
| 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 |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Vitaliy Margolen | 80444df | 2005-11-30 19:22:57 +0100 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include "config.h" |
| 23 | #include "wine/port.h" |
| 24 | |
| 25 | #include <assert.h> |
| 26 | #include <stdarg.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <stdio.h> |
| 29 | #include <sys/types.h> |
| 30 | |
| 31 | #include "ntstatus.h" |
| 32 | #define WIN32_NO_STATUS |
| 33 | #include "winternl.h" |
| 34 | #include "ddk/wdm.h" |
| 35 | |
| 36 | #include "handle.h" |
| 37 | #include "request.h" |
| 38 | #include "object.h" |
| 39 | #include "unicode.h" |
| 40 | |
| 41 | struct symlink |
| 42 | { |
| 43 | struct object obj; /* object header */ |
| 44 | WCHAR *target; /* target of the symlink */ |
Alexandre Julliard | 0f273c1 | 2006-07-26 10:43:25 +0200 | [diff] [blame] | 45 | data_size_t len; /* target len in bytes */ |
Vitaliy Margolen | 80444df | 2005-11-30 19:22:57 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | static void symlink_dump( struct object *obj, int verbose ); |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 49 | static struct object_type *symlink_get_type( struct object *obj ); |
Alexandre Julliard | e57f734 | 2005-12-12 15:01:16 +0100 | [diff] [blame] | 50 | static unsigned int symlink_map_access( struct object *obj, unsigned int access ); |
Vitaliy Margolen | 80444df | 2005-11-30 19:22:57 +0100 | [diff] [blame] | 51 | static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name, |
| 52 | unsigned int attr ); |
| 53 | static void symlink_destroy( struct object *obj ); |
| 54 | |
| 55 | static const struct object_ops symlink_ops = |
| 56 | { |
| 57 | sizeof(struct symlink), /* size */ |
| 58 | symlink_dump, /* dump */ |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 59 | symlink_get_type, /* get_type */ |
Vitaliy Margolen | 80444df | 2005-11-30 19:22:57 +0100 | [diff] [blame] | 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 */ |
Alexandre Julliard | e57f734 | 2005-12-12 15:01:16 +0100 | [diff] [blame] | 66 | symlink_map_access, /* map_access */ |
Rob Shearman | c1707d8 | 2007-10-03 13:10:37 +0100 | [diff] [blame] | 67 | default_get_sd, /* get_sd */ |
| 68 | default_set_sd, /* set_sd */ |
Vitaliy Margolen | 80444df | 2005-11-30 19:22:57 +0100 | [diff] [blame] | 69 | symlink_lookup_name, /* lookup_name */ |
Alexandre Julliard | 7e71c1d | 2007-03-22 11:44:29 +0100 | [diff] [blame] | 70 | no_open_file, /* open_file */ |
Vitaliy Margolen | 80444df | 2005-11-30 19:22:57 +0100 | [diff] [blame] | 71 | no_close_handle, /* close_handle */ |
| 72 | symlink_destroy /* destroy */ |
| 73 | }; |
| 74 | |
| 75 | static void symlink_dump( struct object *obj, int verbose ) |
| 76 | { |
| 77 | struct symlink *symlink = (struct symlink *)obj; |
| 78 | assert( obj->ops == &symlink_ops ); |
| 79 | |
| 80 | fprintf( stderr, "Symlink " ); |
| 81 | dump_object_name( obj ); |
| 82 | fprintf( stderr, " -> L\"" ); |
| 83 | dump_strW( symlink->target, symlink->len / sizeof(WCHAR), stderr, "\"\"" ); |
| 84 | fprintf( stderr, "\"\n" ); |
| 85 | } |
| 86 | |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 87 | static struct object_type *symlink_get_type( struct object *obj ) |
| 88 | { |
| 89 | static const WCHAR name[] = {'S','y','m','b','o','l','i','c','L','i','n','k'}; |
| 90 | static const struct unicode_str str = { name, sizeof(name) }; |
| 91 | return get_object_type( &str ); |
| 92 | } |
| 93 | |
Vitaliy Margolen | 80444df | 2005-11-30 19:22:57 +0100 | [diff] [blame] | 94 | static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name, |
| 95 | unsigned int attr ) |
| 96 | { |
| 97 | struct symlink *symlink = (struct symlink *)obj; |
| 98 | struct unicode_str target_str, name_left; |
| 99 | struct object *target; |
| 100 | |
| 101 | assert( obj->ops == &symlink_ops ); |
Alexandre Julliard | c928aa6 | 2008-10-09 12:43:36 +0200 | [diff] [blame] | 102 | if (!name->len && (attr & OBJ_OPENLINK)) return NULL; |
Vitaliy Margolen | 80444df | 2005-11-30 19:22:57 +0100 | [diff] [blame] | 103 | |
| 104 | target_str.str = symlink->target; |
| 105 | target_str.len = symlink->len; |
| 106 | if ((target = find_object_dir( NULL, &target_str, attr, &name_left ))) |
| 107 | { |
| 108 | if (name_left.len) |
| 109 | { |
| 110 | release_object( target ); |
| 111 | target = NULL; |
| 112 | set_error( STATUS_OBJECT_PATH_NOT_FOUND ); |
| 113 | } |
| 114 | } |
| 115 | return target; |
| 116 | } |
| 117 | |
Alexandre Julliard | e57f734 | 2005-12-12 15:01:16 +0100 | [diff] [blame] | 118 | static unsigned int symlink_map_access( struct object *obj, unsigned int access ) |
| 119 | { |
| 120 | if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYMBOLIC_LINK_QUERY; |
| 121 | if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE; |
| 122 | if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE; |
| 123 | if (access & GENERIC_ALL) access |= SYMBOLIC_LINK_ALL_ACCESS; |
| 124 | return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL); |
| 125 | } |
| 126 | |
Vitaliy Margolen | 80444df | 2005-11-30 19:22:57 +0100 | [diff] [blame] | 127 | static void symlink_destroy( struct object *obj ) |
| 128 | { |
| 129 | struct symlink *symlink = (struct symlink *)obj; |
| 130 | assert( obj->ops == &symlink_ops ); |
| 131 | free( symlink->target ); |
| 132 | } |
| 133 | |
| 134 | struct symlink *create_symlink( struct directory *root, const struct unicode_str *name, |
| 135 | unsigned int attr, const struct unicode_str *target ) |
| 136 | { |
| 137 | struct symlink *symlink; |
| 138 | |
| 139 | if (!target->len) |
| 140 | { |
| 141 | set_error( STATUS_INVALID_PARAMETER ); |
| 142 | return NULL; |
| 143 | } |
| 144 | if ((symlink = create_named_object_dir( root, name, attr, &symlink_ops )) && |
| 145 | (get_error() != STATUS_OBJECT_NAME_EXISTS)) |
| 146 | { |
| 147 | if ((symlink->target = memdup( target->str, target->len ))) |
| 148 | symlink->len = target->len; |
| 149 | else |
| 150 | { |
| 151 | release_object( symlink ); |
| 152 | symlink = NULL; |
| 153 | } |
| 154 | } |
| 155 | return symlink; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /* create a symbolic link object */ |
| 160 | DECL_HANDLER(create_symlink) |
| 161 | { |
| 162 | struct symlink *symlink; |
| 163 | struct unicode_str name, target; |
| 164 | struct directory *root = NULL; |
| 165 | |
| 166 | if (req->name_len > get_req_data_size()) |
| 167 | { |
| 168 | set_error( STATUS_INVALID_PARAMETER ); |
| 169 | return; |
| 170 | } |
| 171 | name.str = get_req_data(); |
| 172 | target.str = name.str + req->name_len / sizeof(WCHAR); |
| 173 | name.len = (target.str - name.str) * sizeof(WCHAR); |
| 174 | target.len = ((get_req_data_size() - name.len) / sizeof(WCHAR)) * sizeof(WCHAR); |
| 175 | |
| 176 | if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 ))) |
| 177 | return; |
| 178 | |
| 179 | if ((symlink = create_symlink( root, &name, req->attributes, &target ))) |
| 180 | { |
Alexandre Julliard | 24560e7 | 2005-12-09 13:58:25 +0100 | [diff] [blame] | 181 | reply->handle = alloc_handle( current->process, symlink, req->access, req->attributes ); |
Vitaliy Margolen | 80444df | 2005-11-30 19:22:57 +0100 | [diff] [blame] | 182 | release_object( symlink ); |
| 183 | } |
| 184 | |
| 185 | if (root) release_object( root ); |
| 186 | } |
| 187 | |
| 188 | /* open a symbolic link object */ |
| 189 | DECL_HANDLER(open_symlink) |
| 190 | { |
| 191 | struct unicode_str name; |
| 192 | struct directory *root = NULL; |
Alexandre Julliard | 3764da6 | 2005-12-05 12:52:05 +0100 | [diff] [blame] | 193 | struct symlink *symlink; |
Vitaliy Margolen | 80444df | 2005-11-30 19:22:57 +0100 | [diff] [blame] | 194 | |
| 195 | get_req_unicode_str( &name ); |
| 196 | if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 ))) |
| 197 | return; |
| 198 | |
Alexandre Julliard | 3764da6 | 2005-12-05 12:52:05 +0100 | [diff] [blame] | 199 | if ((symlink = open_object_dir( root, &name, req->attributes | OBJ_OPENLINK, &symlink_ops ))) |
| 200 | { |
Alexandre Julliard | 24560e7 | 2005-12-09 13:58:25 +0100 | [diff] [blame] | 201 | reply->handle = alloc_handle( current->process, &symlink->obj, req->access, req->attributes ); |
Alexandre Julliard | 3764da6 | 2005-12-05 12:52:05 +0100 | [diff] [blame] | 202 | release_object( symlink ); |
| 203 | } |
Vitaliy Margolen | 80444df | 2005-11-30 19:22:57 +0100 | [diff] [blame] | 204 | |
| 205 | if (root) release_object( root ); |
| 206 | } |
| 207 | |
| 208 | /* query a symbolic link object */ |
| 209 | DECL_HANDLER(query_symlink) |
| 210 | { |
| 211 | struct symlink *symlink; |
| 212 | |
| 213 | symlink = (struct symlink *)get_handle_obj( current->process, req->handle, |
| 214 | SYMBOLIC_LINK_QUERY, &symlink_ops ); |
| 215 | if (!symlink) return; |
| 216 | |
Alexandre Julliard | 4c1f36c | 2010-06-30 16:11:03 +0200 | [diff] [blame] | 217 | reply->total = symlink->len; |
Vitaliy Margolen | 80444df | 2005-11-30 19:22:57 +0100 | [diff] [blame] | 218 | if (get_reply_max_size() < symlink->len) |
| 219 | set_error( STATUS_BUFFER_TOO_SMALL ); |
| 220 | else |
| 221 | set_reply_data( symlink->target, symlink->len ); |
| 222 | release_object( symlink ); |
| 223 | } |