~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to proxy/PluginDB.cc

  • Committer: Bazaar Package Importer
  • Author(s): Arno Toell
  • Date: 2011-01-13 11:49:18 UTC
  • Revision ID: james.westby@ubuntu.com-20110113114918-vu422h8dknrgkj15
Tags: upstream-2.1.5-unstable
ImportĀ upstreamĀ versionĀ 2.1.5-unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file
 
2
 
 
3
  A brief file description
 
4
 
 
5
  @section license License
 
6
 
 
7
  Licensed to the Apache Software Foundation (ASF) under one
 
8
  or more contributor license agreements.  See the NOTICE file
 
9
  distributed with this work for additional information
 
10
  regarding copyright ownership.  The ASF licenses this file
 
11
  to you under the Apache License, Version 2.0 (the
 
12
  "License"); you may not use this file except in compliance
 
13
  with the License.  You may obtain a copy of the License at
 
14
 
 
15
      http://www.apache.org/licenses/LICENSE-2.0
 
16
 
 
17
  Unless required by applicable law or agreed to in writing, software
 
18
  distributed under the License is distributed on an "AS IS" BASIS,
 
19
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
20
  See the License for the specific language governing permissions and
 
21
  limitations under the License.
 
22
 */
 
23
 
 
24
#include "ink_config.h"
 
25
#include <stdlib.h>
 
26
#include <time.h>
 
27
#include <string.h>
 
28
#include "ink_code.h"
 
29
#include "Diags.h"
 
30
#include "ParseRules.h"
 
31
#include "PluginDB.h"
 
32
 
 
33
/***************************************************************************
 
34
 *
 
35
 * An Inktomi Traffic Server plugin license key should look like:
 
36
 *
 
37
 *     XXXXXEEEEDDDDDD
 
38
 *
 
39
 * XXXXX is a 5 digit alphanumeric id used by plugin vendors to
 
40
 * assign to their customers.
 
41
 *
 
42
 * EEEE is the hex encoding of the expiration date. It's the number
 
43
 * of days from January 1, 1970. If a plugin has no expiration date,
 
44
 * 0000 can be used instead.
 
45
 *
 
46
 * DDDDDD is the INK_MD5 encoding of some combination of the following
 
47
 * strings: "Inktomi Traffic Server", "Plugin Name", "XXXXXEEEE".
 
48
 *
 
49
 *
 
50
 ***************************************************************************/
 
51
 
 
52
const char *
 
53
  PluginDB::CheckLicenseResultStr[] = {
 
54
  "license missing",
 
55
  "license expired",
 
56
  "license invalid",
 
57
  "license ok"
 
58
};
 
59
 
 
60
const unsigned int
 
61
  PluginDB::license_custid_len = 5;
 
62
const unsigned int
 
63
  PluginDB::license_expire_len = 4;
 
64
const unsigned int
 
65
  PluginDB::license_digest_len = 6;
 
66
const unsigned int
 
67
  PluginDB::license_total_len = PluginDB::license_custid_len +
 
68
  PluginDB::license_expire_len + PluginDB::license_digest_len;
 
69
 
 
70
PluginDB::PluginDB(const char *plugin_db_file)
 
71
{
 
72
  info_table = ink_hash_table_create(InkHashTableKeyType_String);
 
73
  ReadPluginDB(plugin_db_file);
 
74
}
 
75
 
 
76
PluginDB::~PluginDB(void)
 
77
{
 
78
  ink_hash_table_destroy_and_free_values(info_table);
 
79
}
 
80
 
 
81
void
 
82
PluginDB::ReadPluginDB(const char *plugin_db_file)
 
83
{
 
84
  FILE *pdb = fopen(plugin_db_file, "r");
 
85
  if (pdb == NULL) {
 
86
    Warning("unable to open plugin.db file '%s': %d, %s", plugin_db_file, errno, strerror(errno));
 
87
    return;
 
88
  }
 
89
 
 
90
  char line[1024];
 
91
  char plugin_obj[256];
 
92
  plugin_obj[0] = '\0';
 
93
  PluginDB::PluginInfo * pinfo = new PluginDB::PluginInfo();
 
94
 
 
95
  while (fgets(line, sizeof(line) - 1, pdb) != NULL) {
 
96
    char *p = line;
 
97
    while (*p && ParseRules::is_wslfcr(*p)) {
 
98
      p++;
 
99
    }
 
100
    if ((*p == '\0') || (*p == '#')) {
 
101
      continue;
 
102
    }
 
103
    // We have a non-comment and non-blank line
 
104
 
 
105
    // Nullify the newline character
 
106
    int len = strlen(p);
 
107
    int i;
 
108
    p[len - 1] = '\0';
 
109
 
 
110
    if (p[0] == '[') {
 
111
      if (plugin_obj[0] != '\0' && (pinfo->name[0] != '\0' || pinfo->license[0] != '\0')) {
 
112
        ink_hash_table_insert(info_table, (InkHashTableKey) plugin_obj, (InkHashTableValue) pinfo);
 
113
        plugin_obj[0] = '\0';
 
114
        pinfo = new PluginDB::PluginInfo();
 
115
      }
 
116
      p++;
 
117
      for (i = 0; p[i] != '\0' && p[i] != ']' && i < 255; i++) {
 
118
        pinfo->name[i] = p[i];
 
119
      }
 
120
      pinfo->name[i] = '\0';
 
121
 
 
122
    } else {
 
123
      if (strstr(p, "Object=")) {
 
124
        p = p + sizeof("Object=") - 1;
 
125
        for (i = 0; p[i] != '\0' && i < 255; i++) {
 
126
          plugin_obj[i] = p[i];
 
127
        }
 
128
        plugin_obj[i] = '\0';
 
129
      } else if (strstr(p, "License=")) {
 
130
        p = p + sizeof("License=") - 1;
 
131
        for (i = 0; p[i] != '\0' && i < 255; i++) {
 
132
          pinfo->license[i] = p[i];
 
133
        }
 
134
        pinfo->license[i] = '\0';
 
135
      }
 
136
    }
 
137
  }
 
138
 
 
139
  if (plugin_obj[0] != '\0' && (pinfo->name[0] != '\0' || pinfo->license[0] != '\0')) {
 
140
    ink_hash_table_insert(info_table, (InkHashTableKey) plugin_obj, (InkHashTableValue) pinfo);
 
141
  } else {
 
142
    delete pinfo;
 
143
  }
 
144
  fclose(pdb);
 
145
}
 
146
 
 
147
PluginDB::CheckLicenseResult PluginDB::CheckLicense(const char *plugin_obj)
 
148
{
 
149
  char
 
150
    buffer[1024],
 
151
    buffer_md5[16],
 
152
    buffer_md5_str[33];
 
153
  char
 
154
    expire_str[PluginDB::license_expire_len + 1];
 
155
  unsigned long
 
156
    expire_days;
 
157
  INK_DIGEST_CTX
 
158
    md5_context;
 
159
  PluginDB::PluginInfo * pinfo;
 
160
  char *
 
161
    end_ptr = NULL;
 
162
 
 
163
  InkHashTableEntry *
 
164
    ht_entry = ink_hash_table_lookup_entry(info_table,
 
165
                                           (InkHashTableKey) plugin_obj);
 
166
  if (ht_entry != NULL) {
 
167
    pinfo = (PluginDB::PluginInfo *) ink_hash_table_entry_value(info_table, ht_entry);
 
168
  } else {
 
169
    return PluginDB::license_missing;
 
170
  }
 
171
 
 
172
  if (strlen(pinfo->license) != PluginDB::license_total_len) {
 
173
    return PluginDB::license_invalid;
 
174
  }
 
175
 
 
176
  snprintf(buffer, sizeof(buffer), "Inktomi Traffic Server %s ", pinfo->name);
 
177
  strncat(buffer, pinfo->license, PluginDB::license_custid_len + PluginDB::license_expire_len);
 
178
 
 
179
  ink_code_incr_md5_init(&md5_context);
 
180
  ink_code_incr_md5_update(&md5_context, buffer, strlen(buffer));
 
181
  ink_code_incr_md5_final(buffer_md5, &md5_context);
 
182
  // coverity[uninit_use_in_call]
 
183
  ink_code_md5_stringify(buffer_md5_str, sizeof(buffer_md5_str), buffer_md5);
 
184
 
 
185
  if (strncmp(buffer_md5_str,
 
186
              pinfo->license + PluginDB::license_custid_len
 
187
              + PluginDB::license_expire_len, PluginDB::license_digest_len) != 0) {
 
188
    return PluginDB::license_invalid;
 
189
  }
 
190
 
 
191
  strncpy(expire_str, pinfo->license + PluginDB::license_custid_len, PluginDB::license_expire_len);
 
192
  expire_str[PluginDB::license_expire_len] = '\0';
 
193
 
 
194
  expire_days = strtoul(expire_str, &end_ptr, 16);
 
195
 
 
196
  if (expire_days != 0) {
 
197
    time_t
 
198
      time_now = time(NULL);
 
199
    if ((unsigned long) time_now > expire_days * (60 * 60 * 24)) {
 
200
      return PluginDB::license_expired;
 
201
    }
 
202
  }
 
203
 
 
204
  return PluginDB::license_ok;
 
205
}