]> jfr.im git - irc/quakenet/newserv.git/blame - chanserv/authcmds/authhistory.c
CHANSERV: don't allow non-opers to see last realhosts in AUTHHISTORY / WHOIS
[irc/quakenet/newserv.git] / chanserv / authcmds / authhistory.c
CommitLineData
6b2ca929 1/* Automatically generated by refactor.pl.
2 *
3 *
4 * CMDNAME: authhistory
5 * CMDLEVEL: QCMD_AUTHED
a49f1229 6 * CMDARGS: 2
6b2ca929 7 * CMDDESC: View auth history for an account.
8 * CMDFUNC: csa_doauthhistory
9 * CMDPROTO: int csa_doauthhistory(void *source, int cargc, char **cargv);
50cd26d6 10 * CMDHELP: Usage: AUTHHISTORY
11 * CMDHELP: Displays details of the last 10 logins with your account. Details include
12 * CMDHELP: hostmask, login time, disconnect time and reason.
6b2ca929 13 */
14
15#include "../chanserv.h"
16#include "../authlib.h"
17#include "../../lib/irc_string.h"
ee8cd7d0 18#include "../../dbapi/dbapi.h"
6b2ca929 19
20#include <stdio.h>
21#include <string.h>
6b2ca929 22
23struct authhistoryinfo {
24 unsigned int numeric;
25 unsigned int userID;
ffc0f3ab 26 int realhost;
6b2ca929 27};
28
ee8cd7d0 29void csdb_doauthhistory_real(DBConn *dbconn, void *arg) {
6b2ca929 30 struct authhistoryinfo *ahi=(struct authhistoryinfo*)arg;
31 nick *np=getnickbynumeric(ahi->numeric);
32 reguser *rup;
33 char *ahnick, *ahuser, *ahhost;
34 time_t ahauthtime, ahdisconnecttime;
ee8cd7d0
CP
35 DBResult *pgres;
36 int count=0;
b7eebdcc 37 char tbuf1[TIMELEN], tbuf2[TIMELEN], uhbuf[NICKLEN+HOSTLEN+USERLEN+5];
6b2ca929 38
b3565978
CP
39 if(!dbconn) {
40 free(ahi);
41 return;
42 }
43
ee8cd7d0 44 pgres=dbgetresult(dbconn);
6b2ca929 45
ee8cd7d0 46 if (!dbquerysuccessful(pgres)) {
6b2ca929 47 Error("chanserv", ERR_ERROR, "Error loading auth history data.");
c34af51f 48 free(ahi);
6b2ca929 49 return;
50 }
51
ee8cd7d0 52 if (dbnumfields(pgres) != 7) {
6b2ca929 53 Error("chanserv", ERR_ERROR, "Auth history data format error.");
ee8cd7d0 54 dbclear(pgres);
c34af51f 55 free(ahi);
6b2ca929 56 return;
57 }
58
6b2ca929 59 if (!np) {
ee8cd7d0 60 dbclear(pgres);
6b2ca929 61 free(ahi);
62 return;
63 }
64
65 if (!(rup=getreguserfromnick(np))) {
ee8cd7d0 66 dbclear(pgres);
6b2ca929 67 free(ahi);
68 return;
69 }
ffc0f3ab 70
b7eebdcc 71 chanservstdmessage(np, QM_AUTHHISTORYHEADER); /* @TIMELEN */
ee8cd7d0
CP
72 while(dbfetchrow(pgres)) {
73 if (!UHasHelperPriv(rup) && (strtoul(dbgetvalue(pgres, 0), NULL, 10) != rup->ID)) {
74 dbclear(pgres);
6b2ca929 75 free(ahi);
76 return;
77 }
ee8cd7d0
CP
78 ahnick=dbgetvalue(pgres, 1);
79 ahuser=dbgetvalue(pgres, 2);
80 ahhost=dbgetvalue(pgres, 3);
81 ahauthtime=strtoul(dbgetvalue(pgres, 4), NULL, 10);
82 ahdisconnecttime=strtoul(dbgetvalue(pgres, 5), NULL, 10);
b7eebdcc
CP
83
84 q9strftime(tbuf1, sizeof(tbuf1), ahauthtime);
85 if (ahdisconnecttime)
86 q9strftime(tbuf2, sizeof(tbuf2), ahdisconnecttime);
87
ffc0f3ab 88 snprintf(uhbuf,sizeof(uhbuf),"%s!%s@%s", ahnick, ahuser, ahi->realhost ? ahhost : "(hidden)");
b7eebdcc 89 chanservsendmessage(np, "#%-2d %-50s %-19s %-19s %s", ++count, uhbuf, tbuf1, ahdisconnecttime?tbuf2:"never", dbgetvalue(pgres,6)); /* @TIMELEN */
6b2ca929 90 }
91 chanservstdmessage(np, QM_ENDOFLIST);
92
ee8cd7d0 93 dbclear(pgres);
6b2ca929 94 free(ahi);
95}
96
ffc0f3ab 97void csdb_retreiveauthhistory(nick *np, reguser *rup, int limit, int realhost) {
6b2ca929 98 struct authhistoryinfo *ahi;
a49f1229 99 char limitstr[30];
100
101 if (limit) {
102 sprintf(limitstr, " limit %d",limit);
103 } else {
104 limitstr[0]='\0';
105 }
6b2ca929 106
107 ahi=(struct authhistoryinfo *)malloc(sizeof(struct authhistoryinfo));
108 ahi->numeric=np->numeric;
109 ahi->userID=rup->ID;
ffc0f3ab 110 ahi->realhost=realhost;
b3565978 111 q9a_asyncquery(csdb_doauthhistory_real, (void *)ahi,
522e2b8c 112 "SELECT userID, nick, username, host, authtime, disconnecttime, quitreason from chanserv.authhistory where "
a49f1229 113 "userID=%u order by authtime desc%s", rup->ID, limitstr);
6b2ca929 114}
115
116int csa_doauthhistory(void *source, int cargc, char **cargv) {
117 reguser *rup, *trup;
118 nick *sender=source;
a49f1229 119 unsigned int arg=0;
120 unsigned int limit=10;
ffc0f3ab
CP
121 int realhost;
122
6b2ca929 123 if (!(rup=getreguserfromnick(sender)))
124 return CMD_ERROR;
125
a49f1229 126 if (cargc) {
127 if (!ircd_strcmp(cargv[0], "-a")) {
128 if (UHasOperPriv(rup))
129 limit=0;
130
131 arg++;
132 }
133 }
134
135 if (cargc > arg) {
136 if (!(trup=findreguser(sender, cargv[arg])))
6b2ca929 137 return CMD_ERROR;
138
6a127402
CP
139 /* if target != command issuer */
140 if (trup != rup) {
141 /* only opers and helpers can view authhistory of other users */
142 if (!UHasHelperPriv(rup)) {
063c7d26 143 chanservstdmessage(sender, QM_NOACCESSONUSER, "authhistory", cargv[arg]);
6a127402
CP
144 return CMD_ERROR;
145 }
146
147 /* and only opers can view opers history */
148 if (UHasOperPriv(trup) && !UHasOperPriv(rup)) {
149 chanservwallmessage("%s (%s) just FAILED using AUTHHISTORY on %s", sender->nick, rup->username, trup->username);
063c7d26 150 chanservstdmessage(sender, QM_NOACCESSONUSER, "authhistory", cargv[arg]);
6a127402
CP
151 return CMD_ERROR;
152 }
153
ffc0f3ab
CP
154 if(cs_privcheck(QPRIV_VIEWREALHOST, sender)) {
155 realhost = 1;
156 } else {
157 realhost = 0;
158 }
159
6a127402 160 /* checks passed */
6a127402 161 chanservwallmessage("%s (%s) used AUTHHISTORY on %s", sender->nick, rup->username, trup->username);
ffc0f3ab
CP
162 } else {
163 realhost = 0;
6b2ca929 164 }
1482fb78 165 } else {
6b2ca929 166 trup=rup;
ffc0f3ab 167 realhost = 1;
1482fb78 168 }
063c7d26 169
ffc0f3ab 170 csdb_retreiveauthhistory(sender, trup, limit, realhost);
6b2ca929 171
172 return CMD_OK;
173}