b0b94413d8775b12be6b723d773026456697f54c
[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$
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 #if 0
85 /*
86  * No need to include handlers.h here the signatures must match
87  * and we don't need to force a rebuild of all the handlers everytime
88  * we add a new one to the list. --Bleep
89  */
90 #include "handlers.h"
91 #endif /* 0 */
92 #include "client.h"
93 #include "hash.h"
94 #include "ircd.h"
95 #include "ircd_features.h"
96 #include "ircd_reply.h"
97 #include "ircd_string.h"
98 #include "list.h"
99 #include "msg.h"
100 #include "numeric.h"
101 #include "numnicks.h"
102 #include "s_user.h"
103 #include "send.h"
104 #include "struct.h"
105
106 #include <assert.h>
107 #include <stdlib.h>
108
109 /*
110  * ms_settime - server message handler
111  *
112  * parv[0] = sender prefix
113  * parv[1] = new time
114  * parv[2] = servername (Only used when sptr is an Oper).
115  */
116 int ms_settime(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
117 {
118   time_t t;
119   long int dt;
120   static char tbuf[11];
121   struct DLink *lp;
122
123   if (!IsPrivileged(sptr))
124     return 0;
125
126   if (parc < 2)
127     return need_more_params(sptr, "SETTIME");
128
129   if (parc == 2 && MyUser(sptr))
130     parv[parc++] = cli_name(&me);
131
132   t = atoi(parv[1]);
133   dt = TStime() - t;
134
135   if (t < OLDEST_TS || dt < -9000000)
136   {
137     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :SETTIME: Bad value", sptr);
138     return 0;
139   }
140
141   if (IsServer(sptr))           /* send to unlagged servers */
142   {
143     if (feature_bool(FEAT_RELIABLE_CLOCK)) {
144       ircd_snprintf(0, tbuf, sizeof(tbuf), "%Tu", TStime());
145       parv[1] = tbuf;
146     }
147
148     for (lp = cli_serv(&me)->down; lp; lp = lp->next)
149       if (cptr != lp->value.cptr && MsgQLength(&(cli_sendQ(lp->value.cptr))) < 8000)
150         sendcmdto_one(sptr, CMD_NOTICE, lp->value.cptr, "%s", parv[1]);
151   }
152   else
153   {
154     ircd_snprintf(0, tbuf, sizeof(tbuf), "%Tu", TStime());
155     parv[1] = tbuf;
156     if (hunt_server_cmd(sptr, CMD_SETTIME, cptr, 1, "%s %C", 2, parc, parv) !=
157         HUNTED_ISME)
158       return 0;
159   }
160
161   if (feature_bool(FEAT_RELIABLE_CLOCK)) {
162     if ((dt > 600) || (dt < -600))
163       sendcmdto_serv_butone(&me, CMD_WALLOPS, 0, ":Bad SETTIME from %s: %Tu",
164                             cli_name(sptr), t);
165     if (IsUser(sptr)) {
166       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :clock is not set %ld "
167                     "seconds %s : RELIABLE_CLOCK is defined", sptr,
168                     (dt < 0) ? -dt : dt, (dt < 0) ? "forwards" : "backwards");
169     }
170   } else {
171     sendto_opmask_butone(0, SNO_OLDSNO, "SETTIME from %s, clock is set %ld "
172                          "seconds %s", cli_name(sptr), (dt < 0) ? -dt : dt,
173                          (dt < 0) ? "forwards" : "backwards");
174     TSoffset -= dt;
175     if (IsUser(sptr)) {
176       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :clock is set %ld seconds %s",
177                     sptr, (dt < 0) ? -dt : dt,
178                     (dt < 0) ? "forwards" : "backwards");
179     }
180   }
181
182   return 0;
183 }
184
185 /*
186  * mo_settime - oper message handler
187  *
188  * parv[0] = sender prefix
189  * parv[1] = new time
190  * parv[2] = servername (Only used when sptr is an Oper).
191  */
192 int mo_settime(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
193 {
194   time_t t;
195   long int dt;
196   static char tbuf[11];
197   struct DLink *lp;
198
199   if (!IsPrivileged(sptr))
200     return 0;
201
202   if (parc < 2)
203     return need_more_params(sptr, "SETTIME");
204
205   if (parc == 2 && MyUser(sptr))
206     parv[parc++] = cli_name(&me);
207
208   t = atoi(parv[1]);
209   dt = TStime() - t;
210
211   if (t < OLDEST_TS || dt < -9000000)
212   {
213     sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :SETTIME: Bad value", sptr);
214     return 0;
215   }
216
217   if (IsServer(sptr))           /* send to unlagged servers */
218   {
219     if (feature_bool(FEAT_RELIABLE_CLOCK)) {
220       ircd_snprintf(0, tbuf, sizeof(tbuf), "%Tu", TStime());
221       parv[1] = tbuf;
222     }
223
224     for (lp = cli_serv(&me)->down; lp; lp = lp->next)
225       if (cptr != lp->value.cptr && MsgQLength(&(cli_sendQ(lp->value.cptr))) < 8000)
226         sendcmdto_one(sptr, CMD_SETTIME, lp->value.cptr, "%s", parv[1]);
227   }
228   else
229   {
230     ircd_snprintf(0, tbuf, sizeof(tbuf), "%Tu", TStime());
231     parv[1] = tbuf;
232     if (hunt_server_cmd(sptr, CMD_SETTIME, cptr, 1, "%s %C", 2, parc, parv) !=
233         HUNTED_ISME)
234       return 0;
235   }
236
237   if (feature_bool(FEAT_RELIABLE_CLOCK)) {
238     if ((dt > 600) || (dt < -600))
239       sendcmdto_serv_butone(&me, CMD_WALLOPS, 0, ":Bad SETTIME from %s: %Tu",
240                             cli_name(sptr), t);
241     if (IsUser(sptr)) {
242       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :clock is not set %ld "
243                     "seconds %s : RELIABLE_CLOCK is defined", sptr,
244                     (dt < 0) ? -dt : dt, (dt < 0) ? "forwards" : "backwards");
245     }
246   } else {
247     sendto_opmask_butone(0, SNO_OLDSNO, "SETTIME from %s, clock is set %ld "
248                          "seconds %s", cli_name(sptr), (dt < 0) ? -dt : dt,
249                          (dt < 0) ? "forwards" : "backwards");
250     TSoffset -= dt;
251     if (IsUser(sptr)) {
252       sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :clock is set %ld seconds %s",
253                     sptr, (dt < 0) ? -dt : dt,
254                     (dt < 0) ? "forwards" : "backwards");
255     }
256   }
257
258   return 0;
259 }
260
261   
262 #if 0
263 /*
264  * m_settime
265  *
266  * parv[0] = sender prefix
267  * parv[1] = new time
268  * parv[2] = servername (Only used when sptr is an Oper).
269  */
270 int m_settime(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
271 {
272   time_t t;
273   long int dt;
274   static char tbuf[11];
275   struct DLink *lp;
276
277   if (!IsPrivileged(sptr))
278     return 0;
279
280   if (parc < 2)
281     return need_more_params(sptr, "SETTIME");
282
283   if (parc == 2 && MyUser(sptr))
284     parv[parc++] = me.name;
285
286   t = atoi(parv[1]);
287   dt = TStime() - t;
288
289   if (t < OLDEST_TS || dt < -9000000)
290   {
291     sendto_one(sptr, ":%s NOTICE %s :SETTIME: Bad value", me.name, parv[0]); /* XXX DEAD */
292     return 0;
293   }
294
295   if (IsServer(sptr))           /* send to unlagged servers */
296   {
297 #ifdef RELIABLE_CLOCK
298     sprintf_irc(tbuf, TIME_T_FMT, TStime());
299     parv[1] = tbuf;
300 #endif
301     for (lp = me.serv->down; lp; lp = lp->next)
302       if (cptr != lp->value.cptr && DBufLength(&lp->value.cptr->sendQ) < 8000) /* XXX DEAD */
303         sendto_one(lp->value.cptr, ":%s SETTIME %s", parv[0], parv[1]); /* XXX DEAD */
304   }
305   else
306   {
307     sprintf_irc(tbuf, TIME_T_FMT, TStime());
308     parv[1] = tbuf;
309     if (hunt_server(1, cptr, sptr, "%s%s " TOK_SETTIME " %s %s", 2, parc, parv) != /* XXX DEAD */
310         HUNTED_ISME)
311       return 0;
312   }
313
314 #ifdef RELIABLE_CLOCK
315   if ((dt > 600) || (dt < -600))
316     sendto_serv_butone(0, ":%s " TOK_WALLOPS " :Bad SETTIME from %s: " TIME_T_FMT, /* XXX DEAD */
317                        me.name, sptr->name, t);
318   if (IsUser(sptr))
319   {
320     if (MyUser(sptr) || Protocol(cptr) < 10)
321       sendto_one(sptr, ":%s NOTICE %s :clock is not set %ld seconds %s : " /* XXX DEAD */
322                  "RELIABLE_CLOCK is defined", me.name, parv[0],
323                  (dt < 0) ? -dt : dt, (dt < 0) ? "forwards" : "backwards");
324     else
325       sendto_one(sptr, "%s NOTICE %s%s :clock is not set %ld seconds %s : " /* XXX DEAD */
326                  "RELIABLE_CLOCK is defined", NumServ(&me), NumNick(sptr),
327                  (dt < 0) ? -dt : dt, (dt < 0) ? "forwards" : "backwards");
328   }
329 #else
330   sendto_ops("SETTIME from %s, clock is set %ld seconds %s", /* XXX DEAD */
331              sptr->name, (dt < 0) ? -dt : dt,
332              (dt < 0) ? "forwards" : "backwards");
333   TSoffset -= dt;
334   if (IsUser(sptr))
335   {
336     if (MyUser(sptr) || Protocol(cptr) < 10)
337       sendto_one(sptr, ":%s NOTICE %s :clock is set %ld seconds %s", me.name, /* XXX DEAD */
338                  parv[0], (dt < 0) ? -dt : dt, (dt < 0) ? "forwards" : "backwards");
339     else
340       sendto_one(sptr, "%s NOTICE %s%s :clock is set %ld seconds %s", /* XXX DEAD */
341                  NumServ(&me), NumNick(sptr),
342                  (dt < 0) ? -dt : dt, (dt < 0) ? "forwards" : "backwards");
343   }
344 #endif
345   return 0;
346 }
347 #endif /* 0 */