authdb.cpp 10 KB

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