symlink.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. static const WCHAR symlink_name[] = {'S','y','m','b','o','l','i','c','L','i','n','k'};
  37. struct type_descr symlink_type =
  38. {
  39. { symlink_name, sizeof(symlink_name) }, /* name */
  40. SYMBOLIC_LINK_ALL_ACCESS, /* valid_access */
  41. { /* mapping */
  42. STANDARD_RIGHTS_READ | SYMBOLIC_LINK_QUERY,
  43. STANDARD_RIGHTS_WRITE,
  44. STANDARD_RIGHTS_EXECUTE | SYMBOLIC_LINK_QUERY,
  45. SYMBOLIC_LINK_ALL_ACCESS
  46. },
  47. };
  48. struct symlink
  49. {
  50. struct object obj; /* object header */
  51. WCHAR *target; /* target of the symlink */
  52. data_size_t len; /* target len in bytes */
  53. };
  54. static void symlink_dump( struct object *obj, int verbose );
  55. static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name,
  56. unsigned int attr, struct object *root );
  57. static void symlink_destroy( struct object *obj );
  58. static const struct object_ops symlink_ops =
  59. {
  60. sizeof(struct symlink), /* size */
  61. &symlink_type, /* type */
  62. symlink_dump, /* dump */
  63. no_add_queue, /* add_queue */
  64. NULL, /* remove_queue */
  65. NULL, /* signaled */
  66. NULL, /* satisfied */
  67. no_signal, /* signal */
  68. no_get_fd, /* get_fd */
  69. default_map_access, /* map_access */
  70. default_get_sd, /* get_sd */
  71. default_set_sd, /* set_sd */
  72. default_get_full_name, /* get_full_name */
  73. symlink_lookup_name, /* lookup_name */
  74. directory_link_name, /* link_name */
  75. default_unlink_name, /* unlink_name */
  76. no_open_file, /* open_file */
  77. no_kernel_obj_list, /* get_kernel_obj_list */
  78. no_close_handle, /* close_handle */
  79. symlink_destroy /* destroy */
  80. };
  81. static void symlink_dump( struct object *obj, int verbose )
  82. {
  83. struct symlink *symlink = (struct symlink *)obj;
  84. assert( obj->ops == &symlink_ops );
  85. fputs( "Symlink target=\"", stderr );
  86. dump_strW( symlink->target, symlink->len, stderr, "\"\"" );
  87. fputs( "\"\n", stderr );
  88. }
  89. static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name,
  90. unsigned int attr, struct object *root )
  91. {
  92. struct symlink *symlink = (struct symlink *)obj;
  93. struct unicode_str target_str, name_left;
  94. struct object *target;
  95. assert( obj->ops == &symlink_ops );
  96. if (!name) return NULL;
  97. if (!name->len && (attr & OBJ_OPENLINK)) return NULL;
  98. if (obj == root) return NULL;
  99. target_str.str = symlink->target;
  100. target_str.len = symlink->len;
  101. if ((target = lookup_named_object( NULL, &target_str, attr, &name_left )))
  102. {
  103. if (name_left.len)
  104. {
  105. release_object( target );
  106. target = NULL;
  107. set_error( STATUS_OBJECT_PATH_NOT_FOUND );
  108. }
  109. }
  110. return target;
  111. }
  112. static void symlink_destroy( struct object *obj )
  113. {
  114. struct symlink *symlink = (struct symlink *)obj;
  115. assert( obj->ops == &symlink_ops );
  116. free( symlink->target );
  117. }
  118. struct object *create_symlink( struct object *root, const struct unicode_str *name,
  119. unsigned int attr, const struct unicode_str *target,
  120. const struct security_descriptor *sd )
  121. {
  122. struct symlink *symlink;
  123. if (!target->len)
  124. {
  125. set_error( STATUS_INVALID_PARAMETER );
  126. return NULL;
  127. }
  128. if (!(symlink = create_named_object( root, &symlink_ops, name, attr, sd ))) return NULL;
  129. if (get_error() != STATUS_OBJECT_NAME_EXISTS && !(symlink->target = memdup( target->str, target->len )))
  130. {
  131. release_object( symlink );
  132. return NULL;
  133. }
  134. symlink->len = target->len;
  135. return &symlink->obj;
  136. }
  137. /* create a symlink pointing to an existing object */
  138. struct object *create_obj_symlink( struct object *root, const struct unicode_str *name,
  139. unsigned int attr, struct object *target,
  140. const struct security_descriptor *sd )
  141. {
  142. struct symlink *symlink;
  143. data_size_t len;
  144. WCHAR *target_name;
  145. if (!(target_name = target->ops->get_full_name( target, &len )))
  146. {
  147. set_error( STATUS_INVALID_PARAMETER );
  148. return NULL;
  149. }
  150. if ((symlink = create_named_object( root, &symlink_ops, name, attr, sd )) &&
  151. (get_error() != STATUS_OBJECT_NAME_EXISTS))
  152. {
  153. symlink->target = target_name;
  154. symlink->len = len;
  155. }
  156. else free( target_name );
  157. return &symlink->obj;
  158. }
  159. /* create a symbolic link object */
  160. DECL_HANDLER(create_symlink)
  161. {
  162. struct object *symlink;
  163. struct unicode_str name, target;
  164. struct object *root;
  165. const struct security_descriptor *sd;
  166. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  167. if (!objattr) return;
  168. target.str = get_req_data_after_objattr( objattr, &target.len );
  169. target.len = (target.len / sizeof(WCHAR)) * sizeof(WCHAR);
  170. if ((symlink = create_symlink( root, &name, objattr->attributes, &target, sd )))
  171. {
  172. reply->handle = alloc_handle( current->process, symlink, req->access, objattr->attributes );
  173. release_object( symlink );
  174. }
  175. if (root) release_object( root );
  176. }
  177. /* open a symbolic link object */
  178. DECL_HANDLER(open_symlink)
  179. {
  180. struct unicode_str name = get_req_unicode_str();
  181. reply->handle = open_object( current->process, req->rootdir, req->access,
  182. &symlink_ops, &name, req->attributes | OBJ_OPENLINK );
  183. }
  184. /* query a symbolic link object */
  185. DECL_HANDLER(query_symlink)
  186. {
  187. struct symlink *symlink;
  188. symlink = (struct symlink *)get_handle_obj( current->process, req->handle,
  189. SYMBOLIC_LINK_QUERY, &symlink_ops );
  190. if (!symlink) return;
  191. reply->total = symlink->len;
  192. if (get_reply_max_size() < symlink->len)
  193. set_error( STATUS_BUFFER_TOO_SMALL );
  194. else
  195. set_reply_data( symlink->target, symlink->len );
  196. release_object( symlink );
  197. }