📄 adv.c
字号:
/* @(#)adv.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 "@(#)adv:adv.c 1.14.1.1"#include <stdio.h>#include <fcntl.h>#include <ctype.h>#include <string.h>#include <rfs/nserve.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/errno.h>#define ALIAS "alias" /* keyword for alias entries */#define ALTAB "/etc/host.alias" /* alias file */#define ADVLOCK "/etc/adv.lck" /* lock file for /etc/advtab */#define NUM_MACHS 100#define MASK 00644#define BUFSIZE 512#define MAXFIELD 5#define RES 1#define MACH 2#define SZ_CLIENT (SZ_DELEMENT + SZ_MACH + 1)#define SAME 0void exit();static int pos = 0;static int aflag = 0;static int mflag = 0;static int dflag = 0;static int aliases = 0;static int low_index = 0;static char *fieldv[MAXFIELD];static char *atab_buf; /* pointer to alias table buffer */static char **clist = NULL; /* pointer to list of clients */static char *cmd;static char *flg[] = { "read/write", "read-only"};char *malloc();static struct altab { char *alname; /* pointer to alias name */ char *alist; /* pointer to corresponding list */} *altptr;static struct stat stbuf;extern int errno;extern int optind, opterr;extern char *optarg;extern char *dompart();extern char *namepart();main(argc,argv)int argc;char *argv[];{ int chr, temprec; short req_type; short roflag = A_RDWR, errflag = 0; char *path = NULL; char *resource; char *desc = ""; char *usage1 = "adv [ [-r] [-d desc] resource pathname [clients..] ]"; char *usage2 = "adv -m resource -d desc [clients..]"; char *usage3 = "adv -m resource [-d desc] clients.."; char **mlist; cmd = argv[0]; /* * If no arguments, list the local advertise file, else * check for optional arguments. */ if (argc <= 1) { list_advlog(); exit(0); } /* * Check for optional arguments. * -r : read-only * -m : modify description and/or client information * -d : description */ while ((chr = getopt(argc,argv,"rm:d:")) != EOF) switch(chr) { case 'r': if (roflag == A_RDONLY || mflag) errflag = 1; else roflag = A_RDONLY; break; case 'm': if (mflag || roflag == A_RDONLY) errflag = 1; else { mflag = 1; resource = optarg; } break; case 'd': if (dflag) errflag = 1; else { dflag = 1; desc = optarg; if (strlen(desc) > SZ_DESC) { optarg[SZ_DESC] = '\0'; fprintf(stderr,"%s: warning: description truncated to <%s>\n",cmd,desc); } } break; case '?': errflag = 1; break; } /* * If a resource is being advertised, there * must be at least 2 arguments, the symbolic * name and the directory being advertised. */ if (errflag || (!mflag ? argc <= optind + 1 : 0)) { fprintf(stderr,"Usage: %s\n\t%s\n\t%s\n",usage1,usage2,usage3); exit(1); } /* * If information associated with an advertised resource is * being modified, the description or client fields * must be provided. */ if (mflag && (!dflag && argv[optind] == NULL)) { fprintf(stderr,"Usage: %s\n\t%s\n\t%s\n",usage1,usage2,usage3); exit(1); } if (geteuid() != 0) { fprintf(stderr,"%s: must be super-user\n",cmd); exit(1); } if (!mflag) resource = argv[optind]; /* * Determine if resource name contains '/', '.', or non-printable * characters. */ if (v_resname(resource) != 0) { fprintf(stderr,"%s: invalid characters specified in <%s>\n",cmd,resource); fprintf(stderr,"%s: resource name cannot contain '/', '.' ,' ' or non-printable characters\n",cmd); exit(1); } if (strlen(resource) > SZ_RES) { resource[SZ_RES] = '\0'; fprintf(stderr,"%s: warning: resource name truncated to <%s>\n",cmd,resource); } if (!mflag) { path = argv[optind + 1]; if (*path != '/') { fprintf(stderr,"%s: full pathname required\n",cmd); exit(1); } mlist = &argv[optind + 2]; } else mlist = &argv[optind]; /* * Create and initialize the alias table if the * client field and /etc/host.alias exist, and form * the authorized client list. */ if (mlist[0] != NULL) { if ((stat(ALTAB,&stbuf) != -1) && stbuf.st_size != 0) creat_atbl(stbuf); creatlist(mlist); if (clist[0] == NULL) { fprintf(stderr,"%s: no valid client names\n",cmd); exit(1); } roflag |= A_CLIST; } if (mflag) { req_type = NS_MODADV; roflag |= A_MODIFY; } else req_type = NS_ADV; /* * Lock a temporary file to prevent many advertises from * updating "/etc/advtab" at the same time. */ if ((temprec = creat(ADVLOCK, 0600)) == -1 || lockf(temprec, F_LOCK, 0L) < 0) { fprintf(stderr, "%s: warning: cannot lock temp file <%s>\n",cmd,ADVLOCK); } /* * Invoke the advfs() system call to advertise a new resource, * or to modify the client list of an advertised resource. */ if (advfs(path,resource,roflag,clist) == -1) { rpterr(resource,path); exit(1); } roflag &= ~(A_CLIST | A_MODIFY);#ifdef NAMESERVER /* * Send the advertise information to the name server. */ if (ns_adv(req_type,path,resource,desc,roflag,clist) == RFS_FAILURE) { nserror(cmd); if (req_type == NS_ADV) unadvfs(resource); exit(1); }#endif if (mflag) update_entry(resource,path,desc,roflag,mlist); else add_entry(resource,path,desc,roflag,mlist); exit(0); /* NOTREACHED */}staticlist_advlog(){ int num_args; char res[SZ_RES + 1]; char advbuf[BUFSIZE]; FILE *fp; if (stat(ADVFILE,&stbuf) != -1) { if ((fp = fopen(ADVFILE, "r")) == NULL) { fprintf(stderr,"%s: cannot open <%s>\n",cmd,ADVFILE); exit(1); } while ((num_args = fscanf(fp,"%s",res)) != EOF) { if (num_args != 1) continue; fgets(advbuf,BUFSIZE,fp); fprintf(stdout,"%-14.14s%s",res,advbuf); } fclose(fp); }}staticcreat_atbl(atab)struct stat atab;{ int fd; char *end_of_atab; /* * Allocate a buffer for the alias table and read * in the contents of /etc/host.alias (alias file). */ if ((atab_buf = (char *)malloc((unsigned)(atab.st_size +1))) == NULL) { fprintf(stderr,"%s: cannot allocate memory for the alias table\n",cmd); exit(1); } if ((fd = open(ALTAB, O_RDONLY)) == -1) { fprintf(stderr,"%s: cannot open <%s>\n",cmd,ALTAB); exit(1); } if (read(fd,atab_buf,(unsigned)atab.st_size) != atab.st_size) { fprintf(stderr,"%s: cannot read <%s>\n",cmd,ALTAB); exit(1); } end_of_atab = atab_buf + atab.st_size; *end_of_atab = '\0'; close(fd); /* * Check the syntax of each alias entry in /etc/host.alias * and determine the total number of alias entries. */ if ((aliases = atbl_syntax(atab_buf)) == -1) exit(1); /* * Allocate a structure for each alias entry. Each * structure consists of two pointers, one for the * alias name and the other for its corresponding list. */ if ((altptr = (struct altab *)malloc(sizeof(struct altab)*aliases))==NULL) { fprintf(stderr,"%s: cannot allocate memory for the alias table\n",cmd); exit(1); } process_atbl(altptr,atab_buf); aflag++;}staticatbl_syntax(atab_ptr)char *atab_ptr;{ int line = 0; int errflg = 0; int num_aliases = 0; /* * Check the alias table for syntactic errors, * and determine the number of alias entries. * If the new line character ('\n') of an alias entry * is immediately preceded by a backslash then the * next line is a continuation, or else it represents * the next alias entry. */ while (*atab_ptr != '\0') { atab_ptr += strspn(atab_ptr," \t"); while (*atab_ptr == '\n') { atab_ptr++; atab_ptr += strspn(atab_ptr," \t"); line++; } if (*atab_ptr == '\0') break; if (strncmp(atab_ptr,ALIAS,strlen(ALIAS)) == SAME) { atab_ptr = strpbrk(atab_ptr,"\n"); line++; while (*(atab_ptr - 1) == '\\') { if (*(atab_ptr + 1) == '\0') { fprintf(stderr,"%s: syntax error in <%s>, line <%d>\n",cmd,ALTAB,line); errflg++; break; } *(atab_ptr - 1) = ' '; *atab_ptr = ' '; atab_ptr = strpbrk(atab_ptr,"\n"); line++; } num_aliases++; } else { atab_ptr = strpbrk(atab_ptr,"\n"); line++; errflg++; fprintf(stderr,"%s: syntax error in <%s>, line <%d>\n",cmd,ALTAB,line); } atab_ptr++; } if (errflg) return(-1); else return(num_aliases);}staticprocess_atbl(altbl,atab_ptr)struct altab *altbl;char *atab_ptr;{ register int i; int inval_cnt = 0; register char *buf, *ptr; /* * Set the pointers for each alias entry in the alias table * so that the first pointer points to the alias name * while the second points to the alias list. * NOTE: alias entries without an alias list are ignored. */ for (i = 0; i < aliases; i++) { atab_ptr += strspn(atab_ptr," \t\n"); atab_ptr += strlen(ALIAS); atab_ptr += strspn(atab_ptr," \t"); buf = strpbrk(atab_ptr," \t\n"); if (*buf == '\n' || *(ptr = (buf + strspn(buf," \t"))) == '\n') { atab_ptr = buf + strspn(buf," \t\n"); inval_cnt++; continue; } *buf++ = '\0'; altbl->alname = atab_ptr; buf += strspn(buf," \t"); atab_ptr = strpbrk(buf,"\n"); *atab_ptr++ = '\0'; altbl->alist = buf; altbl++; } aliases -= inval_cnt;}staticcreatlist(clients)char *clients[];{ register int index = 0; int old_pos; char *clname;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -