Debian Codegraph @

modules/nickserv/setpass.c

raw
1 /*
2 * Copyright (c) 2007 Atheme Development Group
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * This file contains code for the NickServ SETPASS function.
6 *
7 */
8
9 #include "atheme.h"
10
11 DECLARE_MODULE_V1
12 (
13 "nickserv/setpass", false, _modinit, _moddeinit,
14 PACKAGE_STRING,
15 VENDOR_STRING
16 );
17
18 static void clear_setpass_key(user_t *u);
19 static void ns_cmd_setpass(sourceinfo_t *si, int parc, char *parv[]);
20 static void show_setpass(hook_user_req_t *hdata);
21
22 command_t ns_setpass = { "SETPASS", N_("Changes a password using an authcode."), AC_NONE, 3, ns_cmd_setpass, { .path = "nickserv/setpass" } };
23
24 void _modinit(module_t *m)
25 {
26 hook_add_event("user_identify");
27 hook_add_user_identify(clear_setpass_key);
28 hook_add_event("user_info");
29 hook_add_user_info(show_setpass);
30 service_named_bind_command("nickserv", &ns_setpass);
31 }
32
33 void _moddeinit(module_unload_intent_t intent)
34 {
35 hook_del_user_identify(clear_setpass_key);
36 hook_del_user_info(show_setpass);
37 service_named_unbind_command("nickserv", &ns_setpass);
38 }
39
40 static void ns_cmd_setpass(sourceinfo_t *si, int parc, char *parv[])
41 {
42 myuser_t *mu;
43 metadata_t *md;
44 char *nick = parv[0];
45 char *key = parv[1];
46 char *password = parv[2];
47
48 if (!nick || !key || !password)
49 {
50 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SETPASS");
51 command_fail(si, fault_needmoreparams, _("Syntax: SETPASS <account> <key> <newpass>"));
52 return;
53 }
54
55 if (strchr(password, ' '))
56 {
57 command_fail(si, fault_badparams, STR_INVALID_PARAMS, "SETPASS");
58 command_fail(si, fault_badparams, _("Syntax: SETPASS <account> <key> <newpass>"));
59 return;
60 }
61
62 if (!(mu = myuser_find(nick)))
63 {
64 command_fail(si, fault_nosuch_target, _("\2%s\2 is not registered."), nick);
65 return;
66 }
67
68 if (si->smu == mu)
69 {
70 command_fail(si, fault_already_authed, _("You are logged in and can change your password using the SET PASSWORD command."));
71 return;
72 }
73
74 if (strlen(password) >= PASSLEN)
75 {
76 command_fail(si, fault_badparams, STR_INVALID_PARAMS, "SETPASS");
77 command_fail(si, fault_badparams, _("Registration passwords may not be longer than \2%d\2 characters."), PASSLEN - 1);
78 return;
79 }
80
81 if (!strcasecmp(password, entity(mu)->name))
82 {
83 command_fail(si, fault_badparams, _("You cannot use your nickname as a password."));
84 command_fail(si, fault_badparams, _("Syntax: SETPASS <account> <key> <newpass>"));
85 return;
86 }
87
88 md = metadata_find(mu, "private:setpass:key");
89 if (md == NULL || crypt_verify_password(key, md->value) == NULL)
90 {
91 if (md != NULL)
92 logcommand(si, CMDLOG_SET, "failed SETPASS (invalid key)");
93 command_fail(si, fault_badparams, _("Verification failed. Invalid key for \2%s\2."), entity(mu)->name);
94 return;
95 }
96
97 logcommand(si, CMDLOG_SET, "SETPASS: \2%s\2", entity(mu)->name);
98
99 metadata_delete(mu, "private:setpass:key");
100 metadata_delete(mu, "private:sendpass:sender");
101 metadata_delete(mu, "private:sendpass:timestamp");
102
103 set_password(mu, password);
104 command_success_nodata(si, _("The password for \2%s\2 has been changed to \2%s\2."), entity(mu)->name, password);
105
106 if (mu->flags & MU_NOPASSWORD)
107 {
108 mu->flags &= ~MU_NOPASSWORD;
109 command_success_nodata(si, _("The \2%s\2 flag has been removed for account \2%s\2."), "NOPASSWORD", entity(mu)->name);
110 }
111 }
112
113 static void clear_setpass_key(user_t *u)
114 {
115 myuser_t *mu = u->myuser;
116
117 if (!metadata_find(mu, "private:setpass:key"))
118 return;
119
120 metadata_delete(mu, "private:setpass:key");
121 metadata_delete(mu, "private:sendpass:sender");
122 metadata_delete(mu, "private:sendpass:timestamp");
123
124 notice(nicksvs.nick, u->nick, "Warning: SENDPASS had been used to mail you a password recovery "
125 "key. Since you have identified, that key is no longer valid.");
126 }
127
128 static void show_setpass(hook_user_req_t *hdata)
129 {
130 if (has_priv(hdata->si, PRIV_USER_AUSPEX))
131 {
132 if (metadata_find(hdata->mu, "private:setpass:key"))
133 command_success_nodata(hdata->si, "%s has an active password reset key", entity(hdata->mu)->name);
134
135 metadata_t *md;
136 char strfbuf[BUFSIZE];
137
138 if ((md = metadata_find(hdata->mu, "private:sendpass:sender")) != NULL)
139 {
140 const char *sender = md->value;
141 time_t ts;
142 struct tm tm;
143
144 md = metadata_find(hdata->mu, "private:sendpass:timestamp");
145 ts = md != NULL ? atoi(md->value) : 0;
146
147 tm = *localtime(&ts);
148 strftime(strfbuf, sizeof strfbuf, TIME_FORMAT, &tm);
149
150 command_success_nodata(hdata->si, _("%s was \2SENDPASSED\2 by %s on %s"), entity(hdata->mu)->name, sender, strfbuf);
151 }
152 }
153 }