symlink.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 <assert.h>
  23. #include <stdarg.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <sys/types.h>
  27. #include "ntstatus.h"
  28. #define WIN32_NO_STATUS
  29. #include "winternl.h"
  30. #include "ddk/wdm.h"
  31. #include "handle.h"
  32. #include "request.h"
  33. #include "object.h"
  34. #include "unicode.h"
  35. static const WCHAR symlink_name[] = {'S','y','m','b','o','l','i','c','L','i','n','k'};
  36. struct type_descr symlink_type =
  37. {
  38. { symlink_name, sizeof(symlink_name) }, /* name */
  39. SYMBOLIC_LINK_ALL_ACCESS, /* valid_access */
  40. { /* mapping */
  41. STANDARD_RIGHTS_READ | SYMBOLIC_LINK_QUERY,
  42. STANDARD_RIGHTS_WRITE,
  43. STANDARD_RIGHTS_EXECUTE | SYMBOLIC_LINK_QUERY,
  44. SYMBOLIC_LINK_ALL_ACCESS
  45. },
  46. };
  47. struct symlink
  48. {
  49. struct object obj; /* object header */
  50. WCHAR *target; /* target of the symlink */
  51. data_size_t len; /* target len in bytes */
  52. };
  53. static void symlink_dump( struct object *obj, int verbose );
  54. static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name,
  55. unsigned int attr, struct object *root );
  56. static void symlink_destroy( struct object *obj );
  57. static const struct object_ops symlink_ops =
  58. {
  59. sizeof(struct symlink), /* size */
  60. &symlink_type, /* type */
  61. symlink_dump, /* dump */
  62. no_add_queue, /* add_queue */
  63. NULL, /* remove_queue */
  64. NULL, /* signaled */
  65. NULL, /* satisfied */
  66. no_signal, /* signal */
  67. no_get_fd, /* get_fd */
  68. default_map_access, /* map_access */
  69. default_get_sd, /* get_sd */
  70. default_set_sd, /* set_sd */
  71. default_get_full_name, /* get_full_name */
  72. symlink_lookup_name, /* lookup_name */
  73. directory_link_name, /* link_name */
  74. default_unlink_name, /* unlink_name */
  75. no_open_file, /* open_file */
  76. no_kernel_obj_list, /* get_kernel_obj_list */
  77. no_get_fast_sync, /* get_fast_sync */
  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. if (!symlink->len) return get_root_directory();
  100. target_str.str = symlink->target;
  101. target_str.len = symlink->len;
  102. if ((target = lookup_named_object( NULL, &target_str, attr, &name_left )))
  103. {
  104. if (name_left.len)
  105. {
  106. release_object( target );
  107. target = NULL;
  108. set_error( STATUS_OBJECT_PATH_NOT_FOUND );
  109. }
  110. }
  111. return target;
  112. }
  113. static void symlink_destroy( struct object *obj )
  114. {
  115. struct symlink *symlink = (struct symlink *)obj;
  116. assert( obj->ops == &symlink_ops );
  117. free( symlink->target );
  118. }
  119. struct object *create_root_symlink( struct object *root, const struct unicode_str *name,
  120. unsigned int attr, const struct security_descriptor *sd )
  121. {
  122. struct symlink *symlink;
  123. if (!(symlink = create_named_object( root, &symlink_ops, name, attr, sd ))) return NULL;
  124. symlink->target = NULL;
  125. symlink->len = 0;
  126. return &symlink->obj;
  127. }
  128. struct object *create_symlink( struct object *root, const struct unicode_str *name,
  129. unsigned int attr, const struct unicode_str *target,
  130. const struct security_descriptor *sd )
  131. {
  132. struct symlink *symlink;
  133. if (!target->len)
  134. {
  135. set_error( STATUS_INVALID_PARAMETER );
  136. return NULL;
  137. }
  138. if (!(symlink = create_named_object( root, &symlink_ops, name, attr, sd ))) return NULL;
  139. if (get_error() != STATUS_OBJECT_NAME_EXISTS && !(symlink->target = memdup( target->str, target->len )))
  140. {
  141. release_object( symlink );
  142. return NULL;
  143. }
  144. symlink->len = target->len;
  145. return &symlink->obj;
  146. }
  147. /* create a symlink pointing to an existing object */
  148. struct object *create_obj_symlink( struct object *root, const struct unicode_str *name,
  149. unsigned int attr, struct object *target,
  150. const struct security_descriptor *sd )
  151. {
  152. struct symlink *symlink;
  153. data_size_t len;
  154. WCHAR *target_name;
  155. if (!(target_name = target->ops->get_full_name( target, &len )))
  156. {
  157. set_error( STATUS_INVALID_PARAMETER );
  158. return NULL;
  159. }
  160. if ((symlink = create_named_object( root, &symlink_ops, name, attr, sd )) &&
  161. (get_error() != STATUS_OBJECT_NAME_EXISTS))
  162. {
  163. symlink->target = target_name;
  164. symlink->len = len;
  165. }
  166. else free( target_name );
  167. return &symlink->obj;
  168. }
  169. /* create a symbolic link object */
  170. DECL_HANDLER(create_symlink)
  171. {
  172. struct object *symlink;
  173. struct unicode_str name, target;
  174. struct object *root;
  175. const struct security_descriptor *sd;
  176. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  177. if (!objattr) return;
  178. target.str = get_req_data_after_objattr( objattr, &target.len );
  179. target.len = (target.len / sizeof(WCHAR)) * sizeof(WCHAR);
  180. if ((symlink = create_symlink( root, &name, objattr->attributes, &target, sd )))
  181. {
  182. reply->handle = alloc_handle( current->process, symlink, req->access, objattr->attributes );
  183. release_object( symlink );
  184. }
  185. if (root) release_object( root );
  186. }
  187. /* open a symbolic link object */
  188. DECL_HANDLER(open_symlink)
  189. {
  190. struct unicode_str name = get_req_unicode_str();
  191. reply->handle = open_object( current->process, req->rootdir, req->access,
  192. &symlink_ops, &name, req->attributes | OBJ_OPENLINK );
  193. }
  194. /* query a symbolic link object */
  195. DECL_HANDLER(query_symlink)
  196. {
  197. struct symlink *symlink;
  198. symlink = (struct symlink *)get_handle_obj( current->process, req->handle,
  199. SYMBOLIC_LINK_QUERY, &symlink_ops );
  200. if (!symlink) return;
  201. reply->total = symlink->len;
  202. if (get_reply_max_size() < symlink->len)
  203. set_error( STATUS_BUFFER_TOO_SMALL );
  204. else
  205. set_reply_data( symlink->target, symlink->len );
  206. release_object( symlink );
  207. }