📄 rfadmin.c
字号:
/* @(#)rfadmin.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 "@(#)rfadmin:rfadmin.c 1.3.3.1"#include <sys/types.h>#include <sys/stat.h>#include <sys/utsname.h>#include <rfs/rfsys.h>#include <rfs/sema.h>#include <rfs/rfs_misc.h>#include <rfs/comm.h>#include <rfs/rdebug.h>#include <stdio.h>#include <ctype.h>#include <rfs/nserve.h>#include <signal.h>#include <ctype.h>#include <string.h>#include <unistd.h>#include <time.h>#define EQ(s1,s2) (strcmp(s1, s2) == 0)#define PRIM_RTN 1#define REMOVE 2#define ADD 4#define QUERY 8#define OPTION 16#define MINLENGTH 6#define YES 1#define NO 0#define PRIMARY 1#define SECONDARY 2#define SEM_FILE "/etc/.rfadmin"extern char *optarg;extern int optind;extern int ns_errno;extern char *namepart();extern char *dompart();static char *cmd_name;static char *getdname();main( argc, argv )int argc;char *argv[];{ int flag = 0; int error = 0; int c; char *host; char *optname; cmd_name = argv[0]; /* * Process arguments. */ while ((c = getopt(argc, argv, "pqa:r:o:")) != EOF) { switch (c) { case 'p': if (flag) error = 1; else flag |= PRIM_RTN; break; case 'q': if (flag) error = 1; else flag |= QUERY; break; case 'r': if (flag) error = 1; else { flag |= REMOVE; host = optarg; } break; case 'a': if (flag) error = 1; else { flag |= ADD; host = optarg; } break;/* case 'o': if (flag) error = 1; else { flag |= OPTION; optname = optarg; } break;*/ case '?': error = 1; } } if (optind < argc) { fprintf(stderr, "%s: extra arguments given\n", cmd_name); error = 1; } if (error) { fprintf(stderr, "%s: usage: %s [-p]\n", cmd_name, cmd_name); fprintf(stderr, " %s [-q]\n", cmd_name); fprintf(stderr, " %s [-a domain.host]\n", cmd_name); fprintf(stderr, " %s [-r domain.host]\n", cmd_name); fprintf(stderr, " %s [-o optionname]\n", cmd_name); exit(1); } /* * If the "-q" option was specified, print out a message * stating if RFS is running or not (do not have to be * root in this case). */ if (flag & QUERY) exit(query_rtn()); /* * Handle -o options. Super-user restrictions must be done by * each option individually. *//* if (flag & OPTION) exit(handle_opt(optname));*/ /* * At this point, all other options require super-user * priviledges. */ if (geteuid() != 0) { fprintf(stderr, "%s: must be super user\n", cmd_name); exit(1); } /* * if no arguments given, print out name of primary */ if (flag == 0) exit(pr_primary()); if (flag & ADD) exit(update_passwd(host, ADD)); if (flag & REMOVE) exit(update_passwd(host, REMOVE)); if (flag & PRIM_RTN) exit(primary_rtn()); exit(0); /* NOTREACHED */}staticquery_rtn(){ int rtn; if ((rtn = rfsys(RF_RUNSTATE)) < 0) { perror(cmd_name); return(2); } if (rtn == DU_UP) { printf("RFS is running\n"); return(0); } printf("RFS is not currently running\n"); return(1);}staticupdate_passwd(host, cmd)char *host;int cmd;{ char domain[MAXDNAME], name[SZ_MACH]; char filename[BUFSIZ]; int sem; struct utsname uts; /* * Separate host name into machine name and domain. */ strncpy(name, namepart(host), SZ_MACH); name[SZ_MACH - 1] = '\0'; strncpy(domain, dompart(host), MAXDNAME); domain[MAXDNAME - 1] = '\0'; if (domain[0] == '\0' || name[0] == '\0') { fprintf(stderr, "%s: host name <%s> must be specified as domain.host\n", cmd_name, name); return(1); } /* * make sure that this machine is authorized to add or remove * hosts (i.e, it is primary name server). */ if (uname(&uts) == -1) { fprintf(stderr, "%s: cannot get current uname\n", cmd_name); return(1); } if (is_prime(uts.nodename, domain, PRIMARY) == NO) { fprintf(stderr, "%s: Not primary name server for the domain <%s>\n", cmd_name, domain); return(1); } /* * Verify that the name given is syntactically correct. * pv_[du]name prints out what's wrong withe the names * if anything. The functions return 0 on success. */ if (pv_dname(cmd_name, domain, 0) != 0 || pv_uname(cmd_name, name, 0, "host") != 0) { return(1); } sprintf(filename, DOMPASSWD, domain); /* * Create and lock a temporary file. This file will be * used as a semaphore so only one process updates the * password file at a time. */ if ((sem = creat(SEM_FILE, 0600)) == -1 || lockf(sem, F_LOCK, 0L) < 0) { fprintf(stderr, "%s: cannot create temp semaphore file <%s>\n", cmd_name, SEM_FILE); return(1); } if (cmd == ADD) return(add_rtn(filename, name)); if (is_prime(name, domain, SECONDARY) == YES) { fprintf(stderr, "%s: removal of secondary name server <%s> disallowed\n", cmd_name, name); return(1); } if (is_prime(name, domain, PRIMARY) == YES) { fprintf(stderr, "%s: removal of primary name server disallowed\n", cmd_name); return(1); } if (adv_res(name) == YES) fprintf(stderr, "%s: warning: %s currently has resources advertised\n", cmd_name, name); return(remove_rtn(filename, name));}staticadv_res(name)char *name;{ char cmd[512]; FILE *fp; int rtn; /* * Determine if the host has resources advertised. */ sprintf(cmd, "nsquery -h %s 2>/dev/null", name); if ((fp = popen(cmd, "r")) == NULL) return(NO); /* * Get the first character. This is done beacuse * a new-line will be present even if no resources * are advertised. */ fgetc(fp); if (fgetc(fp) == EOF) rtn = NO; else rtn = YES; pclose(fp); return(rtn);}staticadd_rtn(filename, name)char filename[];char *name;{ FILE *fp; char buf[BUFSIZ]; char *pw; char *getnewpass(); char *m_name, *ptr; if (make_file(filename) < 0) { fprintf(stderr, "%s: error in creating the password file directory <%s>\n", cmd_name, filename); return(1); } if ((fp = fopen(filename, "a+")) == NULL) { fprintf(stderr, "%s: cannot open password file <%s>\n", cmd_name, filename); return(1); } else { /* * Check to make sure that the host is not already * in the password file. */ while(fgets(buf, sizeof(buf), fp) != NULL) { m_name = ptr = buf; while (*ptr != ':' && *ptr != '\0') ptr ++; *ptr = '\0'; if (EQ(name, m_name)) { fprintf(stderr, "%s: host <%s> is already in domain\n", cmd_name, name); return(1); } } /* * Add the new entry for the specified host. */ if ((pw = getnewpass(name)) == (char *)NULL) return(1); sprintf(buf, "%s:%s\n", name, pw); if (fputs(buf, fp) == EOF) { fprintf(stderr, "%s: error in writing to password file <%s>\n", cmd_name, filename); return(1); } fclose(fp); } return(0);}staticremove_rtn(filename, name)char filename[];char *name;{ FILE *fp, *tempfp; char buf[BUFSIZ]; char tempfile[BUFSIZ]; char tchr; register char *m_name, *ptr; int found = 0; strcpy(tempfile, filename); strcat(tempfile, ".t"); if ((fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "%s: cannot open password file <%s>\n", cmd_name, filename); return(1); } if ((tempfp = fopen(tempfile, "a+")) == NULL) { fprintf(stderr, "%s: cannot open temporary password file\n", cmd_name); return(1); } /* * Go through the password file and eliminate * the entry for the specified host. */ while(fgets(buf, sizeof(buf), fp) != NULL) { m_name = ptr = buf; while (*ptr != ':' && *ptr != '\0') ptr ++; tchr = *ptr; *ptr = '\0'; if (EQ(name, m_name)) { found = 1; continue; } else { *ptr = tchr; if (fputs(buf, tempfp) == -1) { fprintf(stderr, "%s: error in writing to password file <%s>\n", cmd_name, filename); fclose(tempfp); unlink(tempfile); return(1); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -