authdb.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 <stdio.h>
  42. #include <string.h>
  43. #include <plhash.h>
  44. #include <netsite.h>
  45. #include <ldaputil/errors.h>
  46. #include <ldaputil/certmap.h>
  47. #include <ldaputil/dbconf.h>
  48. #include <libaccess/acl.h>
  49. #include "aclpriv.h"
  50. #include <libaccess/authdb.h>
  51. #include <libaccess/aclproto.h>
  52. #include <libaccess/las.h>
  53. #include <libaccess/acl.h>
  54. #include <libaccess/aclglobal.h>
  55. #include <libaccess/dbtlibaccess.h>
  56. #include <libaccess/aclerror.h>
  57. #define BIG_LINE 1024
  58. char *ACL_default_dbname = 0;
  59. ACLDbType_t ACL_default_dbtype = ACL_DBTYPE_INVALID;
  60. ACLMethod_t ACL_default_method = ACL_METHOD_INVALID;
  61. int acl_registered_dbcnt = 0;
  62. extern int acl_registered_names(PLHashTable *ht, int count, char ***names);
  63. /************************** Database Types *************************/
  64. #define databaseNamesHashTable ACLDbNameHash
  65. int acl_num_databases ()
  66. {
  67. return acl_registered_dbcnt;
  68. }
  69. static int reg_dbname_internal (NSErr_t *errp, ACLDbType_t dbtype,
  70. const char *dbname, const char *url,
  71. PList_t plist)
  72. {
  73. DbParseFn_t parseFunc;
  74. void *db;
  75. int rv;
  76. AuthdbInfo_t *authdb_info;
  77. if (!ACL_DbTypeIsRegistered(errp, dbtype)) {
  78. nserrGenerate(errp, ACLERRFAIL, ACLERR4400, ACL_Program, 2, XP_GetAdminStr(DBT_DbtypeNotDefinedYet), dbname);
  79. return -1;
  80. }
  81. parseFunc = ACL_DbTypeParseFn(errp, dbtype);
  82. if (!parseFunc) {
  83. nserrGenerate(errp, ACLERRFAIL, ACLERR4400, ACL_Program, 2, XP_GetAdminStr(DBT_DbtypeNotDefinedYet), dbname);
  84. return -1;
  85. }
  86. rv = (*parseFunc)(errp, dbtype, dbname, url, plist, (void **)&db);
  87. if (rv < 0) {
  88. /* plist contains error message/code */
  89. return rv;
  90. }
  91. /* Store the db returned by the parse function in the hash table.
  92. */
  93. authdb_info = (AuthdbInfo_t *)pool_malloc(ACL_DATABASE_POOL, sizeof(AuthdbInfo_t));
  94. if (!authdb_info) {
  95. nserrGenerate(errp, ACLERRNOMEM, ACLERR4420, ACL_Program, 0);
  96. return -1;
  97. }
  98. authdb_info->dbname = pool_strdup(ACL_DATABASE_POOL, dbname);
  99. authdb_info->dbtype = dbtype;
  100. authdb_info->dbinfo = db; /* value returned from parseFunc */
  101. PR_HashTableAdd(ACLDbNameHash, authdb_info->dbname, authdb_info);
  102. acl_registered_dbcnt++;
  103. return 0;
  104. }
  105. NSAPI_PUBLIC int ACL_DatabaseRegister (NSErr_t *errp, ACLDbType_t dbtype,
  106. const char *dbname, const char *url,
  107. PList_t plist)
  108. {
  109. if (!dbname || !*dbname) {
  110. nserrGenerate(errp, ACLERRFAIL, ACLERR4500, ACL_Program, 1, XP_GetAdminStr(DBT_DatabaseRegisterDatabaseNameMissing));
  111. return -1;
  112. }
  113. return reg_dbname_internal(errp, dbtype, dbname, url, plist);
  114. }
  115. NSAPI_PUBLIC int
  116. ACL_DatabaseNamesGet(NSErr_t *errp, char ***names, int *count)
  117. {
  118. *count = acl_registered_dbcnt;
  119. return acl_registered_names (ACLDbNameHash, *count, names);
  120. }
  121. NSAPI_PUBLIC int
  122. ACL_DatabaseNamesFree(NSErr_t *errp, char **names, int count)
  123. {
  124. int i;
  125. for (i = count-1; i; i--) FREE(names[i]);
  126. FREE(names);
  127. return 0;
  128. }
  129. /* try to determine the dbtype from the database url */
  130. static int acl_url_to_dbtype (const char *url, ACLDbType_t *dbtype_out)
  131. {
  132. ACLDbType_t dbtype;
  133. NSErr_t *errp = 0;
  134. *dbtype_out = dbtype = ACL_DBTYPE_INVALID;
  135. if (!url || !*url) return -1;
  136. // urls with ldap:, ldaps: and ldapdb: are all of type ACL_DBTYPE_LDAP.
  137. if (!strncmp(url, URL_PREFIX_LDAP, URL_PREFIX_LDAP_LEN))
  138. dbtype = ACL_DbTypeLdap;
  139. else {
  140. /* treat prefix in the url as dbtype if it has been registered.
  141. */
  142. size_t prefix_len = strcspn(url, ":");
  143. char dbtypestr[BIG_LINE];
  144. if (prefix_len && (prefix_len < sizeof(dbtypestr))) {
  145. strncpy(dbtypestr, url, prefix_len);
  146. dbtypestr[prefix_len] = 0;
  147. if (!ACL_DbTypeFind(errp, dbtypestr, &dbtype)) {
  148. /* prefix is not a registered dbtype */
  149. dbtype = ACL_DBTYPE_INVALID;
  150. }
  151. }
  152. }
  153. if (ACL_DbTypeIsEqual(errp, dbtype, ACL_DBTYPE_INVALID)) {
  154. /* try all the registered parse functions to determine the dbtype */
  155. }
  156. if (ACL_DbTypeIsEqual(errp, dbtype, ACL_DBTYPE_INVALID)) return -1;
  157. *dbtype_out = dbtype;
  158. return 0;
  159. }
  160. NSAPI_PUBLIC int ACL_RegisterDbFromACL (NSErr_t *errp, const char *url,
  161. ACLDbType_t *dbtype)
  162. {
  163. /* If the database by name url is already registered, don't do anything.
  164. * If it is not registered, determine the dbtype from the url.
  165. * If the dbtype can be determined, register the database with dbname same
  166. * as the url. Return the dbtype.
  167. */
  168. void *db;
  169. int rv;
  170. PList_t plist;
  171. if (ACL_DatabaseFind(errp, url, dbtype, &db) == LAS_EVAL_TRUE)
  172. return 0;
  173. /* The database is not registered yet. Parse the url to find out its
  174. * type. If parsing fails, return failure.
  175. */
  176. rv = acl_url_to_dbtype(url, dbtype);
  177. if (rv < 0) {
  178. return rv;
  179. }
  180. plist = PListNew(NULL);
  181. rv = ACL_DatabaseRegister(errp, *dbtype, url, url, plist);
  182. PListDestroy(plist);
  183. return rv;
  184. }
  185. NSAPI_PUBLIC int ACL_DatabaseFind(NSErr_t *errp, const char *name,
  186. ACLDbType_t *dbtype, void **db)
  187. {
  188. AuthdbInfo_t *info;
  189. *dbtype = ACL_DBTYPE_INVALID;
  190. *db = 0;
  191. if (ACLDbNameHash) {
  192. info = (AuthdbInfo_t *)PR_HashTableLookup(ACLDbNameHash,
  193. name
  194. );
  195. if (info) {
  196. *dbtype = info->dbtype;
  197. *db = info->dbinfo;
  198. return LAS_EVAL_TRUE;
  199. }
  200. }
  201. return LAS_EVAL_FAIL;
  202. }
  203. void
  204. ACL_DatabaseDestroy(void)
  205. {
  206. pool_destroy(ACL_DATABASE_POOL);
  207. ACL_DATABASE_POOL = NULL;
  208. ACLDbNameHash = NULL;
  209. return;
  210. }