📄 main.c
字号:
for ( ; *from ; ) *to++ = *from++; *to = '\0'; fprintf(tfp, "%s", copy); free(bp); free(copy); copy_file(fp, n+1, -1); (void) fclose(fp); if (!close_temp(fname)) return(0); return(1);}isdir(s) /* check to see if a directory exists */char *s;{ struct stat sbuf; if (stat(s, &sbuf) != 0) { fprintf(stderr, "%s: could not stat %s, errno = %d\n",arg0,s,errno); Errno = NLSSTAT; return(0); } if ((sbuf.st_mode & FILETYPE) == DIRECTORY) return(1); else return(0);}isnum(s) /* check to see if s is a numeric string */register char *s;{ if (!*s) return(0); while(*s) if (!isdigit(*s++)) return(0); return(1);}isactive(name) /* is a listener active? */char *name;{ char lockname[MAXPATH]; int fd; struct stat sbuf; sprintf(lockname, "%s/%s", homedir, name); if (access(lockname, 0) < 0) { fprintf(stderr, erropen, arg0, name); exit(NLSNOTF); } strcat(lockname, "/"); strcat(lockname, LOCKFILE); /* check to see if modes are okay */ if (stat(lockname, &sbuf) != 0) { /* Errno = NLSSTAT */ return(0); } if ((sbuf.st_mode & ALLPERM) != LOCKPERM) { if (uid == 0 || uid == nlsuid) chmod(lockname, LOCKPERM); else { fprintf(stderr,"%s: bad modes for file %s - retry as super user\n",arg0,lockname); exit(NLSPERM); } } /* open file with open(2) */ if ((fd = open(lockname, O_RDWR)) < 0) { /* Errno = NLSOPEN */ return(0); } if (LOCK(fd, 2, 0L) == -1) { close(fd); return(1); } close(fd); return(0);}make_db(fname, nspec) /* create the database file */char *fname;char *nspec;{ register FILE *fp = NULL; register FILE *afp = NULL; register int i; register char *ptr, *savptr; char addrname[MAXPATH]; char newname[MAXPATH]; char parent[MAXPATH]; /* assume homedir already exists */ /* make directory(s) for net_spec */ umask(0); sprintf(addrname, "%s/%s", homedir, nspec); strcpy(parent, homedir); ptr = addrname+strlen(homedir)+1; do { savptr = ptr; ptr = strchr(ptr, (int)'/'); if (ptr == savptr || !ptr) i = strlen(addrname); else i = ptr - addrname; strncpy(newname, addrname, i); newname[i] = (char)0; if ((access(newname,0) < 0) && (errno == ENOENT)){ if (mkdir(newname, 0777) < 0) { fprintf(stderr, "%s: could not create %s\n", arg0, newname); perror("mkdir"); Errno = NLSCREAT; return(0); } } strcpy(parent, newname); } while ((ptr != savptr) && (ptr++ != (char *)NULL)); umask(022); /* create files */ strcat(addrname, "/"); strcat(addrname, ADDRFILE); if ((fp = fopen(fname, "w")) == NULL) { fprintf(stderr,"%s: could not create %s, errno = %d\n",arg0,fname,errno); Errno = NLSCREAT; return(0); } if ((afp = fopen(addrname, "w")) == NULL) { fprintf(stderr,"%s: could not create %s, errno = %d\n",arg0,addrname,errno); Errno = NLSCREAT; return(0); } init_db(fp); if (fclose(fp) != 0) { fprintf(stderr,errclose,arg0,fname,errno); Errno = NLSCLOSE; return(0); } if (chown(fname, nlsuid, nlsgid) < 0) { fprintf(stderr,errchown,arg0, fname, errno); Errno = NLSCHOWN; return(0); } fprintf(afp,"\n\n"); if (fclose(afp) != 0) { fprintf(stderr,errclose,arg0,addrname,errno); Errno = NLSCLOSE; return(0); } if (chown(addrname, nlsuid, nlsgid) < 0) { fprintf(stderr,errchown,arg0, addrname, errno); Errno = NLSCHOWN; return(0); } umask(0); sprintf(addrname, "%s/%s/%s", homedir, nspec, LOCKFILE); if ((fp = fopen(addrname, "w")) == NULL) { fprintf(stderr,"%s: could not create %s, errno = %d\n",arg0,addrname,errno); Errno = NLSCREAT; return(0); } if (fclose(fp) != 0) { fprintf(stderr,errclose,arg0,addrname,errno); Errno = NLSCLOSE; return(0); } if (chown(addrname, nlsuid, nlsgid) < 0) { fprintf(stderr,errchown,arg0, addrname, errno); Errno = NLSCHOWN; return(0); } umask(022); return(1);}chg_addr(fname, la, ta, flag)char *fname;char *la;char *ta;register int flag;{ register FILE *fp = NULL; char addrname[MAXPATH]; char adr1[NAMEBUFSZ]; char adr2[NAMEBUFSZ]; char *p; adr1[0] = (char)0; adr2[0] = (char)0; sprintf(addrname, "%s/%s/%s", homedir, fname, ADDRFILE); if ((fp = fopen(addrname, "r")) == NULL) { fprintf(stderr, erropen, arg0, Netspec); Errno = NLSOPEN; return(0); } (void) fgets(adr1, NAMEBUFSZ, fp); (void) fgets(adr2, NAMEBUFSZ, fp); if (ferror(fp)) { fprintf(stderr,"%s: error in reading address file\n",arg0); (void) fclose(fp); Errno = NLSADRF; return(0); } (void) fclose(fp); if (adr1[0] == (char)0) adr1[0] = '\n'; if (adr2[0] == (char)0) adr2[0] = '\n'; if(p = strrchr(adr1, (int)'\n')) *p = (char)0; if(p = strrchr(adr2, (int)'\n')) *p = (char)0; fp = NULL;/* * check to make sure the addresses are different before we zap the addr file * 3 cases exist, we are changing both, just la, or just ta */ if ((la && ta && !strcmp(la, ta)) || (la && !ta && !strcmp(la, adr2)) || (!la && ta && !strcmp(adr1, ta))) { fprintf(stderr, "%s: listening addresses not unique\n", arg0); Errno = NLSNOTUNIQ;/* * this little piece allows stuff like "nlsadmin -l addr -t- netspec" * to still print out the 'other' address even if the update fails. Note * that fp will be NULL after this. */ if (la) { flag &= ~LISTEN; la = NULL; } if (ta) { flag &= ~TTY; ta = NULL; } } if (!la) { if (flag & LISTEN) printf("%s\n",adr1); } else { if ((fp = fopen(addrname, "w")) == NULL) { fprintf(stderr, erropen, arg0, Netspec); Errno = NLSOPEN; return(0); } fprintf(fp, "%s\n", la); } if (!ta) { if (flag & TTY) { if (adr2[0] != (char)0) printf("%s\n",adr2); else fprintf(stderr, "Terminal login service address not configured\n"); } if (fp) fprintf(fp, "%s\n", adr2); } else { if (!fp) { if ((fp = fopen(addrname, "w")) == NULL) { fprintf(stderr, erropen, arg0, Netspec); Errno = NLSOPEN; return(0); } fprintf(fp, "%s\n", adr1); } fprintf(fp, "%s\n", ta); } if (fp) { if (fclose(fp) != 0) { fprintf(stderr,errclose,arg0,addrname,errno); Errno = NLSCLOSE; return(0); } if (chown(addrname, nlsuid, nlsgid) < 0) { fprintf(stderr,errchown,arg0, addrname, errno); Errno = NLSCHOWN; return(0); } } if (Errno == NLSNOTUNIQ) return(0); else return(1);}init_db(fp) /* initialize the database file */FILE *fp;{ register int i; for ( i=0; init[i]; i++) fprintf(fp,"%s\n",init[i]);}copy_file(fp, start, finish)register FILE *fp;register int start;register int finish;{ register int i; char dummy[DBFLINESZ]; fseek(fp,0L,0); if (start != 0) { for (i = 0; i < start; i++) if (!fgets(dummy,DBFLINESZ,fp)) { (void) unlink(tempfile); error(errdbf, NLSRDFIL); } } if (finish != -1) { for (i = start; i <= finish; i++) { if (!fgets(dummy, DBFLINESZ, fp)) { (void) unlink(tempfile); error(errdbf, NLSRDFIL); } if (fputs(dummy,tfp) == EOF) { (void) unlink(tempfile); error("error in writing tempfile", NLSDBF); } } /* for */ } else { for ( ; ; ) { if (fgets(dummy, DBFLINESZ, fp) == NULL) { if (feof(fp)) { break; } else { (void) unlink(tempfile); error(errdbf, NLSRDFIL); } } if (fputs(dummy,tfp) == EOF) { (void) unlink(tempfile); error("error in writing tempfile", NLSDBF); } } /* for */ } /* if */}find_pid(name)char *name;{ char pidname[MAXPATH]; char pidchar[10]; FILE *fp; sprintf(pidname, "%s/%s/%s", homedir, name, PIDFILE); if ((fp = fopen(pidname, "r")) == NULL) return(0); if (fscanf(fp, "%s", pidchar) != 1) { (void) fclose(fp); return(0); } (void) fclose(fp); return((int)strtol(pidchar, (char **)NULL, 10));}start_nls(name, flag)char *name;int flag;{ register char *lstr, *tstr; char addrname[MAXPATH]; char buff[100]; FILE *fp; struct netbuf *lbuf = NULL; struct netbuf *tbuf = NULL; int taddr = 0; /* true if remote tty address specified */ extern struct netbuf *stoa(); extern void nlsaddr2c(); if (isactive(name)) error("listener already active",NLSACTIV); if (flag) sprintf(buff, "%s/%s/%s", homedir, name, LISTENER); else sprintf(buff, "%s/%s", homedir, LISTENER); /* read addr file */ sprintf(addrname, "%s/%s/%s", homedir, name, ADDRFILE); if ((fp = fopen(addrname, "r")) == NULL) { fprintf(stderr, erropen, arg0, Netspec); exit(NLSOPEN); } if ((lstr = (char *)malloc(NAMEBUFSZ))==NULL) error(errmalloc, NLSMEM); if ((tstr = (char *)malloc(NAMEBUFSZ))==NULL) error(errmalloc, NLSMEM); *lstr = (char)0; *tstr = (char)0; (void) fgets(lstr, NAMEBUFSZ, fp); (void) fgets(tstr, NAMEBUFSZ, fp); if (*lstr == '\n') { fprintf(stderr,"%s: address not initialized for %s\n",arg0,name); (void) fclose(fp); exit(NLSADRF); } if (*lstr == (char)0 || *tstr == (char)0) { fprintf(stderr,"%s: error in reading, or bad address file\n",arg0); (void) fclose(fp); exit(NLSADRF); } *(lstr + strlen(lstr) -1) = (char)0; *(tstr + strlen(tstr) -1) = (char)0; if (*tstr) taddr = 1; /* call stoa - convert from rfs address to netbuf */ if ((lbuf = stoa(lstr, lbuf)) == NULL) error(errmalloc, NLSMEM); if (taddr) { if ((tbuf = stoa(tstr, tbuf)) == NULL) error(errmalloc, NLSMEM); } /* call nlsaddr2c - convert from netbuf to listener format */ free(lstr); free(tstr); if ((lstr = (char *)malloc(NAMEBUFSZ*2+1))==NULL) error(errmalloc, NLSMEM); if (taddr) { if ((tstr = (char *)malloc(NAMEBUFSZ*2+1))==NULL) error(errmalloc, NLSMEM); } nlsaddr2c(lstr, lbuf->buf, lbuf->len); if (taddr) nlsaddr2c(tstr, tbuf->buf, tbuf->len); (void) fclose(fp); if (taddr) execl( buff, LISTENER, "-n", name, "-l", lstr, "-r", tstr, 0); else execl( buff, LISTENER, "-n", name, "-l", lstr, 0); error("could not exec listener", NLSEXEC ); }kill_nls(name)char *name;{ int pid; if (!isactive(name)) { fprintf(stderr,"%s: there is no listener active for %s\n",arg0,name); Errno = NLSDEAD; return(0); } if((pid = find_pid(name)) == 0) { fprintf(stderr, "%s: could not find pid to send SIGTERM\n",arg0); Errno = NLSFINDP; return(0); } if (kill(pid, SIGTERM) != 0) { fprintf(stderr, "%s: could not send SIGTERM to listener, errno = %d\n",arg0,errno); Errno = NLSSIG; return(0); } return(1);}close_temp(fname)char *fname;{ if (fclose(tfp) == EOF) { fprintf(stderr,errclose,arg0,errno); Errno = NLSCLOSE; (void) unlink(tempfile); return(0); } if (unlink(fname)) { fprintf(stderr,"%s: cannot unlink %s, errno = %d\n", arg0, fname,errno); Errno = NLSULINK; (void) unlink(tempfile); return(0); } if (link(tempfile, fname)) { fprintf(stderr,"%s: cannot link %s to %s, errno = %d\n", arg0, tempfile, fname,errno); Errno = NLSLINK; (void) unlink(tempfile); return(0); } if (unlink(tempfile)) { fprintf(stderr,"%s: cannot unlink %s, errno = %d\n",arg0,tempfile,errno); Errno = NLSULINK; return(0); } if (chown(fname, nlsuid, nlsgid) < 0) { fprintf(stderr,errchown,arg0, fname, errno); Errno = NLSCHOWN; return(0); } return(1);}open_temp(){ signal(SIGHUP, SIG_IGN); signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); umask(0333); if (access(tempfile,0) >= 0) { fprintf(stderr, "%s: tempfile busy; try again later\n", arg0); Errno = NLSBUSY; return(0); } if ((tfp = fopen(tempfile, "w")) == NULL) { fprintf(stderr, "%s: cannot create tempfile, errno = %d\n", arg0,errno); Errno = NLSCREAT; return(0); } umask(022); return(1);}find_serv(fp, code) /* find a service routine */FILE *fp;char *code;{ register int n; register int total = -1; extern int read_dbf();/* * read_dbf() returns a line number relative to the file pointer. * total is the absolute file offset. */ while ((n = read_dbf(fp)) >= 0) { if (total == -1) total = n; else total = total + n + 1; if (strncmp(serv_str, code, SVC_CODE_SZ) == 0) return(total); } if (n == -2) error("error in reading database file", Errno); return(-1);}print_spec(fname)char *fname;{ FILE *fp; register int n; int ignore; if ((fp = fopen(fname, "r")) == NULL) { fprintf(stderr, erropen, arg0, Netspec); Errno = NLSOPEN; return; } while ((n = read_dbf(fp)) >= 0) { ignore = (int)strchr(flag_str, (int)'x') + (int)strchr(flag_str, (int)'X');#ifdef S4 printf("%s\t%s\t%s\t%s\t%s\n",serv_str, ignore?"DISABLED":"ENABLED ",id_str,path_str,cmnt_str);#else if (mod_str[0] == (char)0) printf("%s\t%s\t%s\tNOMODULES\t%s\t%s\n",serv_str, ignore?"DISABLED":"ENABLED ",id_str,path_str,cmnt_str); else printf("%s\t%s\t%s\t%s\t%s\t%s\n",serv_str, ignore?"DISABLED":"ENABLED ",id_str,mod_str,path_str,cmnt_str);#endif /* S4 */ } if (n == -2) error("error in reading database file", Errno); (void) fclose(fp);}print_all(dir) /* CAUTION: recursive routine */char *dir;{ register char dirname[MAXNAMLEN]; char *nspec; struct dirent *dp; DIR *dfp; if ((dfp = opendir(dir)) == (DIR *) NULL) error(errdbf, NLSRDFIL); while ((dp = readdir(dfp)) != (struct dirent *) NULL) { sprintf(dirname,"%s/%s",dir,dp->d_name); if (!strcmp(dp->d_name, ".") || ! strcmp(dp->d_name, "..")) continue; if (isdir(dirname)) { print_all(dirname); } else { if (strcmp(dp->d_name, DBFILE) == 0) { nspec = dir + strlen(homedir) + 1; printf("%s\t%s\n",nspec,isactive(nspec)?"ACTIVE":"INACTIVE"); } } } closedir(dfp);}print_serv(fname, code, flag)char *fname;char *code;int flag;{ register FILE *fp; register int ignore; if ((fp = fopen(fname, "r")) == NULL) { fprintf(stderr, erropen, arg0, Netspec); exit(NLSOPEN); } if (find_serv(fp, code) == -1) { fprintf(stderr,errscode, arg0, code, fname); exit(NLSNOCODE); } (void) fclose(fp); ignore = (int)strchr(flag_str, (int)'x') + (int)strchr(flag_str, (int)'X'); if (ignore) ignore = 1; if (flag) exit(ignore);#ifdef S4 printf("%s\t%s\t%s\t%s\t%s\n",serv_str, ignore?"DISABLED":"ENABLED ",id_str,path_str,cmnt_str);#else if (mod_str[0] == (char)0) printf("%s\t%s\t%s\tNOMODULES\t%s\t%s\n",serv_str, ignore?"DISABLED":"ENABLED ",id_str,path_str,cmnt_str); else printf("%s\t%s\t%s\t%s\t%s\t%s\n",serv_str, ignore?"DISABLED":"ENABLED ",id_str,mod_str,path_str,cmnt_str);#endif /* S4 */ exit(NLSOK);}ok_netspec(nspec) /* validates a net_spec name */char *nspec;{ register int size; register char *cp, *n; if (!nspec) return(0); if (*nspec == '/') return(0); for (cp = nspec; *cp; cp += size) { if (*cp == '.') return(0); if ((n = strchr(cp, (int)'/')) == NULL) size = strlen(cp); else size = n - cp + 1; } return(1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -