unrealircd

- supernets unrealircd source & configuration
git clone git://git.acid.vegas/unrealircd.git
Log | Files | Refs | Archive | README | LICENSE

tls_cipher.c (2315B)

      1 /*
      2  * Store TLS cipher in ModData
      3  * (C) Copyright 2021-.. Syzop and The UnrealIRCd Team
      4  * License: GPLv2 or later
      5  */
      6 
      7 #include "unrealircd.h"
      8 
      9 ModuleHeader MOD_HEADER
     10   = {
     11 	"tls_cipher",
     12 	"5.0",
     13 	"Store and retrieve TLS cipher string",
     14 	"UnrealIRCd Team",
     15 	"unrealircd-6",
     16     };
     17 
     18 /* Forward declarations */
     19 void tls_cipher_free(ModData *m);
     20 const char *tls_cipher_serialize(ModData *m);
     21 void tls_cipher_unserialize(const char *str, ModData *m);
     22 int tls_cipher_handshake(Client *client);
     23 int tls_cipher_connect(Client *client);
     24 int tls_cipher_whois(Client *client, Client *target);
     25 int tls_json_expand_client(Client *client, int detail, json_t *j);
     26 
     27 ModDataInfo *tls_cipher_md; /* Module Data structure which we acquire */
     28 
     29 MOD_INIT()
     30 {
     31 ModDataInfo mreq;
     32 
     33 	MARK_AS_OFFICIAL_MODULE(modinfo);
     34 	
     35 	memset(&mreq, 0, sizeof(mreq));
     36 	mreq.name = "tls_cipher";
     37 	mreq.free = tls_cipher_free;
     38 	mreq.serialize = tls_cipher_serialize;
     39 	mreq.unserialize = tls_cipher_unserialize;
     40 	mreq.sync = MODDATA_SYNC_EARLY;
     41 	mreq.type = MODDATATYPE_CLIENT;
     42 	tls_cipher_md = ModDataAdd(modinfo->handle, mreq);
     43 	if (!tls_cipher_md)
     44 		abort();
     45 
     46 	HookAdd(modinfo->handle, HOOKTYPE_HANDSHAKE, 0, tls_cipher_handshake);
     47 	HookAdd(modinfo->handle, HOOKTYPE_SERVER_HANDSHAKE_OUT, 0, tls_cipher_handshake);
     48 
     49 	HookAdd(modinfo->handle, HOOKTYPE_JSON_EXPAND_CLIENT, 0, tls_json_expand_client);
     50 
     51 	return MOD_SUCCESS;
     52 }
     53 
     54 MOD_LOAD()
     55 {
     56 	return MOD_SUCCESS;
     57 }
     58 
     59 
     60 MOD_UNLOAD()
     61 {
     62 	return MOD_SUCCESS;
     63 }
     64 
     65 int tls_cipher_handshake(Client *client)
     66 {
     67 	if (client->local->ssl)
     68 	{
     69 		const char *cipher = tls_get_cipher(client);
     70 
     71 		if (!cipher)
     72 			return 0;
     73 
     74 		moddata_client_set(client, "tls_cipher", cipher);
     75 	}
     76 	return 0;
     77 }
     78 
     79 void tls_cipher_free(ModData *m)
     80 {
     81 	safe_free(m->str);
     82 }
     83 
     84 const char *tls_cipher_serialize(ModData *m)
     85 {
     86 	if (!m->str)
     87 		return NULL;
     88 	return m->str;
     89 }
     90 
     91 void tls_cipher_unserialize(const char *str, ModData *m)
     92 {
     93 	safe_strdup(m->str, str);
     94 }
     95 
     96 int tls_json_expand_client(Client *client, int detail, json_t *j)
     97 {
     98 	json_t *tls;
     99 	const char *str;
    100 
    101 	if (detail < 2)
    102 		return 0;
    103 
    104 	str = moddata_client_get(client, "tls_cipher");
    105 	if (!str)
    106 		return 0;
    107 
    108 	tls = json_object_get(j, "tls");
    109 	if (!tls)
    110 	{
    111 		tls = json_object();
    112 		json_object_set_new(j, "tls", tls);
    113 	}
    114 
    115 	json_object_set_new(tls, "cipher", json_string_unreal(str));
    116 
    117 	return 0;
    118 }