-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuhm-module.c
139 lines (123 loc) · 4.18 KB
/
uhm-module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/************************************************************************
* Bahamut IRCd - Sample UHM (User Host-Masking) Module
* Copyright (C) 2018, Kobi Shmueli
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define BIRCMODULE 1
#include "struct.h"
#include "common.h"
#include "sys.h"
#include "h.h"
#include "hooks.h"
#include "structfunc.h"
/******************************************/
/* Note: Please change the following key: */
/******************************************/
#define SECRET_UHM_KEY "changeme"
#ifndef H_H
#error "Couldn't load h.h!"
#endif
#ifndef USE_HOOKMODULES
#error "Couldn't load setup.h! did you run ./configure ???"
#else
static void *opaque; /* A pointer to the IRCd's opaque */
/* maskhost_handler - MASKHOST hook handler
Returns: UHM_SUCCESS = Success (1)
UHM_SOFT_FAILURE = Soft failure (0)
UHM_HARD_FAILURE = Hard failure (-2)
*/
int maskhost_handler(char *orghost, char **newhostptr, int type)
{
char key[] = SECRET_UHM_KEY;
char newhost[HOSTLEN + 1];
int len;
unsigned char *digest;
int digest_pos;
int orghostlen = strlen(orghost); /* save strlen() result for orghost as we'll use it a lot */
unsigned int hmaclen;
unsigned char hmacraw[EVP_MAX_MD_SIZE];
if(type == 1)
{
len = 0; /* Init the len of the new (masked) hostname */
HMAC(EVP_sha256(), key, strlen(key), (const unsigned char *)orghost, orghostlen, hmacraw, &hmaclen);
digest_pos = 0;
while(digest_pos < hmaclen)
{
if(digest_pos!=0 && digest_pos%2 == 0) newhost[len++] = '-';
len += sprintf(&newhost[len], "%02x", (unsigned int)digest[++digest_pos]);
if(len >= HOSTLEN)
{
/* This really really really shouldn't happen! */
return FLUSH_BUFFER; /* Masked hostname is too long! */
}
}
/* Suffix it with .fake */
if(len+5 >= HOSTLEN)
{
return FLUSH_BUFFER; /* Masked hostname is too long! */
}
newhost[len++] = '.';
newhost[len++] = 'f';
newhost[len++] = 'a';
newhost[len++] = 'k';
newhost[len++] = 'e';
newhost[len] = '\0';
strcpy(*newhostptr, newhost);
return UHM_SUCCESS; /* Success */
}
if(type == 2)
{
/* Temporary for debugging! */
strcpy(*newhostptr, "fake.kobi.co.il");
return UHM_SUCCESS; /* Success */
}
sendto_realops_lev(DEBUG_LEV, "UHM Error (unknown uhm type %d) for %s", type, orghost);
return UHM_SOFT_FAILURE; /* Unknown error (will try other modules if available) */
}
/* Called before initialized to make sure the IRCd's and the module interface versions match */
void bircmodule_check(int *ver)
{
*ver = MODULE_INTERFACE_VERSION;
}
/* Called when a server admin uses the MODULE CMD command */
int bircmodule_command(aClient *sptr, int parc, char *parv[])
{
return 0;
}
/* Called when a u:lined server uses the MODULE CGLOBAL command */
int bircmodule_globalcommand(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
return 0;
}
/* Called when the module is initialized */
int bircmodule_init(void *real_opaque)
{
opaque = real_opaque;
if(!bircmodule_add_hook(CHOOK_MASKHOST, opaque, maskhost_handler))
return -1;
return 0;
}
/* Called when the module is unloaded */
void bircmodule_shutdown()
{
}
/* Module information for MODULE LIST and MODULE info commands */
void bircmodule_getinfo(char **version, char **desc)
{
*version = "1.0";
*desc = "Sample UHM Module";
}
#endif