1 /*
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 */
18
19 //////////////////////////////////////////////////////////////////////////////////////////////
20 //
21 // Main entry points for the plugin hooks etc.
22 //
23 #include <ts/ts.h>
24 #include <ts/remap.h>
25 #include <cstdio>
26 #include <cstring>
27
28 #include "lulu.h"
29 #include "acl.h"
30
31 ///////////////////////////////////////////////////////////////////////////////
32 // Initialize the plugin as a remap plugin.
33 //
34 TSReturnCode
TSRemapInit(TSRemapInterface * api_info,char * errbuf,int errbuf_size)35 TSRemapInit(TSRemapInterface *api_info, char *errbuf, int errbuf_size)
36 {
37 if (api_info->size < sizeof(TSRemapInterface)) {
38 strncpy(errbuf, "[tsremap_init] - Incorrect size of TSRemapInterface structure", errbuf_size - 1);
39 return TS_ERROR;
40 }
41
42 if (api_info->tsremap_version < TSREMAP_VERSION) {
43 snprintf(errbuf, errbuf_size, "[tsremap_init] - Incorrect API version %ld.%ld", api_info->tsremap_version >> 16,
44 (api_info->tsremap_version & 0xffff));
45 return TS_ERROR;
46 }
47
48 if (Acl::init()) {
49 TSDebug(PLUGIN_NAME, "remap plugin is successfully initialized");
50 return TS_SUCCESS; /* success */
51 } else {
52 return TS_ERROR;
53 }
54 }
55
56 TSReturnCode
TSRemapNewInstance(int argc,char * argv[],void ** ih,char *,int)57 TSRemapNewInstance(int argc, char *argv[], void **ih, char * /* errbuf */, int /* errbuf_size */)
58 {
59 if (argc < 3) {
60 TSError("[%s] Unable to create remap instance, need more parameters", PLUGIN_NAME);
61 return TS_ERROR;
62 } else {
63 Acl *a = nullptr;
64
65 // ToDo: Should do better processing here, to make it easier to deal with
66 // rules other then country codes.
67 if (!strncmp(argv[2], "country", 11)) {
68 TSDebug(PLUGIN_NAME, "creating an ACL rule with ISO country codes");
69 a = new CountryAcl();
70 }
71
72 if (a) {
73 if (a->process_args(argc, argv) > 0) {
74 *ih = static_cast<void *>(a);
75 } else {
76 TSError("[%s] Unable to create remap instance, no geo-identifying tokens provided", PLUGIN_NAME);
77 return TS_ERROR;
78 }
79 } else {
80 TSError("[%s] Unable to create remap instance, no supported ACL specified as first parameter", PLUGIN_NAME);
81 return TS_ERROR;
82 }
83 }
84
85 return TS_SUCCESS;
86 }
87
88 void
TSRemapDeleteInstance(void * ih)89 TSRemapDeleteInstance(void *ih)
90 {
91 Acl *a = static_cast<Acl *>(ih);
92
93 delete a;
94 }
95
96 ///////////////////////////////////////////////////////////////////////////////
97 // Main entry point when used as a remap plugin.
98 //
99 TSRemapStatus
TSRemapDoRemap(void * ih,TSHttpTxn rh,TSRemapRequestInfo * rri)100 TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri)
101 {
102 if (nullptr == ih) {
103 TSDebug(PLUGIN_NAME, "No ACLs configured, this is probably a plugin bug");
104 } else {
105 Acl *a = static_cast<Acl *>(ih);
106
107 if (!a->eval(rri, rh)) {
108 TSDebug(PLUGIN_NAME, "denying request");
109 TSHttpTxnStatusSet(rh, static_cast<TSHttpStatus>(403));
110 a->send_html(rh);
111 }
112 }
113
114 return TSREMAP_NO_REMAP;
115 }
116