]> jfr.im git - irc/quakenet/newserv.git/blob - 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
1 /* Automatically generated by refactor.pl.
2 *
3 *
4 * CMDNAME: authhistory
5 * CMDLEVEL: QCMD_AUTHED
6 * CMDARGS: 2
7 * CMDDESC: View auth history for an account.
8 * CMDFUNC: csa_doauthhistory
9 * CMDPROTO: int csa_doauthhistory(void *source, int cargc, char **cargv);
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.
13 */
14
15 #include "../chanserv.h"
16 #include "../authlib.h"
17 #include "../../lib/irc_string.h"
18 #include "../../dbapi/dbapi.h"
19
20 #include <stdio.h>
21 #include <string.h>
22
23 struct authhistoryinfo {
24 unsigned int numeric;
25 unsigned int userID;
26 int realhost;
27 };
28
29 void csdb_doauthhistory_real(DBConn *dbconn, void *arg) {
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;
35 DBResult *pgres;
36 int count=0;
37 char tbuf1[TIMELEN], tbuf2[TIMELEN], uhbuf[NICKLEN+HOSTLEN+USERLEN+5];
38
39 if(!dbconn) {
40 free(ahi);
41 return;
42 }
43
44 pgres=dbgetresult(dbconn);
45
46 if (!dbquerysuccessful(pgres)) {
47 Error("chanserv", ERR_ERROR, "Error loading auth history data.");
48 free(ahi);
49 return;
50 }
51
52 if (dbnumfields(pgres) != 7) {
53 Error("chanserv", ERR_ERROR, "Auth history data format error.");
54 dbclear(pgres);
55 free(ahi);
56 return;
57 }
58
59 if (!np) {
60 dbclear(pgres);
61 free(ahi);
62 return;
63 }
64
65 if (!(rup=getreguserfromnick(np))) {
66 dbclear(pgres);
67 free(ahi);
68 return;
69 }
70
71 chanservstdmessage(np, QM_AUTHHISTORYHEADER); /* @TIMELEN */
72 while(dbfetchrow(pgres)) {
73 if (!UHasHelperPriv(rup) && (strtoul(dbgetvalue(pgres, 0), NULL, 10) != rup->ID)) {
74 dbclear(pgres);
75 free(ahi);
76 return;
77 }
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);
83
84 q9strftime(tbuf1, sizeof(tbuf1), ahauthtime);
85 if (ahdisconnecttime)
86 q9strftime(tbuf2, sizeof(tbuf2), ahdisconnecttime);
87
88 snprintf(uhbuf,sizeof(uhbuf),"%s!%s@%s", ahnick, ahuser, ahi->realhost ? ahhost : "(hidden)");
89 chanservsendmessage(np, "#%-2d %-50s %-19s %-19s %s", ++count, uhbuf, tbuf1, ahdisconnecttime?tbuf2:"never", dbgetvalue(pgres,6)); /* @TIMELEN */
90 }
91 chanservstdmessage(np, QM_ENDOFLIST);
92
93 dbclear(pgres);
94 free(ahi);
95 }
96
97 void csdb_retreiveauthhistory(nick *np, reguser *rup, int limit, int realhost) {
98 struct authhistoryinfo *ahi;
99 char limitstr[30];
100
101 if (limit) {
102 sprintf(limitstr, " limit %d",limit);
103 } else {
104 limitstr[0]='\0';
105 }
106
107 ahi=(struct authhistoryinfo *)malloc(sizeof(struct authhistoryinfo));
108 ahi->numeric=np->numeric;
109 ahi->userID=rup->ID;
110 ahi->realhost=realhost;
111 q9a_asyncquery(csdb_doauthhistory_real, (void *)ahi,
112 "SELECT userID, nick, username, host, authtime, disconnecttime, quitreason from chanserv.authhistory where "
113 "userID=%u order by authtime desc%s", rup->ID, limitstr);
114 }
115
116 int csa_doauthhistory(void *source, int cargc, char **cargv) {
117 reguser *rup, *trup;
118 nick *sender=source;
119 unsigned int arg=0;
120 unsigned int limit=10;
121 int realhost;
122
123 if (!(rup=getreguserfromnick(sender)))
124 return CMD_ERROR;
125
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])))
137 return CMD_ERROR;
138
139 /* if target != command issuer */
140 if (trup != rup) {
141 /* only opers and helpers can view authhistory of other users */
142 if (!UHasHelperPriv(rup)) {
143 chanservstdmessage(sender, QM_NOACCESSONUSER, "authhistory", cargv[arg]);
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);
150 chanservstdmessage(sender, QM_NOACCESSONUSER, "authhistory", cargv[arg]);
151 return CMD_ERROR;
152 }
153
154 if(cs_privcheck(QPRIV_VIEWREALHOST, sender)) {
155 realhost = 1;
156 } else {
157 realhost = 0;
158 }
159
160 /* checks passed */
161 chanservwallmessage("%s (%s) used AUTHHISTORY on %s", sender->nick, rup->username, trup->username);
162 } else {
163 realhost = 0;
164 }
165 } else {
166 trup=rup;
167 realhost = 1;
168 }
169
170 csdb_retreiveauthhistory(sender, trup, limit, realhost);
171
172 return CMD_OK;
173 }