2018-06-19 11:32:16 +08:00
|
|
|
/*************************************************************************
|
|
|
|
|
> File Name: cert_session.c
|
|
|
|
|
> Author:
|
|
|
|
|
> Mail:
|
|
|
|
|
> Created Time: Fri 01 Jun 2018 02:00:56 AM PDT
|
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
/* openssl**/
|
|
|
|
|
#include <opensslv.h>
|
|
|
|
|
#include <ssl.h>
|
|
|
|
|
#include <err.h>
|
|
|
|
|
#include <rand.h>
|
|
|
|
|
#include <x509.h>
|
|
|
|
|
#include <x509v3.h>
|
|
|
|
|
#include <crypto.h>
|
|
|
|
|
#include <engine.h>
|
|
|
|
|
|
|
|
|
|
#include "rt_string.h"
|
|
|
|
|
#include "rt_common.h"
|
|
|
|
|
#include "rt_stdlib.h"
|
|
|
|
|
#include "rt_file.h"
|
2018-07-09 14:32:41 +08:00
|
|
|
#include "rt_time.h"
|
2018-06-19 11:32:16 +08:00
|
|
|
#include "cert_init.h"
|
|
|
|
|
#include "async.h"
|
|
|
|
|
#include "read.h"
|
|
|
|
|
#include "bufferevent.h"
|
|
|
|
|
#include "listener.h"
|
|
|
|
|
#include "libevent.h"
|
|
|
|
|
#include "cert_session.h"
|
|
|
|
|
#include "event_compat.h"
|
|
|
|
|
#include "http.h"
|
|
|
|
|
#include "buffer.h"
|
|
|
|
|
#include "util-internal.h"
|
2018-07-05 14:05:28 +08:00
|
|
|
#include "moodycamel_field_stat2.h"
|
2018-06-19 11:32:16 +08:00
|
|
|
#include "logging.h"
|
|
|
|
|
|
|
|
|
|
#define SG_DATA_SIZE 2048
|
|
|
|
|
|
2018-06-20 16:24:49 +08:00
|
|
|
#define DEFAULT_PRIVATEKEY_NAME "private.key"
|
|
|
|
|
#define DEFAULT_CA_CERTIFICATE "ca.cer"
|
2018-06-19 11:32:16 +08:00
|
|
|
|
2018-06-29 17:18:33 +08:00
|
|
|
static libevent_thread *threads;
|
|
|
|
|
|
2018-07-05 14:05:28 +08:00
|
|
|
struct fs_stats_t{
|
2018-07-09 14:32:41 +08:00
|
|
|
int line_ids[4];
|
2018-07-05 14:05:28 +08:00
|
|
|
screen_stat_handle_t handle;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct fs_stats_t SGstats = {
|
|
|
|
|
.line_ids = {0},
|
|
|
|
|
.handle = NULL,
|
|
|
|
|
};
|
|
|
|
|
|
2018-06-29 17:18:33 +08:00
|
|
|
#define sizeof_seconds(x) (x * 24 * 60 * 60)
|
|
|
|
|
|
2018-07-09 14:32:41 +08:00
|
|
|
rt_mutex entries_mtx = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
#define RD_SYNC
|
|
|
|
|
|
2018-06-19 11:32:16 +08:00
|
|
|
void connectCallback(const struct redisAsyncContext *c, int status) {
|
|
|
|
|
if (status != REDIS_OK) {
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Redis connect error : %s\n", c->errstr);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mesa_runtime_log(RLOG_LV_INFO, MODULE_NAME, "Redis server connected...\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void disconnectCallback(const struct redisAsyncContext *c, int status) {
|
|
|
|
|
if (status != REDIS_OK) {
|
2018-07-18 10:47:45 +08:00
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Redis disconnect error: %s\n", c->errstr);
|
2018-06-19 11:32:16 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2018-07-18 10:47:45 +08:00
|
|
|
mesa_runtime_log(RLOG_LV_INFO, MODULE_NAME, "Redis server disconnected...\n");
|
2018-06-19 11:32:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
ssl_rand(void *p, size_t sz)
|
|
|
|
|
{
|
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L
|
|
|
|
|
rv = RAND_pseudo_bytes((unsigned char*)p, sz);
|
|
|
|
|
if (rv == 1)
|
|
|
|
|
return 0;
|
|
|
|
|
#endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
|
|
|
|
|
rv = RAND_bytes((unsigned char*)p, sz);
|
|
|
|
|
if (rv == 1)
|
|
|
|
|
return 0;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
ssl_x509_serial_copyrand(X509 *dstcrt, X509 *srccrt)
|
|
|
|
|
{
|
|
|
|
|
ASN1_INTEGER *srcptr, *dstptr;
|
|
|
|
|
BIGNUM *bnserial;
|
2018-07-26 17:18:00 +08:00
|
|
|
long rand;
|
2018-06-19 11:32:16 +08:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
|
|
#ifndef PURIFY
|
|
|
|
|
rv = ssl_rand(&rand, sizeof(rand));
|
|
|
|
|
#else /* PURIFY */
|
|
|
|
|
rand = 0xF001;
|
|
|
|
|
rv = 0;
|
|
|
|
|
#endif /* PURIFY */
|
|
|
|
|
dstptr = X509_get_serialNumber(dstcrt);
|
|
|
|
|
srcptr = X509_get_serialNumber(srccrt);
|
|
|
|
|
if ((rv == -1) || !dstptr || !srcptr)
|
|
|
|
|
return -1;
|
|
|
|
|
bnserial = ASN1_INTEGER_to_BN(srcptr, NULL);
|
|
|
|
|
if (!bnserial) {
|
|
|
|
|
/* random 32-bit serial */
|
|
|
|
|
ASN1_INTEGER_set(dstptr, rand);
|
|
|
|
|
} else {
|
|
|
|
|
/* original serial plus random 32-bit offset */
|
|
|
|
|
BN_add_word(bnserial, rand);
|
|
|
|
|
BN_to_ASN1_INTEGER(bnserial, dstptr);
|
|
|
|
|
BN_free(bnserial);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
ssl_x509_v3ext_add(X509V3_CTX *ctx, X509 *crt, char *k, char *v)
|
|
|
|
|
{
|
|
|
|
|
X509_EXTENSION *ext;
|
|
|
|
|
|
|
|
|
|
if (!(ext = X509V3_EXT_conf(NULL, ctx, k, v))) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (X509_add_ext(crt, ext, -1) != 1) {
|
|
|
|
|
X509_EXTENSION_free(ext);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
X509_EXTENSION_free(ext);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
ssl_x509_v3ext_copy_by_nid(X509 *crt, X509 *origcrt, int nid)
|
|
|
|
|
{
|
|
|
|
|
X509_EXTENSION *ext;
|
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
|
|
pos = X509_get_ext_by_NID(origcrt, nid, -1);
|
|
|
|
|
if (pos == -1)
|
|
|
|
|
return 0;
|
|
|
|
|
ext = X509_get_ext(origcrt, pos);
|
|
|
|
|
if (!ext)
|
|
|
|
|
return -1;
|
|
|
|
|
if (X509_add_ext(crt, ext, -1) != 1)
|
|
|
|
|
return -1;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
X509 *
|
2018-07-26 17:18:00 +08:00
|
|
|
x509_modify_by_cert_bak(X509 *cacrt, EVP_PKEY *cakey, X509 *origcrt, EVP_PKEY *key,
|
2018-06-27 10:58:23 +08:00
|
|
|
int days, const char *extraname, const char *crlurl)
|
2018-06-19 11:32:16 +08:00
|
|
|
{
|
|
|
|
|
X509_NAME *subject, *issuer;
|
|
|
|
|
GENERAL_NAMES *names;
|
|
|
|
|
GENERAL_NAME *gn;
|
|
|
|
|
X509 *crt;
|
|
|
|
|
int rv;
|
|
|
|
|
//subjectname,issuername
|
|
|
|
|
subject = X509_get_subject_name(origcrt);
|
|
|
|
|
issuer = X509_get_subject_name(cacrt);
|
|
|
|
|
if (!subject || !issuer)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
crt = X509_new();
|
|
|
|
|
if (!crt)
|
|
|
|
|
return NULL;
|
|
|
|
|
//version,subjectname,issuername,serialnum,time,pubkey
|
|
|
|
|
if (!X509_set_version(crt, 0x02) ||
|
|
|
|
|
!X509_set_subject_name(crt, subject) ||
|
|
|
|
|
!X509_set_issuer_name(crt, issuer) ||
|
|
|
|
|
ssl_x509_serial_copyrand(crt, origcrt) == -1 ||
|
2018-06-27 10:58:23 +08:00
|
|
|
!X509_gmtime_adj(X509_get_notBefore(crt), 0) ||
|
|
|
|
|
!X509_time_adj_ex(X509_get_notAfter(crt), days, 0, NULL) ||
|
2018-06-19 11:32:16 +08:00
|
|
|
!X509_set_pubkey(crt, key))
|
|
|
|
|
goto errout;
|
|
|
|
|
|
|
|
|
|
/* add standard v3 extensions; cf. RFC 2459 */
|
|
|
|
|
//extensions
|
|
|
|
|
X509V3_CTX ctx;
|
|
|
|
|
X509V3_set_ctx(&ctx, cacrt, crt, NULL, NULL, 0);
|
|
|
|
|
|
|
|
|
|
if (ssl_x509_v3ext_add(&ctx, crt, "subjectKeyIdentifier",
|
|
|
|
|
"hash") == -1 ||
|
|
|
|
|
ssl_x509_v3ext_add(&ctx, crt, "authorityKeyIdentifier",
|
|
|
|
|
"keyid,issuer:always") == -1)
|
|
|
|
|
goto errout;
|
|
|
|
|
|
|
|
|
|
rv = ssl_x509_v3ext_copy_by_nid(crt, origcrt,
|
|
|
|
|
NID_basic_constraints);
|
|
|
|
|
if (rv == 0)
|
|
|
|
|
rv = ssl_x509_v3ext_add(&ctx, crt, "basicConstraints",
|
|
|
|
|
"CA:FALSE");
|
|
|
|
|
if (rv == -1)
|
|
|
|
|
goto errout;
|
|
|
|
|
|
|
|
|
|
rv = ssl_x509_v3ext_copy_by_nid(crt, origcrt,
|
|
|
|
|
NID_key_usage);
|
|
|
|
|
if (rv == 0)
|
|
|
|
|
rv = ssl_x509_v3ext_add(&ctx, crt, "keyUsage",
|
|
|
|
|
"digitalSignature,"
|
|
|
|
|
"keyEncipherment");
|
|
|
|
|
if (rv == -1)
|
|
|
|
|
goto errout;
|
|
|
|
|
|
|
|
|
|
rv = ssl_x509_v3ext_copy_by_nid(crt, origcrt,
|
|
|
|
|
NID_ext_key_usage);
|
|
|
|
|
if (rv == 0)
|
|
|
|
|
rv = ssl_x509_v3ext_add(&ctx, crt, "extendedKeyUsage",
|
|
|
|
|
"serverAuth");
|
|
|
|
|
if (rv == -1)
|
|
|
|
|
goto errout;
|
|
|
|
|
|
|
|
|
|
char *crlurlval;
|
|
|
|
|
if (crlurl) {
|
|
|
|
|
crlurlval = (char *)malloc(strlen(crlurl) + 1);
|
|
|
|
|
if (sprintf(crlurlval, "URI:%s", crlurl) < 0)
|
|
|
|
|
goto errout;
|
|
|
|
|
if (ssl_x509_v3ext_add(&ctx, crt, "crlDistributionPoints",
|
|
|
|
|
crlurlval) == -1) {
|
|
|
|
|
free(crlurlval);
|
|
|
|
|
goto errout;
|
|
|
|
|
}
|
|
|
|
|
free(crlurlval);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *cfval;
|
|
|
|
|
if (!extraname) {
|
|
|
|
|
/* no extraname provided: copy original subjectAltName ext */
|
|
|
|
|
if (ssl_x509_v3ext_copy_by_nid(crt, origcrt,
|
|
|
|
|
NID_subject_alt_name) == -1)
|
|
|
|
|
goto errout;
|
|
|
|
|
} else {
|
|
|
|
|
names = (GENERAL_NAMES *)X509_get_ext_d2i(origcrt, NID_subject_alt_name, 0, 0);
|
|
|
|
|
if (!names) {
|
|
|
|
|
/* no subjectAltName present: add new one */
|
|
|
|
|
cfval = (char *)malloc(strlen(extraname) + 1);
|
|
|
|
|
if (sprintf(cfval, "DNS:%s", extraname) < 0)
|
|
|
|
|
goto errout;
|
|
|
|
|
if (ssl_x509_v3ext_add(&ctx, crt, "subjectAltName",
|
|
|
|
|
cfval) == -1) {
|
|
|
|
|
free(cfval);
|
|
|
|
|
goto errout;
|
|
|
|
|
}
|
|
|
|
|
free(cfval);
|
|
|
|
|
} else {
|
|
|
|
|
/* add extraname to original subjectAltName
|
|
|
|
|
* and add it to the new certificate */
|
|
|
|
|
gn = GENERAL_NAME_new();
|
|
|
|
|
if (!gn)
|
|
|
|
|
goto errout2;
|
|
|
|
|
gn->type = GEN_DNS;
|
|
|
|
|
gn->d.dNSName = ASN1_IA5STRING_new();
|
|
|
|
|
if (!gn->d.dNSName)
|
|
|
|
|
goto errout3;
|
|
|
|
|
ASN1_STRING_set(gn->d.dNSName,
|
|
|
|
|
(unsigned char *)extraname,
|
|
|
|
|
strlen(extraname));
|
|
|
|
|
sk_GENERAL_NAME_push(names, gn);
|
|
|
|
|
X509_EXTENSION *ext = X509V3_EXT_i2d(
|
|
|
|
|
NID_subject_alt_name, 0, names);
|
|
|
|
|
if (!X509_add_ext(crt, ext, -1)) {
|
|
|
|
|
if (ext) {
|
|
|
|
|
X509_EXTENSION_free(ext);
|
|
|
|
|
}
|
|
|
|
|
goto errout3;
|
|
|
|
|
}
|
|
|
|
|
X509_EXTENSION_free(ext);
|
|
|
|
|
sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#ifdef DEBUG_CERTIFICATE
|
|
|
|
|
ssl_x509_v3ext_add(&ctx, crt, "nsComment", "Generated by " PKGLABEL);
|
|
|
|
|
#endif /* DEBUG_CERTIFICATE */
|
|
|
|
|
|
|
|
|
|
const EVP_MD *md;
|
|
|
|
|
switch (EVP_PKEY_type(EVP_PKEY_base_id(cakey))) {
|
|
|
|
|
#ifndef OPENSSL_NO_RSA
|
|
|
|
|
case EVP_PKEY_RSA:
|
|
|
|
|
switch (X509_get_signature_nid(origcrt)) {
|
|
|
|
|
case NID_md5WithRSAEncryption:
|
|
|
|
|
md = EVP_md5();
|
|
|
|
|
break;
|
|
|
|
|
case NID_ripemd160WithRSA:
|
|
|
|
|
md = EVP_ripemd160();
|
|
|
|
|
break;
|
|
|
|
|
case NID_sha1WithRSAEncryption:
|
|
|
|
|
md = EVP_sha1();
|
|
|
|
|
break;
|
|
|
|
|
case NID_sha224WithRSAEncryption:
|
|
|
|
|
md = EVP_sha224();
|
|
|
|
|
break;
|
|
|
|
|
case NID_sha256WithRSAEncryption:
|
|
|
|
|
md = EVP_sha256();
|
|
|
|
|
break;
|
|
|
|
|
case NID_sha384WithRSAEncryption:
|
|
|
|
|
md = EVP_sha384();
|
|
|
|
|
break;
|
|
|
|
|
case NID_sha512WithRSAEncryption:
|
|
|
|
|
md = EVP_sha512();
|
|
|
|
|
break;
|
|
|
|
|
#ifndef OPENSSL_NO_SHA0
|
|
|
|
|
case NID_shaWithRSAEncryption:
|
|
|
|
|
md = EVP_sha();
|
|
|
|
|
break;
|
|
|
|
|
#endif /* !OPENSSL_NO_SHA0 */
|
|
|
|
|
default:
|
|
|
|
|
md = EVP_sha256();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
#endif /* !OPENSSL_NO_RSA */
|
|
|
|
|
#ifndef OPENSSL_NO_DSA
|
|
|
|
|
case EVP_PKEY_DSA:
|
|
|
|
|
switch (X509_get_signature_nid(origcrt)) {
|
|
|
|
|
case NID_dsaWithSHA1:
|
|
|
|
|
case NID_dsaWithSHA1_2:
|
|
|
|
|
md = EVP_sha1();
|
|
|
|
|
break;
|
|
|
|
|
case NID_dsa_with_SHA224:
|
|
|
|
|
md = EVP_sha224();
|
|
|
|
|
break;
|
|
|
|
|
case NID_dsa_with_SHA256:
|
|
|
|
|
md = EVP_sha256();
|
|
|
|
|
break;
|
|
|
|
|
#ifndef OPENSSL_NO_SHA0
|
|
|
|
|
case NID_dsaWithSHA:
|
|
|
|
|
md = EVP_sha();
|
|
|
|
|
break;
|
|
|
|
|
#endif /* !OPENSSL_NO_SHA0 */
|
|
|
|
|
default:
|
|
|
|
|
md = EVP_sha256();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
#endif /* !OPENSSL_NO_DSA */
|
|
|
|
|
#ifndef OPENSSL_NO_ECDSA
|
|
|
|
|
case EVP_PKEY_EC:
|
|
|
|
|
switch (X509_get_signature_nid(origcrt)) {
|
|
|
|
|
case NID_ecdsa_with_SHA1:
|
|
|
|
|
md = EVP_sha1();
|
|
|
|
|
break;
|
|
|
|
|
case NID_ecdsa_with_SHA224:
|
|
|
|
|
md = EVP_sha224();
|
|
|
|
|
break;
|
|
|
|
|
case NID_ecdsa_with_SHA256:
|
|
|
|
|
md = EVP_sha256();
|
|
|
|
|
break;
|
|
|
|
|
case NID_ecdsa_with_SHA384:
|
|
|
|
|
md = EVP_sha384();
|
|
|
|
|
break;
|
|
|
|
|
case NID_ecdsa_with_SHA512:
|
|
|
|
|
md = EVP_sha512();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
md = EVP_sha256();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
#endif /* !OPENSSL_NO_ECDSA */
|
|
|
|
|
default:
|
|
|
|
|
goto errout;
|
|
|
|
|
}
|
|
|
|
|
if (!X509_sign(crt, cakey, md))
|
|
|
|
|
goto errout;
|
|
|
|
|
|
|
|
|
|
return crt;
|
|
|
|
|
|
|
|
|
|
errout3:
|
|
|
|
|
GENERAL_NAME_free(gn);
|
|
|
|
|
errout2:
|
|
|
|
|
sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
|
|
|
|
|
errout:
|
|
|
|
|
X509_free(crt);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-28 11:05:53 +08:00
|
|
|
void x509_get_msg_from_ca(X509 *x509, char *ca_s)
|
2018-06-19 11:32:16 +08:00
|
|
|
{
|
2018-06-28 11:05:53 +08:00
|
|
|
BIO *bp = NULL;
|
2018-06-19 11:32:16 +08:00
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
|
|
if ( (bp=BIO_new(BIO_s_mem())) == NULL){
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "unable to create BIO for output\n");
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
2018-06-28 11:05:53 +08:00
|
|
|
PEM_write_bio_X509(bp, x509);
|
|
|
|
|
len = BIO_read(bp, ca_s, SG_DATA_SIZE * 2);
|
2018-06-19 11:32:16 +08:00
|
|
|
if(len <= 0) {
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Error reading signature file");
|
|
|
|
|
goto err;
|
|
|
|
|
}
|
2018-06-28 11:05:53 +08:00
|
|
|
ca_s[len] ='\0';
|
2018-06-19 11:32:16 +08:00
|
|
|
err:
|
|
|
|
|
BIO_free(bp);
|
|
|
|
|
finish:
|
2018-06-28 11:05:53 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-26 17:18:00 +08:00
|
|
|
void x509_get_private_key(EVP_PKEY *pkey, char *pubkey)
|
2018-06-28 11:05:53 +08:00
|
|
|
{
|
|
|
|
|
BIO *bp = NULL;
|
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
|
|
if ( (bp=BIO_new(BIO_s_mem())) == NULL){
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "unable to create BIO for output\n");
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-26 17:18:00 +08:00
|
|
|
PEM_write_bio_PrivateKey(bp, pkey, NULL, NULL, 0, NULL, NULL);
|
2018-06-28 11:05:53 +08:00
|
|
|
len = BIO_read(bp, pubkey, SG_DATA_SIZE);
|
|
|
|
|
if(len <= 0) {
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Error reading signature file");
|
2018-07-26 17:18:00 +08:00
|
|
|
goto free_err;
|
2018-06-28 11:05:53 +08:00
|
|
|
}
|
|
|
|
|
pubkey[len] = '\0';
|
2018-07-26 17:18:00 +08:00
|
|
|
|
2018-06-28 11:05:53 +08:00
|
|
|
free_err:
|
|
|
|
|
BIO_free(bp);
|
|
|
|
|
finish:
|
|
|
|
|
return;
|
2018-06-19 11:32:16 +08:00
|
|
|
}
|
|
|
|
|
|
2018-06-27 10:58:23 +08:00
|
|
|
/*
|
|
|
|
|
* Add extension using V3 code: we can set the config file as NULL because we
|
|
|
|
|
* wont reference any other sections.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-07-26 17:18:00 +08:00
|
|
|
int add_ext(X509 *cacrt, X509 *cert, int nid, char *value)
|
2018-06-27 10:58:23 +08:00
|
|
|
{
|
|
|
|
|
X509_EXTENSION *ex;
|
|
|
|
|
X509V3_CTX ctx;
|
|
|
|
|
/* This sets the 'context' of the extensions. */
|
|
|
|
|
/* No configuration database */
|
|
|
|
|
X509V3_set_ctx_nodb(&ctx);
|
|
|
|
|
/*
|
|
|
|
|
* Issuer and subject certs: both the target since it is self signed, no
|
|
|
|
|
* request and no CRL
|
|
|
|
|
*/
|
2018-07-26 17:18:00 +08:00
|
|
|
X509V3_set_ctx(&ctx, cacrt, cert, NULL, NULL, 0);
|
2018-06-27 10:58:23 +08:00
|
|
|
ex = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
|
|
|
|
|
if (!ex)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
X509_add_ext(cert, ex, -1);
|
|
|
|
|
X509_EXTENSION_free(ex);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
#if 0
|
|
|
|
|
static int fs_internal_operate(int id, int id2, int column_id, int column_id2, long long diffTime)
|
2018-07-09 14:32:41 +08:00
|
|
|
{
|
2018-07-18 10:47:45 +08:00
|
|
|
int ret = -1;
|
|
|
|
|
screen_stat_handle_t handle = SGstats.handle;
|
2018-07-09 14:32:41 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
FS_internal_operate(handle, id, column_id, FS_OP_ADD, 1);
|
2018-07-09 14:32:41 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
if (id2 < 0)
|
|
|
|
|
goto finish;
|
2018-07-09 14:32:41 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
FS_internal_operate(handle, id2, 0, FS_OP_ADD, 1);
|
2018-07-09 14:32:41 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
if (column_id2 < 0)
|
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
|
|
ret = FS_internal_operate(handle, id, column_id2, FS_OP_SET, diffTime);
|
|
|
|
|
finish:
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2018-07-09 14:32:41 +08:00
|
|
|
|
2018-07-26 17:18:00 +08:00
|
|
|
static
|
2018-07-09 14:32:41 +08:00
|
|
|
int redis_rsync_init(struct event_base *base, struct redisAsyncContext **cl_ctx)
|
2018-06-19 11:32:16 +08:00
|
|
|
{
|
|
|
|
|
int xret = -1;
|
|
|
|
|
struct config_bucket_t *redis = cert_default_config();
|
|
|
|
|
|
2018-06-29 17:18:33 +08:00
|
|
|
*cl_ctx = redisAsyncConnect(redis->r_ip, redis->r_port);
|
2018-07-05 14:05:28 +08:00
|
|
|
if((*cl_ctx)->err ) {
|
2018-06-29 17:18:33 +08:00
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Redis Connect error : %s", (*cl_ctx)->errstr);
|
2018-06-19 11:32:16 +08:00
|
|
|
goto finish;
|
|
|
|
|
}
|
2018-06-29 17:18:33 +08:00
|
|
|
redisLibeventAttach((*cl_ctx), base);
|
|
|
|
|
redisAsyncSetConnectCallback((*cl_ctx), connectCallback);
|
|
|
|
|
redisAsyncSetDisconnectCallback((*cl_ctx), disconnectCallback);
|
2018-07-18 10:47:45 +08:00
|
|
|
|
2018-06-19 11:32:16 +08:00
|
|
|
xret = 0;
|
|
|
|
|
|
|
|
|
|
finish:
|
|
|
|
|
return xret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Callback used for the /dump URI, and for every non-GET request:
|
|
|
|
|
* dumps all information to stdout and gives back a trivial 200 ok */
|
|
|
|
|
static int
|
2018-06-28 11:05:53 +08:00
|
|
|
evhttp_socket_send(struct evhttp_request *req, char *sendbuf)
|
2018-06-19 11:32:16 +08:00
|
|
|
{
|
|
|
|
|
struct evbuffer *evb = NULL;
|
|
|
|
|
|
|
|
|
|
/* This holds the content we're sending. */
|
|
|
|
|
evb = evbuffer_new();
|
|
|
|
|
|
2018-06-29 17:18:33 +08:00
|
|
|
if (sendbuf[0] == '\0' && req == NULL){
|
2018-06-19 11:32:16 +08:00
|
|
|
goto err;
|
|
|
|
|
}
|
|
|
|
|
evhttp_add_header(evhttp_request_get_output_headers(req),
|
|
|
|
|
"Content-Type", "test");
|
2018-06-28 11:05:53 +08:00
|
|
|
evbuffer_add_printf(evb, "%s", sendbuf);
|
2018-07-18 10:47:45 +08:00
|
|
|
evhttp_send_reply(req, HTTP_OK, "OK", evb);
|
2018-06-19 11:32:16 +08:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
|
|
err:
|
2018-07-18 10:47:45 +08:00
|
|
|
evhttp_send_error(req, HTTP_NOTFOUND, "Document was not found");
|
2018-06-19 11:32:16 +08:00
|
|
|
done:
|
|
|
|
|
evbuffer_free(evb);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-27 10:58:23 +08:00
|
|
|
static void
|
2018-07-18 10:47:45 +08:00
|
|
|
redis_reget_callback(redisAsyncContext __attribute__((__unused__))*cl_ctx,
|
|
|
|
|
void *r, void *privdata)
|
2018-06-27 10:58:23 +08:00
|
|
|
{
|
2018-07-18 10:47:45 +08:00
|
|
|
redisReply *reply = (redisReply*)r;
|
2018-06-27 10:58:23 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
struct request_t *request = (struct request_t *)privdata;
|
|
|
|
|
|
|
|
|
|
struct evhttp_request *evh_req = request->evh_req;
|
|
|
|
|
|
|
|
|
|
evhttp_socket_send(evh_req, reply->str);
|
|
|
|
|
|
|
|
|
|
kfree(request->sendbuf);
|
|
|
|
|
kfree(request);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-26 17:18:00 +08:00
|
|
|
static void __attribute__((__unused__))
|
2018-07-18 10:47:45 +08:00
|
|
|
redis_set_callback(redisAsyncContext *cl_ctx, void *r,
|
|
|
|
|
void *privdata)
|
|
|
|
|
{
|
|
|
|
|
struct request_t *request = (struct request_t *)privdata;
|
|
|
|
|
struct evhttp_request *evh_req = request->evh_req;
|
|
|
|
|
|
|
|
|
|
redisReply *reply = (redisReply*)r;
|
|
|
|
|
|
|
|
|
|
if(reply->type == REDIS_REPLY_ERROR){
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Set redis data(key = %s) returns failed", request->host);
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
Synchronous reader data is +OK
|
|
|
|
|
Asynchronous redis data is $-1\r\n+OK
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
libevent_thread *thread = threads + request->t_id;
|
|
|
|
|
redisReader *reader = cl_ctx->c.reader;
|
|
|
|
|
|
|
|
|
|
switch (reader->buf[5]) {
|
|
|
|
|
case '+' :
|
|
|
|
|
mesa_runtime_log(RLOG_LV_INFO, MODULE_NAME, "Writing data(key = %s) to redis successfully", request->host);
|
|
|
|
|
FS_internal_operate(SGstats.handle, thread->column_ids, SGstats.line_ids[2], FS_OP_ADD, 1);
|
|
|
|
|
|
|
|
|
|
evhttp_socket_send(evh_req, request->sendbuf);
|
|
|
|
|
goto free;
|
|
|
|
|
case '\0':
|
|
|
|
|
case '$' :
|
|
|
|
|
mesa_runtime_log(RLOG_LV_INFO, MODULE_NAME, "Writing data(key = %s) to redis failed", request->host);
|
|
|
|
|
FS_internal_operate(SGstats.handle, thread->column_ids, SGstats.line_ids[1], FS_OP_ADD, 1);
|
|
|
|
|
|
|
|
|
|
redisAsyncCommand(cl_ctx, redis_reget_callback, request, "GET %s", request->host);
|
|
|
|
|
goto finish;
|
|
|
|
|
default :
|
|
|
|
|
mesa_runtime_log(RLOG_LV_INFO, MODULE_NAME, "Read redis data(key = %s) return code failed", request->host);
|
|
|
|
|
evhttp_send_error(request->evh_req, HTTP_NOTFOUND, 0);
|
|
|
|
|
goto free;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free:
|
|
|
|
|
kfree(request->sendbuf);
|
|
|
|
|
kfree(request);
|
|
|
|
|
finish:
|
|
|
|
|
return;
|
2018-06-27 10:58:23 +08:00
|
|
|
}
|
|
|
|
|
|
2018-07-26 17:18:00 +08:00
|
|
|
static
|
|
|
|
|
int create_client_key(EVP_PKEY** pkey, char *pubkey, int bits)
|
|
|
|
|
{
|
|
|
|
|
RSA *rsa = NULL;
|
|
|
|
|
EVP_PKEY *pk = NULL;
|
|
|
|
|
|
|
|
|
|
if((pk = EVP_PKEY_new()) == NULL){
|
|
|
|
|
mesa_runtime_log(RLOG_LV_INFO, MODULE_NAME, "create_client_key, gen new key failed!");
|
|
|
|
|
goto err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rsa = RSA_generate_key(bits, RSA_F4, NULL, NULL);
|
|
|
|
|
if(!EVP_PKEY_assign_RSA(pk, rsa)){
|
|
|
|
|
mesa_runtime_log(RLOG_LV_INFO, MODULE_NAME, "create_client_key, assign key failed!");
|
|
|
|
|
EVP_PKEY_free(pk);
|
|
|
|
|
goto err;
|
|
|
|
|
}
|
|
|
|
|
x509_get_private_key(pk, pubkey);
|
|
|
|
|
rsa = NULL;
|
|
|
|
|
|
|
|
|
|
*pkey = pk;
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
err:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int add_cert_ctx(X509_NAME* name, char* ctx[], int num)
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
int max = 0;
|
|
|
|
|
|
|
|
|
|
int item[] = {NID_commonName, NID_countryName,
|
|
|
|
|
NID_stateOrProvinceName, NID_localityName,
|
|
|
|
|
NID_organizationName, NID_organizationalUnitName,
|
|
|
|
|
NID_pkcs9_emailAddress};
|
|
|
|
|
|
|
|
|
|
max = sizeof(item)/sizeof(item[0]);
|
|
|
|
|
max = max > num ? num : max;
|
|
|
|
|
|
|
|
|
|
for(i = 0; i< max; ++i){
|
|
|
|
|
if(!X509_NAME_add_entry_by_NID(name, item[i], MBSTRING_UTF8, (unsigned char *)ctx[i], -1, -1, 0)){
|
|
|
|
|
mesa_runtime_log(RLOG_LV_INFO, MODULE_NAME, "add_cert_ctx, add entry:%d to %s failed!", item[i], ctx[i]);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int rand_serial(BIGNUM *b, ASN1_INTEGER *ai)
|
|
|
|
|
{
|
|
|
|
|
#define SERIAL_RAND_BITS 124
|
|
|
|
|
BIGNUM *btmp;
|
|
|
|
|
int ret = 0;
|
|
|
|
|
if (b)
|
|
|
|
|
btmp = b;
|
|
|
|
|
else
|
|
|
|
|
btmp = BN_new();
|
|
|
|
|
if (!btmp)
|
|
|
|
|
return 0;
|
|
|
|
|
if (!BN_pseudo_rand(btmp, SERIAL_RAND_BITS, 0, 0))
|
|
|
|
|
goto error;
|
|
|
|
|
if (ai && !BN_to_ASN1_INTEGER(btmp, ai))
|
|
|
|
|
goto error;
|
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
if (!b)
|
|
|
|
|
BN_free(btmp);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
X509 *x509_modify_by_cert(X509 *cacrt, EVP_PKEY *cakey, const char* host,
|
|
|
|
|
char *pubkey, const int days)
|
|
|
|
|
{
|
|
|
|
|
X509* x = NULL;
|
|
|
|
|
EVP_PKEY* pk = NULL;
|
|
|
|
|
|
|
|
|
|
char* ctx[] = {(char*)host, "CN", "mystate",
|
|
|
|
|
"mycity", "myorganization", "mygroup",
|
|
|
|
|
"sample@sample.com"};
|
|
|
|
|
|
|
|
|
|
if(!create_client_key(&pk, pubkey, 1024)){
|
|
|
|
|
goto err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if((x = X509_new()) == NULL){
|
|
|
|
|
goto err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!X509_set_version(x, 0x02)){
|
|
|
|
|
goto err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!X509_set_version(x, 0x02) ||
|
|
|
|
|
!X509_set_issuer_name(x, X509_get_subject_name(cacrt)) ||
|
|
|
|
|
!rand_serial(NULL, X509_get_serialNumber(x)) ||
|
|
|
|
|
!X509_gmtime_adj(X509_get_notBefore(x), 0L) ||
|
|
|
|
|
!X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL) ||
|
|
|
|
|
!X509_set_pubkey(x, pk) ||
|
|
|
|
|
!add_cert_ctx(X509_get_subject_name(x), ctx, 7))
|
|
|
|
|
goto err;
|
|
|
|
|
#if 1
|
|
|
|
|
|
|
|
|
|
/* Add various extensions: standard extensions */
|
|
|
|
|
add_ext(cacrt, x, NID_basic_constraints, "critical,CA:FALSE");
|
|
|
|
|
add_ext(cacrt, x, NID_subject_key_identifier, "hash");
|
|
|
|
|
add_ext(cacrt, x, NID_key_usage, "Digital Signature, Key Encipherment, Data Encipherment");
|
|
|
|
|
|
|
|
|
|
/**/
|
|
|
|
|
|
|
|
|
|
add_ext(cacrt, x, NID_authority_key_identifier, "keyid:always");
|
|
|
|
|
|
|
|
|
|
add_ext(cacrt, x, NID_ext_key_usage, "serverAuth,clientAuth");
|
|
|
|
|
/*NID_certificate_policies*/
|
|
|
|
|
/*
|
|
|
|
|
char dns[128] = {0}, domain[16] = {0};
|
|
|
|
|
sscanf(host, "%*[^.].%[^.]", domain);
|
|
|
|
|
snprintf(dns, 127, "DNS:%s.com, DNS:*.%s.com, DNS:www.%s.cn", domain, domain, domain);
|
|
|
|
|
add_ext(cacrt, x, NID_subject_alt_name, dns);
|
|
|
|
|
*/
|
|
|
|
|
#endif
|
|
|
|
|
if(!X509_sign(x, cakey, EVP_sha256())){
|
|
|
|
|
goto err;
|
|
|
|
|
}
|
|
|
|
|
return x;
|
|
|
|
|
|
|
|
|
|
err:
|
|
|
|
|
if(x)
|
|
|
|
|
X509_free(x);
|
|
|
|
|
if(pk)
|
|
|
|
|
EVP_PKEY_free(pk);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-05 14:05:28 +08:00
|
|
|
int x509_online_append(char *host, EVP_PKEY *key, X509 *root, char *ca_s, char *pubkey)
|
2018-06-27 10:58:23 +08:00
|
|
|
{
|
|
|
|
|
struct config_bucket_t *rte = cert_default_config();
|
|
|
|
|
|
2018-07-26 17:18:00 +08:00
|
|
|
X509* x509 = x509_modify_by_cert(root, key, host, pubkey, rte->days);
|
2018-06-27 10:58:23 +08:00
|
|
|
if (!x509){
|
2018-07-26 17:18:00 +08:00
|
|
|
goto finish;
|
2018-06-27 10:58:23 +08:00
|
|
|
}
|
2018-06-28 11:05:53 +08:00
|
|
|
x509_get_msg_from_ca(x509, ca_s);
|
2018-06-27 10:58:23 +08:00
|
|
|
|
|
|
|
|
X509_free(x509);
|
|
|
|
|
finish:
|
2018-07-26 17:18:00 +08:00
|
|
|
return 0;
|
2018-06-27 10:58:23 +08:00
|
|
|
}
|
|
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
static char readBytes(char *str)
|
2018-07-09 14:32:41 +08:00
|
|
|
{
|
2018-07-18 10:47:45 +08:00
|
|
|
char c;
|
2018-07-09 14:32:41 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
if (str && STRCMP(str, "OK") == 0)
|
|
|
|
|
c = '+';
|
|
|
|
|
if (!str)
|
|
|
|
|
c= '$';
|
2018-07-09 14:32:41 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
return c;
|
|
|
|
|
}
|
2018-07-09 14:32:41 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
static int
|
|
|
|
|
rediSyncCommand(redisAsyncContext *cl_ctx, struct request_t *request, char *sendbuf)
|
|
|
|
|
{
|
|
|
|
|
int xret = -1;
|
|
|
|
|
redisReply *reply;
|
2018-07-09 14:32:41 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
libevent_thread *thread = threads + request->t_id;
|
|
|
|
|
struct evhttp_request *evh_req = request->evh_req;
|
|
|
|
|
|
|
|
|
|
reply = (redisReply *)redisCommand(thread->sync, "set %s %s ex %d nx", request->host, sendbuf,
|
|
|
|
|
sizeof_seconds(cert_default_config()->days));
|
|
|
|
|
if (NULL == reply)
|
|
|
|
|
goto free;
|
|
|
|
|
|
|
|
|
|
switch (readBytes(reply->str)) {
|
|
|
|
|
case '+' :
|
|
|
|
|
mesa_runtime_log(RLOG_LV_INFO, MODULE_NAME, "Writing data(key = %s) to redis successfully", request->host);
|
|
|
|
|
FS_internal_operate(SGstats.handle, thread->column_ids, SGstats.line_ids[2], FS_OP_ADD, 1);
|
|
|
|
|
|
|
|
|
|
evhttp_socket_send(evh_req, request->sendbuf);
|
|
|
|
|
goto free;
|
|
|
|
|
case '$' :
|
|
|
|
|
mesa_runtime_log(RLOG_LV_INFO, MODULE_NAME, "Writing data(key = %s) to redis failed", request->host);
|
|
|
|
|
FS_internal_operate(SGstats.handle, thread->column_ids, SGstats.line_ids[1], FS_OP_ADD, 1);
|
|
|
|
|
|
|
|
|
|
redisAsyncCommand(cl_ctx, redis_reget_callback, request, "GET %s", request->host);
|
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
goto finish;
|
|
|
|
|
default :
|
|
|
|
|
mesa_runtime_log(RLOG_LV_INFO, MODULE_NAME, "Read redis data(key = %s) return code failed", request->host);
|
|
|
|
|
evhttp_send_error(request->evh_req, HTTP_NOTFOUND, 0);
|
|
|
|
|
goto free;
|
|
|
|
|
}
|
|
|
|
|
xret = 0;
|
|
|
|
|
|
|
|
|
|
free:
|
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
kfree(request->sendbuf);
|
|
|
|
|
kfree(request);
|
2018-07-09 14:32:41 +08:00
|
|
|
|
|
|
|
|
finish:
|
2018-07-18 10:47:45 +08:00
|
|
|
return xret;
|
2018-07-09 14:32:41 +08:00
|
|
|
}
|
|
|
|
|
|
2018-07-05 14:05:28 +08:00
|
|
|
static int
|
2018-07-18 10:47:45 +08:00
|
|
|
redis_encode_sendbuf(struct request_t *request, redisAsyncContext *c)
|
2018-06-19 11:32:16 +08:00
|
|
|
{
|
|
|
|
|
int xret = -1;
|
2018-07-09 14:32:41 +08:00
|
|
|
uint64_t startTime = 0, endTime = 0;
|
2018-07-18 10:47:45 +08:00
|
|
|
libevent_thread *thread = threads + request->t_id;
|
|
|
|
|
char cert[SG_DATA_SIZE] = {0}, pubkey[SG_DATA_SIZE] = {0};
|
2018-06-29 17:18:33 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
char *sendbuf = (char *)kmalloc(SG_DATA_SIZE * 2, MPF_CLR, -1);
|
2018-06-19 11:32:16 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
startTime = rt_time_ns();
|
2018-07-12 10:56:13 +08:00
|
|
|
|
2018-07-05 14:05:28 +08:00
|
|
|
x509_online_append(request->host, thread->key, thread->root, cert, pubkey);
|
2018-06-29 17:18:33 +08:00
|
|
|
if (cert[0] == '\0' && pubkey[0] == '\0'){
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Failed to issue certificate");
|
2018-07-05 14:05:28 +08:00
|
|
|
evhttp_send_error(request->evh_req, HTTP_NOTFOUND, 0);
|
2018-06-29 17:18:33 +08:00
|
|
|
goto finish;
|
|
|
|
|
}
|
2018-07-05 14:05:28 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
endTime = rt_time_ns();
|
2018-07-09 14:32:41 +08:00
|
|
|
thread->diffTime += (endTime - startTime);
|
2018-07-12 10:56:13 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
mesa_runtime_log(RLOG_LV_DEBUG, MODULE_NAME, "%lu - %lu = %lu\n", startTime, endTime, endTime - startTime);
|
|
|
|
|
|
|
|
|
|
FS_internal_operate(SGstats.handle, thread->column_ids, SGstats.line_ids[3], FS_OP_SET, thread->diffTime);
|
2018-07-05 14:05:28 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
FS_internal_operate(SGstats.handle, thread->field_ids, 0, FS_OP_ADD, 1);
|
2018-06-28 11:05:53 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
snprintf(sendbuf, SG_DATA_SIZE * 2 - 1, "%s%s", pubkey, cert);
|
|
|
|
|
|
|
|
|
|
request->sendbuf = sendbuf;
|
|
|
|
|
#ifdef RD_SYNC
|
|
|
|
|
xret = rediSyncCommand(c, request, sendbuf);
|
|
|
|
|
#else
|
|
|
|
|
xret = redisAsyncCommand(c, redis_set_callback, request, "set %s %s ex %d nx",
|
|
|
|
|
request->host, sendbuf, sizeof_seconds(cert_default_config()->days));
|
|
|
|
|
#endif
|
2018-06-29 17:18:33 +08:00
|
|
|
if (xret < 0){
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Failed to set information to redis server");
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
2018-07-05 14:05:28 +08:00
|
|
|
xret = 0;
|
2018-07-18 10:47:45 +08:00
|
|
|
|
2018-06-29 17:18:33 +08:00
|
|
|
finish:
|
|
|
|
|
return xret;
|
|
|
|
|
}
|
2018-06-28 11:05:53 +08:00
|
|
|
|
2018-07-05 14:05:28 +08:00
|
|
|
static int
|
2018-07-18 10:47:45 +08:00
|
|
|
redis_decode_sendbuf(struct request_t *request, redisReply *reply)
|
2018-07-05 14:05:28 +08:00
|
|
|
{
|
2018-07-18 10:47:45 +08:00
|
|
|
int xret = -1;
|
2018-07-05 14:05:28 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
char sendbuf[SG_DATA_SIZE * 2] = {0};
|
|
|
|
|
libevent_thread *thread = threads + request->t_id;
|
2018-07-09 14:32:41 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
if (!reply && !reply->str){
|
|
|
|
|
evhttp_send_error(request->evh_req, HTTP_NOTFOUND, 0);
|
|
|
|
|
goto finish;
|
2018-07-05 14:05:28 +08:00
|
|
|
}
|
2018-07-09 14:32:41 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
FS_internal_operate(SGstats.handle, thread->column_ids, SGstats.line_ids[1], FS_OP_ADD, 1);
|
|
|
|
|
|
|
|
|
|
FS_internal_operate(SGstats.handle, thread->field_ids, 0, FS_OP_ADD, 1);
|
|
|
|
|
|
|
|
|
|
snprintf(sendbuf, SG_DATA_SIZE * 2, "%s", reply->str);
|
|
|
|
|
|
|
|
|
|
evhttp_socket_send(request->evh_req, sendbuf);
|
|
|
|
|
|
|
|
|
|
finish:
|
2018-07-09 14:32:41 +08:00
|
|
|
kfree(request);
|
2018-07-05 14:05:28 +08:00
|
|
|
return xret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
void redis_get_callback(redisAsyncContext *c, void *r, void *privdata)
|
2018-06-29 17:18:33 +08:00
|
|
|
{
|
2018-07-26 17:18:00 +08:00
|
|
|
int __attribute__((__unused__))xret = -1;
|
2018-06-20 16:24:49 +08:00
|
|
|
|
2018-07-12 10:56:13 +08:00
|
|
|
redisReply *reply = (redisReply*)r;
|
|
|
|
|
struct request_t *request = (struct request_t *)privdata;
|
|
|
|
|
|
2018-06-29 17:18:33 +08:00
|
|
|
switch(reply->type){
|
|
|
|
|
case REDIS_REPLY_STRING:
|
2018-07-18 10:47:45 +08:00
|
|
|
mesa_runtime_log(RLOG_LV_DEBUG, MODULE_NAME, "Sends the certificate information to the requestor");
|
2018-07-05 14:05:28 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
xret = redis_decode_sendbuf(request, reply);
|
2018-06-29 17:18:33 +08:00
|
|
|
break;
|
2018-07-05 14:05:28 +08:00
|
|
|
|
2018-06-29 17:18:33 +08:00
|
|
|
case REDIS_REPLY_NIL:
|
2018-07-18 10:47:45 +08:00
|
|
|
mesa_runtime_log(RLOG_LV_DEBUG, MODULE_NAME, "Generating certificate information");
|
2018-07-05 14:05:28 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
xret = redis_encode_sendbuf(request, c);
|
2018-06-29 17:18:33 +08:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2018-06-19 11:32:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-29 17:18:33 +08:00
|
|
|
int x509_privatekey_init(EVP_PKEY **key, X509 **root)
|
2018-06-19 11:32:16 +08:00
|
|
|
{
|
2018-07-26 17:18:00 +08:00
|
|
|
int xret = -1;
|
2018-06-19 11:32:16 +08:00
|
|
|
FILE *fp; RSA *rsa = NULL;
|
|
|
|
|
char key_path[128] = {0}, cert_path[128] = {0};
|
|
|
|
|
struct config_bucket_t *rte = cert_default_config();
|
|
|
|
|
|
|
|
|
|
snprintf(key_path, sizeof(key_path), "%s/%s", rte->ca_path, DEFAULT_PRIVATEKEY_NAME);
|
|
|
|
|
snprintf(cert_path, sizeof(cert_path), "%s/%s", rte->ca_path, DEFAULT_CA_CERTIFICATE);
|
|
|
|
|
|
2018-06-29 17:18:33 +08:00
|
|
|
*key = EVP_PKEY_new();
|
|
|
|
|
if (NULL == *key){
|
2018-06-19 11:32:16 +08:00
|
|
|
goto finish;
|
|
|
|
|
}
|
2018-06-27 10:58:23 +08:00
|
|
|
|
2018-06-19 11:32:16 +08:00
|
|
|
rsa = RSA_new();
|
|
|
|
|
if (NULL == rsa){
|
|
|
|
|
goto pkey_free;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fp = fopen(key_path, "r");
|
|
|
|
|
if (NULL == fp){
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Failed to open file(%s)", key_path);
|
2018-06-27 10:58:23 +08:00
|
|
|
RSA_free(rsa);
|
|
|
|
|
goto pkey_free;
|
2018-06-19 11:32:16 +08:00
|
|
|
}
|
2018-06-29 17:18:33 +08:00
|
|
|
if ( !PEM_read_RSAPrivateKey(fp, &rsa, NULL, NULL) || !EVP_PKEY_assign_RSA(*key,rsa))
|
2018-06-19 11:32:16 +08:00
|
|
|
{
|
2018-06-20 16:24:49 +08:00
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Private key read failed");
|
2018-06-27 10:58:23 +08:00
|
|
|
goto pkey_free;
|
2018-06-19 11:32:16 +08:00
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
2018-07-26 17:18:00 +08:00
|
|
|
BIO *in;
|
|
|
|
|
in = BIO_new_file(cert_path, "r");
|
|
|
|
|
if (!in){
|
2018-06-19 11:32:16 +08:00
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Failed to open file(%s)", cert_path);
|
2018-06-27 10:58:23 +08:00
|
|
|
goto pkey_free;
|
2018-06-19 11:32:16 +08:00
|
|
|
}
|
|
|
|
|
|
2018-07-26 17:18:00 +08:00
|
|
|
if ((*root = PEM_read_bio_X509(in, NULL, 0, NULL)) == NULL )
|
2018-06-19 11:32:16 +08:00
|
|
|
{
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Application for x509 failed");
|
2018-06-27 10:58:23 +08:00
|
|
|
goto pkey_free;
|
2018-06-19 11:32:16 +08:00
|
|
|
}
|
2018-07-26 17:18:00 +08:00
|
|
|
BIO_free(in);
|
|
|
|
|
|
2018-06-19 11:32:16 +08:00
|
|
|
xret = 0;
|
2018-06-20 16:24:49 +08:00
|
|
|
goto finish;
|
2018-06-19 11:32:16 +08:00
|
|
|
|
|
|
|
|
pkey_free:
|
2018-06-29 17:18:33 +08:00
|
|
|
EVP_PKEY_free(*key);
|
2018-06-19 11:32:16 +08:00
|
|
|
finish:
|
|
|
|
|
return xret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-28 17:49:13 +08:00
|
|
|
static int
|
2018-07-26 17:18:00 +08:00
|
|
|
ev_decode_uri(const char *uri, char *host,
|
2018-06-29 17:18:33 +08:00
|
|
|
int *flag, int *valid)
|
2018-06-19 11:32:16 +08:00
|
|
|
{
|
2018-07-26 17:18:00 +08:00
|
|
|
const char *fg = NULL, *vl = NULL, *hst = NULL;
|
2018-06-28 17:49:13 +08:00
|
|
|
char *decoded_uri = NULL;
|
|
|
|
|
struct evkeyvalq params;
|
2018-06-19 11:32:16 +08:00
|
|
|
|
2018-06-28 17:49:13 +08:00
|
|
|
decoded_uri = evhttp_decode_uri(uri);
|
|
|
|
|
if (!decoded_uri){
|
2018-06-19 11:32:16 +08:00
|
|
|
goto finish;
|
|
|
|
|
}
|
2018-07-18 10:47:45 +08:00
|
|
|
|
2018-06-28 17:49:13 +08:00
|
|
|
evhttp_parse_query(decoded_uri, ¶ms);
|
2018-07-18 10:47:45 +08:00
|
|
|
|
2018-07-26 17:18:00 +08:00
|
|
|
hst = evhttp_find_header(¶ms, "host");
|
|
|
|
|
if (hst[0] != '\0')
|
|
|
|
|
memcpy(host, hst, strlen(hst));
|
2018-06-19 11:32:16 +08:00
|
|
|
|
2018-06-28 17:49:13 +08:00
|
|
|
fg = evhttp_find_header(¶ms, "flag");
|
|
|
|
|
if (fg)
|
|
|
|
|
*flag = atoi(fg);
|
|
|
|
|
|
|
|
|
|
vl = evhttp_find_header(¶ms, "valid");
|
|
|
|
|
if (vl)
|
|
|
|
|
*valid = atoi(vl);
|
|
|
|
|
|
2018-07-02 16:39:09 +08:00
|
|
|
evhttp_clear_headers(¶ms);
|
2018-06-28 17:49:13 +08:00
|
|
|
free(decoded_uri);
|
2018-06-19 11:32:16 +08:00
|
|
|
|
|
|
|
|
finish:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-05 14:05:28 +08:00
|
|
|
static void
|
|
|
|
|
evhttp_socket_close_cb(struct evhttp_connection *evcon,
|
|
|
|
|
void __attribute__((__unused__))*arg)
|
|
|
|
|
{
|
2018-07-18 10:47:45 +08:00
|
|
|
mesa_runtime_log(RLOG_LV_DEBUG, MODULE_NAME, "Evhttp connection is broken");
|
2018-07-05 14:05:28 +08:00
|
|
|
if (NULL == evcon){
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
finish:
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 11:32:16 +08:00
|
|
|
void
|
|
|
|
|
pthread_work_proc(struct evhttp_request *evh_req, void *arg)
|
|
|
|
|
{
|
2018-07-12 10:56:13 +08:00
|
|
|
int xret = -1;
|
|
|
|
|
const char *cmdtype = NULL;
|
|
|
|
|
struct request_t *request = NULL;
|
|
|
|
|
struct evhttp_uri *decoded = NULL;
|
|
|
|
|
libevent_thread *t = (libevent_thread *)arg;
|
2018-06-29 17:18:33 +08:00
|
|
|
|
2018-07-05 14:05:28 +08:00
|
|
|
/* we want to know if this connection closes on us */
|
|
|
|
|
evhttp_connection_set_closecb(evhttp_request_get_connection(evh_req), evhttp_socket_close_cb, NULL);
|
|
|
|
|
|
2018-06-19 11:32:16 +08:00
|
|
|
const char *uri = evhttp_request_get_uri(evh_req);
|
2018-06-29 17:18:33 +08:00
|
|
|
/* Decode the URI */
|
|
|
|
|
decoded = evhttp_uri_parse(uri);
|
|
|
|
|
if (!decoded) {
|
2018-07-18 10:47:45 +08:00
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "It's not a good URI. Sending BADREQUEST\n");
|
2018-06-29 17:18:33 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
2018-07-05 14:05:28 +08:00
|
|
|
|
2018-06-29 17:18:33 +08:00
|
|
|
request = (struct request_t *) kmalloc (sizeof(struct request_t), MPF_CLR, -1);
|
|
|
|
|
if (request != NULL){
|
2018-07-18 10:47:45 +08:00
|
|
|
request->t_id = t->id;
|
2018-07-12 10:56:13 +08:00
|
|
|
request->evh_req = evh_req;
|
2018-06-29 17:18:33 +08:00
|
|
|
}
|
2018-06-19 11:32:16 +08:00
|
|
|
|
|
|
|
|
switch (evhttp_request_get_command(evh_req)) {
|
2018-07-12 10:56:13 +08:00
|
|
|
case EVHTTP_REQ_GET: cmdtype = "GET"; break;
|
|
|
|
|
default: cmdtype = "unknown"; break;
|
2018-06-19 11:32:16 +08:00
|
|
|
}
|
2018-07-12 10:56:13 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
FS_internal_operate(SGstats.handle, t->column_ids, SGstats.line_ids[0], FS_OP_ADD, 1);
|
2018-06-19 11:32:16 +08:00
|
|
|
|
2018-07-26 17:18:00 +08:00
|
|
|
ev_decode_uri(uri, request->host, &request->flag, &request->valid);
|
2018-07-18 10:47:45 +08:00
|
|
|
mesa_runtime_log(RLOG_LV_DEBUG, MODULE_NAME, "[Thread %d]Received a %s request for %s, host:%s, flag:%d, valid:%d\nHeaders:",
|
|
|
|
|
request->t_id, cmdtype, uri, request->host,
|
2018-07-12 10:56:13 +08:00
|
|
|
request->flag, request->valid);
|
2018-06-19 11:32:16 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
if (request->host[0] == '\0' || !request->evh_req){
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Failed to resolve the request url");
|
2018-06-29 17:18:33 +08:00
|
|
|
kfree(request);
|
|
|
|
|
evhttp_uri_free(decoded);
|
|
|
|
|
goto error;
|
2018-06-19 11:32:16 +08:00
|
|
|
}
|
2018-07-12 10:56:13 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
xret = redisAsyncCommand(t->cl_ctx, redis_get_callback, request, "GET %s", request->host);
|
|
|
|
|
if (xret < 0)
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Failed to get information from redis server");
|
|
|
|
|
|
2018-06-19 11:32:16 +08:00
|
|
|
evhttp_uri_free(decoded);
|
2018-06-29 17:18:33 +08:00
|
|
|
goto finish;
|
|
|
|
|
|
|
|
|
|
error:
|
2018-07-18 10:47:45 +08:00
|
|
|
evhttp_send_error(evh_req, HTTP_BADREQUEST, 0);
|
2018-06-19 11:32:16 +08:00
|
|
|
finish:
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-22 14:03:27 +08:00
|
|
|
int redis_sync_init(struct redisContext **c)
|
2018-07-18 10:47:45 +08:00
|
|
|
{
|
2018-08-22 14:03:27 +08:00
|
|
|
int xret = -1;
|
2018-07-18 10:47:45 +08:00
|
|
|
struct config_bucket_t *redis = cert_default_config();
|
|
|
|
|
|
|
|
|
|
struct timeval timeout = { 1, 500000 }; // 1.5 seconds
|
|
|
|
|
|
|
|
|
|
*c = redisConnectWithTimeout(redis->r_ip, redis->r_port, timeout);
|
2018-08-22 14:03:27 +08:00
|
|
|
if (*c == NULL || (*c)->err) {
|
|
|
|
|
if (*c) {
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Sync connection error: %s\n", (*c)->errstr);
|
|
|
|
|
redisFree(*c);
|
|
|
|
|
*c = NULL;
|
|
|
|
|
} else {
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Connection error: can't allocate redis context\n");
|
|
|
|
|
}
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
xret = 0;
|
|
|
|
|
finish:
|
|
|
|
|
return xret;
|
2018-07-18 10:47:45 +08:00
|
|
|
}
|
|
|
|
|
|
2018-06-29 17:18:33 +08:00
|
|
|
static int
|
2018-08-22 14:03:27 +08:00
|
|
|
cert_task_private_init(struct event_base *base, libevent_thread *me)
|
2018-06-19 11:32:16 +08:00
|
|
|
{
|
|
|
|
|
int xret = -1;
|
|
|
|
|
|
|
|
|
|
/* Initialize the redis connection*/
|
2018-07-09 14:32:41 +08:00
|
|
|
xret = redis_rsync_init(base, &me->cl_ctx);
|
2018-06-29 17:18:33 +08:00
|
|
|
if (xret < 0 || !me->cl_ctx){
|
2018-06-19 11:32:16 +08:00
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Initialize the redis connection is failure\n");
|
2018-06-20 16:24:49 +08:00
|
|
|
goto finish;
|
2018-06-19 11:32:16 +08:00
|
|
|
}
|
2018-07-18 10:47:45 +08:00
|
|
|
|
2018-08-22 14:03:27 +08:00
|
|
|
xret = redis_sync_init(&me->sync);
|
|
|
|
|
if (xret < 0 || !me->sync){
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Initialize the sync redis connection is failure\n");
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
2018-07-09 14:32:41 +08:00
|
|
|
|
2018-06-19 11:32:16 +08:00
|
|
|
/* Initialize the X509 CA*/
|
2018-06-29 17:18:33 +08:00
|
|
|
xret = x509_privatekey_init(&me->key, &me->root);
|
|
|
|
|
if (xret < 0 || !me->key || !me->root){
|
2018-06-19 11:32:16 +08:00
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Failed to initialize the x509 certificate");
|
2018-06-20 16:24:49 +08:00
|
|
|
goto finish;
|
2018-06-19 11:32:16 +08:00
|
|
|
}
|
|
|
|
|
finish:
|
2018-06-29 17:18:33 +08:00
|
|
|
return xret;
|
2018-06-19 11:32:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void *pthread_worker_libevent(void *arg)
|
|
|
|
|
{
|
2018-07-12 10:56:13 +08:00
|
|
|
int xret = -1;
|
|
|
|
|
struct evhttp *http = NULL;
|
|
|
|
|
struct event_base *base = NULL;
|
2018-06-19 11:32:16 +08:00
|
|
|
struct evhttp_bound_socket *bound = NULL;
|
|
|
|
|
|
2018-08-22 14:03:27 +08:00
|
|
|
libevent_thread *thread = (libevent_thread *)arg;
|
2018-06-19 11:32:16 +08:00
|
|
|
|
|
|
|
|
base = event_base_new();
|
|
|
|
|
if (! base) {
|
2018-08-22 14:03:27 +08:00
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Can'thread allocate event base\n");
|
2018-06-19 11:32:16 +08:00
|
|
|
goto finish;
|
|
|
|
|
}
|
2018-07-12 10:56:13 +08:00
|
|
|
|
2018-06-19 11:32:16 +08:00
|
|
|
http = evhttp_new(base);
|
|
|
|
|
if (!http) {
|
2018-08-22 14:03:27 +08:00
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "couldn'thread create evhttp. Exiting.\n");
|
2018-06-19 11:32:16 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Context initialization */
|
2018-08-22 14:03:27 +08:00
|
|
|
xret = cert_task_private_init(base, thread);
|
2018-06-29 17:18:33 +08:00
|
|
|
if (xret < 0){
|
2018-06-19 11:32:16 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
2018-08-22 14:03:27 +08:00
|
|
|
evhttp_set_cb(http, "/ca", pthread_work_proc, thread);
|
2018-06-19 11:32:16 +08:00
|
|
|
|
2018-08-22 14:03:27 +08:00
|
|
|
bound = evhttp_accept_socket_with_handle(http, thread->accept_fd);
|
2018-06-19 11:32:16 +08:00
|
|
|
if (bound != NULL) {
|
2018-07-18 10:47:45 +08:00
|
|
|
mesa_runtime_log(RLOG_LV_INFO, MODULE_NAME, "Bound(%p) to port %d - Awaiting connections ... ", bound,
|
|
|
|
|
cert_default_config()->e_port);
|
2018-06-19 11:32:16 +08:00
|
|
|
}
|
|
|
|
|
|
2018-07-12 10:56:13 +08:00
|
|
|
event_base_dispatch(base);
|
2018-06-19 11:32:16 +08:00
|
|
|
error:
|
|
|
|
|
event_base_free(base);
|
|
|
|
|
finish:
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static evutil_socket_t
|
|
|
|
|
evhttp_listen_socket_byuser(const struct sockaddr *sa, int socklen,
|
|
|
|
|
unsigned flags, int backlog)
|
|
|
|
|
{
|
|
|
|
|
evutil_socket_t fd;
|
|
|
|
|
int on = 1;
|
|
|
|
|
int family = sa ? sa->sa_family : AF_UNSPEC;
|
|
|
|
|
int socktype = SOCK_STREAM | EVUTIL_SOCK_NONBLOCK;
|
|
|
|
|
|
|
|
|
|
if (flags & LEV_OPT_CLOSE_ON_EXEC)
|
|
|
|
|
socktype |= EVUTIL_SOCK_CLOEXEC;
|
|
|
|
|
|
|
|
|
|
fd = evutil_socket_(family, socktype, 0);
|
|
|
|
|
if (fd == -1)
|
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void*)&on, sizeof(on))<0)
|
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
|
|
if (flags & LEV_OPT_REUSEABLE) {
|
|
|
|
|
if (evutil_make_listen_socket_reuseable(fd) < 0)
|
|
|
|
|
goto err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flags & LEV_OPT_REUSEABLE_PORT) {
|
2018-06-20 16:24:49 +08:00
|
|
|
if (evutil_make_listen_socket_reuseable_port(fd) < 0){
|
2018-06-19 11:32:16 +08:00
|
|
|
goto err;
|
2018-06-20 16:24:49 +08:00
|
|
|
}
|
2018-06-19 11:32:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sa) {
|
|
|
|
|
if (bind(fd, sa, socklen)<0)
|
|
|
|
|
goto err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (listen(fd, backlog) == -1) {
|
|
|
|
|
goto err;
|
|
|
|
|
}
|
|
|
|
|
return fd;
|
|
|
|
|
err:
|
|
|
|
|
evutil_closesocket(fd);
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-05 14:05:28 +08:00
|
|
|
static int
|
|
|
|
|
fs_screen_preview(libevent_thread *thread)
|
2018-06-28 11:05:53 +08:00
|
|
|
{
|
2018-07-05 14:05:28 +08:00
|
|
|
char buff[128] = {0};
|
2018-06-28 11:05:53 +08:00
|
|
|
|
2018-07-05 14:05:28 +08:00
|
|
|
snprintf(buff, sizeof(buff),"Thread_%02d", thread->id);
|
|
|
|
|
thread->field_ids = FS_internal_register(SGstats.handle, FS_STYLE_FIELD, FS_CALC_CURRENT, buff);
|
2018-06-28 11:05:53 +08:00
|
|
|
|
2018-07-05 14:05:28 +08:00
|
|
|
snprintf(buff, sizeof(buff),"Thread_%d", thread->id);
|
|
|
|
|
thread->column_ids = FS_internal_register(SGstats.handle, FS_STYLE_LINE, FS_CALC_CURRENT, buff);
|
2018-06-28 11:05:53 +08:00
|
|
|
|
2018-07-05 14:05:28 +08:00
|
|
|
return 0;
|
2018-06-28 11:05:53 +08:00
|
|
|
}
|
|
|
|
|
|
2018-07-05 14:05:28 +08:00
|
|
|
static int
|
|
|
|
|
libevent_socket_init()
|
2018-06-19 11:32:16 +08:00
|
|
|
{
|
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
evutil_socket_t accept_fd = -1;
|
2018-07-18 10:47:45 +08:00
|
|
|
int xret = -1;
|
|
|
|
|
unsigned int tid = 0;
|
|
|
|
|
libevent_thread *thread = NULL;
|
2018-06-19 11:32:16 +08:00
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
unsigned int thread_nu = cert_default_config()->thread_nu;
|
2018-06-19 11:32:16 +08:00
|
|
|
|
|
|
|
|
/* Create a new evhttp object to handle requests. */
|
|
|
|
|
memset(&sin, 0, sizeof(struct sockaddr_in));
|
|
|
|
|
sin.sin_family = AF_INET;
|
2018-07-18 10:47:45 +08:00
|
|
|
sin.sin_port = htons(cert_default_config()->e_port);
|
2018-07-12 10:56:13 +08:00
|
|
|
accept_fd = evhttp_listen_socket_byuser((struct sockaddr*)&sin, sizeof(struct sockaddr_in),
|
|
|
|
|
LEV_OPT_REUSEABLE_PORT|LEV_OPT_CLOSE_ON_FREE, -1);
|
2018-06-19 11:32:16 +08:00
|
|
|
if (accept_fd < 0) {
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Could not create a listen!\n");
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
threads = calloc(thread_nu, sizeof(libevent_thread));
|
|
|
|
|
if (! threads) {
|
2018-07-18 10:47:45 +08:00
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Can't allocate thread descriptors");
|
2018-06-19 11:32:16 +08:00
|
|
|
goto finish;
|
|
|
|
|
}
|
2018-07-05 14:05:28 +08:00
|
|
|
memset(threads, 0, thread_nu * sizeof(libevent_thread));
|
2018-06-19 11:32:16 +08:00
|
|
|
|
|
|
|
|
/* Create threads after we've done all the libevent setup. */
|
|
|
|
|
for (tid = 0; tid < thread_nu; tid++) {
|
2018-07-12 10:56:13 +08:00
|
|
|
thread = threads + tid;
|
2018-06-19 11:32:16 +08:00
|
|
|
|
2018-07-12 10:56:13 +08:00
|
|
|
thread->id = tid;
|
2018-06-19 11:32:16 +08:00
|
|
|
thread->accept_fd = accept_fd;
|
2018-07-12 10:56:13 +08:00
|
|
|
thread->routine = pthread_worker_libevent;
|
2018-06-19 11:32:16 +08:00
|
|
|
|
2018-07-05 14:05:28 +08:00
|
|
|
fs_screen_preview(thread);
|
|
|
|
|
|
2018-06-19 11:32:16 +08:00
|
|
|
if (pthread_create(&thread->pid, thread->attr, thread->routine, &threads[tid])){
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "%s", strerror(errno));
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pthread_detach(thread->pid)){
|
|
|
|
|
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "%s", strerror(errno));
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FOREVER{
|
|
|
|
|
sleep(1);
|
|
|
|
|
}
|
|
|
|
|
finish:
|
|
|
|
|
return xret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-05 14:05:28 +08:00
|
|
|
static void
|
|
|
|
|
rt_get_pname_by_pid(pid_t pid, char *task_name)
|
|
|
|
|
{
|
|
|
|
|
#define BUF_SIZE 1024
|
|
|
|
|
char proc_pid_path[BUF_SIZE];
|
|
|
|
|
char buf[BUF_SIZE];
|
|
|
|
|
sprintf(proc_pid_path, "/proc/%d/status", pid);
|
|
|
|
|
FILE* fp = fopen(proc_pid_path, "r");
|
|
|
|
|
if(NULL != fp){
|
|
|
|
|
if( fgets(buf, BUF_SIZE-1, fp)== NULL ){
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
sscanf(buf, "%*s %s", task_name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-12 10:56:13 +08:00
|
|
|
void sigproc(int __attribute__((__unused__))sig)
|
|
|
|
|
{
|
2018-07-18 10:47:45 +08:00
|
|
|
unsigned int tid = 0;
|
|
|
|
|
libevent_thread *thread = NULL;
|
2018-07-12 10:56:13 +08:00
|
|
|
|
|
|
|
|
struct config_bucket_t *rte = cert_default_config();
|
|
|
|
|
|
|
|
|
|
for (tid = 0; tid < rte->thread_nu; tid++) {
|
|
|
|
|
thread = threads + tid;
|
2018-08-22 14:03:27 +08:00
|
|
|
if (thread->sync){
|
2018-07-12 10:56:13 +08:00
|
|
|
redisAsyncDisconnect(thread->cl_ctx);
|
2018-07-18 10:47:45 +08:00
|
|
|
redisFree(thread->sync);
|
2018-08-22 14:03:27 +08:00
|
|
|
}
|
|
|
|
|
X509_free(thread->root);
|
|
|
|
|
EVP_PKEY_free(thread->key);
|
2018-07-12 10:56:13 +08:00
|
|
|
}
|
|
|
|
|
kfree(threads);
|
|
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int cert_screen_init()
|
2018-07-05 14:05:28 +08:00
|
|
|
{
|
|
|
|
|
int value=0;
|
|
|
|
|
char stat_path[63] = {0};
|
|
|
|
|
char pname[32]= {0}, buff[128] = {0};
|
|
|
|
|
|
|
|
|
|
SGstats.handle = FS_internal_create_handle();
|
|
|
|
|
|
|
|
|
|
rt_get_pname_by_pid(getpid(), &pname[0]);
|
|
|
|
|
FS_internal_set_para(SGstats.handle, APP_NAME, pname, strlen(pname)+1);
|
|
|
|
|
value=0;
|
|
|
|
|
FS_internal_set_para(SGstats.handle, FLUSH_BY_DATE, &value, sizeof(value));
|
|
|
|
|
|
|
|
|
|
snprintf(stat_path, 63, "%s/fs2_%s.status", logging_sc_lid.run_log_path, pname);
|
|
|
|
|
FS_internal_set_para(SGstats.handle, OUTPUT_DEVICE, stat_path, strlen(stat_path)+1);
|
|
|
|
|
value=1;
|
|
|
|
|
FS_internal_set_para(SGstats.handle, PRINT_MODE, &value, sizeof(value));
|
|
|
|
|
value=1;
|
|
|
|
|
FS_internal_set_para(SGstats.handle, CREATE_THREAD, &value, sizeof(value));
|
|
|
|
|
value=3;
|
|
|
|
|
FS_internal_set_para(SGstats.handle, STAT_CYCLE, &value, sizeof(value));
|
|
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
snprintf(buff,sizeof(buff),"%s", "Req");
|
2018-07-05 14:05:28 +08:00
|
|
|
SGstats.line_ids[0] = FS_internal_register(SGstats.handle, FS_STYLE_COLUMN, FS_CALC_CURRENT, buff);
|
|
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
snprintf(buff,sizeof(buff),"%s", "DB");
|
2018-07-05 14:05:28 +08:00
|
|
|
SGstats.line_ids[1] = FS_internal_register(SGstats.handle, FS_STYLE_COLUMN, FS_CALC_CURRENT, buff);
|
|
|
|
|
|
2018-07-18 10:47:45 +08:00
|
|
|
snprintf(buff,sizeof(buff),"%s", "Local");
|
2018-07-05 14:05:28 +08:00
|
|
|
SGstats.line_ids[2] = FS_internal_register(SGstats.handle, FS_STYLE_COLUMN, FS_CALC_CURRENT, buff);
|
|
|
|
|
|
2018-07-10 10:53:10 +08:00
|
|
|
snprintf(buff,sizeof(buff),"%s", "take-time");
|
2018-07-09 14:32:41 +08:00
|
|
|
SGstats.line_ids[3] = FS_internal_register(SGstats.handle, FS_STYLE_COLUMN, FS_CALC_CURRENT, buff);
|
|
|
|
|
|
2018-07-12 10:56:13 +08:00
|
|
|
value=SGstats.line_ids[3];
|
|
|
|
|
FS_internal_set_para(SGstats.handle, ID_INVISBLE, &value, sizeof(value));
|
2018-07-18 10:47:45 +08:00
|
|
|
snprintf(buff,sizeof(buff),"Cert/Nsec");
|
2018-07-10 10:53:10 +08:00
|
|
|
FS_internal_register_ratio(SGstats.handle, SGstats.line_ids[3],
|
|
|
|
|
SGstats.line_ids[2], 1,
|
|
|
|
|
FS_STYLE_COLUMN, FS_CALC_CURRENT,
|
|
|
|
|
buff);
|
2018-07-05 14:05:28 +08:00
|
|
|
FS_internal_start(SGstats.handle);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 11:32:16 +08:00
|
|
|
int cert_session_init()
|
|
|
|
|
{
|
|
|
|
|
int xret = 0;
|
|
|
|
|
|
2018-07-12 10:56:13 +08:00
|
|
|
cert_screen_init();
|
2018-07-05 14:05:28 +08:00
|
|
|
|
2018-06-19 11:32:16 +08:00
|
|
|
libevent_socket_init();
|
|
|
|
|
|
|
|
|
|
return xret;
|
|
|
|
|
}
|
|
|
|
|
|