1 /* zxidutil.h  -  (Private) Utility API functions
2  * Copyright (c) 2015 Synergetics NV (sampo@synergetics.be), All Rights Reserved.
3  * Copyright (c) 2009-2012 Sampo Kellomaki (sampo@iki.fi), All Rights Reserved.
4  * Copyright (c) 2006-2009 Symlabs (symlabs@symlabs.com), All Rights Reserved.
5  * Author: Sampo Kellomaki (sampo@iki.fi)
6  * This is confidential unpublished proprietary source code of the author.
7  * NO WARRANTY, not even implied warranties. Contains trade secrets.
8  * Distribution prohibited unless authorized in writing.
9  * Licensed under Apache License 2.0, see file COPYING.
10  * $Id: zxid.h,v 1.94 2010-01-08 02:10:09 sampo Exp $
11  *
12  * 12.8.2006,  created --Sampo
13  * 31.5.2010,  eliminated many include dependencies from the public API --Sampo
14  * 13.11.2010, added ZXID_DECL for benefit of the Windows port --Sampo
15  * 12.12.2010, separate zxidpriv.h and zxidutil.h from zxid.h --Sampo
16  * 10.1.2011,  added TrustPDP support --Sampo
17  * 29.5.2015,  tweaked some prototypes --Sampo
18  */
19 
20 #ifndef _zxidutil_h
21 #define _zxidutil_h
22 
23 #include <memory.h>
24 #include <string.h>
25 #include <zx/zx.h>
26 #include <zx/zxid.h>
27 
28 /* ZXID_DECL allows all API functions to be qualified with a declatation, such
29  * as relating to the calling convention (e.g. c-decl). Such qualification
30  * is very important in the Windows environment. In such environment ZXID_DECL
31  * will be defined in Makefile to cause the desired effect. */
32 
33 #ifndef ZXID_DECL
34 #define ZXID_DECL
35 #endif
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /* zxpw */
42 
43 ZXID_DECL int zx_password_authn(const char* path, char* uid, const char* passw, const char* pin, int fd_hint);
44 
45 /* zxidsso - trivial mapping functions */
46 
47 ZXID_DECL int zxid_protocol_binding_map_saml2(struct zx_str* b);
48 ZXID_DECL const char* zxid_saml2_map_nid_fmt(const char* f);
49 ZXID_DECL const char* zxid_saml2_map_protocol_binding(const char* b);
50 ZXID_DECL char* zxid_saml2_map_authn_ctx(char* c);
51 ZXID_DECL void zxid_sigres_map(int sigres, char** sigval, char** sigmsg);
52 ZXID_DECL int zxid_map_sec_mech(zxid_epr* epr);
53 
54 /* zxlog */
55 
56 #define ZXLOG_TIME_SIZ 19 /* not including nul termination */
57 #define ZXLOG_TIME_FMT "%04d%02d%02d-%02d%02d%02d.%03ld"
58 #define ZXLOG_TIME_ARG(t,usec) t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, \
59                                t.tm_hour, t.tm_min, t.tm_sec, usec/1000
60 
61 /* zxlog(cf, ourts, srcts, ipport4, entid, msgid6, a7nid, nid8, sigval, res, op, arg, fmt, ...); */
62 
63 ZXID_DECL struct zx_str* zxlog_path(zxid_conf* cf, struct zx_str* entid, struct zx_str* objid, const char* dir, const char* kind, int create_dirs);
64 
65 /* zxidepr */
66 
67 ZXID_DECL int zxid_nice_sha1(zxid_conf* cf, char* buf, int buf_len, struct zx_str* name, struct zx_str* contint, int ign_prefix);
68 ZXID_DECL void zxid_fold_svc(char* path, int len);
69 ZXID_DECL int zxid_epr_path(zxid_conf* cf, char* dir, char* sid, char* buf, int buf_len, struct zx_str* svc, int rank, struct zx_str* cont);
70 
71 ZXID_DECL int zxid_call_trustpdp(zxid_conf* cf, zxid_cgi* cgi, zxid_ses* ses, struct zxid_map* pepmap, const char* start, const char* lim, zxid_epr* epr);
72 
73 /* zxutil */
74 
75 ZXID_DECL int zx_match(const char* pat, const char* target);
76 ZXID_DECL char* base64_fancy_raw(const char* p, int len, char* r, const char* basis_64, int line_len, int eol_len, const char* eol, char eq_pad);
77 ZXID_DECL char* unbase64_raw(const char* p, const char* lim, char* r, const unsigned char* index_64);
78 ZXID_DECL char* zx_zlib_raw_deflate(struct zx_ctx* c, int in_len, const char* in, int* out_len);  /* gzip */
79 ZXID_DECL char* zx_zlib_raw_inflate(struct zx_ctx* c, int in_len, const char* in, int* out_len);  /* gunzip */
80 ZXID_DECL char* zxid_deflate_safe_b64_raw(struct zx_ctx* c, int len, const char* s);
81 ZXID_DECL char* zxid_deflate_safe_b64(struct zx_ctx* c, struct zx_str* ss);
82 ZXID_DECL char* zxid_unbase64_inflate(struct zx_ctx* c, int in_len, const char* in, int* out_len);
83 ZXID_DECL int   zx_url_encode_len(int in_len, const char* in);
84 ZXID_DECL char* zx_url_encode_raw(int in_len, const char* in, char* out);
85 ZXID_DECL char* zx_url_encode(struct zx_ctx* c, int in_len, const char* in, int* out_len);
86 ZXID_DECL char* zx_mk_basic_auth_b64(struct zx_ctx* c, const char* uid, const char* pw);
87 ZXID_DECL char* zxid_qs_nv_scan(char* qs, char** name, char** val, int url_decode_val_flag);
88 ZXID_DECL char* zx_hexdec(char* dst, char* src, int src_len, const unsigned char* trans);
89 
90 ZXID_DECL int get_file_size(fdtype fd);
91 ZXID_DECL char* read_all_alloc(struct zx_ctx* c, const char* logkey, int reperr, int* lenp, const char* name_fmt, ...);
92 ZXID_DECL char* read_all_malloc(const char* logkey, int reperr, int* lenp, const char* name_fmt, ...);
93 ZXID_DECL int read_all(int maxlen, char* buf, const char* logkey, int reperr, const char* name_fmt, ...);
94 ZXID_DECL int name_from_path(char* buf, int buf_len, const char* name_fmt, ...);
95 ZXID_DECL fdtype open_fd_from_path(int flags, int mode, const char* logkey, int reperr, const char* name_fmt, ...);
96 ZXID_DECL int read_all_fd(fdtype fd, char* p, int want, int* got_all);
97 ZXID_DECL int write_all_fd(fdtype fd, const char* p, int pending);
98 ZXID_DECL int write_all_fd_fmt(fdtype fd, const char* logkey, int maxlen, char* buf, const char* data_fmt, ...)
99 ;
100 ZXID_DECL int write_all_path_fmt(const char* logkey, int len, char* buf, const char* path_fmt, const char* prepath, const char* postpath, const char* data_fmt, ...);
101 ZXID_DECL int write_all_path(const char* logkey, const char* path_fmt, const char* prepath, const char* postpath, int len, const char* data);
102 ZXID_DECL int copy_file(const char* from, const char* to, const char* logkey, int may_link);
103 ZXID_DECL int close_file(fdtype fd, const char* logkey);
104 ZXID_DECL int send_all_socket(fdtype fd, const char* p, int pending);
105 ZXID_DECL char* zx_zap_inplace_raw(char* s, const char* zap);
106 
107 ZXID_DECL const char* zx_json_extract_raw(const char* hay, const char* key, int* len);
108 ZXID_DECL char* zx_json_extract_dup(struct zx_ctx* c, const char* hay, const char* key);
109 ZXID_DECL int zx_json_extract_int(const char* hay, const char* key);
110 ZXID_DECL const char* zx_qs_extract_raw(const char* hay, const char* key, int* len);
111 ZXID_DECL char* zx_qs_extract_dup(struct zx_ctx* c, const char* hay, const char* key);
112 
113 struct zxid_curl_ctx {
114   char* p;
115   char* buf;
116   char* lim;
117 };
118 
119 ZXID_DECL size_t zxid_curl_write_data(void* buffer, size_t size, size_t nmemb, void* userp);
120 ZXID_DECL size_t zxid_curl_read_data(void* buffer, size_t size, size_t nmemb, void* userp);
121 ZXID_DECL char* zxid_http_get(zxid_conf* cf, const char* url, char** lim);
122 
123 #include <zx/zxidnoswig.h>
124 
125 #ifdef __cplusplus
126 } // extern "C"
127 #endif
128 
129 #endif /* _zxidutil_h */
130