aboutsummaryrefslogtreecommitdiffstats
path: root/gnu/libexec/uucp/libuuconf/hsinfo.c
blob: d20c00ff90677b2986a9a38b6fe13c5950b367cd (plain) (blame)
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
/* hsinfo.c
   Get information about a system from the HDB configuration files.

   Copyright (C) 1992, 1993 Ian Lance Taylor

   This file is part of the Taylor UUCP uuconf library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License
   as published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   The author of the program may be contacted at ian@airs.com or
   c/o Cygnus Support, Building 200, 1 Kendall Square, Cambridge, MA 02139.
   */

#include "uucnfi.h"

#if USE_RCS_ID
const char _uuconf_hsinfo_rcsid[] = "$Id: hsinfo.c,v 1.2 1994/05/07 18:12:27 ache Exp $";
#endif

#include <errno.h>
#include <ctype.h>

static int ihadd_machine_perm P((struct sglobal *qglobal,
				 struct uuconf_system *qsys,
				 struct shpermissions *qperm));
static int ihadd_logname_perm P((struct sglobal *qglobal,
				 struct uuconf_system *qsys,
				 struct shpermissions *qperm));

/* Get the information for a particular system from the HDB
   configuration files.  This does not make sure that all the default
   values are set.  */

int
_uuconf_ihdb_system_internal (qglobal, zsystem, qsys)
     struct sglobal *qglobal;
     const char *zsystem;
     struct uuconf_system *qsys;
{
  int iret;
  struct shpermissions *qperm;
  char *zline;
  size_t cline;
  char **pzsplit;
  size_t csplit;
  char **pzcomma;
  size_t ccomma;
  pointer pblock;
  char **pz;
  boolean ffound_machine, ffound_login;
  struct shpermissions *qother_machine;
  struct uuconf_system *qalt;

  if (! qglobal->qprocess->fhdb_read_permissions)
    {
      iret = _uuconf_ihread_permissions (qglobal);
      if (iret != UUCONF_SUCCESS)
	return iret;
    }

  /* First look through the Permissions information to see if this is
     an alias for some system.  I assume that an alias applies to the
     first name in the corresponding MACHINE entry.  */

  for (qperm = qglobal->qprocess->qhdb_permissions;
       qperm != NULL;
       qperm = qperm->qnext)
    {
      if (qperm->pzalias == NULL
	  || qperm->pzmachine == NULL
	  || qperm->pzalias == (char **) &_uuconf_unset
	  || qperm->pzmachine == (char **) &_uuconf_unset)
	continue;

      for (pz = qperm->pzalias; *pz != NULL; pz++)
	{
	  if (strcmp (*pz, zsystem) == 0)
	    {
	      zsystem = qperm->pzmachine[0];
	      break;
	    }
	}
      if (*pz != NULL)
	break;
    }

  zline = NULL;
  cline = 0;
  pzsplit = NULL;
  csplit = 0;
  pzcomma = NULL;
  ccomma = 0;

  pblock = NULL;

  iret = UUCONF_SUCCESS;

