Debian Codegraph @

modules/nickserv/sendpass.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 CService SENDPASS function.
6 *
7 */
8
9 #include "atheme.h"
10
11 DECLARE_MODULE_V1
12 (
13 "nickserv/sendpass", false, _modinit, _moddeinit,
14 PACKAGE_STRING,
15 VENDOR_STRING
16 );
17
18 static void ns_cmd_sendpass(sourceinfo_t *si, int parc, char *parv[]);
19
20 command_t ns_sendpass = { "SENDPASS", N_("Email registration passwords."), PRIV_USER_SENDPASS, 2, ns_cmd_sendpass, { .path = "nickserv/sendpass" } };
21
22 void _modinit(module_t *m)
23 {
24 MODULE_CONFLICT(m, "nickserv/sendpass_user")
25 service_named_bind_command("nickserv", &ns_sendpass);
26 }
27
28 void _moddeinit(module_unload_intent_t intent)
29 {
30 service_named_unbind_command("nickserv", &ns_sendpass);
31 }
32
33 enum specialoperation
34 {
35 op_none,
36 op_force,
37 op_clear
38 };
39
40 static void ns_cmd_sendpass(sourceinfo_t *si, int parc, char *parv[])
41 {
42 myuser_t *mu;
43 char *name = parv[0];
44 char *newpass = NULL;
45 char *key;
46 metadata_t *md;
47 enum specialoperation op = op_none;
48 bool ismarked = false;
49 char cmdtext[NICKLEN + 20];
50 hook_user_needforce_t needforce_hdata;
51
52 if (!name)
53 {
54 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SENDPASS");
55 command_fail(si, fault_needmoreparams, _("Syntax: SENDPASS <account>"));
56 return;
57 }
58
59 if (parc > 1)
60 {
61 if (!strcasecmp(parv[1], "FORCE"))
62 op = op_force;
63 else if (!strcasecmp(parv[1], "CLEAR"))
64 op = op_clear;
65 else
66 {
67 command_fail(si, fault_badparams, STR_INVALID_PARAMS, "SENDPASS");
68 command_fail(si, fault_badparams, _("Syntax: SENDPASS <account> [FORCE|CLEAR]"));
69 return;
70 }
71 }
72
73 if (!(mu = myuser_find_by_nick(name)))
74 {
75 command_fail(si, fault_nosuch_target, _("\2%s\2 is not registered."), name);
76 return;
77 }
78
79 if (is_soper(mu) && !has_priv(si, PRIV_ADMIN))
80 {
81 logcommand(si, CMDLOG_ADMIN, "failed SENDPASS \2%s\2 (is SOPER)", name);
82 command_fail(si, fault_badparams, _("\2%s\2 belongs to a services operator; you need %s privilege to send the password."), name, PRIV_ADMIN);
83 return;
84 }
85
86 if (mu->flags & MU_WAITAUTH)
87 {
88 command_fail(si, fault_badparams, _("\2%s\2 is not verified."), entity(mu)->name);
89 return;
90 }
91
92 if ((md = metadata_find(mu, "private:mark:setter")))
93 {
94 ismarked = true;
95 if (op == op_none)
96 {
97 logcommand(si, CMDLOG_ADMIN, "failed SENDPASS \2%s\2 (marked by \2%s\2)", entity(mu)->name, md->value);
98 command_fail(si, fault_badparams, _("This operation cannot be performed on %s, because the account has been marked by %s."), entity(mu)->name, md->value);
99 if (has_priv(si, PRIV_MARK))
100 {
101 snprintf(cmdtext, sizeof cmdtext, "SENDPASS %s FORCE", entity(mu)->name);
102 command_fail(si, fault_badparams, _("Use %s to override this restriction."), cmdtext);
103 }
104 return;
105 }
106 else if (!has_priv(si, PRIV_MARK))
107 {
108 logcommand(si, CMDLOG_ADMIN, "failed SENDPASS \2%s\2 (marked by \2%s\2)", entity(mu)->name, md->value);
109 command_fail(si, fault_noprivs, STR_NO_PRIVILEGE, PRIV_MARK);
110 return;
111 }
112 }
113
114 needforce_hdata.si = si;
115 needforce_hdata.mu = mu;
116 needforce_hdata.allowed = 1;
117
118 hook_call_user_needforce(&needforce_hdata);
119
120 if (!needforce_hdata.allowed)
121 {
122 ismarked = true;
123 if (op == op_none)
124 {
125 logcommand(si, CMDLOG_ADMIN, "failed SENDPASS \2%s\2 (marked)", entity(mu)->name);
126 command_fail(si, fault_badparams, _("This operation cannot be performed on %s, because the account has been marked."), entity(mu)->name);
127 if (has_priv(si, PRIV_MARK))
128 {
129 snprintf(cmdtext, sizeof cmdtext, "SENDPASS %s FORCE", entity(mu)->name);
130 command_fail(si, fault_badparams, _("Use %s to override this restriction."), cmdtext);
131 }
132 return;
133 }
134 else if (!has_priv(si, PRIV_MARK))
135 {
136 logcommand(si, CMDLOG_ADMIN, "failed SENDPASS \2%s\2 (marked)", entity(mu)->name);
137 command_fail(si, fault_noprivs, STR_NO_PRIVILEGE, PRIV_MARK);
138 return;
139 }
140 }
141
142 if (op == op_clear)
143 {
144 if (metadata_find(mu, "private:setpass:key"))
145 {
146 metadata_delete(mu, "private:setpass:key");
147 metadata_delete(mu, "private:sendpass:sender");
148 metadata_delete(mu, "private:sendpass:timestamp");
149 logcommand(si, CMDLOG_ADMIN, "SENDPASS:CLEAR: \2%s\2", entity(mu)->name);
150 command_success_nodata(si, _("The password change key for \2%s\2 has been cleared."), entity(mu)->name);
151 }
152 else
153 command_fail(si, fault_nochange, _("\2%s\2 did not have a password change key outstanding."), entity(mu)->name);
154 return;
155 }
156
157 if (MOWGLI_LIST_LENGTH(&mu->logins) > 0)
158 {
159 command_fail(si, fault_noprivs, _("This operation cannot be performed on %s, because someone is logged in to it."), entity(mu)->name);
160 return;
161 }
162
163 if (metadata_find(mu, "private:freeze:freezer"))
164 {
165 command_fail(si, fault_noprivs, _("%s has been frozen by the %s administration."), entity(mu)->name, me.netname);
166 return;
167 }
168
169 if (command_find(si->service->commands, "SETPASS"))
170 {
171 if (metadata_find(mu, "private:setpass:key"))
172 {
173 command_fail(si, fault_alreadyexists, _("\2%s\2 already has a password change key outstanding."), entity(mu)->name);
174 command_fail(si, fault_alreadyexists, _("Use SENDPASS %s CLEAR to clear it so that a new one can be sent."), entity(mu)->name);
175 return;
176 }
177
178 if (ismarked)
179 {
180 wallops("%s sent the password for the \2MARKED\2 account %s.", get_oper_name(si), entity(mu)->name);
181 if (md)
182 command_success_nodata(si, _("Overriding MARK placed by %s on the account %s."), md->value, entity(mu)->name);
183 else
184 command_success_nodata(si, _("Overriding MARK on the account %s."), entity(mu)->name);
185 }
186 logcommand(si, CMDLOG_ADMIN, "SENDPASS: \2%s\2 (change key)", name);
187
188 key = random_string(12);
189 metadata_add(mu, "private:sendpass:sender", get_oper_name(si));
190 metadata_add(mu, "private:sendpass:timestamp", number_to_string(time(NULL)));
191
192 if (!sendemail(si->su != NULL ? si->su : si->service->me, mu, EMAIL_SETPASS, mu->email, key))
193 {
194 command_fail(si, fault_emailfail, _("Email send failed."));
195 free(key);
196 return;
197 }
198
199 metadata_add(mu, "private:setpass:key", crypt_string(key, gen_salt()));
200 free(key);
201
202 command_success_nodata(si, _("The password change key for \2%s\2 has been sent to \2%s\2."), entity(mu)->name, mu->email);
203 }
204 else {
205 if (ismarked)
206 {
207 wallops("%s sent the password for the \2MARKED\2 account %s.", get_oper_name(si), entity(mu)->name);
208 if (md)
209 command_success_nodata(si, _("Overriding MARK placed by %s on the account %s."), md->value, entity(mu)->name);
210 else
211 command_success_nodata(si, _("Overriding MARK on the account %s."), entity(mu)->name);
212 }
213 logcommand(si, CMDLOG_ADMIN, "SENDPASS: \2%s\2", name);
214
215 newpass = random_string(12);
216 metadata_add(mu, "private:sendpass:sender", get_oper_name(si));
217 metadata_add(mu, "private:sendpass:timestamp", number_to_string(time(NULL)));
218
219 if (!sendemail(si->su != NULL ? si->su : si->service->me, mu, EMAIL_SENDPASS, mu->email, newpass))
220 {
221 command_fail(si, fault_emailfail, _("Email send failed."));
222 free(newpass);
223 return;
224 }
225
226 set_password(mu, newpass);
227 free(newpass);
228
229 command_success_nodata(si, _("The password for \2%s\2 has been sent to \2%s\2."), entity(mu)->name, mu->email);
230
231 if (mu->flags & MU_NOPASSWORD)
232 {
233 mu->flags &= ~MU_NOPASSWORD;
234 command_success_nodata(si, _("The \2%s\2 flag has been removed for account \2%s\2."), "NOPASSWORD", entity(mu)->name);
235 }
236 }
237 }
238
239 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
240 * vim:ts=8
241 * vim:sw=8
242 * vim:noexpandtab
243 */