authdb.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 "permhash.h"
  46. #include <ldaputil/errors.h>
  47. #include <ldaputil/certmap.h>
  48. #include <ldaputil/dbconf.h>
  49. #include <libaccess/acl.h>
  50. #include "aclpriv.h"
  51. #include <libaccess/authdb.h>
  52. #include <libaccess/aclproto.h>
  53. #include <libaccess/las.h>
  54. #include <libaccess/acl.h>
  55. #include <libaccess/aclglobal.h>
  56. #include <libaccess/dbtlibaccess.h>
  57. #include <libaccess/aclerror.h>
  58. #define BIG_LINE 1024
  59. char *ACL_default_dbname = 0;
  60. ACLDbType_t ACL_default_dbtype = ACL_DBTYPE_INVALID;
  61. ACLMethod_t ACL_default_method = ACL_METHOD_INVALID;
  62. int acl_registered_dbcnt = 0;
  63. extern int acl_registered_names(PLHashTable *ht, int count, char ***names);
  64. /************************** Database Types *************************/
  65. #define databaseNamesHashTable ACLDbNameHash
  66. int acl_num_databases ()
  67. {
  68. return acl_registered_dbcnt;
  69. }
  70. static int reg_dbname_internal (NSErr_t *errp, ACLDbType_t dbtype,
  71. const char *dbname, const char *url,
  72. PList_t plist)
  73. {
  74. DbParseFn_t parseFunc;
  75. void *db;
  76. int rv;
  77. AuthdbInfo_t *authdb_info;
  78. if (!ACL_DbTypeIsRegistered(errp, dbtype)) {
  79. nserrGenerate(errp, ACLERRFAIL, ACLERR4400, ACL_Program, 2, XP_GetAdminStr(DBT_DbtypeNotDefinedYet), dbname);
  80. return -1;
  81. }
  82. parseFunc = ACL_DbTypeParseFn(errp, dbtype);
  83. if (!parseFunc) {
  84. nserrGenerate(errp, ACLERRFAIL, ACLERR4400, ACL_Program, 2, XP_GetAdminStr(DBT_DbtypeNotDefinedYet), dbname);
  85. return -1;
  86. }
  87. rv = (*parseFunc)(errp, dbtype, dbname, url, plist, (void **)&db);
  88. if (rv < 0) {
  89. /* plist contains error message/code */
  90. return rv;
  91. }
  92. /* Store the db returned by the parse function in the hash table.
  93. */
  94. authdb_info = (AuthdbInfo_t *)pool_malloc(ACL_DATABASE_POOL, sizeof(AuthdbInfo_t));
  95. if (!authdb_info) {
  96. nserrGenerate(errp, ACLERRNOMEM, ACLERR4420, ACL_Program, 0);
  97. return -1;
  98. }
  99. authdb_info->dbname = pool_strdup(ACL_DATABASE_POOL, dbname);
  100. authdb_info->dbtype = dbtype;
  101. authdb_info->dbinfo = db; /* value returned from parseFunc */
  102. PR_HashTableAdd(ACLDbNameHash, authdb_info->dbname, authdb_info);
  103. acl_registered_dbcnt++;
  104. return 0;
  105. }
  106. NSAPI_PUBLIC int ACL_DatabaseRegister (NSErr_t *errp, ACLDbType_t dbtype,
  107. const char *dbname, const char *url,
  108. PList_t plist)
  109. {
  110. if (!dbname || !*dbname) {
  111. nserrGenerate(errp, ACLERRFAIL, ACLERR4500, ACL_Program, 1, XP_GetAdminStr(DBT_DatabaseRegisterDatabaseNameMissing));
  112. return -1;
  113. }
  114. return reg_dbname_internal(errp, dbtype, dbname, url, plist);
  115. }
  116. NSAPI_PUBLIC int
  117. ACL_DatabaseNamesGet(NSErr_t *errp, char ***names, int *count)
  118. {
  119. *count = acl_registered_dbcnt;
  120. return acl_registered_names (ACLDbNameHash, *count, names);
  121. }
  122. NSAPI_PUBLIC int
  123. ACL_DatabaseNamesFree(NSErr_t *errp, char **names, int count)
  124. {
  125. int i;
  126. for (i = count-1; i; i--) FREE(names[i]);
  127. FREE(names);
  128. return 0;
  129. }
  130. /* try to determine the dbtype from the database url */
  131. static int acl_url_to_dbtype (const char *url, ACLDbType_t *dbtype_out)
  132. {
  133. ACLDbType_t dbtype;
  134. NSErr_t *errp = 0;
  135. *dbtype_out = dbtype = ACL_DBTYPE_INVALID;
  136. if (!url || !*url) return -1;
  137. // urls with ldap:, ldaps: and ldapdb: are all of type ACL_DBTYPE_LDAP.
  138. if (!strncmp(url, URL_PREFIX_LDAP, URL_PREFIX_LDAP_LEN))
  139. dbtype = ACL_DbTypeLdap;
  140. else {
  141. /* treat prefix in the url as dbtype if it has been registered.
  142. */
  143. size_t prefix_len = strcspn(url, ":");
  144. char dbtypestr[BIG_LINE];
  145. if (prefix_len && (prefix_len < sizeof(dbtypestr))) {
  146. strncpy(dbtypestr, url, prefix_len);
  147. dbtypestr[prefix_len] = 0;
  148. if (!ACL_DbTypeFind(errp, dbtypestr, &dbtype)) {
  149. /* prefix is not a registered dbtype */
  150. dbtype = ACL_DBTYPE_INVALID;
  151. }
  152. }
  153. }
  154. if (ACL_DbTypeIsEqual(errp, dbtype, ACL_DBTYPE_INVALID)) {
  155. /* try all the registered parse functions to determine the dbtype */
  156. }
  157. if (ACL_DbTypeIsEqual(errp, dbtype, ACL_DBTYPE_INVALID)) return -1;
  158. *dbtype_out = dbtype;
  159. return 0;
  160. }
  161. NSAPI_PUBLIC int ACL_RegisterDbFromACL (NSErr_t *errp, const char *url,
  162. ACLDbType_t *dbtype)
  163. {
  164. /* If the database by name url is already registered, don't do anything.
  165. * If it is not registered, determine the dbtype from the url.
  166. * If the dbtype can be determined, register the database with dbname same
  167. * as the url. Return the dbtype.
  168. */
  169. void *db;
  170. int rv;
  171. PList_t plist;
  172. if (ACL_DatabaseFind(errp, url, dbtype, &db) == LAS_EVAL_TRUE)
  173. return 0;
  174. /* The database is not registered yet. Parse the url to find out its
  175. * type. If parsing fails, return failure.
  176. */
  177. rv = acl_url_to_dbtype(url, dbtype);
  178. if (rv < 0) {
  179. return rv;
  180. }
  181. plist = PListNew(NULL);
  182. rv = ACL_DatabaseRegister(errp, *dbtype, url, url, plist);
  183. PListDestroy(plist);
  184. return rv;
  185. }
  186. NSAPI_PUBLIC int ACL_DatabaseFind(NSErr_t *errp, const char *name,
  187. ACLDbType_t *dbtype, void **db)
  188. {
  189. AuthdbInfo_t *info;
  190. *dbtype = ACL_DBTYPE_INVALID;
  191. *db = 0;
  192. if (ACLDbNameHash) {
  193. info = (AuthdbInfo_t *)PR_HashTableLookup(ACLDbNameHash,
  194. name
  195. );
  196. if (info) {
  197. *dbtype = info->dbtype;
  198. *db = info->dbinfo;
  199. return LAS_EVAL_TRUE;
  200. }
  201. }
  202. return LAS_EVAL_FAIL;
  203. }
  204. NSAPI_PUBLIC int ACL_ReadDbMapFile (NSErr_t *errp, const char *map_file,
  205. int default_only)
  206. {
  207. DBConfInfo_t *info;
  208. DBConfDBInfo_t *db_info;
  209. DBPropVal_t *propval;
  210. PList_t plist;
  211. int rv;
  212. int seen_default = 0;
  213. if (default_only)
  214. rv = dbconf_read_default_dbinfo(map_file, &db_info);
  215. else
  216. rv = dbconf_read_config_file(map_file, &info);
  217. if (rv != LDAPU_SUCCESS) {
  218. nserrGenerate(errp, ACLERRFAIL, ACLERR4600, ACL_Program, 3, XP_GetAdminStr(DBT_ReadDbMapFileErrorReadingFile), map_file, ldapu_err2string(rv));
  219. return -1;
  220. }
  221. rv = 0;
  222. if (!default_only)
  223. db_info = info->firstdb;
  224. while(db_info) {
  225. char *url = db_info->url;
  226. char *dbname = db_info->dbname;
  227. ACLDbType_t dbtype;
  228. /* process db_info */
  229. if (url) {
  230. rv = acl_url_to_dbtype(url, &dbtype);
  231. if (rv < 0) {
  232. nserrGenerate(errp, ACLERRFAIL, ACLERR4610, ACL_Program, 2,
  233. XP_GetAdminStr(DBT_ReadDbMapFileCouldntDetermineDbtype), url);
  234. break;
  235. }
  236. }
  237. else {
  238. nserrGenerate(errp, ACLERRFAIL, ACLERR4620, ACL_Program, 2,
  239. XP_GetAdminStr(DBT_ReadDbMapFileMissingUrl), dbname);
  240. rv = -1;
  241. break;
  242. }
  243. /* convert any property-value pairs in db_info into plist */
  244. plist = PListNew(NULL);
  245. propval = db_info->firstprop;
  246. while(propval) {
  247. if (propval->prop) {
  248. PListInitProp(plist, 0, propval->prop, propval->val, 0);
  249. }
  250. else {
  251. nserrGenerate(errp, ACLERRINVAL, ACLERR4630, ACL_Program, 2,
  252. XP_GetAdminStr(DBT_ReadDbMapFileInvalidPropertyPair), dbname);
  253. rv = -1;
  254. break;
  255. }
  256. propval = propval->next;
  257. }
  258. if (rv < 0) break;
  259. /* register the database */
  260. rv = ACL_DatabaseRegister(errp, dbtype, dbname, url, plist);
  261. PListDestroy(plist);
  262. if (rv < 0) {
  263. /* Failed to register database */
  264. nserrGenerate(errp, ACLERRFAIL, ACLERR4640, ACL_Program, 2,
  265. XP_GetAdminStr(DBT_ReadDbMapFileRegisterDatabaseFailed), dbname);
  266. break;
  267. }
  268. /* If the dbname is "default", set the default_dbtype */
  269. if (!strcmp(dbname, DBCONF_DEFAULT_DBNAME)) {
  270. if (!ACL_DbTypeIsEqual(errp, dbtype, ACL_DbTypeLdap)) {
  271. nserrGenerate(errp, ACLERRINVAL, ACLERR4350, ACL_Program, 1,
  272. XP_GetAdminStr(DBT_ReadDbMapFileDefaultDatabaseNotLdap));
  273. rv = -1;
  274. break;
  275. }
  276. if (seen_default) {
  277. nserrGenerate(errp, ACLERRINVAL, ACLERR4360, ACL_Program, 1, XP_GetAdminStr(DBT_ReadDbMapFileMultipleDefaultDatabases));
  278. rv = -1;
  279. break;
  280. }
  281. seen_default = 1;
  282. ACL_DatabaseSetDefault(errp, dbname);
  283. }
  284. db_info = db_info->next;
  285. }
  286. if (!seen_default) {
  287. nserrGenerate(errp, ACLERRINVAL, ACLERR4370, ACL_Program, 1, XP_GetAdminStr(DBT_ReadDbMapFileMissingDefaultDatabase));
  288. rv = -1;
  289. }
  290. if (default_only)
  291. dbconf_free_dbinfo(db_info);
  292. else
  293. dbconf_free_confinfo(info);
  294. return rv;
  295. }
  296. void
  297. ACL_DatabaseDestroy(void)
  298. {
  299. pool_destroy(ACL_DATABASE_POOL);
  300. ACL_DATABASE_POOL = NULL;
  301. ACLDbNameHash = NULL;
  302. return;
  303. }