  for (pz = qglobal->qprocess->pzhdb_systems; *pz != NULL; pz++)
    {
      FILE *e;
      int cchars;

      qglobal->ilineno = 0;

      e = fopen (*pz, "r");
      if (e == NULL)
	{
	  if (FNO_SUCH_FILE ())
	    continue;
	  qglobal->ierrno = errno;
	  iret = UUCONF_FOPEN_FAILED | UUCONF_ERROR_ERRNO;
	  break;
	}

      while ((cchars = _uuconf_getline (qglobal, &zline, &cline, e)) > 0)
	{
	  int ctoks, ctimes, i;
	  struct uuconf_system *qset;
	  char *z, *zretry;
	  int cretry;

	  ++qglobal->ilineno;

	  --cchars;
	  if (zline[cchars] == '\n')
	    zline[cchars] = '\0';
	  if (isspace (BUCHAR (zline[0])) || zline[0] == '#')
	    continue;

	  ctoks = _uuconf_istrsplit (zline, '\0', &pzsplit, &csplit);
	  if (ctoks < 0)
	    {
	      qglobal->ierrno = errno;
	      iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
	      break;
	    }

	  /* If this isn't the system we're looking for, keep reading
	     the file.  */
	  if (ctoks < 1
	      || strcmp (zsystem, pzsplit[0]) != 0)
	    continue;

	  /* If this is the first time we've found the system, we want
	     to set *qsys directly.  Otherwise, we allocate a new
	     alternate.  */
	  if (pblock == NULL)
	    {
	      pblock = uuconf_malloc_block ();
	      if (pblock == NULL)
		{
		  qglobal->ierrno = errno;
		  iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
		  break;
		}
	      _uuconf_uclear_system (qsys);
	      qsys->uuconf_palloc = pblock;
	      qset = qsys;
	    }
	  else
	    {
	      struct uuconf_system **pq;

	      qset = ((struct uuconf_system *)
		      uuconf_malloc (pblock, sizeof (struct uuconf_system)));
	      if (qset == NULL)
		{
		  qglobal->ierrno = errno;
		  iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
		  break;
		}
	      _uuconf_uclear_system (qset);
	      for (pq = &qsys->uuconf_qalternate;
		   *pq != NULL;
		   pq = &(*pq)->uuconf_qalternate)
		;
	      *pq = qset;
	    }

	  /* Add this line to the memory block we are building for the
	     system.  */
	  if (uuconf_add_block (pblock, zline) != 0)
	    {
	      qglobal->ierrno = errno;
	      iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
	      break;
	    }

	  zline = NULL;
	  cline = 0;

	  /* The format of a line in Systems is
	     system time device speed phone chat
	     For example,
	     airs Any ACU 9600 5551212 ogin: foo pass: bar
	     */

	  /* Get the system name.  */

	  qset->uuconf_zname = pzsplit[0];
	  qset->uuconf_fcall = TRUE;
	  qset->uuconf_fcalled = FALSE;

	  if (ctoks < 2)
	    continue;

	  /* A time string is "time/grade,time/grade;retry".  A
	     missing grade is taken as BGRADE_LOW.  */
	  zretry = strchr (pzsplit[1], ';');
	  if (zretry == NULL)
	    cretry = 0;
	  else
	    {
	      *zretry = '\0';
	      cretry = (int) strtol (zretry + 1, (char **) NULL, 10);
	    }

	  ctimes = _uuconf_istrsplit (pzsplit[1], ',', &pzcomma, &ccomma);
	  if (ctimes < 0)
	    {
	      qglobal->ierrno = errno;
	      iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
	      break;
	    }

	  for (i = 0; i < ctimes; i++)
	    {
	      char *zslash;
	      char bgrade;

	      z = pzcomma[i];
	      zslash = strchr (z, '/');
	      if (zslash == NULL)
		bgrade = UUCONF_GRADE_LOW;
	      else
		{
		  *zslash = '\0';
		  bgrade = zslash[1];
		  if (! UUCONF_GRADE_LEGAL (bgrade))
		    bgrade = UUCONF_GRADE_LOW;
		}

	      iret = _uuconf_itime_parse (qglobal, z, (long) bgrade,
					  cretry, _uuconf_itime_grade_cmp,
					  &qset->uuconf_qtimegrade,
					  pblock);
	      if (iret != UUCONF_SUCCESS)
		break;

	      /* Treat any time/grade setting as both a timegrade and
		 a call-timegrade.  */
	      if (bgrade != UUCONF_GRADE_LOW)
		qset->uuconf_qcalltimegrade = qset->uuconf_qtimegrade;
	    }

	  if (iret != UUCONF_SUCCESS)
	    break;

	  if (ctoks < 3)
	    continue;

	  /* Pick up the device name.  It can be followed by a comma
	     and a list of protocols.  */
	  qset->uuconf_zport = pzsplit[2];
	  z = strchr (pzsplit[2], ',');
	  if (z != NULL)
	    {
	      qset->uuconf_zprotocols = z + 1;
	      *z = '\0';
	    }

	  if (ctoks < 4)
	    continue;

	  /* The speed entry can be a numeric speed, or a range of
	     speeds, or "Any", or "-".  If it starts with a letter,
	     the initial nonnumeric prefix is a modem class, which
	     gets appended to the port name.  */
	  z = pzsplit[3];
	  if (strcasecmp (z, "Any") != 0
	      && strcmp (z, "-") != 0)
	    {
	      char *zend;

	      while (*z != '\0' && ! isdigit (BUCHAR (*z)))
		++z;

	      qset->uuconf_ibaud = strtol (z, &zend, 10);
	      if (*zend == '-')
		qset->uuconf_ihighbaud = strtol (zend + 1, (char **) NULL,
						 10);

	      if (z != pzsplit[3])
		{
		  size_t cport, cclass;

		  cport = strlen (pzsplit[2]);
		  cclass = z - pzsplit[3];
		  qset->uuconf_zport = uuconf_malloc (pblock,
						      cport + cclass + 1);
		  if (qset->uuconf_zport == NULL)
		    {
		      qglobal->ierrno = errno;
		      iret = UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
		      break;
		    }
		  memcpy ((pointer) qset->uuconf_zport, (pointer) pzsplit[2],
			  cport);
		  memcpy ((pointer) (qset->uuconf_zport + cport),
			  (pointer) pzsplit[3], cclass);
		  qset->uuconf_zport[cport + cclass] = '\0';
		}
	    }

	  if (ctoks < 5)
	    continue;

	  /* Get the phone number.  */
	  qset->uuconf_zphone = pzsplit[4];

	  if (ctoks < 6)
	    continue;

	  /* Get the chat script.  We just hand this off to the chat
	     script processor, so that it will parse subsend and
	     subexpect strings correctly.  */
	  pzsplit[4] = (char *) "chat";
	  iret = _uuconf_ichat_cmd (qglobal, ctoks - 4, pzsplit + 4,
				    &qset->uuconf_schat, pblock);
	  iret &=~ UUCONF_CMDTABRET_KEEP;
	  if (iret != UUCONF_SUCCESS)
	    break;
	}
  
      (void) fclose (e);

      if (iret != UUCONF_SUCCESS)
	break;
    }

