Debian Codegraph @

modules/nickserv/identify.c

raw
1 /*
2 * Copyright (c) 2005-2006 William Pitcock, et al.
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * This file contains code for the NickServ IDENTIFY and LOGIN functions.
6 *
7 */
8
9 #include "atheme.h"
10
11 /* Check whether we are compiling IDENTIFY or LOGIN */
12 #ifdef NICKSERV_LOGIN
13 #define COMMAND_UC "LOGIN"
14 #define COMMAND_LC "login"
15 #else
16 #define COMMAND_UC "IDENTIFY"
17 #define COMMAND_LC "identify"
18 #endif
19
20 DECLARE_MODULE_V1
21 (
22 "nickserv/" COMMAND_LC, false, _modinit, _moddeinit,
23 PACKAGE_STRING,
24 VENDOR_STRING
25 );
26
27 static void ns_cmd_login(sourceinfo_t *si, int parc, char *parv[]);
28
29 #ifdef NICKSERV_LOGIN
30 command_t ns_login = { "LOGIN", N_("Authenticates to a services account."), AC_NONE, 2, ns_cmd_login, { .path = "nickserv/login" } };
31 #else
32 command_t ns_identify = { "IDENTIFY", N_("Identifies to services for a nickname."), AC_NONE, 2, ns_cmd_login, { .path = "nickserv/identify" } };
33 #endif
34
35 void _modinit(module_t *m)
36 {
37 #ifdef NICKSERV_LOGIN
38 service_named_bind_command("nickserv", &ns_login);
39 #else
40 service_named_bind_command("nickserv", &ns_identify);
41 #endif
42
43 hook_add_event("user_can_login");
44 }
45
46 void _moddeinit(module_unload_intent_t intent)
47 {
48 #ifdef NICKSERV_LOGIN
49 service_named_unbind_command("nickserv", &ns_login);
50 #else
51 service_named_unbind_command("nickserv", &ns_identify);
52 #endif
53 }
54
55 static void ns_cmd_login(sourceinfo_t *si, int parc, char *parv[])
56 {
57 user_t *u = si->su;
58 myuser_t *mu;
59 mowgli_node_t *n, *tn;
60 const char *target = parv[0];
61 const char *password = parv[1];
62 char lau[BUFSIZE];
63 hook_user_login_check_t req;
64
65 if (si->su == NULL)
66 {
67 command_fail(si, fault_noprivs, _("\2%s\2 can only be executed via IRC."), COMMAND_UC);
68 return;
69 }
70
71 #ifndef NICKSERV_LOGIN
72 if (!nicksvs.no_nick_ownership && target && !password)
73 {
74 password = target;
75 target = si->su->nick;
76 }
77 #endif
78
79 if (!target || !password)
80 {
81 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, COMMAND_UC);
82 command_fail(si, fault_needmoreparams, nicksvs.no_nick_ownership ? "Syntax: " COMMAND_UC " <account> <password>" : "Syntax: " COMMAND_UC " [nick] <password>");
83 return;
84 }
85
86 mu = myuser_find_by_nick(target);
87 if (!mu)
88 {
89 command_fail(si, fault_nosuch_target, _("\2%s\2 is not a registered nickname."), target);
90 return;
91 }
92
93 req.si = si;
94 req.mu = mu;
95 req.allowed = true;
96 hook_call_user_can_login(&req);
97 if (!req.allowed)
98 {
99 command_fail(si, fault_authfail, nicksvs.no_nick_ownership ? "You cannot log in as \2%s\2 because the server configuration disallows it."
100 : "You cannot identify to \2%s\2 because the server configuration disallows it.", entity(mu)->name);
101 logcommand(si, CMDLOG_LOGIN, "failed " COMMAND_UC " to \2%s\2 (denied by hook)", entity(mu)->name);
102 return;
103 }
104
105 if (metadata_find(mu, "private:freeze:freezer"))
106 {
107 command_fail(si, fault_authfail, nicksvs.no_nick_ownership ? "You cannot log in as \2%s\2 because the account has been frozen."
108 : "You cannot identify to \2%s\2 because the nickname has been frozen.", entity(mu)->name);
109 logcommand(si, CMDLOG_LOGIN, "failed " COMMAND_UC " to \2%s\2 (frozen)", entity(mu)->name);
110 return;
111 }
112
113 if (mu->flags & MU_NOPASSWORD)
114 {
115 command_fail(si, fault_authfail, _("Password authentication is disabled for this account."));
116 logcommand(si, CMDLOG_LOGIN, "failed " COMMAND_UC " to \2%s\2 (password authentication disabled)", entity(mu)->name);
117 return;
118 }
119
120 if (u->myuser == mu)
121 {
122 command_fail(si, fault_nochange, _("You are already logged in as \2%s\2."), entity(u->myuser)->name);
123 if (mu->flags & MU_WAITAUTH)
124 command_fail(si, fault_nochange, _("Please check your email for instructions to complete your registration."));
125 return;
126 }
127 else if (u->myuser != NULL && !command_find(si->service->commands, "LOGOUT"))
128 {
129 command_fail(si, fault_alreadyexists, _("You are already logged in as \2%s\2."), entity(u->myuser)->name);
130 return;
131 }
132
133 if (verify_password(mu, password))
134 {
135 if (MOWGLI_LIST_LENGTH(&mu->logins) >= me.maxlogins)
136 {
137 command_fail(si, fault_toomany, _("There are already \2%zu\2 sessions logged in to \2%s\2 (maximum allowed: %u)."), MOWGLI_LIST_LENGTH(&mu->logins), entity(mu)->name, me.maxlogins);
138 lau[0] = '\0';
139 MOWGLI_ITER_FOREACH(n, mu->logins.head)
140 {
141 if (lau[0] != '\0')
142 mowgli_strlcat(lau, ", ", sizeof lau);
143 mowgli_strlcat(lau, ((user_t *)n->data)->nick, sizeof lau);
144 }
145 command_fail(si, fault_toomany, _("Logged in nicks are: %s"), lau);
146 logcommand(si, CMDLOG_LOGIN, "failed " COMMAND_UC " to \2%s\2 (too many logins)", entity(mu)->name);
147 return;
148 }
149
150 /* if they are identified to another account, nuke their session first */
151 if (u->myuser)
152 {
153 command_success_nodata(si, _("You have been logged out of \2%s\2."), entity(u->myuser)->name);
154
155 if (ircd_on_logout(u, entity(u->myuser)->name))
156 /* logout killed the user... */
157 return;
158 u->myuser->lastlogin = CURRTIME;
159 MOWGLI_ITER_FOREACH_SAFE(n, tn, u->myuser->logins.head)
160 {
161 if (n->data == u)
162 {
163 mowgli_node_delete(n, &u->myuser->logins);
164 mowgli_node_free(n);
165 break;
166 }
167 }
168 u->myuser = NULL;
169 }
170
171 command_success_nodata(si, nicksvs.no_nick_ownership ? _("You are now logged in as \2%s\2.") : _("You are now identified for \2%s\2."), entity(mu)->name);
172
173 myuser_login(si->service, u, mu, true);
174 logcommand(si, CMDLOG_LOGIN, COMMAND_UC);
175
176 return;
177 }
178
179 logcommand(si, CMDLOG_LOGIN, "failed " COMMAND_UC " to \2%s\2 (bad password)", entity(mu)->name);
180
181 command_fail(si, fault_authfail, _("Invalid password for \2%s\2."), entity(mu)->name);
182 bad_password(si, mu);
183 }
184
185 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
186 * vim:ts=8
187 * vim:sw=8
188 * vim:noexpandtab
189 */