File: server.c

package info (click to toggle)
slrn 1.0.3+dfsg-3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,412 kB
  • sloc: ansic: 39,852; sh: 3,777; perl: 467; makefile: 229; sed: 24
file content (693 lines) | stat: -rw-r--r-- 13,749 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/*
 This file is part of SLRN.

 Copyright (c) 1994, 1999, 2007-2016 John E. Davis <jed@jedsoft.org>
 Copyright (c) 2001-2006 Thomas Schultz <tststs@gmx.de>

 This program 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 2 of the License, or (at your option)
 any later version.

 This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "slrnfeat.h"

#include <stdio.h>

#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif

#ifdef VMS
# include <file.h>
#else
# include <sys/types.h>
# include <sys/stat.h>
#endif
#ifdef __MINGW32__
# include <process.h>
#endif

#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#ifdef HAVE_SYS_FCNTL_H
# include <sys/fcntl.h>
#endif

#include <slang.h>
#include "jdmacros.h"

#include "slrn.h"
#include "util.h"
#include "misc.h"
#include "server.h"
#include "startup.h"
#include "snprintf.h"
#if SLRN_USE_SLTCP
# if SLRN_HAS_NNTP_SUPPORT || SLRN_HAS_GROUP_LENS
#  include "sltcp.c"
# endif
#endif

#if SLRN_HAS_NNTP_SUPPORT
# if !SLRN_USE_SLTCP
#  include "clientlib.c"
# endif
# include "nntplib.c"
# include "nntp.c"
#endif

#if SLRN_HAS_SPOOL_SUPPORT
# include "spool.c"
#endif

int Slrn_Server_Id;
int Slrn_Post_Id;

Slrn_Server_Obj_Type *Slrn_Server_Obj;
Slrn_Post_Obj_Type *Slrn_Post_Obj;

#if SLRN_HAS_PULL_SUPPORT
static int pull_init_objects (void);
static int pull_select_post_object (void);
static int pull_parse_args (char **, int);
int Slrn_Use_Pull_Post;
#endif

#if SLRN_HAS_INEWS_SUPPORT
static FILE *Fp_Inews;
char *Slrn_Inews_Pgm;

#if defined(IBMPC_SYSTEM)
static char Inews_Outfile [SLRN_MAX_PATH_LEN];
#define MAX_LINE_BUFLEN	2048
#endif

static int inews_start_post (void)
{
#if defined(IBMPC_SYSTEM)
   if (NULL == (Fp_Inews = slrn_open_tmpfile (Inews_Outfile,
					      sizeof (Inews_Outfile))))
#else
   /* pipe a message to inews. Its done this way because inews expects the
    * article on stdin. Inews errors WILL mess up the screen.
    */
   /* !HACK! should we use slrn_popen() and slrn_pclose()?
    * They stop the screen getting messed up, but the error messages aren't
    * very appropriate
    */
   if (NULL == (Fp_Inews = popen (Slrn_Inews_Pgm, "w")))
#endif
     {
	slrn_error (_("Couldn't open pipe to inews! -- Article not posted."));
	return -1;
     }
   return CONT_POST;
}

static int inews_end_post (void)
{
   int res = 0;

#if defined(IBMPC_SYSTEM)
   char buf [MAX_LINE_BUFLEN];
   slrn_fclose (Fp_Inews);
   slrn_snprintf (buf, sizeof (buf), "%s %s", Slrn_Inews_Pgm, Inews_Outfile);
   slrn_posix_system (buf, 0);
#else
   if (-1 == pclose (Fp_Inews))
     {
	slrn_error (_("pclose() failed -- check if article was posted")); /* !HACK! can we do better? */
	res = -1;
     }
#endif
   Fp_Inews=NULL;

   return res;
}

static int inews_puts (char *s)
{
   fputs (s, Fp_Inews );
   return 0;
}

static int inews_vprintf (const char *fmt, va_list ap)
{
   char *line;
   int retval;

   line = slrn_strdup_vprintf (fmt, ap);
   retval = inews_puts (line);
   slrn_free (line);

   return retval;
}
static int inews_printf (char *fmt, ...)
{
   va_list ap;
   int retval;

   va_start (ap, fmt);
   retval = inews_vprintf (fmt, ap);
   va_end (ap);

   return retval;
}
static char *inews_get_recom_id (void)
{
   return NULL;
}

static Slrn_Post_Obj_Type Inews_Post_Obj;

static int inews_init_objects (void)
{
   Inews_Post_Obj.po_start = inews_start_post;
   Inews_Post_Obj.po_end = inews_end_post;
   Inews_Post_Obj.po_vprintf = inews_vprintf;
   Inews_Post_Obj.po_printf = inews_printf;
   Inews_Post_Obj.po_puts = inews_puts;
   Inews_Post_Obj.po_get_recom_id = inews_get_recom_id;
   Inews_Post_Obj.po_can_post = 1;
   Slrn_Inews_Pgm = slrn_safe_strmalloc (SLRN_INEWS_COMMAND);
   return 0;
}

static int inews_select_post_object (void)
{
   char inews [SLRN_MAX_PATH_LEN + 1];
   char *p;

   strncpy (inews, Slrn_Inews_Pgm, SLRN_MAX_PATH_LEN);
   inews[SLRN_MAX_PATH_LEN - 1] = 0;

   p = inews;
   while (*p && (*p != ' ')) p++;
   *p = 0;

   if (1 != slrn_file_exists (inews))
     {
	slrn_error (_("Unable to locate inews program \"%s\""), inews);
	return -1;
     }

   Slrn_Post_Obj = &Inews_Post_Obj;
   return 0;
}

#endif				       /* HAS_INEWS_SUPPORT */

int slrn_init_objects (void)
{
   char *server;

#if SLRN_HAS_NNTP_SUPPORT
   if (-1 == nntp_init_objects ())
     return -1;
#endif
#if SLRN_HAS_SPOOL_SUPPORT
   if (-1 == spool_init_objects ())
     return -1;
#endif
#if SLRN_HAS_INEWS_SUPPORT
   if (-1 == inews_init_objects ())
     return -1;
#endif
#if SLRN_HAS_PULL_SUPPORT
   if (-1 == pull_init_objects ())
     return -1;
#endif

   switch (Slrn_Server_Id)
     {
      case SLRN_SERVER_ID_NNTP:
	server = "NNTP";
	break;

      case SLRN_SERVER_ID_SPOOL:
	server = "SPOOL";
	break;

      default:
	server = NULL;
     }

   if ((server != NULL)
       && (-1 == SLdefine_for_ifdef (server)))
     {
	slrn_exit_error (_("Unable to add preprocessor name %s."), server);
     }

   return 0;
}

#if !SLRN_HAS_INEWS_SUPPORT
# undef SLRN_FORCE_INEWS
# define SLRN_FORCE_INEWS 0
#endif

int slrn_select_post_object (int id)
{
   char *post;

   switch (id)
     {
#if SLRN_HAS_INEWS_SUPPORT
      case SLRN_POST_ID_INEWS:
# if SLRN_HAS_PULL_SUPPORT
	if (Slrn_Use_Pull_Post
	    && (Slrn_Server_Id == SLRN_SERVER_ID_SPOOL))
	  {
	     slrn_error (_("inews cannot be used with an slrnpull spool."));
	     return -1;
	  }
# endif
	return inews_select_post_object ();
#endif

#if !SLRN_FORCE_INEWS
# if SLRN_HAS_NNTP_SUPPORT
      case SLRN_POST_ID_NNTP:
	return nntp_select_post_object ();
# endif
#endif

#if SLRN_HAS_PULL_SUPPORT
      case SLRN_POST_ID_PULL:
	return pull_select_post_object ();
#endif

      default:
	post = slrn_map_object_id_to_name (1, id);
	if (post == NULL)
	  post = "UNKNOWN";

	slrn_error (_("%s is not a supported posting agent."), post);
     }

   return -1;
}

int slrn_select_server_object (int id)
{
#if SLRN_HAS_SPOOL_SUPPORT
   int ret;
#endif
   char *server;

   switch (id)
     {
#if SLRN_HAS_NNTP_SUPPORT
      case SLRN_SERVER_ID_NNTP:
	return nntp_select_server_object ();
#endif

#if SLRN_HAS_SPOOL_SUPPORT
      case SLRN_SERVER_ID_SPOOL:
	ret = spool_select_server_object ();

# if SLRN_HAS_PULL_SUPPORT
	if (ret == -1)
	  return ret;

	if (Slrn_Use_Pull_Post)
	  Slrn_Post_Id = SLRN_POST_ID_PULL;
# endif
	return ret;
#endif

      default:
	server = slrn_map_object_id_to_name (0, id);

	if (server == NULL)
	  server = "UNKNOWN";

	slrn_error (_("%s is not a supported server object."), server);
     }
   return -1;
}

int slrn_parse_object_args (char *name, char **argv, int argc)
{
   int num_parsed = -1;

   if (name == NULL) return -1;

   if (!strcmp (name, "nntp"))
     {
#if SLRN_HAS_NNTP_SUPPORT
	num_parsed = nntp_parse_args (argv, argc);
	if (Slrn_Server_Id == 0)
	  Slrn_Server_Id = SLRN_SERVER_ID_NNTP;
# if !SLRN_FORCE_INEWS
	if (Slrn_Post_Id == 0)
	  Slrn_Post_Id = SLRN_POST_ID_NNTP;
# endif
	return num_parsed;
#else
	return -2;
#endif
     }

   if (!strcmp (name, "spool"))
     {
#if SLRN_HAS_SPOOL_SUPPORT
	Slrn_Server_Id = SLRN_SERVER_ID_SPOOL;
	return 0;
#else
	return -2;
#endif
     }

   if (!strcmp (name, "inews"))
     {
#if SLRN_HAS_INEWS_SUPPORT
	Slrn_Post_Id = SLRN_POST_ID_INEWS;
	return 0;
#else
	return -2;
#endif
     }

   if (!strcmp (name, "pull"))
     {
#if SLRN_HAS_PULL_SUPPORT
	num_parsed = pull_parse_args (argv, argc);
	Slrn_Post_Id = SLRN_POST_ID_PULL;
	return num_parsed;
#else
	return -2;
#endif
     }

   return num_parsed;
}

typedef struct
{
   char *name;
   int id;
}
Object_Name_Id_Type;

static Object_Name_Id_Type Server_Objects_and_Ids [] =
{
   {"nntp", SLRN_SERVER_ID_NNTP},
   {"spool", SLRN_SERVER_ID_SPOOL},
   {NULL, 0}
};

static Object_Name_Id_Type Post_Objects_and_Ids [] =
{
   {"nntp", SLRN_POST_ID_NNTP},
   {"inews", SLRN_POST_ID_INEWS},
   {"slrnpull", SLRN_POST_ID_PULL},
   {NULL, 0}
};

/* These functions should not attempt to write any error messages */
static Object_Name_Id_Type *map_object_type (int type, int *def_id)
{
   Object_Name_Id_Type *obj;

   switch (type)
     {
      case 0:
	obj = Server_Objects_and_Ids;
	if (def_id != NULL)
	  *def_id = SLRN_DEFAULT_SERVER_OBJ;
	break;

      case 1:
	obj = Post_Objects_and_Ids;
	if (def_id != NULL)
	  *def_id = SLRN_DEFAULT_POST_OBJ;
	break;

      default:
	obj = NULL;
     }

   return obj;
}

char *slrn_map_object_id_to_name (int type, int id)
{
   Object_Name_Id_Type *obj;

   if (NULL == (obj = map_object_type (type, NULL)))
     return NULL;

   while (obj->name != NULL)
     {
	if (obj->id == id)
	  return obj->name;
	obj++;
     }

   return NULL;
}

int slrn_map_name_to_object_id (int type, char *name)
{
   Object_Name_Id_Type *obj;
   int default_object_id;

   if (NULL == (obj = map_object_type (type, &default_object_id)))
     return -1;

   if (name == NULL)
     {
	name = slrn_map_object_id_to_name (type, default_object_id);
	if (name == NULL)
	  return -1;
     }

   while (obj->name != NULL)
     {
	if (0 == strcmp (name, obj->name))
	  return obj->id;
	obj++;
     }
   return -1;
}

#if SLRN_HAS_PULL_SUPPORT
static Slrn_Post_Obj_Type Pull_Post_Obj;
static FILE *Pull_Fp;
unsigned int Pull_Num_Posted;
char Pull_Post_Filename [SLRN_MAX_PATH_LEN + 1];
char Pull_Post_Dir [SLRN_MAX_PATH_LEN + 1];

static int pull_make_tempname (char *file, size_t n, char *prefix,
			       unsigned int num)
{
   int pid;
   time_t now;
   char name[256];
   char *login_name;

   pid = getpid ();
   time (&now);

   login_name = Slrn_User_Info.login_name;
   if ((login_name == NULL) || (*login_name == 0))
     login_name = "unknown";

   slrn_snprintf (name, sizeof (name), "%s%lu-%d-%u.%s", prefix,
		  (unsigned long) now, pid, num, login_name);

   if (-1 == slrn_dircat (Pull_Post_Dir, name, file, n))
     return -1;

   return 0;
}

