fixed ssl.c bug when ssl backend returns IO_BLOCKED but IO engine doesn't get informe...
[ircu2.10.12-pk.git] / ircd / m_settime.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_settime.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 1, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id: m_settime.c 1271 2004-12-11 05:14:07Z klmitch $
24  */
25
26 /*
27  * m_functions execute protocol messages on this server:
28  *
29  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
30  *            structure (with an open socket connected!). This
31  *            identifies the physical socket where the message
32  *            originated (or which caused the m_function to be
33  *            executed--some m_functions may call others...).
34  *
35  *    sptr    is the source of the message, defined by the
36  *            prefix part of the message if present. If not
37  *            or prefix not found, then sptr==cptr.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
63  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64  *            *cannot* be a local connection, unless it's
65  *            actually cptr!). [MyConnect(x) should probably
66  *            be defined as (x == x->from) --msa ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
73  *                    parv[0], sender (prefix string), if not present
74  *                            this points to an empty string.
75  *                    parv[1]...parv[parc-1]
76  *                            pointers to additional parameters
77  *                    parv[parc] == NULL, *always*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #include "config.h"
83
84 #include "client.h"
85 #include "hash.h"
86 #include "ircd.h"
87 #include "ircd_features.h"
88 #include "ircd_log.h"
89 #include "ircd_reply.h"
90 #include "ircd_snprintf.h"
91 #include "ircd_string.h"
92 #include "list.h"
93 #include "msg.h"
94 #include "numeric.h"
95 #include "numnicks.h"
96 #include "s_user.h"
97 #include "send.h"
98 #include "struct.h"
99
100 /* #include <assert.h> -- Now using assert in ircd_log.h */
101 #include <stdlib.h>
102
103 /*
104  * ms_settime - server message handler
105  *
106  * parv[0] = sender prefix
107  * parv[1] = new time
108  * parv[2] = server name (Only used when sptr is an Oper).
109  */
110 int ms_settime(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
111 {
112   time_t t;
113   long dt;
114   static char tbuf[11];
115   struct DLink *lp;
116
117   if (parc < 2) /* verify argument count */
118     return need_more_params(sptr, "SETTIME");
119
120   t = atoi(parv[1]); /* convert time and compute delta */
121   dt = TStime() - t;
122
123   /* verify value */
124   if (t < OLDEST_TS || dt < -9000000)
125   {
126     if (IsServer(sptr)) /* protocol violation if it's from a server */
127       protocol_violation(sptr, "SETTIME: Bad value (%Tu, delta %ld)", t, dt);
128     else
129       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :SETTIME: Bad value (%Tu, "
130                     "delta %ld)", sptr, t, dt);
131       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :SETTIME: Bad value", sptr);
132     return 0;
133   }
134
135   /* reset time... */
136   if (feature_bool(FEAT_RELIABLE_CLOCK))
137   {
138     ircd_snprintf(0, tbuf, sizeof(tbuf), "%Tu", TStime());
139     parv[1] = tbuf;
140   }
141
142   if (BadPtr(parv[2])) /* spam the network */
143   {
144     for (lp = cli_serv(&me)->down; lp; lp = lp->next)
145       if (cptr != lp->value.cptr)
146         sendcmdto_prio_one(sptr, CMD_SETTIME, lp->value.cptr, "%s", parv[1]);
147   }
148   else
149   {
150     if (hunt_server_prio_cmd(sptr, CMD_SETTIME, cptr, 1, "%s %C", 2, parc,
151                              parv) != HUNTED_ISME)
152     {
153       /* If the destination was *not* me, but I'm RELIABLE_CLOCK and the
154        * delta is more than 30 seconds off, bounce back a corrected
155        * SETTIME
156        */
157       if (feature_bool(FEAT_RELIABLE_CLOCK) && (dt > 30 || dt < -30))
158         sendcmdto_prio_one(&me, CMD_SETTIME, cptr, "%s %C", parv[1], cptr);
159       return 0;
160     }
161   }
162
163   if (feature_bool(FEAT_RELIABLE_CLOCK))
164   {
165     /* don't apply settime--reliable */
166     if ((dt > 600) || (dt < -600))
167       sendcmdto_serv_butone(&me, CMD_DESYNCH, 0, ":Bad SETTIME from %s: %Tu "
168                             "(delta %ld)", cli_name(sptr), t, dt);
169     /* Let user know we're ignoring him */
170     if (IsUser(sptr))
171     {
172       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :clock is not set %ld "
173                     "seconds %s : RELIABLE_CLOCK is defined", sptr,
174                     (dt < 0) ? -dt : dt, (dt < 0) ? "forwards" : "backwards");
175     }
176   }
177   else /* tell opers about time change */
178   {
179     sendto_opmask_butone(0, SNO_OLDSNO, "SETTIME from %s, clock is set %ld "
180                          "seconds %s", cli_name(sptr), (dt < 0) ? -dt : dt,
181                          (dt < 0) ? "forwards" : "backwards");
182     /* Apply time change... */
183     TSoffset -= dt;
184     /* Let the issuing user know what we did... */
185     if (IsUser(sptr))
186     {
187       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :clock is set %ld seconds %s",
188                     sptr, (dt < 0) ? -dt : dt,
189                     (dt < 0) ? "forwards" : "backwards");
190     }
191   }
192
193   return 0;
194 }
195
196 /*
197  * mo_settime - oper message handler
198  *
199  * parv[0] = sender prefix
200  * parv[1] = new time
201  * parv[2] = servername (Only used when sptr is an Oper).
202  */
203 int mo_settime(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
204 {
205   time_t t;
206   long dt;
207   static char tbuf[11];
208
209   /* Must be a global oper */
210   if (!IsOper(sptr))
211     return send_reply(sptr, ERR_NOPRIVILEGES);
212
213   if (parc < 2) /* verify argument count */
214     return need_more_params(sptr, "SETTIME");
215
216   if (parc == 2 && MyUser(sptr)) /* default to me */
217     parv[parc++] = cli_name(&me);
218
219   t = atoi(parv[1]); /* convert the time */
220
221   /* If we're reliable_clock or if the oper specified a 0 time, use current */
222   if (!t || feature_bool(FEAT_RELIABLE_CLOCK))
223   {
224     t = TStime();
225     ircd_snprintf(0, tbuf, sizeof(tbuf), "%Tu", TStime());
226     parv[1] = tbuf;
227   }
228
229   dt = TStime() - t; /* calculate the delta */
230
231   if (t < OLDEST_TS || dt < -9000000) /* verify value */
232   {
233     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :SETTIME: Bad value", sptr);
234     return 0;
235   }
236
237   /* OK, send the message off to its destination */
238   if (hunt_server_prio_cmd(sptr, CMD_SETTIME, cptr, 1, "%s %C", 2, parc,
239                            parv) != HUNTED_ISME)
240     return 0;
241
242   if (feature_bool(FEAT_RELIABLE_CLOCK)) /* don't apply settime--reliable */
243   {
244     if ((dt > 600) || (dt < -600))
245       sendcmdto_serv_butone(&me, CMD_DESYNCH, 0, ":Bad SETTIME from %s: %Tu "
246                             "(delta %ld)", cli_name(sptr), t, dt);
247     if (IsUser(sptr)) /* Let user know we're ignoring him */
248       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :clock is not set %ld seconds "
249                     "%s: RELIABLE_CLOCK is defined", sptr, (dt < 0) ? -dt : dt,
250                     (dt < 0) ? "forwards" : "backwards");
251   }
252   else /* tell opers about time change */
253   {
254     sendto_opmask_butone(0, SNO_OLDSNO, "SETTIME from %s, clock is set %ld "
255                          "seconds %s", cli_name(sptr), (dt < 0) ? -dt : dt,
256                          (dt < 0) ? "forwards" : "backwards");
257     TSoffset -= dt; /* apply time change */
258     if (IsUser(sptr)) /* let user know what we did */
259       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :clock is set %ld seconds %s",
260                     sptr, (dt < 0) ? -dt : dt,
261                     (dt < 0) ? "forwards" : "backwards");
262   }
263
264   return 0;
265 }