admin.c 9 KB
Newer Older
Linus Tolke's avatar
Linus Tolke committed
1
/*
2
 * $Id: admin.c,v 0.25 1998/06/14 14:50:34 byers Exp $
Per Cederqvist's avatar
Per Cederqvist committed
3
 * Copyright (C) 1991, 1993, 1994, 1995, 1996  Lysator Academic Computer Association.
Linus Tolke's avatar
Linus Tolke committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 1, or (at your option) 
 * any later version.
 * 
 * LysKOM is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */
Per Cederqvist's avatar
Per Cederqvist committed
25
26
27
28
29
/*
 * admin.c
 *
 * Administrative calls.
 */
Per Cederqvist's avatar
Per Cederqvist committed
30

31
static char *rcsid = "$Id: admin.c,v 0.25 1998/06/14 14:50:34 byers Exp $";
32
33
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
34

Per Cederqvist's avatar
Per Cederqvist committed
35
36
#include <stdio.h>
#include <setjmp.h>
37
38
39
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
40
41
#include <sys/types.h>
#include <time.h>
Per Cederqvist's avatar
Per Cederqvist committed
42
43

#include "misc-types.h"
44
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
45
#include "kom-types.h"
Per Cederqvist's avatar
Per Cederqvist committed
46
47
#include "manipulate.h"
#include "com.h"
48
#include "async.h"
Per Cederqvist's avatar
Per Cederqvist committed
49
#include "connections.h"
Per Cederqvist's avatar
Per Cederqvist committed
50
51
#include "kom-errno.h"
#include "cache.h"
Per Cederqvist's avatar
Per Cederqvist committed
52
#include "config.h"
Per Cederqvist's avatar
Per Cederqvist committed
53
54
#include "log.h"
#include "send-async.h"
55
#include "param.h"
56
#include "string-malloc.h"
57
58
#include "services.h"
#include "version-info.h"
59
#include "aux-items.h"
Per Cederqvist's avatar
Per Cederqvist committed
60

Per Cederqvist's avatar
Per Cederqvist committed
61
62
63
64
65
66
/* All of the fields in this structure except the version number
   is set from the configuration file at startup (see ramkomd.c).
   The default values are also set in ramkomd.c; the values below are
   never used.  The defaults are as of this writing (1994-01-11
   19:57:22) equal to the values below, but don't trust that that is
   so when you read this. */
67
68
69

/* Actually, these values are stored in the database */

Per Cederqvist's avatar
Per Cederqvist committed
70
71
72
73
74
75
76
77
Info kom_info = 
{
#include "version.incl"
    ,				/* version */
    1,				/* conf_pres_conf */
    2,				/* pers_pres_conf */
    3,				/* motd_conf */
    4,				/* kom_news_conf */
78
79
80
    0,				/* motd_of_lyskom */
    0,                          /* highest_aux_no */
    { 0, NULL }                 /* aux_item_list */
Per Cederqvist's avatar
Per Cederqvist committed
81
82
83
84
};

/*
 * Return info about this server. This can (and should) be done
85
 * before logging in. motd_of_lyskom should be displayed before
Per Cederqvist's avatar
Per Cederqvist committed
86
87
 * prompting for username if it isn't 0.
 */
88
89
90
91
92
93
94
extern Success
get_info_old( Info *result )
{
    *result = kom_info;
    return OK;
}

Per Cederqvist's avatar
Per Cederqvist committed
95
96
97
extern Success
get_info( Info *result )
{
98
99
    Aux_item_list   filtered;

Per Cederqvist's avatar
Per Cederqvist committed
100
    *result = kom_info;
101
102
103
104
105
    filter_aux_item_list(&result->aux_item_list,
                         &filtered,
                         ACTPERS,
                         ACT_P);
    result->aux_item_list = filtered;
Per Cederqvist's avatar
Per Cederqvist committed
106
107
108
    return OK;
}

109
110
111
112
113
114
115
116
117
118
extern Success
get_version_info( Version_info *result )
{
    /* Allowed before login. */
    result->protocol_version = kom_version_info.protocol_version;
    result->server_name = s_fcrea_str(kom_version_info.server_name);
    result->server_version = s_fcrea_str(kom_version_info.server_version);
    return OK;
}

