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 #include "mmdb.h"
20 
21 ///////////////////////////////////////////////////////////////////////////////
22 // Initialize the plugin as a remap plugin.
23 //
24 TSReturnCode
TSRemapInit(TSRemapInterface * api_info,char * errbuf,int errbuf_size)25 TSRemapInit(TSRemapInterface *api_info, char *errbuf, int errbuf_size)
26 {
27   if (api_info->size < sizeof(TSRemapInterface)) {
28     strncpy(errbuf, "[tsremap_init] - Incorrect size of TSRemapInterface structure", errbuf_size - 1);
29     return TS_ERROR;
30   }
31 
32   if (api_info->tsremap_version < TSREMAP_VERSION) {
33     snprintf(errbuf, errbuf_size, "[tsremap_init] - Incorrect API version %ld.%ld", api_info->tsremap_version >> 16,
34              (api_info->tsremap_version & 0xffff));
35     return TS_ERROR;
36   }
37 
38   TSDebug(PLUGIN_NAME, "remap plugin is successfully initialized");
39   return TS_SUCCESS;
40 }
41 
42 TSReturnCode
TSRemapNewInstance(int argc,char * argv[],void ** ih,char *,int)43 TSRemapNewInstance(int argc, char *argv[], void **ih, char * /* errbuf */, int /* errbuf_size */)
44 {
45   if (argc < 3) {
46     TSError("[%s] Unable to create remap instance, missing configuration file", PLUGIN_NAME);
47     return TS_ERROR;
48   }
49 
50   Acl *a = new Acl();
51   *ih    = static_cast<void *>(a);
52   if (!a->init(argv[2])) {
53     TSError("[%s] Failed to initialize maxmind with %s", PLUGIN_NAME, argv[2]);
54     return TS_ERROR;
55   }
56 
57   TSDebug(PLUGIN_NAME, "created remap instance with configuration %s", argv[2]);
58   return TS_SUCCESS;
59 }
60 
61 void
TSRemapDeleteInstance(void * ih)62 TSRemapDeleteInstance(void *ih)
63 {
64   if (nullptr != ih) {
65     Acl *const a = static_cast<Acl *>(ih);
66     delete a;
67   }
68 }
69 
70 ///////////////////////////////////////////////////////////////////////////////
71 // Main entry point when used as a remap plugin.
72 //
73 TSRemapStatus
TSRemapDoRemap(void * ih,TSHttpTxn rh,TSRemapRequestInfo * rri)74 TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri)
75 {
76   if (nullptr == ih) {
77     TSDebug(PLUGIN_NAME, "No ACLs configured");
78   } else {
79     Acl *a = static_cast<Acl *>(ih);
80     if (!a->eval(rri, rh)) {
81       TSDebug(PLUGIN_NAME, "denying request");
82       TSHttpTxnStatusSet(rh, TS_HTTP_STATUS_FORBIDDEN);
83       a->send_html(rh);
84     }
85   }
86   return TSREMAP_NO_REMAP;
87 }
88