  if (zline != NULL)
    free ((pointer) zline);
  if (pzsplit != NULL)
    free ((pointer) pzsplit);
  if (pzcomma != NULL)
    free ((pointer) pzcomma);

  if (iret != UUCONF_SUCCESS)
    {
      qglobal->zfilename = *pz;
      return iret | UUCONF_ERROR_FILENAME | UUCONF_ERROR_LINENO;
    }

  if (pblock == NULL)
    return UUCONF_NOT_FOUND;

  /* Now we have to put in the Permissions information.  The relevant
     Permissions entries are those with this system in the MACHINE
     list and (if this system does not have a VALIDATE entry) those
     with a LOGNAME list but no MACHINE list.  If no entry is found
     with this system in the MACHINE list, then we must look for an
     entry with "OTHER" in the MACHINE list.  */
  ffound_machine = FALSE;
  ffound_login = FALSE;
  qother_machine = NULL;
  for (qperm = qglobal->qprocess->qhdb_permissions;
       qperm != NULL;
       qperm = qperm->qnext)
    {
      boolean fmachine;

      /* MACHINE=OTHER is recognized specially.  It appears that OTHER
	 need only be recognized by itself, not when combined with
	 other machine names.  */
      if (qother_machine == NULL
	  && qperm->pzmachine != NULL
	  && qperm->pzmachine != (char **) &_uuconf_unset
	  && qperm->pzmachine[0][0] == 'O'
	  && strcmp (qperm->pzmachine[0], "OTHER") == 0)
	qother_machine = qperm;

      /* If this system is named in a MACHINE entry, we must add the
	 appropriate information to every alternate that could be used
	 for calling out.  */
      fmachine = FALSE;
      if (! ffound_machine
	  && qperm->pzmachine != NULL
	  && qperm->pzmachine != (char **) &_uuconf_unset)
	{
	  for (pz = qperm->pzmachine; *pz != NULL; pz++)
	    {
	      if ((*pz)[0] == zsystem[0]
		  && strcmp (*pz, zsystem) == 0)
		{
		  for (qalt = qsys;
		       qalt != NULL;
		       qalt = qalt->uuconf_qalternate)
		    {
		      if (qalt->uuconf_fcall)
			{
			  iret = ihadd_machine_perm (qglobal, qalt, qperm);
			  if (iret != UUCONF_SUCCESS)
			    return iret;
			}
		    }

		  fmachine = TRUE;
		  ffound_machine = TRUE;

		  break;
		}
	    }
	}

      /* A LOGNAME line applies to this machine if it is listed in the
	 corresponding VALIDATE entry, or if it is not listed in any
	 VALIDATE entry.  On this pass through the Permissions entry
	 we pick up the information if the system appears in a
	 VALIDATE entry; if it does not, we make another pass to put
	 in all the LOGNAME lines.  */
      if (qperm->pzlogname != NULL
	  && qperm->pzlogname != (char **) &_uuconf_unset
	  && qperm->pzvalidate != NULL
	  && qperm->pzvalidate != (char **) &_uuconf_unset)
	{
	  for (pz = qperm->pzvalidate; *pz != NULL; ++pz)
	    if ((*pz)[0] == zsystem[0]
		&& strcmp (*pz, zsystem) == 0)
	      break;
	  if (*pz != NULL)
	    {
	      for (pz = qperm->pzlogname; *pz != NULL; ++pz)
		{
		  /* If this LOGNAME line is also a matching MACHINE
		     line, we can add the LOGNAME permissions to the
		     first alternate.  Otherwise, we must create a new
		     alternate.  We cannot put a LOGNAME line in the
		     first alternate if MACHINE does not match,
		     because certain permissions (e.g. READ) may be
		     specified by both types of lines, and we must use
		     LOGNAME entries only when accepting calls and
		     MACHINE entries only when placing calls.  */
		  if (fmachine
		      && (qsys->uuconf_zcalled_login == NULL
			  || (qsys->uuconf_zcalled_login
			      == (char *) &_uuconf_unset)))
		    {
		      qsys->uuconf_zcalled_login = *pz;
		      iret = ihadd_logname_perm (qglobal, qsys, qperm);
		    }
		  else
		    {
		      struct uuconf_system *qnew;
		      struct uuconf_system **pq;

		      qnew = ((struct uuconf_system *)
			      uuconf_malloc (pblock,
					     sizeof (struct uuconf_system)));
		      if (qnew == NULL)
			{
			  qglobal->ierrno = errno;
			  return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
			}

		      *qnew = *qsys;
		      qnew->uuconf_qalternate = NULL;
		      for (pq = &qsys->uuconf_qalternate;
			   *pq != NULL;
			   pq = &(*pq)->uuconf_qalternate)
			;
		      *pq = qnew;

		      qnew->uuconf_zcalled_login = *pz;
		      qnew->uuconf_fcall = FALSE;
		      iret = ihadd_logname_perm (qglobal, qnew, qperm);
		    }

		  if (iret != UUCONF_SUCCESS)
		    return iret;
		}

	      ffound_login = TRUE;
	    }
	}
    }

