| 1 |
|
| #ifndef PSAML_REG_H |
| 2 |
|
| #define PSAML_REG_H |
| 3 |
|
| |
| 4 |
|
| /* The registrar surface: how this plugin reaches Punk. |
| 5 |
|
| * |
| 6 |
|
| * Punk is reached by ordinary method dispatch, not through a C table. |
| 7 |
|
| * pk_abi.h is an observer interface with no install_kw, so there is |
| 8 |
|
| * nothing to consume from Punk through ExtUtils::Depends and the |
| 9 |
|
| * Makefile.PL does not load it. This is the same choice Punk-Feed and |
| 10 |
|
| * Punk-Challenge made, for the same reason. |
| 11 |
|
| * |
| 12 |
|
| * NO STRUCTS ON THE APP. Every piece of state this plugin keeps - the |
| 13 |
|
| * option hash, the providers, the ACS URL, the mount - is a key on the |
| 14 |
|
| * application HV, exactly as punk_sitemap.h and pfeed_*.h keep theirs. |
| 15 |
|
| * A struct hung off the app would need a free hook, would need to |
| 16 |
|
| * survive a fork, and would be invisible to anything that dumps the |
| 17 |
|
| * application; three problems bought for nothing. |
| 18 |
|
| * |
| 19 |
|
| * The only aggregates in this dist are the closure capture in |
| 20 |
|
| * psaml_clos.h and the per-document frx_doc *, which lives exactly as |
| 21 |
|
| * long as one verification. */ |
| 22 |
|
| |
| 23 |
|
| #include "psaml_compat.h" |
| 24 |
|
| #include "psaml_clos.h" |
| 25 |
|
| |
| 26 |
|
| /* The keys this plugin owns on the application hash. Namespaced, because |
| 27 |
|
| * the application hash belongs to the application and every plugin is a |
| 28 |
|
| * guest on it. */ |
| 29 |
|
| #define PSAML_K_OPTS "punk_saml.opts" |
| 30 |
|
| #define PSAML_K_IDPS "punk_saml.idps" |
| 31 |
|
| #define PSAML_K_IDPORDER "punk_saml.idp_order" |
| 32 |
|
| #define PSAML_K_LOGIN "punk_saml.login" |
| 33 |
|
| #define PSAML_K_ACS "punk_saml.acs" |
| 34 |
|
| #define PSAML_K_MOUNT "punk_saml.mount" |
| 35 |
|
| #define PSAML_K_ENTITY "punk_saml.entity_id" |
| 36 |
|
| #define PSAML_K_INSTALLED "punk_saml.installed" |
| 37 |
|
| |
| 38 |
|
| /* The application hash behind $app. Croaks rather than returning NULL: |
| 39 |
|
| * every caller is inside a keyword body, and a keyword body with no |
| 40 |
|
| * application is a bug in this dist, not a condition to handle. */ |
| 41 |
908 |
| PERL_STATIC_INLINE HV *psaml_app_hv(pTHX_ SV *app) { |
| 42 |
908 |
▶ | if (!app || !SvROK(app) || SvTYPE(SvRV(app)) != SVt_PVHV) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!app || !SvROK(app) || SvTYPE(SvRV(app)) != SVt_PVHV) |
908 |
0 |
908 |
| if (!app || !SvROK(app) || SvTYPE(SvRV(app)) != SVt_PVHV) |
908 |
0 |
908 |
| if (!app || !SvROK(app) || SvTYPE(SvRV(app)) != SVt_PVHV) |
0 |
908 |
908 |
|
| 43 |
0 |
| croak("Punk::Plugin::SAML: expected the application, got something else"); |
| 44 |
908 |
| return (HV *)SvRV(app); |
| 45 |
|
| } |
| 46 |
|
| |
| 47 |
|
| /* Fetch one of this plugin's keys, borrowed, or NULL. */ |
| 48 |
729 |
| PERL_STATIC_INLINE SV *psaml_app_get(pTHX_ SV *app, const char *key) { |
| 49 |
729 |
| HV *hv = psaml_app_hv(aTHX_ app); |
| 50 |
729 |
| SV **e = hv_fetch(hv, key, (I32)strlen(key), 0); |
| 51 |
729 |
▶ | return (e && *e && SvOK(*e)) ? *e : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| return (e && *e && SvOK(*e)) ? *e : NULL; |
583 |
146 |
729 |
| return (e && *e && SvOK(*e)) ? *e : NULL; |
583 |
0 |
583 |
| return (e && *e && SvOK(*e)) ? *e : NULL; |
583 |
0 |
583 |
|
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
|
| /* Store one, taking ownership of val. */ |
| 55 |
164 |
| PERL_STATIC_INLINE void psaml_app_set(pTHX_ SV *app, const char *key, SV *val) { |
| 56 |
164 |
| HV *hv = psaml_app_hv(aTHX_ app); |
| 57 |
164 |
▶ | if (!hv_store(hv, key, (I32)strlen(key), val, 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hv_store(hv, key, (I32)strlen(key), val, 0)) |
0 |
164 |
164 |
|
| 58 |
0 |
| SvREFCNT_dec(val); |
| 59 |
164 |
| } |
| 60 |
|
| |
| 61 |
|
| /* This plugin's key as a hashref, created on first use. Returns the RV, |
| 62 |
|
| * borrowed. */ |
| 63 |
25 |
| PERL_STATIC_INLINE SV *psaml_app_hash(pTHX_ SV *app, const char *key) { |
| 64 |
25 |
| SV *v = psaml_app_get(aTHX_ app, key); |
| 65 |
25 |
▶ | if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) return v; |
Branch: 3 branches
| Branch | True | False | Total |
| if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) return v; |
3 |
22 |
25 |
| if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) return v; |
3 |
0 |
3 |
| if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) return v; |
3 |
0 |
3 |
|
| 66 |
22 |
| v = newRV_noinc((SV *)newHV()); |
| 67 |
22 |
| psaml_app_set(aTHX_ app, key, v); |
| 68 |
22 |
| return v; |
| 69 |
|
| } |
| 70 |
|
| |
| 71 |
|
| /* The same for an arrayref. */ |
| 72 |
25 |
| PERL_STATIC_INLINE SV *psaml_app_array(pTHX_ SV *app, const char *key) { |
| 73 |
25 |
| SV *v = psaml_app_get(aTHX_ app, key); |
| 74 |
25 |
▶ | if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) return v; |
Branch: 3 branches
| Branch | True | False | Total |
| if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) return v; |
3 |
22 |
25 |
| if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) return v; |
3 |
0 |
3 |
| if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) return v; |
3 |
0 |
3 |
|
| 75 |
22 |
| v = newRV_noinc((SV *)newAV()); |
| 76 |
22 |
| psaml_app_set(aTHX_ app, key, v); |
| 77 |
22 |
| return v; |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
|
| /* Call a method on $app and return the single result as a mortal, or |
| 81 |
|
| * NULL when the call returned nothing. |
| 82 |
|
| * |
| 83 |
|
| * Returns +1 and NOT mortal: the caller owns it and frees it. Most |
| 84 |
|
| * callers here want exactly that, because they call for the side effect |
| 85 |
|
| * and drop the result with SvREFCNT_dec, and a mortal handed to |
| 86 |
|
| * SvREFCNT_dec is freed twice - once by the caller and once when the |
| 87 |
|
| * temps stack unwinds. A caller that wants a mortal says sv_2mortal at |
| 88 |
|
| * its own call site. |
| 89 |
|
| * |
| 90 |
|
| * The value is incremented BEFORE FREETMPS, not after: a value the callee |
| 91 |
|
| * mortalised is freed by that FREETMPS, so the reference has to be taken |
| 92 |
|
| * while the frame is still standing. This is the lifetime bug Crypt::JWS |
| 93 |
|
| * hit with a resolver-returned key. */ |
| 94 |
641 |
| PERL_STATIC_INLINE SV *psaml_call(pTHX_ SV *invocant, const char *method, |
| 95 |
|
| SV **args, int nargs) { |
| 96 |
641 |
| dSP; |
| 97 |
|
| int count, i; |
| 98 |
641 |
| SV *out = NULL; |
| 99 |
641 |
| ENTER; SAVETMPS; |
| 100 |
641 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
641 |
641 |
|
| 101 |
641 |
▶ | XPUSHs(invocant); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(invocant); |
0 |
641 |
641 |
|
| 102 |
1910 |
▶ | for (i = 0; i < nargs; i++) XPUSHs(args[i]); |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < nargs; i++) XPUSHs(args[i]); |
0 |
1269 |
1269 |
| for (i = 0; i < nargs; i++) XPUSHs(args[i]); |
1269 |
641 |
1910 |
|
| 103 |
641 |
| PUTBACK; |
| 104 |
641 |
| count = call_method(method, G_SCALAR); |
| 105 |
641 |
| SPAGAIN; |
| 106 |
641 |
▶ | if (count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) { |
641 |
0 |
641 |
|
| 107 |
641 |
| SV *r = POPs; |
| 108 |
641 |
▶ | if (SvOK(r)) out = SvREFCNT_inc_simple_NN(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(r)) out = SvREFCNT_inc_simple_NN(r); |
560 |
81 |
641 |
|
| 109 |
|
| } |
| 110 |
641 |
| PUTBACK; |
| 111 |
641 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
641 |
0 |
641 |
|
| 112 |
641 |
| return out; /* +1, caller owns; may be NULL */ |
| 113 |
|
| } |
| 114 |
|
| |
| 115 |
|
| /* Call a coderef, the same shape as psaml_call above. |
| 116 |
|
| * |
| 117 |
|
| * on_login and on_error both go through this rather than each opening |
| 118 |
|
| * their own ENTER/PUSHMARK/call_sv inline. One shape, tested once: the |
| 119 |
|
| * inline version of this in the ACS handed the callback a glob instead |
| 120 |
|
| * of the context, and the failure surfaced as Punk trying to JSON-encode |
| 121 |
|
| * a GLOB reference three frames away from the cause. */ |
| 122 |
13 |
| PERL_STATIC_INLINE SV *psaml_call_cv(pTHX_ SV *code, SV **args, int nargs) { |
| 123 |
13 |
| dSP; |
| 124 |
|
| int count, i; |
| 125 |
13 |
| SV *out = NULL; |
| 126 |
13 |
| ENTER; SAVETMPS; |
| 127 |
13 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
13 |
13 |
|
| 128 |
39 |
▶ | for (i = 0; i < nargs; i++) XPUSHs(args[i] ? args[i] : &PL_sv_undef); |
Branch: 3 branches
| Branch | True | False | Total |
| for (i = 0; i < nargs; i++) XPUSHs(args[i] ? args[i] : &PL_sv_undef); |
0 |
26 |
26 |
| for (i = 0; i < nargs; i++) XPUSHs(args[i] ? args[i] : &PL_sv_undef); |
26 |
0 |
26 |
| for (i = 0; i < nargs; i++) XPUSHs(args[i] ? args[i] : &PL_sv_undef); |
26 |
13 |
39 |
|
| 129 |
13 |
| PUTBACK; |
| 130 |
13 |
| count = call_sv(code, G_SCALAR); |
| 131 |
13 |
| SPAGAIN; |
| 132 |
13 |
▶ | if (count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) { |
13 |
0 |
13 |
|
| 133 |
13 |
| SV *r = POPs; |
| 134 |
13 |
▶ | if (SvOK(r)) out = SvREFCNT_inc_simple_NN(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(r)) out = SvREFCNT_inc_simple_NN(r); |
1 |
12 |
13 |
|
| 135 |
|
| } |
| 136 |
13 |
| PUTBACK; |
| 137 |
13 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
13 |
0 |
13 |
|
| 138 |
13 |
| return out; /* +1, caller owns; may be NULL */ |
| 139 |
|
| } |
| 140 |
|
| |
| 141 |
|
| /* The same, on the application object. */ |
| 142 |
|
| #define psaml_app_call(app, method, args, n) \ |
| 143 |
|
| psaml_call(aTHX_ (app), (method), (args), (n)) |
| 144 |
|
| |
| 145 |
|
| |
| 146 |
|
| /* ---- the owner ------------------------------------------------------- */ |
| 147 |
|
| |
| 148 |
|
| /* PSAML_WHO is in psaml_compat.h, where every header can see it. It is |
| 149 |
|
| * install_kw's third argument and the prefix of every croak: Punk makes a |
| 150 |
|
| * second install by the same owner a no-op, which is what lets `import` |
| 151 |
|
| * and `register` both ask and only the first do anything. */ |
| 152 |
|
| |
| 153 |
|
| /* ---- calling into Punk ----------------------------------------------- */ |
| 154 |
|
| |
| 155 |
|
| /* Handles both an object and a bare package name: `import` runs with the |
| 156 |
|
| * application's package before any object exists, and register runs with |
| 157 |
|
| * the object. */ |
| 158 |
102 |
| PERL_STATIC_INLINE int psaml_can(pTHX_ SV *sv, const char *method) { |
| 159 |
|
| HV *stash; |
| 160 |
102 |
▶ | if (!sv || !SvOK(sv)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!sv || !SvOK(sv)) return 0; |
102 |
0 |
102 |
| if (!sv || !SvOK(sv)) return 0; |
0 |
102 |
102 |
|
| 161 |
102 |
▶ | stash = SvROK(sv) ? SvSTASH(SvRV(sv)) : gv_stashsv(sv, 0); |
Branch: 1 branch
| Branch | True | False | Total |
| stash = SvROK(sv) ? SvSTASH(SvRV(sv)) : gv_stashsv(sv, 0); |
67 |
35 |
102 |
|
| 162 |
102 |
▶ | return stash && gv_fetchmethod_autoload(stash, method, FALSE) != NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| return stash && gv_fetchmethod_autoload(stash, method, FALSE) != NULL; |
102 |
0 |
102 |
| return stash && gv_fetchmethod_autoload(stash, method, FALSE) != NULL; |
101 |
1 |
102 |
|
| 163 |
|
| } |
| 164 |
|
| |
| 165 |
|
| /* ---- option validation ------------------------------------------------ */ |
| 166 |
|
| |
| 167 |
2 |
| PERL_STATIC_INLINE SV *psaml_name_list(pTHX_ const char *const *known) { |
| 168 |
2 |
| SV *out = sv_2mortal(newSVpvs("")); |
| 169 |
|
| int i; |
| 170 |
25 |
▶ | for (i = 0; known[i]; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; known[i]; i++) { |
23 |
2 |
25 |
|
| 171 |
23 |
▶ | if (i) sv_catpvs(out, ", "); |
Branch: 1 branch
| Branch | True | False | Total |
| if (i) sv_catpvs(out, ", "); |
21 |
2 |
23 |
|
| 172 |
23 |
| sv_catpv(out, known[i]); |
| 173 |
|
| } |
| 174 |
2 |
| return out; |
| 175 |
|
| } |
| 176 |
|
| |
| 177 |
|
| /* An unknown key croaks naming the noun, the key and what was available. |
| 178 |
|
| * The alternative is a typo that configures nothing and changes no |
| 179 |
|
| * behaviour, which is how `require_signd` becomes a site that checks |
| 180 |
|
| * nothing and nobody finds out until an audit. */ |
| 181 |
84 |
| PERL_STATIC_INLINE void psaml_check_opts(pTHX_ const char *noun, HV *opts, |
| 182 |
|
| const char *const *known) { |
| 183 |
|
| HE *he; |
| 184 |
84 |
▶ | if (!opts) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!opts) return; |
0 |
84 |
84 |
|
| 185 |
84 |
| hv_iterinit(opts); |
| 186 |
192 |
▶ | while ((he = hv_iternext(opts))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(opts))) { |
110 |
82 |
192 |
|
| 187 |
|
| STRLEN kl; |
| 188 |
110 |
▶ | const char *k = HePV(he, kl); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *k = HePV(he, kl); |
0 |
110 |
110 |
|
| 189 |
110 |
| int i, ok = 0; |
| 190 |
256 |
▶ | for (i = 0; known[i]; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; known[i]; i++) |
254 |
2 |
256 |
|
| 191 |
254 |
▶ | if (strlen(known[i]) == kl && memEQ(known[i], k, kl)) { ok = 1; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (strlen(known[i]) == kl && memEQ(known[i], k, kl)) { ok = 1; break; } |
115 |
139 |
254 |
| if (strlen(known[i]) == kl && memEQ(known[i], k, kl)) { ok = 1; break; } |
108 |
7 |
115 |
|
| 192 |
110 |
▶ | if (!ok) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) |
2 |
108 |
110 |
|
| 193 |
4 |
| croak("%s: unknown %s '%.*s' (known: %s)", PSAML_WHO, noun, |
| 194 |
|
| (int)kl, k, SvPV_nolen(psaml_name_list(aTHX_ known))); |
| 195 |
|
| } |
| 196 |
|
| } |
| 197 |
|
| |
| 198 |
|
| /* ---- installing ------------------------------------------------------- */ |
| 199 |
|
| |
| 200 |
120 |
| PERL_STATIC_INLINE void psaml_keyword(pTHX_ SV *app, const char *name, |
| 201 |
|
| XSUBADDR_t body, AV *cap) { |
| 202 |
|
| SV *argv[3]; |
| 203 |
120 |
| argv[0] = sv_2mortal(newSVpv(name, 0)); |
| 204 |
120 |
| argv[1] = sv_2mortal(psaml_closure(aTHX_ body, cap)); |
| 205 |
120 |
| argv[2] = sv_2mortal(newSVpvs(PSAML_WHO)); |
| 206 |
120 |
| SvREFCNT_dec(psaml_call(aTHX_ app, "install_kw", argv, 3)); |
| 207 |
120 |
| } |
| 208 |
|
| |
| 209 |
52 |
| PERL_STATIC_INLINE void psaml_helper(pTHX_ SV *app, const char *name, |
| 210 |
|
| XSUBADDR_t body, AV *cap) { |
| 211 |
|
| SV *argv[2]; |
| 212 |
52 |
| argv[0] = sv_2mortal(newSVpv(name, 0)); |
| 213 |
52 |
| argv[1] = sv_2mortal(psaml_closure(aTHX_ body, cap)); |
| 214 |
52 |
| SvREFCNT_dec(psaml_call(aTHX_ app, "helper", argv, 2)); |
| 215 |
52 |
| } |
| 216 |
|
| |
| 217 |
|
| /* A route. Written here and first CALLED in phase 8, which mounts the |
| 218 |
|
| * four; nothing in phase 4 mounts anything. |
| 219 |
|
| * |
| 220 |
|
| * `sitemap => 0` on all of them: Punk::Plugin::Sitemap lists every GET |
| 221 |
|
| * route with no capture and no guard, and /saml/metadata and |
| 222 |
|
| * /saml/login are both. A login URL in a sitemap is a crawler starting |
| 223 |
|
| * an authentication flow, on a schedule, for as long as the site |
| 224 |
|
| * exists. */ |
| 225 |
60 |
| PERL_STATIC_INLINE void psaml_route(pTHX_ SV *app, const char *method, |
| 226 |
|
| SV *path, XSUBADDR_t body, AV *cap) { |
| 227 |
|
| SV *argv[5]; |
| 228 |
60 |
| HV *o = newHV(); |
| 229 |
60 |
| (void)hv_stores(o, "sitemap", newSViv(0)); |
| 230 |
60 |
| argv[0] = sv_2mortal(newSVpv(method, 0)); |
| 231 |
60 |
| argv[1] = path; |
| 232 |
60 |
| argv[2] = sv_2mortal(psaml_closure(aTHX_ body, cap)); |
| 233 |
60 |
| argv[3] = &PL_sv_undef; |
| 234 |
60 |
| argv[4] = sv_2mortal(newRV_noinc((SV *)o)); |
| 235 |
60 |
| SvREFCNT_dec(psaml_call(aTHX_ app, "route", argv, 5)); |
| 236 |
60 |
| } |
| 237 |
|
| |
| 238 |
|
| /* The to_app seam. on_compile runs once, in registration order, after |
| 239 |
|
| * every keyword has recorded and before anything is compiled, which is |
| 240 |
|
| * the only moment at which `host` has certainly been declared and every |
| 241 |
|
| * saml_idp has certainly been seen. Both matter here: the ACS URL |
| 242 |
|
| * derives from `host`, and `saml_login` has to be able to complain that |
| 243 |
|
| * no provider was declared without caring which line came first. |
| 244 |
|
| * |
| 245 |
|
| * The `can` guard is kept although the floor is well past 0.30, so the |
| 246 |
|
| * failure is a croak naming the version rather than a method-not-found |
| 247 |
|
| * from inside a closure. */ |
| 248 |
26 |
| PERL_STATIC_INLINE void psaml_at_compile(pTHX_ SV *app, XSUBADDR_t body, |
| 249 |
|
| AV *cap) { |
| 250 |
|
| SV *argv[2]; |
| 251 |
26 |
▶ | if (!psaml_can(aTHX_ app, "on_compile")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!psaml_can(aTHX_ app, "on_compile")) { |
0 |
26 |
26 |
|
| 252 |
0 |
| SvREFCNT_dec((SV *)cap); |
| 253 |
0 |
| croak("%s needs Punk 0.30 or newer for on_compile", PSAML_WHO); |
| 254 |
|
| } |
| 255 |
26 |
| argv[0] = sv_2mortal(psaml_closure(aTHX_ body, cap)); |
| 256 |
26 |
| argv[1] = sv_2mortal(newSVpvs(PSAML_WHO)); |
| 257 |
26 |
| SvREFCNT_dec(psaml_call(aTHX_ app, "on_compile", argv, 2)); |
| 258 |
26 |
| } |
| 259 |
|
| |
| 260 |
|
| #endif /* PSAML_REG_H */ |