1 /* zxida7n.c  -  Handwritten functions for Assertion handling
2  * Copyright (c) 2010-2011 Sampo Kellomaki (sampo@iki.fi), All Rights Reserved.
3  * Copyright (c) 2007-2008 Symlabs (symlabs@symlabs.com), All Rights Reserved.
4  * Author: Sampo Kellomaki (sampo@iki.fi)
5  * This is confidential unpublished proprietary source code of the author.
6  * NO WARRANTY, not even implied warranties. Contains trade secrets.
7  * Distribution prohibited unless authorized in writing.
8  * Licensed under Apache License 2.0, see file COPYING.
9  * $Id: zxida7n.c,v 1.3 2008-10-08 03:56:55 sampo Exp $
10  *
11  * 3.2.2007, created --Sampo
12  * 7.10.2008, added documentation --Sampo
13  * 17.2.2011, XML whitespace handling fix --Sampo
14  *
15  * See also: zxidsimp.c (attributes to LDIF), and zxidepr.c
16  */
17 
18 #include <string.h>
19 #include "platform.h"
20 #include "errmac.h"
21 #include "zxid.h"
22 #include "zxidconf.h"
23 #include "saml2.h"
24 #include "c/zx-ns.h"
25 #include "c/zx-sa-data.h"
26 
27 /*() Look into attribute statement(s) of an assertion and scan
28  * for nth occurance of named attribute. Ordering of attributes
29  * is accoring to their occurance in attribute statement, or
30  * more broadly according to ordering of the attribute statements
31  * themselves.
32  *
33  * - NULL or zero length nfmt (name format) will match any
34  * - NULL or zero length name will match any
35  * - NULL or zero length friendly (name) will match any
36  * - minus one (-1) as either length field will cause strlen() to be done
37  * - the index n is one based
38  *
39  * *Arguments*
40  *
41  * a7n:: Assertion data structure, obtained from XML parsing
42  * nfmt_len:: Length of the name format, or 0 if no matching by name format is desired
43  * nfmt:: name format to match (or 0)
44  * name_len:: Length of the attribute name, or 0 if no matching by attribute name is desired
45  * name:: attribute name to match (or 0)
46  * friendly_len:: Length of the friendly name, or 0 if no matching by friendly name is desired
47  * friendly:: friendly name to match (or 0)
48  * n:: Howmanieth instance of the matching attribute is desired. 1 means first.
49  * return:: Data structure representing the matching attribute.
50  */
51 
zxid_find_attribute(zxid_a7n * a7n,int nfmt_len,char * nfmt,int name_len,char * name,int friendly_len,char * friendly,int n)52 struct zx_sa_Attribute_s* zxid_find_attribute(zxid_a7n* a7n, int nfmt_len, char* nfmt, int name_len, char* name, int friendly_len, char* friendly, int n)
53 {
54   struct zx_sa_Attribute_s* at;
55   struct zx_sa_AttributeStatement_s* as;
56   if (!nfmt) { nfmt_len = 0; nfmt = ""; }
57   if (nfmt_len == -1 && nfmt) nfmt_len = strlen(nfmt);
58   if (!name) { name_len = 0; name = ""; }
59   if (name_len == -1 && name) name_len = strlen(name);
60   if (!friendly) { friendly_len = 0; friendly = ""; }
61   if (friendly_len == -1 && friendly) friendly_len = strlen(friendly);
62   if (!a7n) {
63     ERR("No assertion supplied (null assertion pointer) when looking for attribute nfmt(%.*s) name(%.*s) friendly(%.*s) n=%d", nfmt_len, nfmt, name_len, name, friendly_len, friendly, n);
64     return 0;
65   }
66   for (as = a7n->AttributeStatement;
67        as;
68        as = (struct zx_sa_AttributeStatement_s*)as->gg.g.n) {
69     if (as->gg.g.tok != zx_sa_AttributeStatement_ELEM)
70       continue;
71     for (at = as->Attribute;
72 	 at;
73 	 at = (struct zx_sa_Attribute_s*)at->gg.g.n) {
74       if (at->gg.g.tok != zx_sa_Attribute_ELEM)
75 	continue;
76       if ((nfmt_len ? (at->NameFormat
77 		       && at->NameFormat->g.len == nfmt_len
78 		       && !memcmp(at->NameFormat->g.s, nfmt, nfmt_len)) : 1)
79 	  && (name_len ? (at->Name
80 			  && at->Name->g.len == name_len
81 			  && !memcmp(at->Name->g.s, name, name_len)) : 1)
82 	  && (friendly_len ? (at->FriendlyName
83 			      && at->FriendlyName->g.len == friendly_len
84 			      && !memcmp(at->FriendlyName->g.s, friendly, friendly_len)) : 1)) {
85 	--n;
86 	if (!n)
87 	  return at;
88       }
89     }
90   }
91   return 0;
92 }
93 
94 /* EOF  --  zxida7n.c */
95