attrlist.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include "slap.h"
  42. void
  43. attrlist_free(Slapi_Attr *alist)
  44. {
  45. Slapi_Attr *a, *next;
  46. for ( a = alist; a != NULL; a = next )
  47. {
  48. next = a->a_next;
  49. slapi_attr_free( &a );
  50. }
  51. }
  52. /*
  53. * Search for the attribute.
  54. * If not found then create it,
  55. * and add it to the end of the list.
  56. * Return 0 for found, 1 for created.
  57. */
  58. int
  59. attrlist_find_or_create(Slapi_Attr **alist, const char *type, Slapi_Attr ***a)
  60. {
  61. return attrlist_find_or_create_locking_optional(alist, type, a, PR_TRUE);
  62. }
  63. int
  64. attrlist_find_or_create_locking_optional(Slapi_Attr **alist, const char *type, Slapi_Attr ***a, PRBool use_lock)
  65. {
  66. int rc= 0; /* found */
  67. if ( *a==NULL )
  68. {
  69. for ( *a = alist; **a != NULL; *a = &(**a)->a_next ) {
  70. if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
  71. break;
  72. }
  73. }
  74. }
  75. if( **a==NULL )
  76. {
  77. **a = slapi_attr_new();
  78. slapi_attr_init_locking_optional(**a, type, use_lock);
  79. rc= 1; /* created */
  80. }
  81. return rc;
  82. }
  83. /*
  84. * attrlist_merge - merge the given type and value with the list of
  85. * attributes in attrs.
  86. */
  87. void
  88. attrlist_merge(Slapi_Attr **alist, const char *type, struct berval **vals)
  89. {
  90. Slapi_Value **values= NULL;
  91. valuearray_init_bervalarray(vals,&values); /* JCM SLOW FUNCTION */
  92. attrlist_merge_valuearray(alist,type,values);
  93. valuearray_free(&values);
  94. }
  95. /*
  96. * attrlist_merge_valuearray - merge the given type and value with the list of
  97. * attributes in attrs.
  98. */
  99. void
  100. attrlist_merge_valuearray(Slapi_Attr **alist, const char *type, Slapi_Value **vals)
  101. {
  102. Slapi_Attr **a= NULL;
  103. attrlist_find_or_create(alist, type, &a);
  104. valueset_add_valuearray( &(*a)->a_present_values, vals );
  105. }
  106. /*
  107. * attrlist_find - find and return attribute type in list a
  108. */
  109. Slapi_Attr *
  110. attrlist_find(Slapi_Attr *a, const char *type)
  111. {
  112. for ( ; a != NULL; a = a->a_next ) {
  113. if ( strcasecmp( a->a_type, type ) == 0 ) {
  114. return( a );
  115. }
  116. }
  117. return( NULL );
  118. }
  119. /*
  120. * attrlist_count_subtypes
  121. *
  122. * Returns a count attributes which conform to type
  123. * in the attr list a. This count includes all subtypes of
  124. * type
  125. */
  126. int
  127. attrlist_count_subtypes(Slapi_Attr *a, const char *type)
  128. {
  129. int counter = 0;
  130. for ( ; a != NULL; a = a->a_next ) {
  131. if ( slapi_attr_type_cmp( type , a->a_type, SLAPI_TYPE_CMP_SUBTYPE) == 0 ) {
  132. counter++;
  133. }
  134. }
  135. return( counter );
  136. }
  137. /*
  138. * attrlist_find_ex
  139. *
  140. * Finds the first subtype in the list which matches "type"
  141. * starting at the beginning or hint depending on whether
  142. * hint has a value
  143. *
  144. * It is intended that hint be zero when first called and then
  145. * passed back in on subsequent calls until 0 is returned to mark
  146. * the end of the filtered list
  147. */
  148. Slapi_Attr *
  149. attrlist_find_ex(
  150. Slapi_Attr *a,
  151. const char *type,
  152. int *type_name_disposition, /* pass null if you're not interested */
  153. char** actual_type_name, /* pass null if you're not interested */
  154. void **hint
  155. )
  156. {
  157. Slapi_Attr **attr_cursor = (Slapi_Attr **)hint;
  158. if (type_name_disposition) *type_name_disposition = 0;
  159. if (actual_type_name) *actual_type_name = NULL;
  160. if(*attr_cursor == NULL)
  161. *attr_cursor = a; /* start at the beginning of the list */
  162. else
  163. *attr_cursor = (*attr_cursor)->a_next;
  164. while(*attr_cursor != NULL) {
  165. /* Determine whether the two types are related:*/
  166. if ( slapi_attr_type_cmp( type , (*attr_cursor)->a_type, SLAPI_TYPE_CMP_SUBTYPE) == 0 ) {
  167. /* We got a match. Now figure out if we matched because it was a subtype */
  168. if (type_name_disposition) {
  169. if ( 0 == slapi_attr_type_cmp( type , (*attr_cursor)->a_type, SLAPI_TYPE_CMP_EXACT) ) {
  170. *type_name_disposition = SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_EXACTLY_OR_ALIAS;
  171. } else {
  172. *type_name_disposition = SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_SUBTYPE;
  173. }
  174. }
  175. if (actual_type_name) {
  176. *actual_type_name = (*attr_cursor)->a_type;
  177. }
  178. a = *attr_cursor; /* the attribute to return */
  179. return( a );
  180. }
  181. *attr_cursor = (*attr_cursor)->a_next; /* no match, move cursor */
  182. }
  183. return( NULL );
  184. }
  185. /*
  186. * attr_remove - remove the attribute from the list of attributes
  187. */
  188. Slapi_Attr *
  189. attrlist_remove(Slapi_Attr **attrs, const char *type)
  190. {
  191. Slapi_Attr **a;
  192. Slapi_Attr *save= NULL;
  193. for ( a = attrs; *a != NULL; a = &(*a)->a_next )
  194. {
  195. if ( strcasecmp( (*a)->a_type, type ) == 0 )
  196. {
  197. break;
  198. }
  199. }
  200. if (*a != NULL)
  201. {
  202. save = *a;
  203. *a = (*a)->a_next;
  204. }
  205. return save;
  206. }
  207. void
  208. attrlist_add(Slapi_Attr **attrs, Slapi_Attr *a)
  209. {
  210. a->a_next= *attrs;
  211. *attrs= a;
  212. }
  213. /*
  214. * attrlist_delete - delete the attribute type in list pointed to by attrs
  215. * return 0 deleted ok
  216. * 1 not found in list a
  217. * -1 something bad happened
  218. */
  219. int
  220. attrlist_delete(Slapi_Attr **attrs, const char *type)
  221. {
  222. Slapi_Attr **a;
  223. Slapi_Attr *save;
  224. for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
  225. if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
  226. break;
  227. }
  228. }
  229. if ( *a == NULL ) {
  230. return( 1 );
  231. }
  232. save = *a;
  233. *a = (*a)->a_next;
  234. slapi_attr_free( &save );
  235. return( 0 );
  236. }
  237. /*
  238. * attrlist_replace - replace the attribute value(s) with this value(s)
  239. *
  240. * Returns
  241. * LDAP_SUCCESS - OK (including the attr not found)
  242. * LDAP_OPERATIONS_ERROR - Existing duplicates in attribute.
  243. */
  244. int attrlist_replace(Slapi_Attr **alist, const char *type, struct berval **vals)
  245. {
  246. Slapi_Attr **a = NULL;
  247. Slapi_Value **values = NULL;
  248. int rc = LDAP_SUCCESS;
  249. if (vals == NULL || vals[0] == NULL) {
  250. (void)attrlist_delete(alist, type);
  251. } else {
  252. attrlist_find_or_create(alist, type, &a);
  253. valuearray_init_bervalarray(vals, &values);
  254. rc = attr_replace(*a, values);
  255. }
  256. return rc;
  257. }
  258. /*
  259. * attrlist_replace_with_flags - replace the attribute value(s) with this value(s)
  260. *
  261. * Returns
  262. * LDAP_SUCCESS - OK (including the attr not found)
  263. * LDAP_OPERATIONS_ERROR - Existing duplicates in attribute.
  264. */
  265. int attrlist_replace_with_flags(Slapi_Attr **alist, const char *type, struct berval **vals, int flags)
  266. {
  267. Slapi_Attr **a = NULL;
  268. Slapi_Value **values = NULL;
  269. int rc = LDAP_SUCCESS;
  270. if (vals == NULL || vals[0] == NULL) {
  271. (void)attrlist_delete(alist, type);
  272. } else {
  273. attrlist_find_or_create(alist, type, &a);
  274. valuearray_init_bervalarray_with_flags(vals, &values, flags);
  275. rc = attr_replace(*a, values);
  276. }
  277. return rc;
  278. }