dsaconfig.c

来自「ftam等标准协议服务器和客户端的源代码。」· C语言 代码 · 共 1,351 行 · 第 1/2 页

C
1,351
字号
/* dsaconfig.c - build a database directory for a Level-1 DSA */#ifndef	lintstatic char *rcsid = "$Header: /xtel/isode/isode/others/quipu/tools/dsaconfig/RCS/dsaconfig.c,v 9.0 1992/06/16 12:43:56 isode Rel $";#endif/*  * $Header: /xtel/isode/isode/others/quipu/tools/dsaconfig/RCS/dsaconfig.c,v 9.0 1992/06/16 12:43:56 isode Rel $ * * * $Log: dsaconfig.c,v $ * Revision 9.0  1992/06/16  12:43:56  isode * Release 8.0 * *//* *				  NOTICE * *    Acquisition, use, and distribution of this module and related *    materials are subject to the restrictions of a license agreement. *    Consult the Preface in the User's Manual for the full terms of *    this agreement. * */#include <ctype.h>#include <errno.h>#include <grp.h>#include <pwd.h>#include <stdio.h>#include <varargs.h>#include "general.h"#include "manifest.h"#include "internet.h"#include "psap.h"#include "tailor.h"#ifdef	SYS5struct group *getgrnam ();struct passwd *getpwnam (), *getpwuid ();#endif/*    DATA */static int debug =0;static int uid = 0;static int gid = 0;static char *myname = "dsaconfig";static char *wildlife = NULL;static char sedfil[BUFSIZ];void	adios (), advise ();char   *version ();extern int errno;extern char  *quipuversion;/*    MAIN *//* ARGSUSED */main (argc, argv, envp)int	argc;char  **argv,      **envp;{    char    buffer[BUFSIZ];    arginit (argv);    if (access (wildlife, 0x00) != NOTOK)	adios (NULLCP, "%s already exists, choose a different name", wildlife);    read_config ();    generate_sed ();    build_root ();    build_TLC ();    build_organization ();    build_unit ();    build_tailor ();    build_startup ();    build_nightly ();    build_dsap ();    build_fred ();#ifndef	SYS5    (void) sprintf (buffer, "find %s -exec /etc/chown %d {} \\;",		    wildlife, uid);#else    (void) sprintf (buffer, "find %s -exec chown %d {} \\;", wildlife, uid);#endif    if (debug)	fprintf (stderr, "%s\n", buffer);    (void) system (buffer);    (void) sprintf (buffer, "find %s -exec chgrp %d {} \\;",		    wildlife, gid);    if (debug)	fprintf (stderr, "%s\n", buffer);    (void) system (buffer);    (void) unlink (sedfil);    exit (0);}/*    CONFIG */struct country {    char   *c_code;    int	    c_number;    char   *c_name;    char   *c_root;    char   *c_master;    char   *c_other;        char   *c_phone;    int	    c_flags;#define	C_SHORT	0x01};struct country *read_country ();/*  */struct pair {    char   *p_name;    char   *p_value;    int	    p_flags;#define	P_NULL	0x00#define	P_OPT	0x01#define	P_MBOX	0x02#define	P_XXX	0x04#define	P_ZAP	0x08#define	P_POST	0x10};static struct pair pairs[] = {    "dsa", NULL,			/* Spectacled Bear */	P_NULL,    "country", NULL,			/* US */        P_OPT,    "organization", NULL,		/* Performance Systems International */	P_NULL,    "domain", NULL,			/* psi.com */        P_NULL,    "unit", NULL,			/* Development */	P_NULL,    "street", NULL,			/* 11800 Sunrise Valley Drive */	P_OPT,    "pob", NULL,			/* 1234 */	P_OPT,    "town", NULL,			/* Reston */	P_NULL,    "state", NULL,			/* Virginia */	P_NULL,    "zipcode", NULL,			/* 22091 */	P_NULL,    "locality", NULL,			/* Reston, VA */        P_OPT,    "postaladdress", NULL,		/* org $ address */	P_OPT | P_POST,    "telephone", NULL,			/* +1 703-620-6651 */	P_NULL,    "fax", NULL,			/* +1 703-620-4586 */	P_OPT,    "description", NULL,		/* value-added ... */	P_NULL,    "ipaddr", NULL,			/* 127.0.0.1 */        P_OPT,    "port", NULL,			/* 17003 */        P_OPT,    "firstname", NULL,			/* Wengyik */	P_NULL,    "lastname", NULL,			/* Yeong */	P_NULL,    "middleinitial", NULL,		/* */	P_OPT,    "middlename", NULL,			/* */	P_OPT,    "mailbox", NULL,			/* yeong@psi.com */	P_MBOX,    "title", NULL,			/* Staff Scientist */	P_NULL,    "userid", NULL,			/* yeongw */	P_OPT,    "groupid", NULL,			/* whitepages */	P_OPT,    "password", NULL,			/* secret */	P_NULL,    "extension", NULL,			/* x1234 */	P_OPT,    "wildlife", NULL,			/* spectacled-bear */       P_XXX,    "bindir", NULL,			/* /usr/local/bin/ */        P_XXX,    "sbindir", NULL,			/* /usr/etc/ */        P_XXX,    "etcdir", NULL,			/* /usr/etc/ */        P_XXX,    "quipuversion", NULL,		/* from -lquipu */        P_XXX,    "rootDSA", NULL,			/* root upstream */        P_XXX,    "countryDSA", NULL,			/* country upstream */        P_XXX,    "otherDSA", NULL,			/* hack... */	P_XXX,    "rootDSAaddress", NULL,		/* PSAP of root upstream */        P_XXX,    "countryDSAaddress", NULL,		/* PSAP of country upstream */        P_XXX,    "otherDSAaddress", NULL,		/* PSAP of hack... */	P_XXX,    NULL};struct pair *n2p ();/*  */static read_config () {    int	    lineno;    register char *cp,		  *dp;    char    buffer[BUFSIZ],	    file[BUFSIZ],	    line[BUFSIZ],	   *vec[NVEC + 1];    FILE   *fp;    register struct country *c;    register struct pair *p,			 *q;    register struct hostent *hp;    struct sockaddr_in in_socket;    register struct sockaddr_in *isock = &in_socket;    (void) sprintf (file, "%s.dsa", wildlife);    if ((fp = fopen (file, "r")) == NULL)	adios (file, "unable to read");    for (lineno = 1; fgets (buffer, sizeof buffer, fp); lineno++) {	if (*buffer == '#')	    continue;	if (cp = index (buffer, '\n'))	    *cp = NULL;	(void) strcpy (line, buffer);	bzero ((char *) vec, sizeof vec);	switch (str2vec (buffer, vec)) {	    case 0:	        continue;	    case 1:		for (p = pairs; p -> p_name; p++)		    if (strcmp (p -> p_name, vec[0]) == 0)			break;		if (!p -> p_name || (p -> p_flags & P_XXX))		    adios (NULLCP, "unknown variable \"%s\"", vec[0]);	        continue;	    case 2:		break;	    default:		adios (NULLCP, "syntax error on line %d:\n%s", lineno, line);		/* NOTREACHED */	}	for (p = pairs; p -> p_name; p++)	    if (strcmp (p -> p_name, vec[0]) == 0)		break;	if (!p -> p_name || (p -> p_flags & P_XXX))	    adios (NULLCP, "unknown variable \"%s\"", vec[0]);	if (p -> p_value)	    adios (NULLCP, "multiple values for \"%s\" starting at line %d",		   p -> p_name, lineno);	if (p -> p_flags & P_MBOX) {	    for (cp = vec[1]; *cp; cp++)		if (!isascii (*cp) || *cp == '$' || *cp == '&') {illegal: ;		    adios (NULLCP,			   "illegal character %c (0%o) in value for \"%s\"",			   *cp, *cp & 0xff, p -> p_name);		}	}	else	    for (cp = vec[1]; *cp; cp++) {		if (isalpha (*cp) || isdigit (*cp))		    continue;		switch (*cp) {		    case 047: /* ' */		    case '(':		    case ')':		    case '+':		    case ',':		    case '-':		    case '.':		    case '/':		    case ':':		    case '?':		    case ' ':			continue;		    default:			if ((p -> p_flags & P_POST) && *cp == '$')			    continue;			goto illegal;		}	    }	p -> p_value = strdup (vec[1]);    }    if (ferror (fp) && !feof (fp))	adios (file, "error reading");    (void) fclose (fp);    for (p = pairs; p -> p_name; p++)	if (!p -> p_value && !(p -> p_flags & (P_OPT | P_XXX)))	    adios (NULLCP, "missing value for \"%s\"", p -> p_name);    if (n2p ("country", 0) == NULL)	n2p ("country", 1) -> p_value = strdup ("US");    if ((c = read_country (cp = n2p ("country", 1) -> p_value)) == NULL)	adios (NULLCP, "unknown country code \"%s\"", cp);    n2p ("rootDSA", 1) -> p_value =			    strdup (c -> c_root ? c -> c_root : c -> c_master);    if (c -> c_root)	read_psap (c -> c_root, &(n2p ("rootDSAaddress", 1) -> p_value));    n2p ("countryDSA", 1) -> p_value = strdup (c -> c_master);    read_psap (c -> c_master, &(n2p ("countryDSAaddress", 1) -> p_value));    if (c -> c_other) {	n2p ("otherDSA", 1) -> p_value = strdup (c -> c_other);	read_psap (c -> c_other, &(n2p ("otherDSAaddress", 1) -> p_value));    }    if (n2p ("postaladdress", 0) == NULL	    && n2p ("street", 0) == NULL	    && n2p ("pob", 0) == NULL)	adios (NULLCP, "must specify either \"street\" or \"pob\"");    if ((p = n2p ("locality", 1)) -> p_value == NULL) {	(void) sprintf (buffer, "%s, %s", n2p ("town", 1) -> p_value,			n2p ("state", 1) -> p_value);	p -> p_value = strdup (buffer);	if (debug)	    printf ("setting locality to \"%s\"\n", p -> p_value);    }        if (*(p = n2p ("telephone", 1)) -> p_value != '+') {	(void) sprintf (buffer, "+%s %s", c -> c_phone, p -> p_value);	free (p -> p_value);	p -> p_value = strdup (buffer);	if (debug)	    printf ("setting telephone number to \"%s\"\n", p -> p_value);    }    if ((p = n2p ("fax", 0)) && *p -> p_value != '+') {	(void) sprintf (buffer, "+%s %s", c -> c_phone, p -> p_value);	free (p -> p_value);	p -> p_value = strdup (buffer);	if (debug)	    printf ("setting fax number to \"%s\"\n", p -> p_value);    }    if ((p = n2p ("ipaddr", 1)) -> p_value == NULL) {	if ((hp = gethostbyname (cp = getlocalhost ())) == NULL)	    adios (NULLCP, "%s: unknown host", cp);    }    else {	if ((hp = gethostbystring (p -> p_value)) == NULL)	    adios (NULLCP, "%s: unknown host", p -> p_value);    }    bzero ((char *) isock, sizeof *isock);    isock -> sin_family = hp -> h_addrtype;    inaddr_copy (hp, isock);    p -> p_value = strdup (inet_ntoa (isock -> sin_addr));    if (debug)	printf ("setting IP address of DSA to \"%s\"\n", p -> p_value);    if ((p = n2p ("port", 1)) -> p_value == NULL) {	p -> p_value = strdup ("17003");	if (debug)	    printf ("setting TCP port of DSA to \"%s\"\n", p -> p_value);    }    if ((p = n2p ("middlename", 1)) -> p_value	    && (q = n2p ("middleinitial", 1)) -> p_value == NULL) {	(void) sprintf (buffer, "%c", *p -> p_value);	p -> p_value = strdup (buffer);	if (debug)	    printf ("setting MiddleInitial to \"%s\"\n", p -> p_value);    }    uid = getuid (), gid = getgid ();    if ((p = n2p ("userid", 1)) -> p_value) {	register struct passwd *pw = getpwnam (p -> p_value);	if (pw == NULL)	    adios (NULLCP, "unknown user name \"%s\"", p -> p_value);	uid = pw -> pw_uid, gid = pw -> pw_gid;    }    else {	register struct passwd *pw = getpwuid (uid);	if (pw == NULL)	    adios (NULLCP, "unknown user name \"%s\"", p -> p_value);	p -> p_value = strdup (pw -> pw_name);	if (debug)	    printf ("setting userid to \"%s\"\n", p -> p_value);    }    if (p = n2p ("groupid", 0)) {	register struct group *gr = getgrnam (p -> p_value);	if (gr == NULL)	    adios (NULLCP, "unknown group name \"%s\"", p -> p_value);	gid = gr -> gr_gid;    }    p = n2p ("extension", 1), q = n2p ("telephone", 1);    if (cp = p -> p_value) {	if (*cp == 'X' || *cp == 'x') {	    (void) sprintf (buffer, "%s x%s", q -> p_value, p -> p_value);	    free (p -> p_value);	    p -> p_value = strdup (buffer);	    if (debug)		printf ("setting user's telephone number to \"%s\"\n",			p -> p_value);	}    }    else {	p -> p_value = strdup (q -> p_value);	if (debug)	    printf ("setting user telephone number to \"%s\"\n", p -> p_value);    }    n2p ("wildlife", 1) -> p_value = strdup (wildlife);    n2p ("bindir", 1) -> p_value = strdup (isodebinpath);    n2p ("sbindir", 1) -> p_value = strdup (isodesbinpath);    n2p ("etcdir", 1) -> p_value = strdup (isodetcpath);    n2p ("quipuversion", 1) -> p_value = strdup (quipuversion);    if (dp = (p = n2p ("postaladdress", 1)) -> p_value) {	int	i;	for (i = 1; cp = index (dp, '$'); dp = cp + 1, i++) {	    *cp = NULL;	    if (strlen (dp) > 30)		goto too_long;	    *cp = '$';	}	if (strlen (dp) > 30) {too_long: ;	    adios (NULLCP,		   "item %d is too long (30 characters maximum): \"%s\"", i,		   dp);	}	if (i > 6)	    adios (NULLCP, "too many items (%d) in postalAddress, 6 maximum",		   i);    }    else {	int	i;	cp = buffer;	(void) sprintf (cp, "%s $ ", n2p ("organization", 1) -> p_value);	if ((i = strlen (cp)) > 30 + 3) {	    advise (NULLCP,		    "your organization name is longer than 30 characters!");postal_problem: ;	    adios (NULLCP,          "You must explicitly define the postalAddress attribute in the\n\           configuration file for your Level-1 DSA.\n\\n\           The format is:\n\\n\               postaladdress \"item1 $ item2 $ ... $ itemN\"\n\\n\           where each item is <= 30 characters in length and there are no\n\           more than six items.  Refer to the Administrator's Guide for more\n\           information.\n\");	}	cp += i;	if (q = n2p ("pob", 0)) {	    (void) sprintf (cp, "POB %s $ ", q -> p_value);	    if ((i = strlen (cp)) > 30 + 3) {		advise (NULLCP, "your POB is longer than 26 characters!");		goto postal_problem;	    }	    cp += i;	}	else	    if (q = n2p ("street", 0)) {		(void) sprintf (cp, "%s $ ", q -> p_value);		if ((i = strlen (cp)) > 30 + 3) {		    advise (NULLCP,			"your street address is longer than 30 characters!");		    goto postal_problem;		}		cp += i;		q -> p_flags |= P_ZAP;	    }	(void) sprintf (cp, "%s, %s %s $ ",			n2p ("town", 1) -> p_value,			n2p ("state", 1) -> p_value,			n2p ("zipcode", 1) -> p_value);	if ((i = strlen (cp)) > 30 + 3) {	    advise (NULLCP,		    "your town/state/zipcode is longer than 30 characters!");	    goto postal_problem;	}	cp += i;	if ((c -> c_flags & C_SHORT) || (i = strlen (dp = c -> c_name)) > 30)	    i = strlen (dp = c -> c_code);	(void) strcpy (cp, dp);	cp += i;	p -> p_value = strdup (buffer);    }}/*  */static struct country *read_country (code)char   *code;{    int	    vecp;    register char *cp,		  *dp;    char    d,	   *ep,	    buffer[BUFSIZ + 1],	    file[BUFSIZ],	   *vec[NVEC + NSLACK + 1];    FILE   *fp;    static struct country cs;    register struct country *c = NULL;    (void) strcpy (file, "iso3166");    if ((fp = fopen (file, "r")) == NULL)	adios (file, "unable to open");    ep = (dp = buffer) + sizeof buffer;    while (fgets (dp, ep - dp, fp)) {	if (*buffer == '#')	    continue;	if (cp = index (buffer, '\n')) {	    *cp = NULL;	    if ((d = getc (fp)) != EOF)		(void) ungetc (d, fp);	    switch (d) {		case '#':		case '\n':		case EOF:		    break;		default:		    *cp++ = ' ';		    if ((dp = cp) + 1 >= ep) {			*ep = NULL;			adios (NULLCP,			       "virtual line too long in %s: \"%s\"",			       file, cp);		    }		    continue;	    }	}	dp = buffer;	switch (vecp = str2vec (buffer, vec)) {	    case 7:		/* standard entry */	    case 8:	        if (strcmp (vec[0], code))		    continue;		c = &cs;		bzero ((char *) c, sizeof *c);		c -> c_code = strdup (vec[0]);		if (sscanf (vec[1], "%d", &c -> c_number) != 1)		    adios (NULLCP,			   "malformed ISO3166 number for country %s in %s",			   code, file);		c -> c_number = atoi (vec[1]);		c -> c_name = strdup (vec[2]);		if (strcmp (vec[3], "NULL"))		    c -> c_root = strdup (vec[3]);		if (strcmp (vec[4], "NULL"))		    c -> c_master = strdup (vec[4]);		else		    adios (NULLCP, "no masterDSA known for country %s in %s",			   code,file);		if (strcmp (vec[5], "NULL"))		    c -> c_other = strdup (vec[5]);		c -> c_phone = strdup (vec[6]);		if (vecp == 8) {		    char   *bp;		    if (strncmp (cp = vec[7], "0x", 2) == 0)			cp += 2, bp = "%x";		    else			if (*cp == '0')			    cp += 1, bp = "%o";		        else			    bp = "%d";		    if (sscanf (cp, bp, &c -> c_flags) != 1)			adios (NULLCP,			      "malformed entry for country %s in %s",			       code, file);		}		break;	    default:	        continue;	}	break;    }    (void) fclose (fp);    return c;}/*  */static	read_psap (dsa, addr)char   *dsa,     **addr;{    int	    i;    register char *cp,		  *dp;    char    buffer[BUFSIZ],	    stuff[BUFSIZ];    FILE   *fp;    if ((fp = fopen (cp = isodefile ("dsaptailor", 0), "r")) == NULL)	adios (cp, "unable to open");

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?