static int pull_start_post (void)
{
   int fd;
   unsigned int n = 0;

   do
     {
	if (-1 == pull_make_tempname (Pull_Post_Filename,
				      sizeof (Pull_Post_Filename), "_", n++))
	  return ERR_FAULT;
	fd = open (Pull_Post_Filename, O_WRONLY | O_CREAT | O_EXCL,
		   S_IREAD | S_IWRITE);
	if ((-1 == fd) && (n == 100))
          {
	     slrn_error (_("Unable to open file in %s."), Pull_Post_Dir);
	     return ERR_FAULT;
	  }
     }
   while (-1 == fd);

   if (NULL == (Pull_Fp = fdopen (fd, "w")))
     {
	close (fd);
	return ERR_FAULT;
     }

   return CONT_POST;
}

static int pull_end_post (void)
{
   char file [SLRN_MAX_PATH_LEN + 1];
   unsigned int n = 0, m = 0;

   if (Pull_Fp == NULL)
     return ERR_FAULT;

   if (-1 == slrn_fclose (Pull_Fp))
     {
	Pull_Fp = NULL;
	return ERR_FAULT;
     }
   Pull_Fp = NULL;

#if SLRNPULL_USE_SETGID_POSTS
    /*
     * 1998/07/07 Sylvain Robitaille (syl@alcor.concordia.ca)
     *            We have our out.going directory setgid to news, and we
     *            want to be sure the file is readable to
     *            group news. This way, the news user will have
     *            appropriate permissions for manipulating this file.
     *            (NOTE: This has only been tested on Linux-2.0.xx)
     *
     * As of version 0.9.7.3, the files is not made group writeable any
     * longer. slrnpull should not need that permission.
     */
    if (-1 == chmod (Pull_Post_Filename, S_IRUSR|S_IWUSR|S_IRGRP))
      {
	 return ERR_FAULT;
      }
#endif

   while (m != 10)
     {
	do
	  {
	     if ((100 == n) || (-1 == pull_make_tempname (file, sizeof (file),
							  "X", n++)))
	       return ERR_FAULT;
	  }
	while (0 != slrn_file_exists (file));

	if (0 == rename (Pull_Post_Filename, file))
	  break;

#ifdef EEXIST
	if (errno == EEXIST)
	  {
	     m++;
	     continue;
	  }
#endif

	slrn_error (_("Unable to rename file. errno = %d."), errno);
	return ERR_FAULT;
     }

   return 0;
}

static int pull_puts (char *s)
{
   unsigned int len;

   len = strlen (s);

   if (len != fwrite (s, 1, len, Pull_Fp))
     {
	slrn_error (_("Write error.  File system full?"));
	return -1;
     }

   return 0;
}

static int pull_vprintf (const char *fmt, va_list ap)
{
   int ret;

   ret = vfprintf (Pull_Fp, fmt, ap);

   if (EOF == ret)
     {
	slrn_error (_("Write failed.  File system full?"));
	return -1;
     }

   return 0;
}

static int pull_printf (char *fmt, ...)
{
   int ret;

   va_list ap;

   va_start (ap, fmt);
   ret = pull_vprintf (fmt, ap);
   va_end (ap);

   return ret;
}

static char *pull_get_recom_id (void)
{
   return NULL;
}

static int pull_init_objects (void)
{
   Pull_Post_Obj.po_start = pull_start_post;
   Pull_Post_Obj.po_end = pull_end_post;
   Pull_Post_Obj.po_vprintf = pull_vprintf;
   Pull_Post_Obj.po_printf = pull_printf;
   Pull_Post_Obj.po_puts = pull_puts;
   Pull_Post_Obj.po_get_recom_id = pull_get_recom_id;
   Pull_Post_Obj.po_can_post = 1;

   return 0;
}

static int pull_select_post_object (void)
{
   if (-1 == slrn_dircat (Slrn_Inn_Root, SLRNPULL_OUTGOING_DIR,
			  Pull_Post_Dir, sizeof (Pull_Post_Dir)))
     return -1;

   if (2 != slrn_file_exists (Pull_Post_Dir))
     {
	slrn_error (_("Posting directory %s does not exist."), Pull_Post_Dir);
	return -1;
     }

   Slrn_Post_Obj = &Pull_Post_Obj;

   return 0;
}

static int pull_parse_args (char **argv, int argc)
{
   (void) argv;
   (void) argc;

   return 0;
}

#endif