match.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. /*
  42. * match.c
  43. *
  44. * routines to "register" matching rules with the server
  45. *
  46. *
  47. *
  48. *
  49. */
  50. #include "slap.h"
  51. struct matchingRuleList *g_get_global_mrl(void);
  52. void g_set_global_mrl(struct matchingRuleList *newglobalmrl);
  53. int slapi_matchingrule_register(Slapi_MatchingRuleEntry *mrule);
  54. int slapi_matchingrule_unregister(char *oid);
  55. Slapi_MatchingRuleEntry *slapi_matchingrule_new(void);
  56. void slapi_matchingrule_free(Slapi_MatchingRuleEntry **mrEntry,
  57. int freeMembers);
  58. int slapi_matchingrule_get(Slapi_MatchingRuleEntry *mr, int arg, void *value);
  59. int slapi_matchingrule_set(Slapi_MatchingRuleEntry *mr, int arg, void *value);
  60. static int _mr_alloc_new(struct matchingRuleList **mrl);
  61. static struct matchingRuleList *global_mrl=NULL;
  62. struct matchingRuleList*
  63. g_get_global_mrl(void)
  64. {
  65. return global_mrl;
  66. }
  67. void
  68. g_set_global_mrl(struct matchingRuleList *newglobalmrl)
  69. {
  70. global_mrl = newglobalmrl;
  71. }
  72. int
  73. slapi_matchingrule_set(Slapi_MatchingRuleEntry *mr, int arg, void *value)
  74. {
  75. if(NULL == mr) {
  76. return(-1);
  77. }
  78. switch(arg) {
  79. case SLAPI_MATCHINGRULE_NAME:
  80. {
  81. mr->mr_name = (char *)value;
  82. break;
  83. }
  84. case SLAPI_MATCHINGRULE_OID:
  85. {
  86. mr->mr_oid = (char *)value;
  87. break;
  88. }
  89. case SLAPI_MATCHINGRULE_DESC:
  90. {
  91. mr->mr_desc = (char *)value;
  92. break;
  93. }
  94. case SLAPI_MATCHINGRULE_SYNTAX:
  95. {
  96. mr->mr_syntax = (char *)value;
  97. break;
  98. }
  99. case SLAPI_MATCHINGRULE_OBSOLETE:
  100. {
  101. mr->mr_obsolete = *((int *)value);
  102. break;
  103. }
  104. default:
  105. {
  106. break;
  107. }
  108. }
  109. return(0);
  110. }
  111. int
  112. slapi_matchingrule_get(Slapi_MatchingRuleEntry *mr, int arg, void *value)
  113. {
  114. if((NULL == mr) || (NULL == value)) {
  115. return(-1);
  116. }
  117. switch(arg) {
  118. case SLAPI_MATCHINGRULE_NAME:
  119. {
  120. (*(char **)value) = mr->mr_name;
  121. break;
  122. }
  123. case SLAPI_MATCHINGRULE_OID:
  124. {
  125. (*(char **)value) = mr->mr_oid;
  126. break;
  127. }
  128. case SLAPI_MATCHINGRULE_DESC:
  129. {
  130. (*(char **)value) = mr->mr_desc;
  131. break;
  132. }
  133. case SLAPI_MATCHINGRULE_SYNTAX:
  134. {
  135. (*(char **)value) = mr->mr_syntax;
  136. break;
  137. }
  138. case SLAPI_MATCHINGRULE_OBSOLETE:
  139. {
  140. (*(int *)value) = mr->mr_obsolete;
  141. break;
  142. }
  143. default:
  144. {
  145. return(-1);
  146. }
  147. }
  148. return(0);
  149. }
  150. Slapi_MatchingRuleEntry *
  151. slapi_matchingrule_new(void)
  152. {
  153. Slapi_MatchingRuleEntry *mrEntry=NULL;
  154. mrEntry = (Slapi_MatchingRuleEntry *)
  155. slapi_ch_calloc(1, sizeof(Slapi_MatchingRuleEntry));
  156. return(mrEntry);
  157. }
  158. void
  159. slapi_matchingrule_free(Slapi_MatchingRuleEntry **mrEntry,
  160. int freeMembers)
  161. {
  162. if((NULL == mrEntry) || (NULL == *mrEntry)) {
  163. return;
  164. }
  165. if(freeMembers) {
  166. slapi_ch_free((void **)&((*mrEntry)->mr_name));
  167. slapi_ch_free((void **)&((*mrEntry)->mr_oid));
  168. slapi_ch_free((void **)&((*mrEntry)->mr_desc));
  169. slapi_ch_free((void **)&((*mrEntry)->mr_syntax));
  170. slapi_ch_free((void **)&((*mrEntry)->mr_oidalias));
  171. }
  172. slapi_ch_free((void **)mrEntry);
  173. return;
  174. }
  175. static int
  176. _mr_alloc_new(struct matchingRuleList **mrl)
  177. {
  178. if(!mrl) {
  179. return(-1);
  180. }
  181. *mrl = NULL;
  182. *mrl = (struct matchingRuleList *)
  183. slapi_ch_calloc(1, sizeof(struct matchingRuleList));
  184. (*mrl)->mr_entry = (Slapi_MatchingRuleEntry *)
  185. slapi_ch_calloc(1, sizeof(Slapi_MatchingRuleEntry));
  186. return(0);
  187. }
  188. #if 0
  189. static int
  190. _mr_free(struct matchingRuleList **mrl /*, int freeEntry */)
  191. {
  192. slapi_ch_free((void **)mrl);
  193. return(0);
  194. }
  195. #endif
  196. int slapi_matchingrule_register(Slapi_MatchingRuleEntry *mrule)
  197. {
  198. struct matchingRuleList *mrl=NULL;
  199. struct matchingRuleList *newmrl=NULL;
  200. if(NULL == mrule) {
  201. return(-1);
  202. }
  203. if(_mr_alloc_new(&newmrl) != 0) {
  204. return(-1);
  205. }
  206. if(NULL != mrule->mr_name) {
  207. newmrl->mr_entry->mr_name =
  208. slapi_ch_strdup((char *) mrule->mr_name);
  209. }
  210. if(NULL != mrule->mr_oid) {
  211. newmrl->mr_entry->mr_oid =
  212. slapi_ch_strdup((char *) mrule->mr_oid);
  213. }
  214. if(NULL != mrule->mr_oidalias) {
  215. newmrl->mr_entry->mr_oidalias =
  216. slapi_ch_strdup((char *) mrule->mr_oidalias);
  217. }
  218. if(NULL != mrule->mr_desc) {
  219. newmrl->mr_entry->mr_desc =
  220. slapi_ch_strdup((char *) mrule->mr_desc);
  221. }
  222. if(NULL != mrule->mr_syntax) {
  223. newmrl->mr_entry->mr_syntax =
  224. slapi_ch_strdup((char *) mrule->mr_syntax);
  225. }
  226. newmrl->mr_entry->mr_obsolete = mrule->mr_obsolete;
  227. for(mrl = g_get_global_mrl();
  228. ((NULL != mrl) && (NULL != mrl->mrl_next));
  229. mrl = mrl->mrl_next);
  230. if(NULL == mrl) {
  231. g_set_global_mrl(newmrl);
  232. mrl = newmrl;
  233. }
  234. mrl->mrl_next = newmrl;
  235. newmrl->mrl_next = NULL;
  236. return(LDAP_SUCCESS);
  237. }
  238. int slapi_matchingrule_unregister(char *oid)
  239. {
  240. /*
  241. * Currently, not implemented.
  242. * For now, the matching rules are read at startup and cannot be modified.
  243. * If and when, we do allow dynamic modifications, this routine will
  244. * have to do some work.
  245. */
  246. return(0);
  247. }
  248. /*
  249. See if a matching rule for this name or OID
  250. is registered and is an ORDERING matching rule that applies
  251. to the given syntax.
  252. */
  253. int slapi_matchingrule_is_ordering(const char *oid_or_name, const char *syntax_oid)
  254. {
  255. struct matchingRuleList *mrl=NULL;
  256. for (mrl = g_get_global_mrl(); mrl != NULL; mrl = mrl->mrl_next) {
  257. if (mrl->mr_entry->mr_name && !strcasecmp(oid_or_name, mrl->mr_entry->mr_name)) {
  258. return (mrl->mr_entry->mr_name &&
  259. PL_strcasestr(mrl->mr_entry->mr_name, "ordering") &&
  260. !strcmp(mrl->mr_entry->mr_syntax, syntax_oid));
  261. }
  262. if (mrl->mr_entry->mr_oid && !strcmp(oid_or_name, mrl->mr_entry->mr_oid)) {
  263. return (mrl->mr_entry->mr_name &&
  264. PL_strcasestr(mrl->mr_entry->mr_name, "ordering") &&
  265. !strcmp(mrl->mr_entry->mr_syntax, syntax_oid));
  266. }
  267. }
  268. return 0;
  269. }