symlink.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. *
  20. */
  21. #include "config.h"
  22. #include "wine/port.h"
  23. #include <assert.h>
  24. #include <stdarg.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <sys/types.h>
  28. #include "ntstatus.h"
  29. #define WIN32_NO_STATUS
  30. #include "winternl.h"
  31. #include "ddk/wdm.h"
  32. #include "handle.h"
  33. #include "request.h"
  34. #include "object.h"
  35. #include "unicode.h"
  36. struct symlink
  37. {
  38. struct object obj; /* object header */
  39. WCHAR *target; /* target of the symlink */
  40. data_size_t len; /* target len in bytes */
  41. };
  42. static void symlink_dump( struct object *obj, int verbose );
  43. static struct object_type *symlink_get_type( struct object *obj );
  44. static unsigned int symlink_map_access( struct object *obj, unsigned int access );
  45. static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name,
  46. unsigned int attr );
  47. static void symlink_destroy( struct object *obj );
  48. static const struct object_ops symlink_ops =
  49. {
  50. sizeof(struct symlink), /* size */
  51. symlink_dump, /* dump */
  52. symlink_get_type, /* get_type */
  53. no_add_queue, /* add_queue */
  54. NULL, /* remove_queue */
  55. NULL, /* signaled */
  56. NULL, /* get_esync_fd */
  57. NULL, /* satisfied */
  58. no_signal, /* signal */
  59. no_get_fd, /* get_fd */
  60. symlink_map_access, /* map_access */
  61. default_get_sd, /* get_sd */
  62. default_set_sd, /* set_sd */
  63. symlink_lookup_name, /* lookup_name */
  64. directory_link_name, /* link_name */
  65. default_unlink_name, /* unlink_name */
  66. no_open_file, /* open_file */
  67. no_kernel_obj_list, /* get_kernel_obj_list */
  68. no_alloc_handle, /* alloc_handle */
  69. no_close_handle, /* close_handle */
  70. symlink_destroy /* destroy */
  71. };
  72. static void symlink_dump( struct object *obj, int verbose )
  73. {
  74. struct symlink *symlink = (struct symlink *)obj;
  75. assert( obj->ops == &symlink_ops );
  76. fputs( "Symlink target=\"", stderr );
  77. dump_strW( symlink->target, symlink->len, stderr, "\"\"" );
  78. fputs( "\"\n", stderr );
  79. }
  80. static struct object_type *symlink_get_type( struct object *obj )
  81. {
  82. static const struct unicode_str str = { type_SymbolicLink, sizeof(type_SymbolicLink) };
  83. return get_object_type( &str );
  84. }
  85. static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name,
  86. unsigned int attr )
  87. {
  88. struct symlink *symlink = (struct symlink *)obj;
  89. struct unicode_str target_str, name_left;
  90. struct object *target;
  91. assert( obj->ops == &symlink_ops );
  92. if (!name) return NULL;
  93. if (!name->len && (attr & OBJ_OPENLINK)) return NULL;
  94. target_str.str = symlink->target;
  95. target_str.len = symlink->len;
  96. if ((target = lookup_named_object( NULL, &target_str, attr, &name_left )))
  97. {
  98. if (name_left.len)
  99. {
  100. release_object( target );
  101. target = NULL;
  102. set_error( STATUS_OBJECT_PATH_NOT_FOUND );
  103. }
  104. }
  105. return target;
  106. }
  107. static unsigned int symlink_map_access( struct object *obj, unsigned int access )
  108. {
  109. if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYMBOLIC_LINK_QUERY;
  110. if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
  111. if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
  112. if (access & GENERIC_ALL) access |= SYMBOLIC_LINK_ALL_ACCESS;
  113. return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
  114. }
  115. static void symlink_destroy( struct object *obj )
  116. {
  117. struct symlink *symlink = (struct symlink *)obj;
  118. assert( obj->ops == &symlink_ops );
  119. free( symlink->target );
  120. }
  121. static struct symlink *create_symlink( struct object *root, const struct unicode_str *name,
  122. unsigned int attr, const struct unicode_str *target,
  123. const struct security_descriptor *sd )
  124. {
  125. struct symlink *symlink;
  126. if (!target->len)
  127. {
  128. set_error( STATUS_INVALID_PARAMETER );
  129. return NULL;
  130. }
  131. if ((symlink = create_named_object( root, &symlink_ops, name, attr, sd )) &&
  132. (get_error() != STATUS_OBJECT_NAME_EXISTS))
  133. {
  134. if ((symlink->target = memdup( target->str, target->len )))
  135. {
  136. symlink->len = target->len;
  137. }
  138. else
  139. {
  140. release_object( symlink );
  141. symlink = NULL;
  142. }
  143. }
  144. return symlink;
  145. }
  146. /* create a symlink pointing to an existing object */
  147. struct object *create_obj_symlink( struct object *root, const struct unicode_str *name,
  148. unsigned int attr, struct object *target,
  149. const struct security_descriptor *sd )
  150. {
  151. struct symlink *symlink;
  152. data_size_t len;
  153. WCHAR *target_name;
  154. if (!(target_name = get_object_full_name( target, &len )))
  155. {
  156. set_error( STATUS_INVALID_PARAMETER );
  157. return NULL;
  158. }
  159. if ((symlink = create_named_object( root, &symlink_ops, name, attr, sd )) &&
  160. (get_error() != STATUS_OBJECT_NAME_EXISTS))
  161. {
  162. symlink->target = target_name;
  163. symlink->len = len;
  164. }
  165. else free( target_name );
  166. return &symlink->obj;
  167. }
  168. /* create a symbolic link object */
  169. DECL_HANDLER(create_symlink)
  170. {
  171. struct symlink *symlink;
  172. struct unicode_str name, target;
  173. struct object *root;
  174. const struct security_descriptor *sd;
  175. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  176. if (!objattr) return;
  177. target.str = get_req_data_after_objattr( objattr, &target.len );
  178. target.len = (target.len / sizeof(WCHAR)) * sizeof(WCHAR);
  179. if ((symlink = create_symlink( root, &name, objattr->attributes, &target, sd )))
  180. {
  181. reply->handle = alloc_handle( current->process, symlink, req->access, objattr->attributes );
  182. release_object( symlink );
  183. }
  184. if (root) release_object( root );
  185. }
  186. /* open a symbolic link object */
  187. DECL_HANDLER(open_symlink)
  188. {
  189. struct unicode_str name = get_req_unicode_str();
  190. reply->handle = open_object( current->process, req->rootdir, req->access,
  191. &symlink_ops, &name, req->attributes | OBJ_OPENLINK );
  192. }
  193. /* query a symbolic link object */
  194. DECL_HANDLER(query_symlink)
  195. {
  196. struct symlink *symlink;
  197. symlink = (struct symlink *)get_handle_obj( current->process, req->handle,
  198. SYMBOLIC_LINK_QUERY, &symlink_ops );
  199. if (!symlink) return;
  200. reply->total = symlink->len;
  201. if (get_reply_max_size() < symlink->len)
  202. set_error( STATUS_BUFFER_TOO_SMALL );
  203. else
  204. set_reply_data( symlink->target, symlink->len );
  205. release_object( symlink );
  206. }