symlink.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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_close_handle, /* close_handle */
  78. symlink_destroy /* destroy */
  79. };
  80. static void symlink_dump( struct object *obj, int verbose )
  81. {
  82. struct symlink *symlink = (struct symlink *)obj;
  83. assert( obj->ops == &symlink_ops );
  84. fputs( "Symlink target=\"", stderr );
  85. dump_strW( symlink->target, symlink->len, stderr, "\"\"" );
  86. fputs( "\"\n", stderr );
  87. }
  88. static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name,
  89. unsigned int attr, struct object *root )
  90. {
  91. struct symlink *symlink = (struct symlink *)obj;
  92. struct unicode_str target_str, name_left;
  93. struct object *target;
  94. assert( obj->ops == &symlink_ops );
  95. if (!name) return NULL;
  96. if (!name->len && (attr & OBJ_OPENLINK)) return NULL;
  97. if (obj == root) return NULL;
  98. if (!symlink->len) return get_root_directory();
  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_root_symlink( struct object *root, const struct unicode_str *name,
  119. unsigned int attr, const struct security_descriptor *sd )
  120. {
  121. struct symlink *symlink;
  122. if (!(symlink = create_named_object( root, &symlink_ops, name, attr, sd ))) return NULL;
  123. symlink->target = NULL;
  124. symlink->len = 0;
  125. return &symlink->obj;
  126. }
  127. struct object *create_symlink( struct object *root, const struct unicode_str *name,
  128. unsigned int attr, const struct unicode_str *target,
  129. const struct security_descriptor *sd )
  130. {
  131. struct symlink *symlink;
  132. if (!target->len)
  133. {
  134. set_error( STATUS_INVALID_PARAMETER );
  135. return NULL;
  136. }
  137. if (!(symlink = create_named_object( root, &symlink_ops, name, attr, sd ))) return NULL;
  138. if (get_error() != STATUS_OBJECT_NAME_EXISTS && !(symlink->target = memdup( target->str, target->len )))
  139. {
  140. release_object( symlink );
  141. return NULL;
  142. }
  143. symlink->len = target->len;
  144. return &symlink->obj;
  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 = target->ops->get_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 object *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. }