Debian Codegraph @

modules/contrib/src/ns_sendpassmail.c

raw
1 /*
2 * Copyright (c) 2015 Atheme Development Group
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * This file contains code for the NicksServ SENDPASSMAIL function.
6 */
7
8 #include "atheme-compat.h"
9
10 struct sendpassmail_state
11 {
12 sourceinfo_t *origin;
13 stringref email_canonical;
14 };
15
16 static bool
17 can_sendpass(sourceinfo_t *si, myuser_t *mu)
18 {
19 if (is_soper(mu) && !has_priv(si, PRIV_ADMIN))
20 {
21 logcommand(si, CMDLOG_ADMIN, "failed SENDPASSMAIL \2%s\2 (\2%s\2) (is SOPER)", entity(mu)->name, mu->email_canonical);
22 return false;
23 }
24
25 if (MOWGLI_LIST_LENGTH(&mu->logins) > 0)
26 {
27 logcommand(si, CMDLOG_ADMIN, "failed SENDPASSMAIL to the logged in account \2%s\2 (\2%s\2)", entity(mu)->name, mu->email_canonical);
28 return false;
29 }
30
31 if (metadata_find(mu, "private:freeze:freezer"))
32 {
33 logcommand(si, CMDLOG_ADMIN, "failed SENDPASSMAIL to the frozen account %s (%s)", entity(mu)->name, mu->email_canonical);
34 return false;
35 }
36
37 if (metadata_find(mu, "private:setpass:key"))
38 {
39 logcommand(si, CMDLOG_ADMIN, "failed SENDPASSMAIL to the account %s (%s) because there is already a key outstanding.", entity(mu)->name, mu->email_canonical);
40 return false;
41 }
42
43 return true;
44 }
45
46 static int
47 sendpassmail_foreach_cb(myentity_t *mt, void *privdata)
48 {
49 struct sendpassmail_state *state = (struct sendpassmail_state *) privdata;
50
51 myuser_t *mu = user(mt);
52 sourceinfo_t *si = state->origin;
53 bool ismarked = false;
54 char *key;
55
56 hook_user_needforce_t needforce_hdata;
57
58 needforce_hdata.si = si;
59 needforce_hdata.mu = mu;
60 needforce_hdata.allowed = 1;
61
62 if (state->email_canonical != mu->email_canonical)
63 {
64 return 0;
65 }
66
67 hook_call_user_needforce(&needforce_hdata);
68
69 if (!needforce_hdata.allowed || metadata_find(mu, "private:mark:setter"))
70 {
71 ismarked = true;
72 }
73
74 if (!can_sendpass(si, mu))
75 {
76 return 0;
77 }
78
79 key = random_string(12);
80
81 const char *const hash = crypt_password(key);
82
83 if (!hash)
84 {
85 command_fail(si, fault_internalerror, _("Hash generation for password change key failed."));
86 sfree(key);
87 return 0;
88 }
89 if (sendemail(si->su != NULL ? si->su : si->service->me, mu, EMAIL_SETPASS, mu->email, key))
90 {
91 if (ismarked)
92 wallops("%s used SENDPASSMAIL for the \2MARKED\2 account %s (%s).", get_oper_name(si), entity(mu)->name, mu->email_canonical);
93
94 logcommand(si, CMDLOG_ADMIN, "SENDPASSMAIL: \2%s\2 (\2%s\2) (change key)", entity(mu)->name, mu->email_canonical);
95 metadata_add(mu, "private:sendpass:sender", get_oper_name(si));
96 metadata_add(mu, "private:sendpass:timestamp", number_to_string(time(NULL)));
97 metadata_add(mu, "private:setpass:key", hash);
98 }
99 else
100 logcommand(si, CMDLOG_ADMIN, "SENDPASSMAIL failed sending email to %s", mu->email_canonical);
101
102 sfree(key);
103 return 0;
104 }
105
106 static void
107 ns_cmd_sendpassmail(sourceinfo_t *si, int parc, char *parv[])
108 {
109 myuser_t *mu;
110 char *email = parv[0];
111 char cmdtext[COMPAT_NICKLEN + 20];
112
113 struct sendpassmail_state state;
114
115 if (!email)
116 {
117 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SENDPASSMAIL");
118 command_fail(si, fault_needmoreparams, _("Syntax: SENDPASSMAIL <email>"));
119 return;
120 }
121
122 state.email_canonical = canonicalize_email(email);
123 state.origin = si;
124
125 if (!validemail(email)) {
126 command_fail(si, fault_badparams, _("\2%s\2 is not a valid email address."), email);
127 return;
128 }
129
130 myentity_foreach_t(ENT_USER, sendpassmail_foreach_cb, &state);
131 strshare_unref(state.email_canonical);
132
133 command_success_nodata(si, _("A password reset email has been sent for all accounts matching address \2%s\2, if any."), email);
134 }
135
136 static command_t ns_sendpassmail = {
137 .name = "SENDPASSMAIL",
138 .desc = N_("Email registration passwords."),
139 .access = AC_NONE,
140 .maxparc = 2,
141 .cmd = &ns_cmd_sendpassmail,
142 .help = { .path = "contrib/ns_sendpassmail" },
143 };
144
145 static void
146 mod_init(module_t *const restrict m)
147 {
148 service_named_bind_command("nickserv", &ns_sendpassmail);
149 }
150
151 static void
152 mod_deinit(const module_unload_intent_t intent)
153 {
154 service_named_unbind_command("nickserv", &ns_sendpassmail);
155 }
156
157 SIMPLE_DECLARE_MODULE_V1("contrib/ns_sendpassmail", MODULE_UNLOAD_CAPABILITY_OK)