]> jfr.im git - irc/quakenet/newserv.git/blob - chanserv/authlib.c
Merge.
[irc/quakenet/newserv.git] / chanserv / authlib.c
1 /* authlib.c */
2
3 #include "authlib.h"
4 #include "chanserv.h"
5 #include "../lib/irc_string.h"
6
7 #include <string.h>
8 #include <ctype.h>
9 #include <sys/types.h>
10 #include <regex.h>
11
12 regex_t remail, raccount;
13 static int regexinit;
14
15 int csa_initregex(void) {
16 if (regexinit)
17 return 1;
18
19 if (regcomp(&remail, VALID_EMAIL, REG_EXTENDED | REG_NOSUB | REG_ICASE))
20 return 0;
21
22 if (regcomp(&raccount, VALID_ACCOUNT_NAME, REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
23 regfree(&remail);
24 return 0;
25 }
26
27 regexinit = 1;
28 return 1;
29 }
30
31 void csa_freeregex(void) {
32 if(!regexinit)
33 return;
34
35 regfree(&remail);
36 regfree(&raccount);
37 regexinit = 0;
38 }
39
40 /*
41 * use regex matching to determine if it's a valid eboy or not
42 */
43 int csa_checkeboy(nick *sender, char *eboy)
44 {
45 int i, len;
46
47 len = (((strlen(eboy)) < (EMAILLEN)) ? (strlen(eboy)) : (EMAILLEN));
48 if (len <= 4) {
49 if (sender)
50 chanservstdmessage(sender, QM_EMAILTOOSHORT, eboy);
51 return (1);
52 }
53
54 if (strstr(&eboy[1], "@") == NULL) {
55 if (sender)
56 chanservstdmessage(sender, QM_EMAILNOAT, eboy);
57 return (1);
58 }
59
60 if (eboy[len - 1] == '@') {
61 if (sender)
62 chanservstdmessage(sender, QM_EMAILATEND, eboy);
63 return (1);
64 }
65
66 for (i = 0; i < len; i++) {
67 if (!isalpha(eboy[i]) && !isdigit(eboy[i])
68 && !(eboy[i] == '@') && !(eboy[i] == '.')
69 && !(eboy[i] == '_') && !(eboy[i] == '-')) {
70 if (sender)
71 chanservstdmessage(sender, QM_EMAILINVCHR, eboy);
72 return (1);
73 }
74 }
75
76 /* catch some real lame attempts */
77 if (!ircd_strncmp("user@mymailhost.xx", eboy, len) || !ircd_strncmp("info@quakenet.org", eboy, len)
78 || !ircd_strncmp("user@mymail.xx", eboy, len) || !ircd_strncmp("user@mail.cc", eboy, len)
79 || !ircd_strncmp("user@host.com", eboy, len) || !ircd_strncmp("Jackie@your.isp.com", eboy, len)
80 || !ircd_strncmp("QBot@QuakeNet.org", eboy, len) || !ircd_strncmp("Q@CServe.quakenet.org", eboy, len)) {
81 if (sender)
82 chanservstdmessage(sender, QM_NOTYOUREMAIL, eboy);
83 return (1);
84 }
85
86 if (regexec(&remail, eboy, (size_t) 0, NULL, 0)) {
87 if (sender)
88 chanservstdmessage(sender, QM_INVALIDEMAIL, eboy);
89 return (1);
90 }
91
92 return (0);
93 }
94
95 /*
96 * use regex matching to determine if it's a valid account name or not
97 */
98 int csa_checkaccountname(nick *sender, char *accountname) {
99 if (regexec(&raccount, accountname, (size_t) 0, NULL, 0)) {
100 if (sender)
101 chanservstdmessage(sender, QM_INVALIDACCOUNTNAME);
102 return (1);
103 }
104 return (0);
105 }
106
107
108 /*
109 * create a random pw. code stolen from fox's O
110 */
111 void csa_createrandompw(char *pw, int n)
112 {
113 int i;
114 char upwdchars[] = "ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789-!";
115
116 for (i = 0; i < n; i++) {
117 pw[i] = upwdchars[rand() % (sizeof(upwdchars) - 1)];
118 }
119 pw[n] = '\0';
120 }
121
122 /*
123 * check if account is "throttled"
124 */
125 int csa_checkthrottled(nick *sender, reguser *rup, char *s)
126 {
127 time_t now;
128 long d;
129 float t;
130
131 now=time(NULL);
132 d=MAX_RESEND_TIME+rup->lastemailchange-now;
133
134 if (d>MAX_RESEND_TIME)
135 d=MAX_RESEND_TIME;
136
137 if (d>0L) {
138 t = ((float) d) / ((float) 3600);
139 chanservstdmessage(sender, QM_MAILTHROTTLED, t);
140 cs_log(sender,"%s FAIL username %s, new request throttled for %.1f hours",s,rup->username,t);
141 return 1;
142 }
143 return 0;
144 }