📄 ntpdc.c
字号:
continue; if (strncmp(str, cl->keyword, (unsigned)clen) == 0) { /* * Could be extact match, could be approximate. * Is exact if the length of the keyword is the * same as the str. */ if (*((cl->keyword) + clen) == '\0') { *cmd = cl; return 1; } nmatch++; nearmatch = cl; } } /* * See if there is more to do. If so, go again. Sorry about the * goto, too much looking at BSD sources... */ if (clist == clist1 && clist2 != 0) { clist = clist2; goto again; } /* * If we got extactly 1 near match, use it, else return number * of matches. */ if (nmatch == 1) { *cmd = nearmatch; return 1; } return nmatch;}/* * getarg - interpret an argument token * * return: 0 - failure * 1 - success * -1 - skip to next token */static intgetarg( char *str, int code, arg_v *argp ){ int isneg; char *cp, *np; static const char *digits = "0123456789"; switch (code & ~OPT) { case NTP_STR: argp->string = str; break; case NTP_ADD: if (!strcmp("-6", str)) { ai_fam_templ = AF_INET6; return -1; } else if (!strcmp("-4", str)) { ai_fam_templ = AF_INET; return -1; } if (!getnetnum(str, &(argp->netnum), (char *)0, 0)) { return 0; } break; case NTP_INT: case NTP_UINT: isneg = 0; np = str; if (*np == '-') { np++; isneg = 1; } argp->uval = 0; do { cp = strchr(digits, *np); if (cp == NULL) { (void) fprintf(stderr, "***Illegal integer value %s\n", str); return 0; } argp->uval *= 10; argp->uval += (cp - digits); } while (*(++np) != '\0'); if (isneg) { if ((code & ~OPT) == NTP_UINT) { (void) fprintf(stderr, "***Value %s should be unsigned\n", str); return 0; } argp->ival = -argp->ival; } break; case IP_VERSION: if (!strcmp("-6", str)) argp->ival = 6 ; else if (!strcmp("-4", str)) argp->ival = 4 ; else { (void) fprintf(stderr, "***Version must be either 4 or 6\n"); return 0; } break; } return 1;}/* * getnetnum - given a host name, return its net number * and (optional) full name */static intgetnetnum( const char *hname, struct sockaddr_storage *num, char *fullhost, int af ){ int sockaddr_len; struct addrinfo hints, *ai = NULL; sockaddr_len = (af == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6); memset((char *)&hints, 0, sizeof(struct addrinfo)); hints.ai_flags = AI_CANONNAME;#ifdef AI_ADDRCONFIG hints.ai_flags |= AI_ADDRCONFIG;#endif /* decodenetnum only works with addresses */ if (decodenetnum(hname, num)) { if (fullhost != 0) { getnameinfo((struct sockaddr *)num, sockaddr_len, fullhost, sizeof(fullhost), NULL, 0, NI_NUMERICHOST); } return 1; } else if (getaddrinfo(hname, "ntp", &hints, &ai) == 0) { memmove((char *)num, ai->ai_addr, ai->ai_addrlen); if (fullhost != 0) (void) strcpy(fullhost, ai->ai_canonname); return 1; } else { (void) fprintf(stderr, "***Can't find host %s\n", hname); return 0; } /*NOTREACHED*/}/* * nntohost - convert network number to host name. This routine enforces * the showhostnames setting. */char *nntohost( struct sockaddr_storage *netnum ){ if (!showhostnames) return stoa(netnum); if ((netnum->ss_family == AF_INET) && ISREFCLOCKADR(netnum)) return refnumtoa(netnum); return socktohost(netnum);}/* * Finally, the built in command handlers *//* * help - tell about commands, or details of a particular command */static voidhelp( struct parse *pcmd, FILE *fp ){ struct xcmd *xcp; char *cmd; const char *list[100]; int word, words; int row, rows; int col, cols; if (pcmd->nargs == 0) { words = 0; for (xcp = builtins; xcp->keyword != 0; xcp++) { if (*(xcp->keyword) != '?') list[words++] = xcp->keyword; } for (xcp = opcmds; xcp->keyword != 0; xcp++) list[words++] = xcp->keyword; qsort(#ifdef QSORT_USES_VOID_P (void *)#else (char *)#endif (list), (size_t)(words), sizeof(char *), helpsort); col = 0; for (word = 0; word < words; word++) { int length = strlen(list[word]); if (col < length) { col = length; } } cols = SCREENWIDTH / ++col; rows = (words + cols - 1) / cols; (void) fprintf(fp, "ntpdc commands:\n"); for (row = 0; row < rows; row++) { for (word = row; word < words; word += rows) { (void) fprintf(fp, "%-*.*s", col, col-1, list[word]); } (void) fprintf(fp, "\n"); } } else { cmd = pcmd->argval[0].string; words = findcmd(cmd, builtins, opcmds, &xcp); if (words == 0) { (void) fprintf(stderr, "Command `%s' is unknown\n", cmd); return; } else if (words >= 2) { (void) fprintf(stderr, "Command `%s' is ambiguous\n", cmd); return; } (void) fprintf(fp, "function: %s\n", xcp->comment); printusage(xcp, fp); }}/* * helpsort - do hostname qsort comparisons */#ifdef QSORT_USES_VOID_Pstatic inthelpsort( const void *t1, const void *t2 ){ char const * const * name1 = (char const * const *)t1; char const * const * name2 = (char const * const *)t2; return strcmp(*name1, *name2);}#elsestatic inthelpsort( char **name1, char **name2 ){ return strcmp(*name1, *name2);}#endif/* * printusage - print usage information for a command */static voidprintusage( struct xcmd *xcp, FILE *fp ){ int i, opt46; opt46 = 0; (void) fprintf(fp, "usage: %s", xcp->keyword); for (i = 0; i < MAXARGS && xcp->arg[i] != NO; i++) { if (opt46 == 0 && (xcp->arg[i] & ~OPT) == NTP_ADD) { (void) fprintf(fp, " [ -4|-6 ]"); opt46 = 1; } if (xcp->arg[i] & OPT) (void) fprintf(fp, " [ %s ]", xcp->desc[i]); else (void) fprintf(fp, " %s", xcp->desc[i]); } (void) fprintf(fp, "\n");}/* * timeout - set time out time */static voidtimeout( struct parse *pcmd, FILE *fp ){ int val; if (pcmd->nargs == 0) { val = tvout.tv_sec * 1000 + tvout.tv_usec / 1000; (void) fprintf(fp, "primary timeout %d ms\n", val); } else { tvout.tv_sec = pcmd->argval[0].uval / 1000; tvout.tv_usec = (pcmd->argval[0].uval - (tvout.tv_sec * 1000)) * 1000; }}/* * my_delay - set delay for auth requests */static voidmy_delay( struct parse *pcmd, FILE *fp ){ int isneg; u_long val; if (pcmd->nargs == 0) { val = delay_time.l_ui * 1000 + delay_time.l_uf / 4294967; (void) fprintf(fp, "delay %lu ms\n", val); } else { if (pcmd->argval[0].ival < 0) { isneg = 1; val = (u_long)(-pcmd->argval[0].ival); } else { isneg = 0; val = (u_long)pcmd->argval[0].ival; } delay_time.l_ui = val / 1000; val %= 1000; delay_time.l_uf = val * 4294967; /* 2**32/1000 */ if (isneg) L_NEG(&delay_time); }}/* * host - set the host we are dealing with. */static voidhost( struct parse *pcmd, FILE *fp ){ int i; if (pcmd->nargs == 0) { if (havehost) (void) fprintf(fp, "current host is %s\n", currenthost); else (void) fprintf(fp, "no current host\n"); return; } i = 0; if (pcmd->nargs == 2) { if (!strcmp("-4", pcmd->argval[i].string)) ai_fam_templ = AF_INET; else if (!strcmp("-6", pcmd->argval[i].string)) ai_fam_templ = AF_INET6; else { if (havehost) (void) fprintf(fp, "current host remains %s\n", currenthost); else (void) fprintf(fp, "still no current host\n"); return; } i = 1; } if (openhost(pcmd->argval[i].string)) { (void) fprintf(fp, "current host set to %s\n", currenthost); } else { if (havehost) (void) fprintf(fp, "current host remains %s\n", currenthost); else (void) fprintf(fp, "still no current host\n"); }}/* * keyid - get a keyid to use for authenticating requests */static voidkeyid( struct parse *pcmd, FILE *fp ){ if (pcmd->nargs == 0) { if (info_auth_keyid == 0) (void) fprintf(fp, "no keyid defined\n"); else (void) fprintf(fp, "keyid is %lu\n", (u_long)info_auth_keyid); } else { info_auth_keyid = pcmd->argval[0].uval; }}/* * keytype - get type of key to use for authenticating requests */static voidkeytype( struct parse *pcmd, FILE *fp ){ if (pcmd->nargs == 0) fprintf(fp, "keytype is %s\n", (info_auth_keytype == KEY_TYPE_MD5) ? "MD5" : "???"); else switch (*(pcmd->argval[0].string)) { case 'm': case 'M': info_auth_keytype = KEY_TYPE_MD5; break; default: fprintf(fp, "keytype must be 'md5'\n"); }}/* * passwd - get an authentication key *//*ARGSUSED*/static voidpasswd( struct parse *pcmd, FILE *fp ){ char *pass; if (info_auth_keyid == 0) { info_auth_keyid = getkeyid("Keyid: "); if (info_auth_keyid == 0) { (void)fprintf(fp, "Keyid must be defined\n"); return; } } if (!interactive) { authusekey(info_auth_keyid, info_auth_keytype, (u_char *)pcmd->argval[0].string); authtrust(info_auth_keyid, 1); } else { pass = getpass("MD5 Password: "); if (*pass == '\0') (void) fprintf(fp, "Password unchanged\n"); else { authusekey(info_auth_keyid, info_auth_keytype, (u_char *)pass); authtrust(info_auth_keyid, 1); } }}/* * hostnames - set the showhostnames flag */static voidhostnames( struct parse *pcmd, FILE *fp ){ if (pcmd->nargs == 0) { if (showhostnames) (void) fprintf(fp, "hostnames being shown\n"); else (void) fprintf(fp, "hostnames not being shown\n"); } else { if (STREQ(pcmd->argval[0].string, "yes")) showhostnames = 1; else if (STREQ(pcmd->argval[0].string, "no")) showhostnames = 0; else (void)fprintf(stderr, "What?\n"); }}/* * setdebug - set/change debugging level */static voidsetdebug( struct parse *pcmd, FILE *fp ){ if (pcmd->nargs == 0) { (void) fprintf(fp, "debug level is %d\n", debug); return; } else if (STREQ(pcmd->argval[0].string, "no")) { debug = 0; } else if (STREQ(pcmd->argval[0].string, "more")) { debug++; } else if (STREQ(pcmd->argval[0].string, "less")) { debug--; } else { (void) fprintf(fp, "What?\n"); return; } (void) fprintf(fp, "debug level set to %d\n", debug);}/* * quit - stop this nonsense *//*ARGSUSED*/static voidquit( struct parse *pcmd, FILE *fp ){ if (havehost) closesocket(sockfd); exit(0);}/* * version - print the current version number *//*ARGSUSED*/static voidversion( struct parse *pcmd, FILE *fp ){ (void) fprintf(fp, "%s\n", Version); return;}/* * warning - print a warning message */static voidwarning( const char *fmt, const char *st1, const char *st2 ){ (void) fprintf(stderr, "%s: ", progname); (void) fprintf(stderr, fmt, st1, st2); (void) fprintf(stderr, ": "); perror("");}/* * error - print a message and exit */static voiderror( const char *fmt, const char *st1, const char *st2 ){ warning(fmt, st1, st2); exit(1);}/* * getkeyid - prompt the user for a keyid to use */static u_longgetkeyid( const char *keyprompt ){ register char *p; register int c; FILE *fi; char pbuf[20];#ifndef SYS_WINNT if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)#else if ((fi = _fdopen((int)GetStdHandle(STD_INPUT_HANDLE), "r")) == NULL)#endif /* SYS_WINNT */ fi = stdin; else setbuf(fi, (char *)NULL); fprintf(stderr, "%s", keyprompt); fflush(stderr); for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) { if (p < &pbuf[18]) *p++ = (char) c; } *p = '\0'; if (fi != stdin) fclose(fi); return (u_int32)atoi(pbuf);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -