fixed ssl.c bug when ssl backend returns IO_BLOCKED but IO engine doesn't get informe...
[ircu2.10.12-pk.git] / ircd / m_webirc.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_svsnick.c
3  * Written by David Herrmann.
4  */
5
6 #include "config.h"
7
8 #include "client.h"
9 #include "hash.h"
10 #include "ircd.h"
11 #include "ircd_alloc.h"
12 #include "ircd_log.h"
13 #include "ircd_reply.h"
14 #include "ircd_string.h"
15 #include "ircd_features.h"
16 #include "ircd_crypt.h"
17 #include "numeric.h"
18 #include "numnicks.h"
19 #include "send.h"
20 #include "s_conf.h"
21 #include "s_misc.h"
22 #include "s_debug.h"
23 #include "match.h"
24 #include "IPcheck.h"
25 #include "ssl.h"
26 #include "res.h"
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31
32 /* Sends response \r (len: \l) to client \c. */
33 #define webirc_resp(c, r, l) \
34    ssl_send(cli_fd(c), cli_socket(c).ssl, r, l)
35
36 /* Buffer used in nick replacements. */
37 static char webirc_buf[513];
38
39 /* Returns 1 if the passwords are the same and 0 if not.
40  * \to_match is the password hash
41  * \passwd is the unhashed password in "$KIND$password" style
42  */
43 static unsigned int webirc_pwmatch(const char* to_match, const char* passwd) {
44     char *crypted;
45     signed int res;
46
47     crypted = ircd_crypt(to_match, passwd);
48     if(!crypted)
49         return 0;
50     res = strcmp(crypted, passwd);
51     MyFree(crypted);
52     return 0 == res;
53 }
54
55 /* Checks whether password/host/spoofed host/ip are allowed and returns the corresponding webirc block.
56  * Returns NULL if nothing found.
57  */
58 static struct webirc_block *webirc_match(const char *passwd, const char *real_host, const char *real_ip, const char *spoofed_host, const char *spoofed_ip) {
59     struct webirc_block *iter;
60     struct webirc_node *inode;
61     unsigned int matched = 0;
62
63     if(!GlobalWebIRCConf) {
64         return NULL;
65     }
66
67     iter = GlobalWebIRCConf;
68     do {
69         if(iter->list) {
70             inode = iter->list;
71
72             /* first check for matching password */
73             do {
74                 /* it's a sorted list an passwords are stored first! */
75
76                 if(inode->type != WEBIRC_PASS) break;
77                 if(webirc_pwmatch(passwd, inode->content)) {
78                     matched = 1;
79                     break;
80                 }
81
82                 inode = inode->next;
83             } while(inode != iter->list);
84
85             /* go to next entry */
86             inode = inode->next;
87
88             /* check for matching real-host/ip */
89             if(matched) {
90                 matched = 0;
91                 /* fast-forward to hosts and then check the hosts */
92                 do {
93                     /* everything greater than WEBIRC_HOSTS are the spoofed masks */
94                     if(inode->type > WEBIRC_HOST) break;
95
96                     if(inode->type == WEBIRC_HOST) {
97                         if(inode->neg) {
98                             if(match(inode->content, real_host) == 0 || match(inode->content, real_ip) == 0) {
99                                 matched = 0;
100                                 break;
101                             }
102                         }
103                         else if(matched) /* do nothing */;
104                         else if(match(inode->content, real_host) == 0 || match(inode->content, real_ip) == 0)
105                             matched = 1;
106                     }
107
108                     inode = inode->next;
109                 } while(inode != iter->list);
110             }
111
112             /* check for matching spoofed host/ip */
113             if(matched) {
114                 matched = 0;
115                 do {
116                     if(inode->type == WEBIRC_SPOOF) {
117                         if(inode->neg) {
118                             if(match(inode->content, spoofed_host) == 0 || match(inode->content, spoofed_ip) == 0) {
119                                 matched = 0;
120                                 break;
121                             }
122                         }
123                         else if(matched) /* do nothing */;
124                         else if(match(inode->content, spoofed_host) == 0 || match(inode->content, spoofed_ip) == 0)
125                             matched = 1;
126                     }
127
128                     inode = inode->next;
129                 } while(inode != iter->list);
130             }
131
132             if(matched) return iter;
133
134             /* nothing found, try next block */
135         }
136         iter = iter->next;
137     } while(iter != GlobalWebIRCConf);
138
139     return NULL;
140 }
141
142 /*
143  * m_webirc
144  *
145  * parv[0] = sender prefix
146  * parv[1] = password
147  * parv[2] = "cgiirc" (ignored)
148  * parv[3] = hostname
149  * parv[4] = ip
150  */
151 int m_webirc(struct Client* cptr, struct Client* sptr, int parc, char* parv[]) {
152     struct webirc_block *block;
153     struct irc_in_addr addr;
154     const char *con_addr;
155     time_t next_target = 0;
156     unsigned int len;
157
158     const char *nick = (*(cli_name(sptr))) ? cli_name(sptr) : "AUTH";
159     unsigned int auth = (*(cli_name(sptr))) ? 0 : 1;
160
161     /* servers cannot spoof their ip */
162     if(IsServerPort(sptr)) {
163         /* If a server sends WEBIRC command it's a protocol violation so exit him and do not check FEAT_WEBIRC_REJECT. */
164         IPcheck_connect_fail(sptr);
165         return exit_client(cptr, sptr, &me, "WebIRC not supported on server ports");
166     }
167
168     /* all 4 parameters are required plus the prefix => 5 */
169     if(parc < 5)
170         return need_more_params(sptr, "WEBIRC");
171
172     /* created ip in dotted notation */
173     con_addr = ircd_ntoa(&(cli_ip(sptr)));
174     if(0 == ipmask_parse(parv[4], &addr, NULL)) {
175         if(feature_bool(FEAT_WEBIRC_REJECT)) {
176             IPcheck_connect_fail(sptr);
177             return exit_client(cptr, sptr, &me, "WebIRC protocol violation (p4).");
178         }
179         else {
180             /* bufferoverflow prevented with NICKLEN check above */
181             len = sprintf(webirc_buf, "NOTICE %s :%sWebIRC protocol violation (p4).\r\n", nick, auth ? "*** " : "");
182             webirc_resp(sptr, webirc_buf, len);
183             return 0; /* continue with normal authentication */
184         }
185     }
186
187     /* find matching webirc block */
188     block = webirc_match(parv[1], cli_sockhost(sptr), con_addr, parv[3], parv[4]);
189     if(!block) {
190         if(feature_bool(FEAT_WEBIRC_REJECT)) {
191             IPcheck_connect_fail(sptr);
192             return exit_client(cptr, sptr, &me, "WebIRC client rejected, no match found.");
193         }
194         else {
195             len = sprintf(webirc_buf, "NOTICE %s :%sWebIRC spoofing rejected, no match found.\r\n", nick, auth?"*** ":"");
196             webirc_resp(sptr, webirc_buf, len);
197             return 0; /* continue with normal authentication */
198         }
199     }
200
201     /* remove the WebIRC ip from the IPcheck entry, we will add the real one later */
202     IPcheck_connect_fail(sptr);
203     IPcheck_disconnect(sptr);
204     ClearIPChecked(sptr);
205
206     /* spoof IP */
207     memcpy(cli_real_ip(sptr).in6_16, cli_ip(sptr).in6_16, 16);
208     memcpy(cli_ip(sptr).in6_16, addr.in6_16, 16);
209
210     /* spoof ip/host strings */
211     ircd_strncpy(cli_real_sock_ip(sptr), cli_sock_ip(sptr), SOCKIPLEN);
212     ircd_strncpy(cli_sock_ip(sptr), parv[4], SOCKIPLEN);
213     ircd_strncpy(cli_real_sockhost(sptr), cli_sockhost(sptr), HOSTLEN);
214     ircd_strncpy(cli_sockhost(sptr), parv[3], HOSTLEN);
215     ircd_strncpy(cli_webirc(sptr), block->name, NICKLEN);
216
217     /* add the real ip to the IPcheck */
218     if(!IPcheck_local_connect(&cli_ip(sptr), &next_target))
219         return exit_client(cptr, sptr, &me, "Too many connections from your host");
220     SetIPChecked(cptr);
221
222     /* set WebIRC umode only if enabled */
223     if(feature_bool(FEAT_WEBIRC_UMODE))
224         SetWebIRC(cptr);
225
226     return 0;
227 }
228