📄 lsdbf.c
字号:
/* @(#)lsdbf.c 1.1 92/07/30 SMI *//* Copyright (c) 1984 AT&T *//* All Rights Reserved *//* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T *//* The copyright notice above does not evidence any *//* actual or intended publication of such source code. */#ident "@(#)listen:lsdbf.c 1.8.1.1"/* * data base routines for the network listener process * Once initialization has completed, check_dbf should be called before * data base access to possibly re-read the data base file. * Currently, the listener does this in listen() before forking. *//* system include files */#include <fcntl.h>#include <stdio.h>#include <string.h>#include <ctype.h>#include <sys/param.h>#include <sys/types.h>#include <sys/stat.h>/* listener include files */#include "lsparam.h" /* listener parameters */#include "lsfiles.h" /* listener files info */#include "lserror.h" /* listener error codes */#include "lsdbf.h" /* data base file stuff *//* static data */static char *dbfinitmsg = "Using data base file: %s";static char *dbfvaldmsg = "Validating data base file: %s";static char *dbfopenmsg = "Trouble opening data base file";static char *dbfrderror = "Error reading data base file: line %d";static char *dbfbadlmsg = "Data base file: Error on line %d";static char *dbfdupcmsg = "Data base file: Duplicate service code: <%s>";static char *dbfunknown = "Unknown error reading data base file: line %d";static char *dbfsvccmsg = "Data base file: Illegal service code: <%s>";static char *dbfignomsg = "Data base file ignored -- 0 entries";static char *dbfstatmsg = "Trouble checking data base file. (Ignored)";static char *dbfnewdmsg = "Using new data base file";static char *dbfcorrupt = "Data base file has been corrupted";static int Dbflineno; /* current line number in dbf */static unsigned Dbfentries; /* number of non-comment lines */static dbf_t *Dbfhead; /* Dbfentries (when allocated) */static char *Server_cmd_lines; /* contains svc_code, cmd_line, mod_list */static time_t Dbfmtime; /* from stat(2) *//* * init_dbf: called at initialization, to read the data base file * * check_dbf: called each time the data base is accessed (before access) * to check if the data base has been modified, and if it has * to re-read the data base. */intinit_dbf(){ return(read_dbf(0));}intcheck_dbf(){ int ret = 0; struct stat statbuf; if (stat(DBFNAME, &statbuf)) { logmessage(dbfstatmsg); sys_error(E_DBF_IO, CONTINUE); return(-1); } if (statbuf.st_mtime != Dbfmtime) { Dbfmtime = statbuf.st_mtime; if (!(ret = read_dbf(1))) logmessage(dbfnewdmsg); } return(ret);}/* * read_dbf: * * read the data base file into internal structures * * all data base routines under read_dbf log there own errors and return -1 * in case of an error. * * if 're_read' is non-zero, this stuff is being called to read a new * data base file after the listener's initialization. */read_dbf(re_read)int re_read; /* zero means first time */{ register unsigned size; int exit_flag = EXIT | NOCORE; register int n; register dbf_t *dbf_p; register char *cmd_p; struct stat statbuf; unsigned scan_dbf(); extern char *calloc(); extern char *Home; char buf[128]; DEBUG((9,"in read_dbf")); /* * if first time, stat the data base file so we can save the * modification time for later use. */ if (check_version()) error(E_BADVER, EXIT | NOCORE); if (re_read) exit_flag = CONTINUE; else { if (stat(DBFNAME, &statbuf)) sys_error(E_DBF_IO, exit_flag); Dbfmtime = statbuf.st_mtime; } /* * note: data base routines log their own error messages */ sprintf(buf,"%s/%s",Home,DBFNAME); if ( (size = scan_dbf(buf)) == (unsigned)(-1) ) error( E_SCAN_DBF, exit_flag | NO_MSG ); DEBUG((5,"read_dbf: scan complete: non-commented lines: %u, size: %u", Dbfentries, size)); if (!size) { logmessage(dbfignomsg); return(0); } /* * allocate enough space for Dbfentries of 'size' bytes (total) * include enough space for default administrative entries in case * they aren't mentioned in the data base. */ if (!(dbf_p = (dbf_t *)calloc(Dbfentries+N_DBF_ADMIN+1,sizeof(dbf_t))) || !(cmd_p = calloc(size, 1))) { DEBUG((1,"cannot calloc %u + %u bytes", size, (Dbfentries+N_DBF_ADMIN+1)*(unsigned)sizeof(dbf_t))); error( E_DBF_ALLOC, exit_flag); /* if init, exit */ /* if still here, this is a re-read */ if (dbf_p) free(dbf_p); if (cmd_p) free(cmd_p); return(-1); } if (get_dbf(dbf_p, cmd_p)) { error(E_DBF_IO, exit_flag | NO_MSG); /* if still here, this is a re_read */ free(dbf_p); free(cmd_p); return(-1); } if (re_read) { if (Dbfhead) free(Dbfhead); if (Server_cmd_lines) free(Server_cmd_lines); } Dbfhead = dbf_p; Server_cmd_lines = cmd_p;#ifdef DEBUGMODE DEBUG((7,"read_dbf: data base dump...")); if (Dbfhead) for (dbf_p = Dbfhead; dbf_p->dbf_svc_code; ++dbf_p) DEBUG((7, "svc code <%s>; id: %s; reserved: %s; modules: %s; cmd line: %s", dbf_p->dbf_svc_code, dbf_p->dbf_id, dbf_p->dbf_reserved, dbf_p->dbf_modules, dbf_p->dbf_cmd_line));#endif /* DEBUGMODE */ return(0);}/* * get_dbf: read the file and fill the structures * checking for duplicate entries as we go */get_dbf(dbf_p, cmd_p)register dbf_t *dbf_p;register char *cmd_p;{ dbf_t *dbfhead = dbf_p; register unsigned size; register int n, i; char buf[DBFLINESZ]; register char *p = buf; char scratch[128]; FILE *dbfilep; char *cmd_line_p; int flags; char *svc_code_p; char *id_p; char *res_p; char *module_p; register dbf_t *tdbf_p; extern int isdigits(), atoi(); Dbflineno = 0; if (!(dbfilep = fopen(DBFNAME,"r"))) { logmessage(dbfopenmsg); error(E_DBF_IO, EXIT | NOCORE | NO_MSG); } while (n = rd_dbf_line(dbfilep,p,&svc_code_p,&flags,&id_p,&res_p,&module_p,&cmd_line_p)) { if (n == -1) { /* read error */ fclose(dbfilep); return(-1); } /* make sure service code is legal */ i = strlen(svc_code_p); if ( (i == 0) || (i >= SVC_CODE_SZ) ) goto reject; if (isdigits(svc_code_p)) { i = atoi(svc_code_p); if (i == 0) goto reject; if (flags & DBF_ADMIN) { if (i > N_DBF_ADMIN) goto reject; } else if (i < MIN_REG_CODE) goto reject; } else if (flags & DBF_ADMIN) goto reject; /* check for duplicate service code */ tdbf_p = dbfhead; while (tdbf_p->dbf_svc_code) { /* duplicate svc code? */ if (!strcmp(svc_code_p, tdbf_p->dbf_svc_code)) { sprintf(scratch, dbfdupcmsg, svc_code_p); logmessage(scratch); return(-1); } ++tdbf_p; } /* * legal, non-duplicate entry: copy it into internal data base */ dbf_p->dbf_flags = flags; strcpy(cmd_p, svc_code_p); dbf_p->dbf_svc_code = cmd_p; cmd_p += strlen(svc_code_p) + 1; strcpy(cmd_p, cmd_line_p); /* copy temp to alloc'ed buf */ dbf_p->dbf_cmd_line = cmd_p; cmd_p += strlen(cmd_line_p) + 1; strcpy(cmd_p, id_p); dbf_p->dbf_id = cmd_p; cmd_p += strlen(id_p) + 1; /* user id + null char */ strcpy(cmd_p, res_p); dbf_p->dbf_reserved = cmd_p; cmd_p += strlen(res_p) + 1; /* not used, a place holder */ strcpy(cmd_p, module_p); dbf_p->dbf_modules = cmd_p; cmd_p += strlen(module_p) + 1; /* cmd line + null char */ ++dbf_p; } fclose(dbfilep); return(0);reject: DEBUG((9, "svc_code <%s> failed validation test", svc_code_p)); sprintf(scratch, dbfsvccmsg, svc_code_p); logmessage(scratch); return(-1);}/* * scan_dbf: Take a quick pass through the data base file to figure out * approx. how many items in the file we'll need to * allocate storage for. Essentially, returns the number * of non-null, non-comment lines in the data base file. * * return: -1 == error. * other == number of non-comment characters. * Dbfentries set. */unsignedscan_dbf(path)register char *path;{ register unsigned int size = 0; register int n; register FILE *dbfilep; char buf[DBFLINESZ]; register char *p = buf; char *svc_code_p; int flags; char *cmd_line_p; char *module_p; char *id_p; char *res_p; extern Validate; DEBUG((9, "In scan_dbf. Scanning data base file %s.", path)); sprintf(buf, (Validate ? dbfvaldmsg: dbfinitmsg), path); logmessage(buf); if (!(dbfilep = fopen(path,"r"))) { logmessage(dbfopenmsg); return(-1); } do { n = rd_dbf_line(dbfilep,p,&svc_code_p,&flags,&id_p,&res_p,&module_p,&cmd_line_p); if (n == -1) { fclose(dbfilep); return(-1); } size += (unsigned)n; if (n) ++Dbfentries; } while (n); fclose(dbfilep); return(size);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -