]> jfr.im git - irc/quakenet/newserv.git/blob - ticketauth/ticketauth.c
Various changes to support authname being stored inside the authext.
[irc/quakenet/newserv.git] / ticketauth / ticketauth.c
1 /* ticketauth.c */
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <strings.h>
6
7 #include "../control/control.h"
8 #include "../core/config.h"
9 #include "../nick/nick.h"
10 #include "../core/error.h"
11 #include "../lib/hmac.h"
12 #include "../lib/version.h"
13 #include "../localuser/localuser.h"
14 #include "../core/hooks.h"
15 #include "../irc/irc.h"
16 #include "../chanserv/chanserv.h"
17
18 #define WARN_CHANNEL "#twilightzone"
19
20 MODULE_VERSION("");
21
22 sstring *sharedsecret = NULL;
23
24 int ta_ticketauth(void *source, int cargc, char **cargv) {
25 nick *np = (nick *)source;
26 char buffer[1024], *uhmac, *acc, *junk, *flags;
27 unsigned char digest[32];
28 int expiry, acclen, id;
29 hmacsha256 hmac;
30 channel *wcp;
31
32 if(IsAccount(np)) {
33 controlreply(np, "You're already authed.");
34 return CMD_ERROR;
35 }
36
37 if(cargc != 6)
38 return CMD_USAGE;
39
40 acc = cargv[0];
41 expiry = atoi(cargv[1]);
42 id = atoi(cargv[2]);
43 acclen = strlen(acc);
44 flags = cargv[3];
45 junk = cargv[4];
46 uhmac = cargv[5];
47
48 if((acclen <= 1) || (acclen > ACCOUNTLEN)) {
49 controlreply(np, "Bad account.");
50 return CMD_ERROR;
51 }
52
53 if(time(NULL) > expiry + 30) {
54 controlwall(NO_OPER, NL_MISC, "%s!%s@%s attempted to TICKETAUTH as %s (expired)", np->nick, np->ident, np->host->name->content, acc);
55 controlreply(np, "Ticket time is bad or has expired.");
56 return CMD_ERROR;
57 }
58
59 hmacsha256_init(&hmac, (unsigned char *)sharedsecret->content, sharedsecret->length);
60 snprintf(buffer, sizeof(buffer), "%s %d %d %s", acc, expiry, id, junk);
61 hmacsha256_update(&hmac, (unsigned char *)buffer, strlen(buffer));
62 hmacsha256_final(&hmac, digest);
63
64 /* hahahaha */
65 snprintf(buffer, sizeof(buffer), "%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x", digest[0], digest[1], digest[2], digest[3], digest[4], digest[5], digest[6], digest[7], digest[8], digest[9], digest[10], digest[11], digest[12], digest[13], digest[14], digest[15], digest[16], digest[17], digest[18], digest[19], digest[20], digest[21], digest[22], digest[23], digest[24], digest[25], digest[26], digest[27], digest[28], digest[29], digest[30], digest[31]);
66
67 if(strcasecmp(buffer, uhmac)) {
68 controlwall(NO_OPER, NL_MISC, "%s!%s@%s attempted to TICKETAUTH as %s (bad HMAC)", np->nick, np->ident, np->host->name->content, acc);
69 controlreply(np, "Bad HMAC.");
70 return CMD_ERROR;
71 }
72
73 controlwall(NO_OPER, NL_MISC, "%s!%s@%s TICKETAUTH'ed as %s", np->nick, np->ident, np->host->name->content, acc);
74
75 wcp = findchannel(WARN_CHANNEL);
76 if(wcp)
77 controlchanmsg(wcp, "WARNING: %s!%s@%s TICKETAUTH'ed as %s", np->nick, np->ident, np->host->name->content, acc);
78
79 controlreply(np, "Ticket valid, authing. . .");
80
81 localusersetaccount(np, acc, id, 0, cs_accountflagmap_str(flags));
82
83 controlreply(np, "Done.");
84 return CMD_OK;
85 }
86
87 void _init() {
88 sharedsecret = getcopyconfigitem("ticketauth", "sharedsecret", "", 512);
89 if(!sharedsecret || !sharedsecret->content || !sharedsecret->content[0]) {
90 Error("ticketauth", ERR_ERROR, "Shared secret not defined in config file.");
91 if(sharedsecret) {
92 freesstring(sharedsecret);
93 sharedsecret = NULL;
94 }
95
96 return;
97 }
98
99 registercontrolhelpcmd("ticketauth", NO_OPERED, 5, ta_ticketauth, "Usage: ticketauth <ticket>");
100 }
101
102 void _fini() {
103 if(!sharedsecret)
104 return;
105
106 deregistercontrolcmd("ticketauth", ta_ticketauth);
107
108 freesstring(sharedsecret);
109 }