symlink.c 7.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Server-side symbolic link object management
 *
 * Copyright (C) 2005 Vitaliy Margolen
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 *
 */

#include "config.h"
#include "wine/port.h"

#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>

#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "winternl.h"
#include "ddk/wdm.h"

#include "handle.h"
#include "request.h"
#include "object.h"
#include "unicode.h"

struct symlink
{
    struct object    obj;       /* object header */
    WCHAR           *target;    /* target of the symlink */
45
    data_size_t      len;       /* target len in bytes */
46 47 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 52 53 54 55 56 57 58
static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name,
                                           unsigned int attr );
static void symlink_destroy( struct object *obj );

static const struct object_ops symlink_ops =
{
    sizeof(struct symlink),       /* size */
    symlink_dump,                 /* dump */
59
    symlink_get_type,             /* get_type */
60 61 62 63 64 65
    no_add_queue,                 /* add_queue */
    NULL,                         /* remove_queue */
    NULL,                         /* signaled */
    NULL,                         /* satisfied */
    no_signal,                    /* signal */
    no_get_fd,                    /* get_fd */
66
    symlink_map_access,           /* map_access */
67 68
    default_get_sd,               /* get_sd */
    default_set_sd,               /* set_sd */
69
    symlink_lookup_name,          /* lookup_name */
70 71
    directory_link_name,          /* link_name */
    default_unlink_name,          /* unlink_name */
72
    no_open_file,                 /* open_file */
73 74 75 76 77 78 79 80 81
    no_close_handle,              /* close_handle */
    symlink_destroy               /* destroy */
};

static void symlink_dump( struct object *obj, int verbose )
{
    struct symlink *symlink = (struct symlink *)obj;
    assert( obj->ops == &symlink_ops );

82
    fputs( "Symlink target=\"", stderr );
83
    dump_strW( symlink->target, symlink->len / sizeof(WCHAR), stderr, "\"\"" );
84
    fputs( "\"\n", stderr );
85 86
}

87 88 89 90 91 92 93
static struct object_type *symlink_get_type( struct object *obj )
{
    static const WCHAR name[] = {'S','y','m','b','o','l','i','c','L','i','n','k'};
    static const struct unicode_str str = { name, sizeof(name) };
    return get_object_type( &str );
}

94 95 96 97 98 99 100 101
static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name,
                                           unsigned int attr )
{
    struct symlink *symlink = (struct symlink *)obj;
    struct unicode_str target_str, name_left;
    struct object *target;

    assert( obj->ops == &symlink_ops );
102 103

    if (!name) return NULL;
104
    if (!name->len && (attr & OBJ_OPENLINK)) return NULL;
105 106 107

    target_str.str = symlink->target;
    target_str.len = symlink->len;
108
    if ((target = lookup_named_object( NULL, &target_str, attr, &name_left )))
109 110 111 112 113 114 115 116 117 118 119
    {
        if (name_left.len)
        {
            release_object( target );
            target = NULL;
            set_error( STATUS_OBJECT_PATH_NOT_FOUND );
        }
    }
    return target;
}

120 121 122 123 124 125 126 127 128
static unsigned int symlink_map_access( struct object *obj, unsigned int access )
{
    if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ | SYMBOLIC_LINK_QUERY;
    if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE;
    if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
    if (access & GENERIC_ALL)     access |= SYMBOLIC_LINK_ALL_ACCESS;
    return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
}

129 130 131 132 133 134 135
static void symlink_destroy( struct object *obj )
{
    struct symlink *symlink = (struct symlink *)obj;
    assert( obj->ops == &symlink_ops );
    free( symlink->target );
}

136 137 138
static struct symlink *create_symlink( struct object *root, const struct unicode_str *name,
                                       unsigned int attr, const struct unicode_str *target,
                                       const struct security_descriptor *sd )
139 140 141 142 143 144 145 146
{
    struct symlink *symlink;

    if (!target->len)
    {
        set_error( STATUS_INVALID_PARAMETER );
        return NULL;
    }
147
    if ((symlink = create_named_object( root, &symlink_ops, name, attr, sd )) &&
148 149 150
        (get_error() != STATUS_OBJECT_NAME_EXISTS))
    {
        if ((symlink->target = memdup( target->str, target->len )))
151
        {
152
            symlink->len = target->len;
153
        }
154 155 156 157 158 159 160 161 162
        else
        {
            release_object( symlink );
            symlink = NULL;
        }
    }
    return symlink;
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
/* create a symlink pointing to an existing object */
struct object *create_obj_symlink( struct object *root, const struct unicode_str *name,
                                    unsigned int attr, struct object *target,
                                    const struct security_descriptor *sd )
{
    struct symlink *symlink;
    data_size_t len;
    WCHAR *target_name;

    if (!(target_name = get_object_full_name( target, &len )))
    {
        set_error( STATUS_INVALID_PARAMETER );
        return NULL;
    }
    if ((symlink = create_named_object( root, &symlink_ops, name, attr, sd )) &&
        (get_error() != STATUS_OBJECT_NAME_EXISTS))
    {
        symlink->target = target_name;
        symlink->len = len;
    }
    else free( target_name );

    return &symlink->obj;
}

188 189 190 191 192 193

/* create a symbolic link object */
DECL_HANDLER(create_symlink)
{
    struct symlink *symlink;
    struct unicode_str name, target;
194
    struct object *root;
195
    const struct security_descriptor *sd;
196
    const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
197

198 199
    if (!objattr) return;

200 201
    target.str = get_req_data_after_objattr( objattr, &target.len );
    target.len = (target.len / sizeof(WCHAR)) * sizeof(WCHAR);
202

203
    if ((symlink = create_symlink( root, &name, objattr->attributes, &target, sd )))
204
    {
205
        reply->handle = alloc_handle( current->process, symlink, req->access, objattr->attributes );
206 207 208 209 210 211 212 213 214
        release_object( symlink );
    }

    if (root) release_object( root );
}

/* open a symbolic link object */
DECL_HANDLER(open_symlink)
{
215
    struct unicode_str name = get_req_unicode_str();
216

217 218
    reply->handle = open_object( current->process, req->rootdir, req->access,
                                 &symlink_ops, &name, req->attributes | OBJ_OPENLINK );
219 220 221 222 223 224 225 226 227 228 229
}

/* query a symbolic link object */
DECL_HANDLER(query_symlink)
{
    struct symlink *symlink;

    symlink = (struct symlink *)get_handle_obj( current->process, req->handle,
                                                SYMBOLIC_LINK_QUERY, &symlink_ops );
    if (!symlink) return;

230
    reply->total = symlink->len;
231 232 233 234 235 236
    if (get_reply_max_size() < symlink->len)
        set_error( STATUS_BUFFER_TOO_SMALL );
    else
        set_reply_data( symlink->target, symlink->len );
    release_object( symlink );
}