  /* If we didn't find an entry for the machine, we must use the
     MACHINE=OTHER entry, if any.  */
  if (! ffound_machine && qother_machine != NULL)
    {
      for (qalt = qsys; qalt != NULL; qalt = qalt->uuconf_qalternate)
	{
	  if (qalt->uuconf_fcall)
	    {
	      iret = ihadd_machine_perm (qglobal, qalt, qother_machine);
	      if (iret != UUCONF_SUCCESS)
		return iret;
	    }
	}
    }

  /* If this system was not listed in any VALIDATE entry, then we must
     add a called-login for each LOGNAME entry in Permissions.  */
  if (! ffound_login)
    {
      for (qperm = qglobal->qprocess->qhdb_permissions;
	   qperm != NULL;
	   qperm = qperm->qnext)
	{
	  if (qperm->pzlogname == NULL
	      || qperm->pzlogname == (char **) &_uuconf_unset)
	    continue;

	  for (pz = qperm->pzlogname; *pz != NULL; pz++)
	    {
	      struct uuconf_system *qnew;
	      struct uuconf_system **pq;

	      qnew = ((struct uuconf_system *)
		      uuconf_malloc (pblock,
				      sizeof (struct uuconf_system)));
	      if (qnew == NULL)
		{
		  qglobal->ierrno = errno;
		  return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
		}

	      *qnew = *qsys;
	      qnew->uuconf_qalternate = NULL;
	      for (pq = &qsys->uuconf_qalternate;
		   *pq != NULL;
		   pq = &(*pq)->uuconf_qalternate)
		;
	      *pq = qnew;

	      /* We recognize LOGNAME=OTHER specially, although this
		 appears to be an SCO innovation.  */
	      if (strcmp (*pz, "OTHER") == 0)
		qnew->uuconf_zcalled_login = (char *) "ANY";
	      else
		qnew->uuconf_zcalled_login = *pz;
	      qnew->uuconf_fcall = FALSE;
	      iret = ihadd_logname_perm (qglobal, qnew, qperm);
	      if (iret != UUCONF_SUCCESS)
		return iret;
	    }
	}
    }

