certmap.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15. /* This was malloc.h - but it's moved to stdlib.h on most platforms, and FBSD is strict */
  16. /* Make it stdlib.h, and revert to malloc.h with ifdefs if we have issues here. WB 2016 */
  17. #include <stdlib.h>
  18. /* removed for ns security integration
  19. #include <sec.h>
  20. */
  21. #include <plstr.h>
  22. #include <prlink.h>
  23. #include <prprf.h>
  24. #include <keyhi.h>
  25. #include <cert.h>
  26. #define DEFINE_LDAPU_STRINGS 1
  27. #include <ldaputil/certmap.h>
  28. #include <ldaputil/ldapauth.h>
  29. #include <ldaputil/errors.h>
  30. #include <ldaputil/ldaputil.h>
  31. #include "ldaputili.h"
  32. #ifndef BIG_LINE
  33. #define BIG_LINE 1024
  34. #endif
  35. /* This is hack, the function is defined in cert/alg1485.c */
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. #ifdef __cplusplus
  40. }
  41. #endif
  42. static char this_dllname[256];
  43. static const char *LIB_DIRECTIVE = "certmap";
  44. static const int LIB_DIRECTIVE_LEN = 7; /* strlen("LIB_DIRECTIVE") */
  45. static void *ldapu_propval_free(void *propval_in, void *arg);
  46. typedef struct
  47. {
  48. FILE *fp;
  49. void *arg;
  50. } LDAPUPrintInfo_t;
  51. static LDAPUCertMapListInfo_t *certmap_listinfo = 0;
  52. static LDAPUCertMapInfo_t *default_certmap_info = 0;
  53. static const char *certmap_attrs[] = {
  54. 0,
  55. 0,
  56. 0,
  57. 0};
  58. const long CERTMAP_BIT_POS_UNKNOWN = 0; /* unknown OID */
  59. const long CERTMAP_BIT_POS_CN = 1L << 1; /* Common Name */
  60. const long CERTMAP_BIT_POS_OU = 1L << 2; /* Organization unit */
  61. const long CERTMAP_BIT_POS_O = 1L << 3; /* Organization */
  62. const long CERTMAP_BIT_POS_C = 1L << 4; /* Country */
  63. const long CERTMAP_BIT_POS_L = 1L << 5; /* Locality */
  64. const long CERTMAP_BIT_POS_ST = 1L << 6; /* State or Province */
  65. const long CERTMAP_BIT_POS_MAIL = 1L << 7; /* E-mail Address */
  66. const long CERTMAP_BIT_POS_UID = 1L << 8; /* UID */
  67. const long CERTMAP_BIT_POS_DC = 1L << 9; /* DC */
  68. const int SEC_OID_AVA_UNKNOWN = 0; /* unknown OID */
  69. static long
  70. certmap_secoid_to_bit_pos(int oid)
  71. {
  72. switch (oid) {
  73. case SEC_OID_AVA_COUNTRY_NAME:
  74. return CERTMAP_BIT_POS_C;
  75. case SEC_OID_AVA_ORGANIZATION_NAME:
  76. return CERTMAP_BIT_POS_O;
  77. case SEC_OID_AVA_COMMON_NAME:
  78. return CERTMAP_BIT_POS_CN;
  79. case SEC_OID_AVA_LOCALITY:
  80. return CERTMAP_BIT_POS_L;
  81. case SEC_OID_AVA_STATE_OR_PROVINCE:
  82. return CERTMAP_BIT_POS_ST;
  83. case SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME:
  84. return CERTMAP_BIT_POS_OU;
  85. case SEC_OID_RFC1274_UID:
  86. return CERTMAP_BIT_POS_UID;
  87. /* Map "E" and "MAIL" to the same bit position */
  88. case SEC_OID_PKCS9_EMAIL_ADDRESS:
  89. return CERTMAP_BIT_POS_MAIL;
  90. case SEC_OID_RFC1274_MAIL:
  91. return CERTMAP_BIT_POS_MAIL;
  92. case SEC_OID_AVA_DC:
  93. return CERTMAP_BIT_POS_DC;
  94. default:
  95. return CERTMAP_BIT_POS_UNKNOWN;
  96. }
  97. }
  98. static const char *
  99. certmap_secoid_to_name(int oid)
  100. {
  101. switch (oid) {
  102. case SEC_OID_AVA_COUNTRY_NAME:
  103. return "C";
  104. case SEC_OID_AVA_ORGANIZATION_NAME:
  105. return "O";
  106. case SEC_OID_AVA_COMMON_NAME:
  107. return "CN";
  108. case SEC_OID_AVA_LOCALITY:
  109. return "L";
  110. case SEC_OID_AVA_STATE_OR_PROVINCE:
  111. return "ST";
  112. case SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME:
  113. return "OU";
  114. case SEC_OID_RFC1274_UID:
  115. return "UID";
  116. /* Map both 'e' and 'mail' to 'mail' in LDAP */
  117. case SEC_OID_PKCS9_EMAIL_ADDRESS:
  118. return "MAIL";
  119. case SEC_OID_RFC1274_MAIL:
  120. return "MAIL";
  121. case SEC_OID_AVA_DC:
  122. return "DC";
  123. default:
  124. return 0;
  125. }
  126. }
  127. static void
  128. tolower_string(char *str)
  129. {
  130. if (str) {
  131. while (*str) {
  132. *str = tolower(*str);
  133. str++;
  134. }
  135. }
  136. }
  137. static long
  138. certmap_name_to_bit_pos(const char *str)
  139. {
  140. if (!ldapu_strcasecmp(str, "c"))
  141. return CERTMAP_BIT_POS_C;
  142. if (!ldapu_strcasecmp(str, "o"))
  143. return CERTMAP_BIT_POS_O;
  144. if (!ldapu_strcasecmp(str, "cn"))
  145. return CERTMAP_BIT_POS_CN;
  146. if (!ldapu_strcasecmp(str, "l"))
  147. return CERTMAP_BIT_POS_L;
  148. if (!ldapu_strcasecmp(str, "st"))
  149. return CERTMAP_BIT_POS_ST;
  150. if (!ldapu_strcasecmp(str, "ou"))
  151. return CERTMAP_BIT_POS_OU;
  152. if (!ldapu_strcasecmp(str, "uid"))
  153. return CERTMAP_BIT_POS_UID;
  154. /* Map "E" and "MAIL" to the same bit position */
  155. if (!ldapu_strcasecmp(str, "e"))
  156. return CERTMAP_BIT_POS_MAIL;
  157. if (!ldapu_strcasecmp(str, "mail"))
  158. return CERTMAP_BIT_POS_MAIL;
  159. if (!ldapu_strcasecmp(str, "dc"))
  160. return CERTMAP_BIT_POS_DC;
  161. return CERTMAP_BIT_POS_UNKNOWN;
  162. }
  163. #if 0 /* may need this in the future */
  164. static int certmap_name_to_secoid (const char *str)
  165. {
  166. if (!ldapu_strcasecmp(str, "c")) return SEC_OID_AVA_COUNTRY_NAME;
  167. if (!ldapu_strcasecmp(str, "o")) return SEC_OID_AVA_ORGANIZATION_NAME;
  168. if (!ldapu_strcasecmp(str, "cn")) return SEC_OID_AVA_COMMON_NAME;
  169. if (!ldapu_strcasecmp(str, "l")) return SEC_OID_AVA_LOCALITY;
  170. if (!ldapu_strcasecmp(str, "st")) return SEC_OID_AVA_STATE_OR_PROVINCE;
  171. if (!ldapu_strcasecmp(str, "ou")) return SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME;
  172. if (!ldapu_strcasecmp(str, "uid")) return SEC_OID_RFC1274_UID;
  173. if (!ldapu_strcasecmp(str, "e")) return SEC_OID_PKCS9_EMAIL_ADDRESS;
  174. if (!ldapu_strcasecmp(str, "mail")) return SEC_OID_RFC1274_MAIL;
  175. if (!ldapu_strcasecmp(str, "dc")) return SEC_OID_AVA_DC;
  176. return SEC_OID_AVA_UNKNOWN; /* return invalid OID */
  177. }
  178. #endif
  179. NSAPI_PUBLIC int
  180. ldapu_list_alloc(LDAPUList_t **list)
  181. {
  182. *list = (LDAPUList_t *)calloc(1, sizeof(LDAPUList_t));
  183. if (!*list)
  184. return LDAPU_ERR_OUT_OF_MEMORY;
  185. return LDAPU_SUCCESS;
  186. }
  187. static int
  188. ldapu_list_add_node(LDAPUList_t *list, LDAPUListNode_t *node)
  189. {
  190. if (list->head) {
  191. node->prev = list->tail;
  192. list->tail->next = node;
  193. } else {
  194. node->prev = 0;
  195. list->head = node;
  196. }
  197. node->next = 0;
  198. list->tail = node;
  199. return LDAPU_SUCCESS;
  200. }
  201. NSAPI_PUBLIC int
  202. ldapu_list_add_info(LDAPUList_t *list, void *info)
  203. {
  204. LDAPUListNode_t *node;
  205. /* Allocate the list node and set info in the node. */
  206. node = (LDAPUListNode_t *)calloc(1, sizeof(LDAPUListNode_t));
  207. if (!node) {
  208. return LDAPU_ERR_OUT_OF_MEMORY;
  209. }
  210. node->info = info;
  211. return ldapu_list_add_node(list, node);
  212. }
  213. static void
  214. ldapu_list_free(LDAPUList_t *list, LDAPUListNodeFn_t free_fn)
  215. {
  216. if (list) {
  217. auto LDAPUListNode_t *node = list->head;
  218. while (node) {
  219. auto LDAPUListNode_t *next = node->next;
  220. if (free_fn) {
  221. (*free_fn)(node->info, 0);
  222. node->info = 0;
  223. }
  224. node->info = 0;
  225. free(node);
  226. node = next;
  227. }
  228. list->head = 0;
  229. list->tail = 0;
  230. }
  231. return;
  232. }
  233. NSAPI_PUBLIC int
  234. ldapu_propval_alloc(const char *prop, const char *val, LDAPUPropVal_t **propval)
  235. {
  236. *propval = (LDAPUPropVal_t *)malloc(sizeof(LDAPUPropVal_t));
  237. if (!*propval)
  238. return LDAPU_ERR_OUT_OF_MEMORY;
  239. (*propval)->prop = prop ? strdup(prop) : 0;
  240. (*propval)->val = val ? strdup(val) : 0;
  241. if ((!prop || (*propval)->prop) && (!val || (*propval)->val)) {
  242. /* strdup worked */
  243. return LDAPU_SUCCESS;
  244. } else {
  245. return LDAPU_ERR_OUT_OF_MEMORY;
  246. }
  247. }
  248. static int
  249. PresentInComps(long comps_bitmask, int tag)
  250. {
  251. long bit = certmap_secoid_to_bit_pos(tag);
  252. if (comps_bitmask & bit)
  253. return 1;
  254. else
  255. return 0;
  256. }
  257. static int
  258. dbconf_to_certmap_err(int err)
  259. {
  260. switch (err) {
  261. case LDAPU_ERR_DBNAME_IS_MISSING:
  262. return LDAPU_ERR_CANAME_IS_MISSING;
  263. case LDAPU_ERR_PROP_IS_MISSING:
  264. return LDAPU_ERR_CAPROP_IS_MISSING;
  265. default:
  266. return err;
  267. }
  268. }
  269. /* CAUTION: this function hijacks some substructures from db_info and make
  270. * the pointers to it NULL in the db_info. It is safe to deallocate db_info.
  271. */
  272. static int
  273. dbinfo_to_certinfo(DBConfDBInfo_t *db_info,
  274. LDAPUCertMapInfo_t **certinfo_out)
  275. {
  276. LDAPUCertMapInfo_t *certinfo = NULL;
  277. LDAPUPropValList_t *propval_list = NULL;
  278. int rv = LDAPU_SUCCESS;
  279. *certinfo_out = 0;
  280. certinfo = (LDAPUCertMapInfo_t *)calloc(1, sizeof(LDAPUCertMapInfo_t));
  281. if (!certinfo) {
  282. rv = LDAPU_ERR_OUT_OF_MEMORY;
  283. goto error;
  284. }
  285. /* hijack few structures rather then copy. Make the pointers to the
  286. structures NULL in the original structure so that they don't freed up
  287. when db_info is freed. */
  288. certinfo->issuerName = db_info->dbname;
  289. db_info->dbname = 0;
  290. /* Parse the Issuer DN. */
  291. certinfo->issuerDN = CERT_AsciiToName(db_info->url);
  292. if (NULL == certinfo->issuerDN /* invalid DN */
  293. && ldapu_strcasecmp(db_info->url, "default") != 0 /* not "default" */) {
  294. rv = LDAPU_ERR_MALFORMED_SUBJECT_DN;
  295. goto error;
  296. }
  297. /* hijack actual prop-vals from dbinfo -- to avoid strdup calls */
  298. if (db_info->firstprop) {
  299. LDAPUPropVal_t *propval;
  300. DBPropVal_t *dbpropval;
  301. dbpropval = db_info->firstprop;
  302. rv = ldapu_list_alloc(&propval_list);
  303. if (rv != LDAPU_SUCCESS) {
  304. goto error;
  305. }
  306. while (dbpropval) {
  307. propval = (LDAPUPropVal_t *)malloc(sizeof(LDAPUPropVal_t));
  308. if (!propval) {
  309. rv = LDAPU_ERR_OUT_OF_MEMORY;
  310. goto error;
  311. }
  312. propval->prop = dbpropval->prop;
  313. dbpropval->prop = 0;
  314. propval->val = dbpropval->val;
  315. dbpropval->val = 0;
  316. rv = ldapu_list_add_info(propval_list, propval);
  317. if (rv != LDAPU_SUCCESS) {
  318. ldapu_propval_free((void *)propval, (void *)propval);
  319. goto error;
  320. }
  321. dbpropval = dbpropval->next;
  322. }
  323. certinfo->propval = propval_list;
  324. }
  325. *certinfo_out = certinfo;
  326. goto done;
  327. error:
  328. if (propval_list)
  329. ldapu_propval_list_free(propval_list);
  330. if (certinfo)
  331. free(certinfo);
  332. done:
  333. return rv;
  334. }
  335. static int
  336. ldapu_binary_cmp_certs(void *subject_cert,
  337. void *entry_cert_binary,
  338. unsigned long entry_cert_len)
  339. {
  340. SECItem derCert = ((CERTCertificate *)subject_cert)->derCert;
  341. int rv;
  342. /* binary compare the two certs */
  343. if (derCert.len == entry_cert_len &&
  344. !memcmp(derCert.data, entry_cert_binary, entry_cert_len)) {
  345. rv = LDAPU_SUCCESS;
  346. } else {
  347. rv = LDAPU_ERR_CERT_VERIFY_FAILED;
  348. }
  349. return rv;
  350. }
  351. static int
  352. ldapu_cert_verifyfn_default(void *subject_cert, LDAP *ld, void *certmap_info __attribute__((unused)), LDAPMessage *res, LDAPMessage **entry_out)
  353. {
  354. LDAPMessage *entry;
  355. struct berval **bvals;
  356. int i;
  357. int rv = LDAPU_ERR_CERT_VERIFY_FAILED;
  358. char *cert_attr = ldapu_strings[LDAPU_STR_ATTR_CERT];
  359. char *cert_attr_nosubtype = ldapu_strings[LDAPU_STR_ATTR_CERT_NOSUBTYPE];
  360. *entry_out = 0;
  361. for (entry = ldapu_first_entry(ld, res); entry != NULL;
  362. entry = ldapu_next_entry(ld, entry)) {
  363. if (((bvals = ldapu_get_values_len(ld, entry, cert_attr)) == NULL) &&
  364. ((bvals = ldapu_get_values_len(ld, entry, cert_attr_nosubtype)) == NULL)) {
  365. rv = LDAPU_ERR_CERT_VERIFY_NO_CERTS;
  366. continue;
  367. }
  368. for (i = 0; bvals[i] != NULL; i++) {
  369. rv = ldapu_binary_cmp_certs(subject_cert,
  370. bvals[i]->bv_val,
  371. bvals[i]->bv_len);
  372. if (rv == LDAPU_SUCCESS) {
  373. break;
  374. }
  375. }
  376. ldapu_value_free_len(ld, bvals);
  377. if (rv == LDAPU_SUCCESS) {
  378. *entry_out = entry;
  379. break;
  380. }
  381. }
  382. return rv;
  383. }
  384. static int
  385. parse_into_bitmask(const char *comps_in, long *bitmask_out, long default_val)
  386. {
  387. long bitmask;
  388. char *comps = comps_in ? strdup(comps_in) : 0;
  389. if (!comps) {
  390. /* Not present in the config file */
  391. bitmask = default_val;
  392. } else if (!*comps) {
  393. /* present but empty */
  394. bitmask = 0;
  395. } else {
  396. char *ptr = comps;
  397. char *name = comps;
  398. long bit;
  399. int break_loop = 0;
  400. bitmask = 0;
  401. while (*name) {
  402. /* advance ptr to delimeter */
  403. while (*ptr && !isspace(*ptr) && *ptr != ',')
  404. ptr++;
  405. if (!*ptr)
  406. break_loop = 1;
  407. else
  408. *ptr++ = 0;
  409. bit = certmap_name_to_bit_pos(name);
  410. bitmask |= bit;
  411. if (break_loop)
  412. break;
  413. /* skip delimeters */
  414. while (*ptr && (isspace(*ptr) || *ptr == ','))
  415. ptr++;
  416. name = ptr;
  417. }
  418. }
  419. if (comps)
  420. free(comps);
  421. *bitmask_out = bitmask;
  422. /* print_oid_bitmask(bitmask); */
  423. return LDAPU_SUCCESS;
  424. }
  425. static int
  426. process_certinfo(LDAPUCertMapInfo_t *certinfo)
  427. {
  428. int rv = LDAPU_SUCCESS;
  429. char *dncomps = 0;
  430. char *filtercomps = 0;
  431. char *libname = 0;
  432. char *verify = 0;
  433. char *fname = 0;
  434. char *searchAttr = 0;
  435. if (!ldapu_strcasecmp(certinfo->issuerName, "default")) {
  436. default_certmap_info = certinfo;
  437. } else if (!certinfo->issuerDN) {
  438. return LDAPU_ERR_NO_ISSUERDN_IN_CONFIG_FILE;
  439. } else {
  440. rv = ldapu_list_add_info(certmap_listinfo, certinfo);
  441. }
  442. if (rv != LDAPU_SUCCESS)
  443. return rv;
  444. /* look for dncomps property and parse it into the dncomps bitmask */
  445. rv = ldapu_certmap_info_attrval(certinfo, LDAPU_ATTR_DNCOMPS, &dncomps);
  446. if (rv == LDAPU_SUCCESS && dncomps) {
  447. certinfo->dncompsState = COMPS_HAS_ATTRS;
  448. tolower_string(dncomps);
  449. } else if (rv == LDAPU_FAILED) {
  450. certinfo->dncompsState = COMPS_COMMENTED_OUT;
  451. rv = LDAPU_SUCCESS;
  452. } else if (rv == LDAPU_SUCCESS && !dncomps) {
  453. certinfo->dncompsState = COMPS_EMPTY;
  454. dncomps = strdup(""); /* present but empty */
  455. }
  456. rv = parse_into_bitmask(dncomps, &certinfo->dncomps, -1);
  457. free(dncomps);
  458. dncomps = NULL;
  459. if (rv != LDAPU_SUCCESS)
  460. return rv;
  461. /* look for filtercomps property and parse it into the filtercomps bitmask */
  462. rv = ldapu_certmap_info_attrval(certinfo, LDAPU_ATTR_FILTERCOMPS,
  463. &filtercomps);
  464. if (rv == LDAPU_SUCCESS && filtercomps) {
  465. certinfo->filtercompsState = COMPS_HAS_ATTRS;
  466. tolower_string(filtercomps);
  467. } else if (rv == LDAPU_FAILED) {
  468. certinfo->filtercompsState = COMPS_COMMENTED_OUT;
  469. rv = LDAPU_SUCCESS;
  470. } else if (rv == LDAPU_SUCCESS && !filtercomps) {
  471. certinfo->filtercompsState = COMPS_EMPTY;
  472. filtercomps = strdup(""); /* present but empty */
  473. }
  474. rv = parse_into_bitmask(filtercomps, &certinfo->filtercomps, 0);
  475. free(filtercomps);
  476. filtercomps = NULL;
  477. if (rv != LDAPU_SUCCESS)
  478. return rv;
  479. /* look for "CmapLdapAttr" property and store it into searchAttr */
  480. rv = ldapu_certmap_info_attrval(certinfo, LDAPU_ATTR_CERTMAP_LDAP_ATTR,
  481. &searchAttr);
  482. if (rv == LDAPU_FAILED || !searchAttr) {
  483. rv = LDAPU_SUCCESS;
  484. } else {
  485. certinfo->searchAttr = searchAttr;
  486. if (searchAttr && !certinfo->searchAttr)
  487. rv = LDAPU_ERR_OUT_OF_MEMORY;
  488. else
  489. rv = LDAPU_SUCCESS;
  490. }
  491. if (rv != LDAPU_SUCCESS)
  492. return rv;
  493. /* look for verifycert property and set the default verify function */
  494. /* The value of the verifycert property is ignored */
  495. rv = ldapu_certmap_info_attrval(certinfo, LDAPU_ATTR_VERIFYCERT, &verify);
  496. if (rv == LDAPU_SUCCESS) {
  497. if (!ldapu_strcasecmp(verify, "on"))
  498. certinfo->verifyCert = 1;
  499. else if (!ldapu_strcasecmp(verify, "off"))
  500. certinfo->verifyCert = 0;
  501. else if (!verify || !*verify) /* for mail/news backward compatibilty */
  502. certinfo->verifyCert = 1; /* otherwise, this should be an error */
  503. else
  504. rv = LDAPU_ERR_MISSING_VERIFYCERT_VAL;
  505. } else if (rv == LDAPU_FAILED)
  506. rv = LDAPU_SUCCESS;
  507. if (verify)
  508. free(verify);
  509. if (rv != LDAPU_SUCCESS)
  510. return rv;
  511. {
  512. PRLibrary *lib = 0;
  513. /* look for the library property and load it */
  514. rv = ldapu_certmap_info_attrval(certinfo, LDAPU_ATTR_LIBRARY, &libname);
  515. if (rv == LDAPU_SUCCESS) {
  516. if (libname && *libname) {
  517. lib = PR_LoadLibrary(libname);
  518. if (!lib)
  519. rv = LDAPU_ERR_UNABLE_TO_LOAD_PLUGIN;
  520. } else {
  521. rv = LDAPU_ERR_MISSING_LIBNAME;
  522. }
  523. } else if (rv == LDAPU_FAILED)
  524. rv = LDAPU_SUCCESS;
  525. if (libname)
  526. free(libname);
  527. if (rv != LDAPU_SUCCESS)
  528. return rv;
  529. /* look for the InitFn property, find it in the libray and call it */
  530. rv = ldapu_certmap_info_attrval(certinfo, LDAPU_ATTR_INITFN, &fname);
  531. if (rv == LDAPU_SUCCESS) {
  532. if (fname && *fname) {
  533. /* If lib is NULL, PR_FindSymbol will search all libs loaded
  534. * through PR_LoadLibrary.
  535. */
  536. CertMapInitFn_t fn = (CertMapInitFn_t)PR_FindSymbol(lib, fname);
  537. if (!fn) {
  538. rv = LDAPU_ERR_MISSING_INIT_FN_IN_LIB;
  539. } else {
  540. rv = (*fn)(certinfo, certinfo->issuerName,
  541. certinfo->issuerDN, this_dllname);
  542. }
  543. } else {
  544. rv = LDAPU_ERR_MISSING_INIT_FN_NAME;
  545. }
  546. } else if (lib) {
  547. /* If library is specified, init function must be specified */
  548. /* If init fn is specified, library may not be specified */
  549. rv = LDAPU_ERR_MISSING_INIT_FN_IN_CONFIG;
  550. } else if (rv == LDAPU_FAILED) {
  551. rv = LDAPU_SUCCESS;
  552. }
  553. if (fname)
  554. free(fname);
  555. if (rv != LDAPU_SUCCESS)
  556. return rv;
  557. }
  558. return rv;
  559. }
  560. /* This function will read multiple certmap directives and set the information
  561. * in the global certmap_listinfo structure.
  562. */
  563. int
  564. certmap_read_certconfig_file(const char *file)
  565. {
  566. DBConfInfo_t *conf_info = 0;
  567. int rv;
  568. /* Read the config file */
  569. rv = dbconf_read_config_file_sub(file, LIB_DIRECTIVE, LIB_DIRECTIVE_LEN,
  570. &conf_info);
  571. /* Convert the conf_info into certmap_listinfo. Some of the
  572. * sub-structures are simply hijacked rather than copied since we are
  573. * going to (carefully) free the conf_info anyway.
  574. */
  575. if (rv == LDAPU_SUCCESS && conf_info) {
  576. DBConfDBInfo_t *nextdb;
  577. DBConfDBInfo_t *curdb;
  578. LDAPUCertMapInfo_t *certinfo;
  579. curdb = conf_info->firstdb;
  580. while (curdb) {
  581. nextdb = curdb->next;
  582. rv = dbinfo_to_certinfo(curdb, &certinfo);
  583. if (rv != LDAPU_SUCCESS) {
  584. dbconf_free_confinfo(conf_info);
  585. return rv;
  586. }
  587. rv = process_certinfo(certinfo);
  588. if (rv != LDAPU_SUCCESS) {
  589. ldapu_certinfo_free(certinfo);
  590. dbconf_free_confinfo(conf_info);
  591. return rv;
  592. }
  593. curdb = nextdb;
  594. }
  595. dbconf_free_confinfo(conf_info);
  596. } else {
  597. rv = dbconf_to_certmap_err(rv);
  598. }
  599. return rv;
  600. }
  601. /* This function will read the "certmap default" directive from the config
  602. * file and set the information in the global certmap_info.
  603. */
  604. int
  605. certmap_read_default_certinfo(const char *file)
  606. {
  607. DBConfDBInfo_t *db_info = 0;
  608. int rv;
  609. rv = dbconf_read_default_dbinfo_sub(file, LIB_DIRECTIVE, LIB_DIRECTIVE_LEN,
  610. &db_info);
  611. if (rv != LDAPU_SUCCESS)
  612. return rv;
  613. rv = dbinfo_to_certinfo(db_info, &default_certmap_info);
  614. dbconf_free_dbinfo(db_info);
  615. return rv;
  616. }
  617. static int
  618. ldapu_cert_searchfn_default(void *cert, LDAP *ld, void *certmap_info_in, const char *basedn, const char *dn, const char *filter, const char **attrs, LDAPMessage ***res)
  619. {
  620. int rv = LDAPU_FAILED;
  621. const char *ldapdn;
  622. LDAPUCertMapInfo_t *certmap_info = (LDAPUCertMapInfo_t *)certmap_info_in;
  623. LDAPMessage *single_res = NULL;
  624. LDAPMessage **multiple_res = NULL;
  625. if (certmap_info && certmap_info->searchAttr) {
  626. char *subjectDN = 0;
  627. char *certFilter = 0;
  628. int len;
  629. rv = ldapu_get_cert_subject_dn(cert, &subjectDN);
  630. if (rv != LDAPU_SUCCESS || !subjectDN) {
  631. return rv;
  632. }
  633. len = strlen(certmap_info->searchAttr) + strlen(subjectDN) +
  634. strlen("=") + 1;
  635. certFilter = (char *)ldapu_malloc(len * sizeof(char));
  636. if (!certFilter) {
  637. free(subjectDN);
  638. return LDAPU_ERR_OUT_OF_MEMORY;
  639. }
  640. sprintf(certFilter, "%s=%s", certmap_info->searchAttr, subjectDN);
  641. free(subjectDN);
  642. if (ldapu_strcasecmp(basedn, "")) {
  643. rv = ldapu_find(ld, basedn, LDAP_SCOPE_SUBTREE, certFilter, attrs, 0, &single_res);
  644. ldapu_free((void *)certFilter);
  645. if (rv == LDAPU_SUCCESS || rv == LDAPU_ERR_MULTIPLE_MATCHES) {
  646. *res = (LDAPMessage **)ldapu_malloc(2 * sizeof(LDAPMessage *));
  647. (*res)[0] = single_res;
  648. (*res)[1] = NULL;
  649. return rv;
  650. } else if (single_res) {
  651. ldapu_msgfree(ld, single_res);
  652. single_res = 0;
  653. }
  654. } else {
  655. rv = ldapu_find_entire_tree(ld, LDAP_SCOPE_SUBTREE, certFilter, attrs, 0, &multiple_res);
  656. ldapu_free((void *)certFilter);
  657. if (rv == LDAPU_SUCCESS || rv == LDAPU_ERR_MULTIPLE_MATCHES) {
  658. *res = multiple_res;
  659. return rv;
  660. } else if (multiple_res) {
  661. int n;
  662. for (n = 0; multiple_res[n] != NULL; n++)
  663. ldapu_msgfree(ld, multiple_res[n]);
  664. ldapu_memfree(ld, multiple_res);
  665. }
  666. }
  667. }
  668. if (dn && *dn) {
  669. /* First do the base level search --- NOT ANY MORE!! */
  670. /* We actually do the search on the whole subtree hanging from "ldapdn" since we want to
  671. * find all the entries that match the filter.
  672. * If we get more than one matching entry in return, it'll be at verify time that we'll
  673. * choose the correct one among them all.
  674. * However, if certificate verify is not active, certificate mapping will fail and will
  675. * consequently display an error message (something done at 'handle_handshake_done' level,
  676. * for instance). */
  677. ldapdn = dn;
  678. if (ldapu_strcasecmp(ldapdn, "")) {
  679. rv = ldapu_find(ld, ldapdn, LDAP_SCOPE_SUBTREE, filter, attrs, 0, &single_res);
  680. if (rv == LDAPU_SUCCESS || rv == LDAPU_ERR_MULTIPLE_MATCHES) {
  681. *res = (LDAPMessage **)ldapu_malloc(2 * sizeof(LDAPMessage *));
  682. (*res)[0] = single_res;
  683. (*res)[1] = NULL;
  684. return rv;
  685. } else if (single_res) {
  686. ldapu_msgfree(ld, single_res);
  687. single_res = 0;
  688. }
  689. } else {
  690. rv = ldapu_find_entire_tree(ld, LDAP_SCOPE_SUBTREE, filter, attrs, 0, &multiple_res);
  691. if (rv == LDAPU_SUCCESS || rv == LDAPU_ERR_MULTIPLE_MATCHES) {
  692. *res = multiple_res;
  693. return rv;
  694. } else if (multiple_res) {
  695. int n;
  696. for (n = 0; multiple_res[n] != NULL; n++)
  697. ldapu_msgfree(ld, multiple_res[n]);
  698. ldapu_memfree(ld, multiple_res);
  699. }
  700. }
  701. } else {
  702. /* default the dn and filter for subtree search */
  703. ldapdn = basedn;
  704. if (!filter || !*filter) {
  705. if (certmap_info && certmap_info->searchAttr) {
  706. /* dn & filter returned by the mapping function are both NULL
  707. and 'searchAttr' based search has failed. Don't do brute
  708. force search if 'searchAttr' is being used. Otherwise,
  709. this search will result in all LDAP entries being
  710. returned.
  711. */
  712. } else {
  713. filter = "objectclass=*";
  714. }
  715. }
  716. }
  717. /* For local LDAP DB, the LDAP_SCOPE_BASE search may fail for dn == basedn
  718. * since that object doesn't actually exists.
  719. */
  720. if ((rv == LDAPU_FAILED || rv == LDAP_NO_SUCH_OBJECT) && filter && (!dn || !*dn)) {
  721. /* Try the subtree search only if the filter is non-NULL */
  722. if (ldapu_strcasecmp(ldapdn, "")) {
  723. rv = ldapu_find(ld, ldapdn, LDAP_SCOPE_SUBTREE, filter, 0, 0, &single_res);
  724. if (rv == LDAPU_SUCCESS || rv == LDAPU_ERR_MULTIPLE_MATCHES) {
  725. *res = (LDAPMessage **)ldapu_malloc(2 * sizeof(LDAPMessage *));
  726. (*res)[0] = single_res;
  727. (*res)[1] = NULL;
  728. return rv;
  729. } else if (single_res) {
  730. ldapu_msgfree(ld, single_res);
  731. single_res = 0;
  732. }
  733. } else {
  734. rv = ldapu_find_entire_tree(ld, LDAP_SCOPE_SUBTREE, filter, 0, 0, &multiple_res);
  735. if (rv == LDAPU_SUCCESS || rv == LDAPU_ERR_MULTIPLE_MATCHES) {
  736. *res = multiple_res;
  737. return rv;
  738. } else if (multiple_res) {
  739. int n;
  740. for (n = 0; multiple_res[n] != NULL; n++)
  741. ldapu_msgfree(ld, multiple_res[n]);
  742. ldapu_memfree(ld, multiple_res);
  743. }
  744. }
  745. }
  746. if (rv == LDAPU_FAILED) {
  747. /* Not an error but couldn't map the cert */
  748. rv = LDAPU_ERR_MAPPED_ENTRY_NOT_FOUND;
  749. } else if ((!dn || !*dn) && (rv == LDAP_NO_SUCH_OBJECT)) {
  750. rv = LDAPU_ERR_INVALID_SUFFIX;
  751. }
  752. return rv;
  753. }
  754. NSAPI_PUBLIC int
  755. ldapu_issuer_certinfo(const CERTName *issuerDN, void **certmap_info)
  756. {
  757. *certmap_info = 0;
  758. if (certmap_listinfo) {
  759. LDAPUListNode_t *cur = certmap_listinfo->head;
  760. while (cur) {
  761. LDAPUCertMapInfo_t *info = (LDAPUCertMapInfo_t *)cur->info;
  762. if (NULL == info->issuerDN) {
  763. /* no DN to compare to (probably the default certmap info) */
  764. continue;
  765. }
  766. if (CERT_CompareName(issuerDN, info->issuerDN) == SECEqual) {
  767. *certmap_info = cur->info;
  768. break;
  769. }
  770. cur = cur->next;
  771. }
  772. }
  773. return *certmap_info ? LDAPU_SUCCESS : LDAPU_FAILED;
  774. }
  775. NSAPI_PUBLIC int
  776. ldapu_certmap_info_attrval(void *certmap_info_in,
  777. const char *attr,
  778. char **val)
  779. {
  780. /* Look for given attr in the certmap_info and return its value */
  781. LDAPUCertMapInfo_t *certmap_info = (LDAPUCertMapInfo_t *)certmap_info_in;
  782. LDAPUListNode_t *curprop = certmap_info->propval ? certmap_info->propval->head : 0;
  783. LDAPUPropVal_t *propval;
  784. int rv = LDAPU_FAILED;
  785. *val = 0;
  786. while (curprop) {
  787. propval = (LDAPUPropVal_t *)curprop->info;
  788. if (!ldapu_strcasecmp(propval->prop, attr)) {
  789. *val = propval->val ? strdup(propval->val) : 0;
  790. rv = LDAPU_SUCCESS;
  791. break;
  792. }
  793. curprop = curprop->next;
  794. }
  795. return rv;
  796. }
  797. static int
  798. AddAVAToBuf(char *buf, int size, int *len, const char *tagName, CERTAVA *ava)
  799. {
  800. int lenLen;
  801. int taglen;
  802. SECStatus rv;
  803. buf += *len;
  804. /* Copied from ns/lib/libsec ...
  805. * XXX this code is incorrect in general
  806. * -- should use a DER template.
  807. */
  808. lenLen = 2;
  809. if (ava->value.len >= 128)
  810. lenLen = 3;
  811. taglen = PL_strlen(tagName);
  812. memcpy(buf, tagName, taglen);
  813. buf[taglen++] = '=';
  814. rv = CERT_RFC1485_EscapeAndQuote(buf + taglen,
  815. size - taglen,
  816. (char *)ava->value.data + lenLen,
  817. ava->value.len - lenLen);
  818. *len += strlen(buf);
  819. return (rv == SECSuccess ? LDAPU_SUCCESS : LDAPU_FAILED);
  820. }
  821. static int
  822. AddToLdapDN(char *ldapdn, int size, int *dnlen, const char *tagName, CERTAVA *ava)
  823. {
  824. char *dn = ldapdn + *dnlen;
  825. if (*dnlen) {
  826. strcat(dn, ", ");
  827. dn += 2;
  828. *dnlen += 2;
  829. }
  830. return AddAVAToBuf(ldapdn, size, dnlen, tagName, ava);
  831. }
  832. static int
  833. AddToFilter(char *filter, int size, int *flen, const char *tagName, CERTAVA *ava)
  834. {
  835. int rv;
  836. /* Append opening parenthesis */
  837. strcat(filter + *flen, " (");
  838. *flen += 2;
  839. rv = AddAVAToBuf(filter, size, flen, tagName, ava);
  840. if (rv != LDAPU_SUCCESS)
  841. return rv;
  842. /* Append closing parenthesis */
  843. strcat(filter + *flen, ")");
  844. (*flen)++;
  845. return rv;
  846. }
  847. NSAPI_PUBLIC int
  848. ldapu_free_cert_ava_val(char **val)
  849. {
  850. char **ptr = val;
  851. if (!val)
  852. return LDAPU_SUCCESS;
  853. while (*ptr)
  854. free(*ptr++);
  855. free(val);
  856. return LDAPU_SUCCESS;
  857. }
  858. static int
  859. ldapu_cert_mapfn_default(void *cert_in, LDAP *ld __attribute__((unused)), void *certmap_info_in, char **ldapDN_out, char **filter_out)
  860. {
  861. CERTCertificate *cert = (CERTCertificate *)cert_in;
  862. LDAPUCertMapInfo_t *certmap_info = (LDAPUCertMapInfo_t *)certmap_info_in;
  863. int rv = LDAPU_SUCCESS;
  864. *ldapDN_out = *filter_out = 0;
  865. if (!certmap_info) {
  866. /* Use subject DN as is -- identity mapping function */
  867. rv = ldapu_get_cert_subject_dn(cert, ldapDN_out);
  868. return rv;
  869. } else {
  870. /*
  871. * Iterate over rdns from the subject and collect AVAs depending on
  872. * dnComps and filtercomps to form ldapDN and filter respectively.
  873. * certmap_info->dncomps
  874. */
  875. CERTName *subject = &cert->subject;
  876. CERTRDN **rdns = subject->rdns;
  877. CERTRDN **lastRdn;
  878. CERTRDN **rdn;
  879. CERTAVA **avas;
  880. CERTAVA *ava;
  881. char ldapdn[BIG_LINE];
  882. char filter[BIG_LINE];
  883. int dnlen = 0; /* ldap DN length */
  884. int flen = 0; /* filter length */
  885. int numfavas = 0; /* no of avas added to filter */
  886. if (rdns == NULL) {
  887. /* error */
  888. return LDAPU_ERR_INTERNAL;
  889. }
  890. /* find last RDN */
  891. lastRdn = rdns;
  892. while (*lastRdn)
  893. lastRdn++;
  894. lastRdn--;
  895. /* Initialize filter to "(&" */
  896. strcpy(filter, "(&");
  897. flen = 2;
  898. /*
  899. * Loop over subject rdns in the _reverse_ order while forming ldapDN
  900. * and filter.
  901. */
  902. for (rdn = lastRdn; rdn >= rdns; rdn--) {
  903. avas = (*rdn)->avas;
  904. while ((ava = *avas++) != NULL) {
  905. int tag = CERT_GetAVATag(ava);
  906. const char *tagName = certmap_secoid_to_name(tag);
  907. if (PresentInComps(certmap_info->dncomps, tag)) {
  908. rv = AddToLdapDN(ldapdn, BIG_LINE, &dnlen, tagName, ava);
  909. if (rv != LDAPU_SUCCESS)
  910. return rv;
  911. }
  912. if (PresentInComps(certmap_info->filtercomps, tag)) {
  913. rv = AddToFilter(filter, BIG_LINE, &flen, tagName, ava);
  914. if (rv != LDAPU_SUCCESS)
  915. return rv;
  916. numfavas++;
  917. }
  918. }
  919. }
  920. if (numfavas == 0) {
  921. /* nothing added to filter */
  922. *filter = 0;
  923. } else if (numfavas == 1) {
  924. /* one ava added to filter -- remove "(& (" from the front and ")"
  925. * from the end.
  926. */
  927. *filter_out = strdup(filter + 4);
  928. if (!*filter_out)
  929. return LDAPU_ERR_OUT_OF_MEMORY;
  930. (*filter_out)[strlen(*filter_out) - 1] = 0;
  931. } else {
  932. /* Add the closing parenthesis to filter */
  933. strcat(filter + flen, ")");
  934. *filter_out = strdup(filter);
  935. }
  936. if (dnlen >= BIG_LINE)
  937. return LDAPU_FAILED;
  938. ldapdn[dnlen] = 0;
  939. *ldapDN_out = *ldapdn ? strdup(ldapdn) : 0;
  940. if ((numfavas && !*filter_out) || (dnlen && !*ldapDN_out)) {
  941. /* strdup failed */
  942. return LDAPU_ERR_OUT_OF_MEMORY;
  943. }
  944. if ((certmap_info->dncompsState == COMPS_HAS_ATTRS && dnlen == 0) ||
  945. (certmap_info->filtercompsState == COMPS_HAS_ATTRS &&
  946. numfavas == 0)) {
  947. /* At least one attr in DNComps should be present in the cert */
  948. /* Same is true for FilterComps */
  949. rv = LDAPU_ERR_MAPPED_ENTRY_NOT_FOUND;
  950. }
  951. }
  952. return rv;
  953. }
  954. NSAPI_PUBLIC int
  955. ldapu_set_cert_mapfn(const CERTName *issuerDN,
  956. CertMapFn_t mapfn)
  957. {
  958. LDAPUCertMapInfo_t *certmap_info;
  959. int rv;
  960. /* don't free the certmap_info -- its a pointer to an internal structure */
  961. rv = ldapu_issuer_certinfo(issuerDN, (void **)&certmap_info);
  962. /* Don't set the mapping function if certmap_info doesen't exist */
  963. if (rv != LDAPU_SUCCESS)
  964. return rv;
  965. certmap_info->mapfn = mapfn;
  966. return LDAPU_SUCCESS;
  967. }
  968. static CertMapFn_t
  969. ldapu_get_cert_mapfn_sub(LDAPUCertMapInfo_t *certmap_info)
  970. {
  971. CertMapFn_t mapfn;
  972. if (certmap_info && certmap_info->mapfn)
  973. mapfn = certmap_info->mapfn;
  974. else if (default_certmap_info && default_certmap_info->mapfn)
  975. mapfn = default_certmap_info->mapfn;
  976. else
  977. mapfn = ldapu_cert_mapfn_default;
  978. return mapfn;
  979. }
  980. NSAPI_PUBLIC CertMapFn_t
  981. ldapu_get_cert_mapfn(const CERTName *issuerDN)
  982. {
  983. LDAPUCertMapInfo_t *certmap_info = 0;
  984. /* don't free the certmap_info -- its a pointer to an internal structure */
  985. ldapu_issuer_certinfo(issuerDN, (void **)&certmap_info);
  986. /* certmap_info may be NULL -- use the default */
  987. return ldapu_get_cert_mapfn_sub(certmap_info);
  988. }
  989. NSAPI_PUBLIC int
  990. ldapu_set_cert_searchfn(const CERTName *issuerDN,
  991. CertSearchFn_t searchfn)
  992. {
  993. LDAPUCertMapInfo_t *certmap_info;
  994. int rv;
  995. /* don't free the certmap_info -- its a pointer to an internal structure */
  996. rv = ldapu_issuer_certinfo(issuerDN, (void **)&certmap_info);
  997. /* Don't set the mapping function if certmap_info doesen't exist */
  998. if (rv != LDAPU_SUCCESS)
  999. return rv;
  1000. certmap_info->searchfn = searchfn;
  1001. return LDAPU_SUCCESS;
  1002. }
  1003. static CertSearchFn_t
  1004. ldapu_get_cert_searchfn_sub(LDAPUCertMapInfo_t *certmap_info)
  1005. {
  1006. CertSearchFn_t searchfn;
  1007. if (certmap_info && certmap_info->searchfn)
  1008. searchfn = certmap_info->searchfn;
  1009. else if (default_certmap_info && default_certmap_info->searchfn)
  1010. searchfn = default_certmap_info->searchfn;
  1011. else
  1012. searchfn = ldapu_cert_searchfn_default;
  1013. return searchfn;
  1014. }
  1015. NSAPI_PUBLIC CertSearchFn_t
  1016. ldapu_get_cert_searchfn(const CERTName *issuerDN)
  1017. {
  1018. LDAPUCertMapInfo_t *certmap_info = 0;
  1019. /* don't free the certmap_info -- its a pointer to an internal structure */
  1020. ldapu_issuer_certinfo(issuerDN, (void **)&certmap_info);
  1021. /* certmap_info may be NULL -- use the default */
  1022. return ldapu_get_cert_searchfn_sub(certmap_info);
  1023. }
  1024. NSAPI_PUBLIC int
  1025. ldapu_set_cert_verifyfn(const CERTName *issuerDN,
  1026. CertVerifyFn_t verifyfn)
  1027. {
  1028. LDAPUCertMapInfo_t *certmap_info;
  1029. int rv;
  1030. /* don't free the certmap_info -- its a pointer to an internal structure */
  1031. rv = ldapu_issuer_certinfo(issuerDN, (void **)&certmap_info);
  1032. /* Don't set the verify function if certmap_info doesen't exist */
  1033. if (rv != LDAPU_SUCCESS)
  1034. return rv;
  1035. certmap_info->verifyfn = verifyfn;
  1036. return LDAPU_SUCCESS;
  1037. }
  1038. static CertVerifyFn_t
  1039. ldapu_get_cert_verifyfn_sub(LDAPUCertMapInfo_t *certmap_info)
  1040. {
  1041. CertVerifyFn_t verifyfn;
  1042. if (certmap_info && certmap_info->verifyfn)
  1043. verifyfn = certmap_info->verifyfn;
  1044. else if (default_certmap_info && default_certmap_info->verifyfn)
  1045. verifyfn = default_certmap_info->verifyfn;
  1046. else
  1047. verifyfn = ldapu_cert_verifyfn_default;
  1048. return verifyfn;
  1049. }
  1050. NSAPI_PUBLIC CertVerifyFn_t
  1051. ldapu_get_cert_verifyfn(const CERTName *issuerDN)
  1052. {
  1053. LDAPUCertMapInfo_t *certmap_info = 0;
  1054. /* don't free the certmap_info -- its a pointer to an internal structure */
  1055. ldapu_issuer_certinfo(issuerDN, (void **)&certmap_info);
  1056. /* certmap_info may be NULL -- use the default */
  1057. return ldapu_get_cert_verifyfn_sub(certmap_info);
  1058. }
  1059. #if 0 /* may need this in the future */
  1060. static int ldapu_certinfo_copy (const LDAPUCertMapInfo_t *from,
  1061. const char *newIssuerName,
  1062. const char *newIssuerDN,
  1063. LDAPUCertMapInfo_t *to)
  1064. {
  1065. /* This function is not tested and is not used */
  1066. int rv;
  1067. to->issuerName = newIssuerName ? strdup(newIssuerName) : 0;
  1068. to->issuerDN = newIssuerDN ? strdup(newIssuerDN) : 0;
  1069. if (from->propval) {
  1070. rv = ldapu_list_copy(from->propval, &to->propval, ldapu_propval_copy);
  1071. if (rv != LDAPU_SUCCESS) return rv;
  1072. }
  1073. else {
  1074. to->propval = 0;
  1075. }
  1076. return process_certinfo(to);
  1077. }
  1078. #endif
  1079. NSAPI_PUBLIC int
  1080. ldapu_cert_to_ldap_entry(void *cert, LDAP *ld, const char *basedn, LDAPMessage **res)
  1081. {
  1082. char *ldapDN = 0;
  1083. char *filter = 0;
  1084. LDAPUCertMapInfo_t *certmap_info;
  1085. LDAPMessage **res_array = NULL;
  1086. CertMapFn_t mapfn;
  1087. CertVerifyFn_t verifyfn;
  1088. CertSearchFn_t searchfn;
  1089. int rv, i, j;
  1090. *res = 0;
  1091. if (!certmap_attrs[0]) {
  1092. /* Initialize certmap_attrs */
  1093. certmap_attrs[0] = ldapu_strings[LDAPU_STR_ATTR_USER];
  1094. certmap_attrs[1] = ldapu_strings[LDAPU_STR_ATTR_CERT];
  1095. certmap_attrs[2] = ldapu_strings[LDAPU_STR_ATTR_CERT_NOSUBTYPE];
  1096. certmap_attrs[3] = 0;
  1097. }
  1098. CERTName *issuerDN = ldapu_get_cert_issuer_dn_as_CERTName(cert);
  1099. /* ^ don't need to free this; it will be freed with ^ the cert */
  1100. if (NULL == issuerDN)
  1101. return LDAPU_ERR_NO_ISSUERDN_IN_CERT;
  1102. /* don't free the certmap_info -- its a pointer to an internal structure */
  1103. rv = ldapu_issuer_certinfo(issuerDN, (void **)&certmap_info);
  1104. if (!certmap_info)
  1105. certmap_info = default_certmap_info;
  1106. /* Get the mapping function from the certmap_info */
  1107. mapfn = ldapu_get_cert_mapfn_sub(certmap_info);
  1108. rv = (*mapfn)(cert, ld, certmap_info, &ldapDN, &filter);
  1109. if (rv != LDAPU_SUCCESS) {
  1110. free(ldapDN);
  1111. free(filter);
  1112. return rv;
  1113. }
  1114. /* Get the search function from the certmap_info - certinfo maybe NULL */
  1115. searchfn = ldapu_get_cert_searchfn_sub(certmap_info);
  1116. rv = (*searchfn)(cert, ld, certmap_info, basedn, ldapDN, filter,
  1117. certmap_attrs, &res_array);
  1118. free(ldapDN);
  1119. free(filter);
  1120. /*
  1121. * Get the verify cert function & call it.
  1122. */
  1123. j = 0;
  1124. if ((rv == LDAPU_SUCCESS || rv == LDAPU_ERR_MULTIPLE_MATCHES) &&
  1125. (certmap_info ? certmap_info->verifyCert : 0)) {
  1126. verifyfn = ldapu_get_cert_verifyfn_sub(certmap_info);
  1127. if (verifyfn) {
  1128. int verify_rv;
  1129. i = 0;
  1130. do {
  1131. LDAPMessage *entry;
  1132. verify_rv = (*verifyfn)(cert, ld, certmap_info, res_array[i], &entry);
  1133. if (rv == LDAPU_ERR_MULTIPLE_MATCHES) {
  1134. if (verify_rv == LDAPU_SUCCESS) {
  1135. /* 'entry' points to the matched entry */
  1136. /* Get the 'res' which only contains this entry */
  1137. char *dn = ldapu_get_dn(ld, entry);
  1138. if (*res)
  1139. ldapu_msgfree(ld, *res);
  1140. rv = ldapu_find(ld, dn, LDAP_SCOPE_BASE, 0, certmap_attrs, 0, res);
  1141. ldapu_memfree(ld, dn);
  1142. } else {
  1143. /* Verify failed for multiple matches -- keep rv */
  1144. /* multiple matches err is probably more interesting to
  1145. the caller then any other error returned by the verify
  1146. fn */
  1147. }
  1148. } else /* rv == LDAPU_SUCCESS */ {
  1149. if (verify_rv == LDAPU_SUCCESS) {
  1150. *res = res_array[0];
  1151. j = 1;
  1152. } else {
  1153. rv = verify_rv;
  1154. }
  1155. }
  1156. } while ((verify_rv != LDAPU_SUCCESS) && (res_array[++i] != NULL));
  1157. }
  1158. } else {
  1159. if (rv == LDAPU_SUCCESS) {
  1160. *res = res_array[0];
  1161. j = 1;
  1162. }
  1163. }
  1164. if (rv != LDAPU_SUCCESS) {
  1165. if (*res) {
  1166. ldapu_msgfree(ld, *res);
  1167. *res = 0;
  1168. }
  1169. }
  1170. i = j; /* ugaston - if the search had been successful, despite verifyCert being "off",
  1171. * mapping is considered successful, so we keep the first (and only) response message.
  1172. * If, on the other hand, the search had returned multiple matches, the fact
  1173. * of having verifyCert "off" automatically turns the mapping faulty, so we
  1174. * don't need to care about keeping any response at all.
  1175. */
  1176. if (res_array) {
  1177. while (res_array[i] != NULL) {
  1178. ldapu_msgfree(ld, res_array[i]);
  1179. res_array[i++] = 0;
  1180. }
  1181. ldapu_memfree(ld, res_array);
  1182. }
  1183. return rv;
  1184. }
  1185. /* The caller shouldn't free the entry */
  1186. NSAPI_PUBLIC int
  1187. ldapu_cert_to_user(void *cert, LDAP *ld, const char *basedn, LDAPMessage **res_out, char **user)
  1188. {
  1189. int rv;
  1190. LDAPMessage *res;
  1191. LDAPMessage *entry;
  1192. int numEntries;
  1193. char **attrVals = NULL;
  1194. *res_out = 0;
  1195. *user = 0;
  1196. rv = ldapu_cert_to_ldap_entry(cert, ld, basedn, &res);
  1197. if (rv != LDAPU_SUCCESS) {
  1198. goto done;
  1199. }
  1200. if (!res) {
  1201. rv = LDAPU_ERR_EMPTY_LDAP_RESULT;
  1202. goto done;
  1203. }
  1204. /* Extract user login (the 'uid' attr) from 'res' */
  1205. numEntries = ldapu_count_entries(ld, res);
  1206. if (numEntries != 1) {
  1207. rv = LDAPU_ERR_MULTIPLE_MATCHES;
  1208. goto done;
  1209. }
  1210. entry = ldapu_first_entry(ld, res);
  1211. if (!entry) {
  1212. rv = LDAPU_ERR_MISSING_RES_ENTRY;
  1213. goto done;
  1214. }
  1215. attrVals = ldapu_get_values(ld, entry,
  1216. ldapu_strings[LDAPU_STR_ATTR_USER]);
  1217. if (!attrVals || !attrVals[0]) {
  1218. rv = LDAPU_ERR_MISSING_UID_ATTR;
  1219. goto done;
  1220. }
  1221. *user = strdup(attrVals[0]);
  1222. /* ldapu_msgfree(res); */
  1223. if (!*user) {
  1224. rv = LDAPU_ERR_OUT_OF_MEMORY;
  1225. goto done;
  1226. }
  1227. *res_out = res;
  1228. done:
  1229. if (attrVals) {
  1230. ldapu_value_free(ld, attrVals);
  1231. }
  1232. return rv;
  1233. }
  1234. static void *
  1235. ldapu_propval_free(void *propval_in, void *arg __attribute__((unused)))
  1236. {
  1237. LDAPUPropVal_t *propval = (LDAPUPropVal_t *)propval_in;
  1238. if (propval->prop)
  1239. free(propval->prop);
  1240. if (propval->val)
  1241. free(propval->val);
  1242. memset((void *)propval, 0, sizeof(LDAPUPropVal_t));
  1243. free(propval);
  1244. return 0;
  1245. }
  1246. void
  1247. ldapu_certinfo_free(void *info_in)
  1248. {
  1249. LDAPUCertMapInfo_t *certmap_info = (LDAPUCertMapInfo_t *)info_in;
  1250. if (certmap_info->issuerName)
  1251. free(certmap_info->issuerName);
  1252. if (certmap_info->issuerDN)
  1253. free(certmap_info->issuerDN);
  1254. if (certmap_info->propval)
  1255. ldapu_list_free(certmap_info->propval, ldapu_propval_free);
  1256. if (certmap_info->searchAttr)
  1257. free(certmap_info->searchAttr);
  1258. memset((void *)certmap_info, 0, sizeof(LDAPUCertMapInfo_t));
  1259. free(certmap_info);
  1260. }
  1261. static void *
  1262. ldapu_certinfo_free_helper(void *info, void *arg __attribute__((unused)))
  1263. {
  1264. ldapu_certinfo_free(info);
  1265. return (void *)LDAPU_SUCCESS;
  1266. }
  1267. void
  1268. ldapu_certmap_listinfo_free(void *_certmap_listinfo)
  1269. {
  1270. LDAPUCertMapListInfo_t *list = (LDAPUCertMapListInfo_t *)_certmap_listinfo;
  1271. ldapu_list_free(list, ldapu_certinfo_free_helper);
  1272. }
  1273. void
  1274. ldapu_propval_list_free(void *propval_list)
  1275. {
  1276. LDAPUPropValList_t *list = (LDAPUPropValList_t *)propval_list;
  1277. ldapu_list_free(list, ldapu_propval_free);
  1278. free(list);
  1279. }
  1280. int
  1281. ldapu_certmap_init(const char *config_file,
  1282. const char *dllname,
  1283. LDAPUCertMapListInfo_t **certmap_list,
  1284. LDAPUCertMapInfo_t **certmap_default)
  1285. {
  1286. int rv;
  1287. certmap_listinfo = (LDAPUCertMapListInfo_t *)calloc(1, sizeof(LDAPUCertMapListInfo_t));
  1288. *certmap_list = 0;
  1289. *certmap_default = 0;
  1290. PR_snprintf(this_dllname, sizeof(this_dllname), "%s", dllname);
  1291. if (!certmap_listinfo)
  1292. return LDAPU_ERR_OUT_OF_MEMORY;
  1293. rv = certmap_read_certconfig_file(config_file);
  1294. if (rv == LDAPU_SUCCESS) {
  1295. *certmap_list = certmap_listinfo;
  1296. *certmap_default = default_certmap_info;
  1297. }
  1298. return rv;
  1299. }
  1300. NSAPI_PUBLIC int
  1301. ldaputil_exit()
  1302. {
  1303. if (default_certmap_info) {
  1304. ldapu_certinfo_free(default_certmap_info);
  1305. default_certmap_info = 0;
  1306. }
  1307. if (certmap_listinfo) {
  1308. ldapu_certmap_listinfo_free(certmap_listinfo);
  1309. certmap_listinfo = 0;
  1310. }
  1311. return LDAPU_SUCCESS;
  1312. }
  1313. NSAPI_PUBLIC void
  1314. ldapu_free(void *ptr)
  1315. {
  1316. if (ptr)
  1317. free(ptr);
  1318. }
  1319. NSAPI_PUBLIC void
  1320. ldapu_free_old(char *ptr)
  1321. {
  1322. free((void *)ptr);
  1323. }
  1324. NSAPI_PUBLIC void *
  1325. ldapu_malloc(int size)
  1326. {
  1327. return malloc(size);
  1328. }
  1329. NSAPI_PUBLIC char *
  1330. ldapu_strdup(const char *ptr)
  1331. {
  1332. return strdup(ptr);
  1333. }
  1334. NSAPI_PUBLIC void *
  1335. ldapu_realloc(void *ptr, int size)
  1336. {
  1337. return realloc(ptr, size);
  1338. }