David Byers's avatar
David Byers committed
119
120
121
122
123
124
125
126
127
128
129
/* /// */
extern Success
set_info(Info *info)
{
    Conference     *conf;
    Text_stat      *text;
    Success         tmp;

    CHK_LOGIN(FAILURE);
    if ( !ENA(admin, 1) )
    {
130
        err_stat = 0;
David Byers's avatar
David Byers committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
	kom_errno = KOM_PERM;
	return FAILURE;
    }

    /* Check that everything mentioned exists */

    GET_T_STAT(text, info->motd_of_lyskom, FAILURE);
    GET_C_STAT(conf, info->conf_pres_conf, FAILURE);
    GET_C_STAT(conf, info->pers_pres_conf, FAILURE);
    GET_C_STAT(conf, info->motd_conf, FAILURE);
    GET_C_STAT(conf, info->kom_news_conf, FAILURE);

    if ((tmp = set_motd_of_lyskom(info->motd_of_lyskom)) != OK)
        return tmp;

    kom_info.conf_pres_conf = info->conf_pres_conf;
    kom_info.pers_pres_conf = info->pers_pres_conf;
    kom_info.motd_conf = info->motd_conf;
    kom_info.kom_news_conf = info->kom_news_conf;

    return OK;
}

Per Cederqvist's avatar
Per Cederqvist committed
154
155
156
157
158
159
160
161
162
163
164
/* /// */
extern Success
set_motd_of_lyskom (Text_no motd)
{
    Text_stat *old_motd = NULL;
    Text_stat *new_motd = NULL;

    CHK_LOGIN(FAILURE);

    if ( !ENA(admin, 1) )
    {
165
        err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
166
167
168
169
170
171
172
173
174
	kom_errno = KOM_PERM;
	return FAILURE;
    }
    
    /* Check that the new motd exists before deleting the old*/

    if ( motd != 0 )
    {
	GET_T_STAT(new_motd, motd, FAILURE);
175
	if ( new_motd->no_of_marks >= param.max_marks_text )
Per Cederqvist's avatar
Per Cederqvist committed
176
	{
177
178
	    log("LIMIT: set_motd_of_lyskom(%lu): New motd has %d marks.\n",
		(unsigned long)motd, new_motd->no_of_marks);
179
            err_stat = motd;
180
	    kom_errno = KOM_MARK_LIMIT;
Per Cederqvist's avatar
Per Cederqvist committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
	    return FAILURE;
	}
    }
    
    /* Unmark the previous motd if it exists. */

    if ( kom_info.motd_of_lyskom != 0
	&& (old_motd = cached_get_text_stat(kom_info.motd_of_lyskom)) != NULL)
    {
	if ( old_motd->no_of_marks > 0 )
	{
	    --old_motd->no_of_marks;
	    mark_text_as_changed( kom_info.motd_of_lyskom );
	}
	else
	{
	    log("ERROR: do_set_motd(): Old motd not marked\n");
	}
    }

    /* Mark the new motd */

    if ( motd != 0 )
    {
	++new_motd->no_of_marks;
	mark_text_as_changed( motd );
    }
    
    kom_info.motd_of_lyskom = motd;

    return OK;
}




/*
 * Force all clients to read a message.
 * Sends an asynchronous message. This is obsoleted by send_message.
 */
extern Success
222
broadcast (const String message)
Per Cederqvist's avatar
Per Cederqvist committed
223
{
224
    return send_message(0, message);
Per Cederqvist's avatar
Per Cederqvist committed
225
226
227
228
229
230
}

/*
 * Send a message
 */
extern Success
231
send_message (Conf_no recipient,
Per Cederqvist's avatar
Per Cederqvist committed
232
233
	      const String message)
{
234
235
236
237
238
    Conference *conf_c;
    unsigned short end;
    unsigned short ix;
    Success retval;

Per Cederqvist's avatar
Per Cederqvist committed
239
240
    CHK_LOGIN(FAILURE);

241
    if (s_strlen(message) > param.broadcast_len)
242
    {
243
        err_stat = 0;
244
245
246
247
	kom_errno = KOM_LONG_STR;
	return FAILURE;
    }

248
249
    if (recipient != 0 && cached_conf_exists(recipient) == FALSE)
    {
250
        err_stat = recipient;
251
252
253
254
255
	kom_errno = KOM_UNDEF_CONF;
	return FAILURE;
    }
    
    if (recipient == 0 || cached_get_conf_type(recipient).letter_box == 1)
256
257
258
259
260
	return async_send_message(recipient,
                                  ACTPERS,
                                  message,
                                  ( recipient != 0 ||
                                    ENA(admin, 1) ));
261
262
263
264
    else
    {
	/* The recipient is a conference.  Send the message to all
	   members of that conference. */
Per Cederqvist's avatar
Per Cederqvist committed
265

266
267
268
269
270
	GET_C_STAT(conf_c, recipient, FAILURE);
	end = conf_c->members.no_of_members;
	retval = FAILURE;
	for (ix = 0; ix < end; ix++)
	{
271
272
273
            if (conf_c->members.members[ix].type.passive)
                continue;

274
	    if (async_send_group_message(conf_c->members.members[ix].member,
275
276
277
278
					 recipient,
                                         ACTPERS,
                                         message,
                                         ENA(admin, 1)) == OK) 
279
280
281
282
283
284
285
	    {
		retval = OK;
	    }
	}
	return retval;
    }
}
Per Cederqvist's avatar
Per Cederqvist committed
286
287
288
289
290
291


/*
 * Make LysKOM sync its files.
 */
extern Success
Per Cederqvist's avatar
Per Cederqvist committed
292
sync_kom (void) 
Per Cederqvist's avatar
Per Cederqvist committed
293
{
David Byers's avatar
David Byers committed
294
295
296
297
298
    if (!param.permissive_sync)
    {
        CHK_LOGIN(FAILURE);
        if ( !ENA(admin, 1) )
        {
299
            err_stat = 0;
David Byers's avatar
David Byers committed
300
301
302
303
304
            kom_errno = KOM_PERM;
            return FAILURE;
        }
    }

Per Cederqvist's avatar
Per Cederqvist committed
305
306
307
308
309
310
311
312
313
    cache_sync_all();
    dump_statistics();
    return OK;
}

/*
 * Close LysKOM. exit_val is (currently) not used. The database is synced.
 */
extern Success
Per Cederqvist's avatar
Per Cederqvist committed
314
shutdown_kom (int exit_val) 
Per Cederqvist's avatar
Per Cederqvist committed
315
{
316
317
318
319
    char *name;
    char *user;
    char *host;

Per Cederqvist's avatar
Per Cederqvist committed
320
321
322
    CHK_LOGIN(FAILURE);
    if ( !ENA(admin, 1) )
    {
323
        err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
324
325
326
327
	kom_errno = KOM_PERM;
	return FAILURE;
    }

328
329
330
331
332
333
334
335
336
    name = s_crea_c_str (active_connection->username);
    user = s_crea_c_str (active_connection->ident_user);
    host = s_crea_c_str (active_connection->hostname);
    log ("shutdown initiated by person %d (%s) via %s@%s.\n", 
	 ACTPERS, name, user, host);
    string_free(host);
    string_free(user);
    string_free(name);
    
Per Cederqvist's avatar
Per Cederqvist committed
337
338
339
    go_and_die = TRUE;
    return OK;
}
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357

extern Success
modify_server_info(Number_list      *items_to_delete,
                   Aux_item_list    *items_to_add)
{
    CHK_LOGIN(FAILURE);
    if ( !ENA(admin, 1) )
    {
        err_stat = 0;
	kom_errno = KOM_PERM;
	return FAILURE;
    }

    prepare_aux_item_list(items_to_add, ACTPERS);

    if (check_delete_aux_item_list(items_to_delete,
                                   &kom_info.aux_item_list)!=OK)
        return FAILURE;
358
359
360
361
    delete_aux_item_list(items_to_delete,
                         &kom_info.aux_item_list,
                         OTHER_OBJECT_TYPE,
                         0, NULL);
362
363
364

    if (system_check_add_aux_item_list(&kom_info, items_to_add, ACTPERS) != OK)
    {
365
366
367
        undelete_aux_item_list(items_to_delete, &kom_info.aux_item_list,
                                   OTHER_OBJECT_TYPE,
                                   0, NULL);
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
        return FAILURE;
    }

    system_add_aux_item_list(&kom_info, items_to_add, ACTPERS);

    return OK;
}



extern Success
get_collate_table (String * result)
{
    s_clear(result);
    result->string = DEFAULT_COLLAT_TAB;
    result->len = COLLAT_TAB_SIZE;
    return OK;
}