Debian Codegraph @

modules/nickserv/register.c

raw
1 /*
2 * Copyright (c) 2005 William Pitcock, et al.
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * This file contains code for the NickServ REGISTER function.
6 *
7 */
8
9 #include "atheme.h"
10
11 DECLARE_MODULE_V1
12 (
13 "nickserv/register", false, _modinit, _moddeinit,
14 PACKAGE_STRING,
15 VENDOR_STRING
16 );
17
18 unsigned int ratelimit_count = 0;
19 time_t ratelimit_firsttime = 0;
20
21 static void ns_cmd_register(sourceinfo_t *si, int parc, char *parv[]);
22
23 command_t ns_register = { "REGISTER", N_("Registers a nickname."), AC_NONE, 3, ns_cmd_register, { .path = "nickserv/register" } };
24
25 void _modinit(module_t *m)
26 {
27 service_named_bind_command("nickserv", &ns_register);
28 }
29
30 void _moddeinit(module_unload_intent_t intent)
31 {
32 service_named_unbind_command("nickserv", &ns_register);
33 }
34
35 static void ns_cmd_register(sourceinfo_t *si, int parc, char *parv[])
36 {
37 myuser_t *mu;
38 mynick_t *mn = NULL;
39 mowgli_node_t *n;
40 const char *account;
41 const char *pass;
42 const char *email;
43 char lau[BUFSIZE], lao[BUFSIZE];
44 hook_user_register_check_t hdata;
45 hook_user_req_t req;
46
47 if (si->smu)
48 {
49 command_fail(si, fault_already_authed, _("You are already logged in as \2%s\2."), entity(si->smu)->name);
50 if (si->su != NULL && !mynick_find(si->su->nick) &&
51 command_find(si->service->commands, "GROUP"))
52 command_fail(si, fault_already_authed, _("Use %s to register %s to your account."), "GROUP", si->su->nick);
53 return;
54 }
55
56 if (nicksvs.no_nick_ownership || si->su == NULL)
57 account = parv[0], pass = parv[1], email = parv[2];
58 else
59 account = si->su->nick, pass = parv[0], email = parv[1];
60
61 if (!account || !pass || !email)
62 {
63 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "REGISTER");
64 if (nicksvs.no_nick_ownership || si->su == NULL)
65 command_fail(si, fault_needmoreparams, _("Syntax: REGISTER <account> <password> <email>"));
66 else
67 command_fail(si, fault_needmoreparams, _("Syntax: REGISTER <password> <email>"));
68 return;
69 }
70
71 if (strlen(pass) >= PASSLEN)
72 {
73 command_fail(si, fault_badparams, STR_INVALID_PARAMS, "REGISTER");
74 command_fail(si, fault_badparams, _("Registration passwords may not be longer than \2%d\2 characters."), PASSLEN - 1);
75 return;
76 }
77
78 if (!nicksvs.no_nick_ownership && si->su == NULL && user_find_named(account))
79 {
80 command_fail(si, fault_noprivs, _("A user matching this account is already on IRC."));
81 return;
82 }
83
84 if (!nicksvs.no_nick_ownership && IsDigit(*account))
85 {
86 command_fail(si, fault_badparams, _("For security reasons, you can't register your UID."));
87 command_fail(si, fault_badparams, _("Please change to a real nickname, and try again."));
88 return;
89 }
90
91 if (nicksvs.no_nick_ownership || si->su == NULL)
92 {
93 if (strchr(account, ' ') || strchr(account, '\n') || strchr(account, '\r') || account[0] == '=' || account[0] == '#' || account[0] == '@' || account[0] == '+' || account[0] == '%' || account[0] == '!' || strchr(account, ','))
94 {
95 command_fail(si, fault_badparams, _("The account name \2%s\2 is invalid."), account);
96 return;
97 }
98 }
99
100 if (strlen(account) >= NICKLEN)
101 {
102 command_fail(si, fault_badparams, _("The account name \2%s\2 is invalid."), account);
103 return;
104 }
105
106 if (!validemail(email))
107 {
108 command_fail(si, fault_badparams, _("\2%s\2 is not a valid email address."), email);
109 return;
110 }
111
112 if (!email_within_limits(email))
113 {
114 command_fail(si, fault_toomany, _("\2%s\2 has too many accounts registered."), email);
115 return;
116 }
117
118 if ((si->su != NULL && !strcasecmp(pass, si->su->nick)) || !strcasecmp(pass, account))
119 {
120 command_fail(si, fault_badparams, _("You cannot use your nickname as a password."));
121 if (nicksvs.no_nick_ownership || si->su == NULL)
122 command_fail(si, fault_needmoreparams, _("Syntax: REGISTER <account> <password> <email>"));
123 else
124 command_fail(si, fault_needmoreparams, _("Syntax: REGISTER <password> <email>"));
125 return;
126 }
127
128 /* make sure it isn't registered already */
129 if (nicksvs.no_nick_ownership ? myuser_find(account) != NULL : mynick_find(account) != NULL)
130 {
131 command_fail(si, fault_alreadyexists, _("\2%s\2 is already registered."), account);
132 return;
133 }
134
135 if ((unsigned int)(CURRTIME - ratelimit_firsttime) > config_options.ratelimit_period)
136 ratelimit_count = 0, ratelimit_firsttime = CURRTIME;
137
138 /* Still do flood priv checking because the user may be in the ircop operclass */
139 if (ratelimit_count > config_options.ratelimit_uses && !has_priv(si, PRIV_FLOOD))
140 {
141 command_fail(si, fault_toomany, _("The system is currently too busy to process your registration, please try again later."));
142 slog(LG_INFO, "NICKSERV:REGISTER:THROTTLED: \2%s\2 by \2%s\2", account, si->su != NULL ? si->su->nick : get_source_name(si));
143 return;
144 }
145
146 hdata.si = si;
147 hdata.account = account;
148 hdata.email = email;
149 hdata.password = pass;
150 hdata.approved = 0;
151 hook_call_user_can_register(&hdata);
152 if (hdata.approved != 0)
153 return;
154 if (!nicksvs.no_nick_ownership)
155 {
156 hook_call_nick_can_register(&hdata);
157 if (hdata.approved != 0)
158 return;
159 }
160
161 mu = myuser_add(account, auth_module_loaded ? "*" : pass, email, config_options.defuflags | MU_NOBURSTLOGIN | (auth_module_loaded ? MU_CRYPTPASS : 0));
162 mu->registered = CURRTIME;
163 mu->lastlogin = CURRTIME;
164 if (!nicksvs.no_nick_ownership)
165 {
166 mn = mynick_add(mu, entity(mu)->name);
167 mn->registered = CURRTIME;
168 mn->lastseen = CURRTIME;
169 }
170 if (config_options.ratelimit_uses && config_options.ratelimit_period)
171 ratelimit_count++;
172
173 if (auth_module_loaded)
174 {
175 if (!verify_password(mu, pass))
176 {
177 command_fail(si, fault_authfail, _("Invalid password for \2%s\2."), entity(mu)->name);
178 bad_password(si, mu);
179 object_unref(mu);
180 return;
181 }
182 }
183
184 if (me.auth == AUTH_EMAIL)
185 {
186 char *key = random_string(12);
187 mu->flags |= MU_WAITAUTH;
188
189 metadata_add(mu, "private:verify:register:key", key);
190 metadata_add(mu, "private:verify:register:timestamp", number_to_string(time(NULL)));
191
192 if (!sendemail(si->su != NULL ? si->su : si->service->me, mu, EMAIL_REGISTER, mu->email, key))
193 {
194 command_fail(si, fault_emailfail, _("Sending email failed, sorry! Registration aborted."));
195 object_unref(mu);
196 free(key);
197 return;
198 }
199
200 command_success_nodata(si, _("An email containing nickname activation instructions has been sent to \2%s\2."), mu->email);
201 command_success_nodata(si, _("If you do not complete registration within one day, your nickname will expire."));
202
203 free(key);
204 }
205
206 if (si->su != NULL)
207 {
208 si->su->myuser = mu;
209 n = mowgli_node_create();
210 mowgli_node_add(si->su, n, &mu->logins);
211
212 if (!(mu->flags & MU_WAITAUTH))
213 /* only grant ircd registered status if it's verified */
214 ircd_on_login(si->su, mu, NULL);
215 }
216
217 command_add_flood(si, FLOOD_MODERATE);
218
219 if (!nicksvs.no_nick_ownership && si->su != NULL)
220 logcommand(si, CMDLOG_REGISTER, "REGISTER: \2%s\2 to \2%s\2", account, email);
221 else
222 logcommand(si, CMDLOG_REGISTER, "REGISTER: \2%s\2 to \2%s\2 by \2%s\2", account, email, si->su != NULL ? si->su->nick : get_source_name(si));
223
224 if (is_soper(mu))
225 {
226 wallops("%s registered the nick \2%s\2 and gained services operator privileges.", get_oper_name(si), entity(mu)->name);
227 logcommand(si, CMDLOG_ADMIN, "SOPER: \2%s\2 as \2%s\2", get_oper_name(si), entity(mu)->name);
228 }
229
230 command_success_nodata(si, _("\2%s\2 is now registered to \2%s\2, with the password \2%s\2."), entity(mu)->name, mu->email, pass);
231 hook_call_user_register(mu);
232
233 if (si->su != NULL)
234 {
235 snprintf(lau, BUFSIZE, "%s@%s", si->su->user, si->su->vhost);
236 metadata_add(mu, "private:host:vhost", lau);
237
238 snprintf(lao, BUFSIZE, "%s@%s", si->su->user, si->su->host);
239 metadata_add(mu, "private:host:actual", lao);
240 }
241
242 if (!(mu->flags & MU_WAITAUTH))
243 {
244 req.si = si;
245 req.mu = mu;
246 req.mn = mn;
247 hook_call_user_verify_register(&req);
248 }
249 }
250
251 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
252 * vim:ts=8
253 * vim:sw=8
254 * vim:noexpandtab
255 */