  /* HDB permits local requests to receive to any directory, which is
     not the default put in by _uuconf_isystem_basic_default.  We set
     it here instead.  */
  for (qalt = qsys; qalt != NULL; qalt = qalt->uuconf_qalternate)
    {
      iret = _uuconf_iadd_string (qglobal, (char *) ZROOTDIR,
				  FALSE, FALSE,
				  &qalt->uuconf_pzlocal_receive,
				  pblock);
      if (iret != UUCONF_SUCCESS)
	return iret;
    }

  /* HDB does not have a maximum number of retries if a retry time is
     given in the time field.  */
  if (qsys->uuconf_qtimegrade != NULL
      && qsys->uuconf_qtimegrade != (struct uuconf_timespan *) &_uuconf_unset
      && qsys->uuconf_qtimegrade->uuconf_cretry > 0)
    qsys->uuconf_cmax_retries = 0;

  return UUCONF_SUCCESS;
}

/* Add the settings of a MACHINE line in Permissions to a system.  */

/*ARGSIGNORED*/
static int
ihadd_machine_perm (qglobal, qsys, qperm)
     struct sglobal *qglobal;
     struct uuconf_system *qsys;
     struct shpermissions *qperm;
{
  if (qperm->frequest >= 0)
    qsys->uuconf_fsend_request = qperm->frequest;
  else
    qsys->uuconf_fsend_request = FALSE;
  qsys->uuconf_pzremote_send = qperm->pzread;
  qsys->uuconf_pzremote_receive = qperm->pzwrite;
  qsys->uuconf_pzcmds = qperm->pzcommands;
  qsys->uuconf_zlocalname = qperm->zmyname;
  qsys->uuconf_zpubdir = qperm->zpubdir;
  qsys->uuconf_pzalias = qperm->pzalias;

  return UUCONF_SUCCESS;
}

/* Add the settings of a LOGNAME line in Permissions to a system.  */

/*ARGSIGNORED*/
static int
ihadd_logname_perm (qglobal, qsys, qperm)
     struct sglobal *qglobal;
     struct uuconf_system *qsys;
     struct shpermissions *qperm;
{
  qsys->uuconf_fcalled = TRUE;
  if (qperm->frequest >= 0)
    qsys->uuconf_fsend_request = qperm->frequest;
  else
    qsys->uuconf_fsend_request = FALSE;
  qsys->uuconf_fcalled_transfer = qperm->fsendfiles;
  qsys->uuconf_pzremote_send = qperm->pzread;
  qsys->uuconf_pzremote_receive = qperm->pzwrite;
  qsys->uuconf_fcallback = qperm->fcallback;
  qsys->uuconf_zlocalname = qperm->zmyname;
  qsys->uuconf_zpubdir = qperm->zpubdir;

  return UUCONF_SUCCESS;
}