FreeBSD/contrib/ntp/ntpq/ntpq-subs.c
/* 
 * config_from_file - remotely configure an ntpd daemon using the
 * specified configuration file
 * SK: This function is a kludge at best and is full of bad design
 * bugs:
 * 1. ntpq uses UDP, which means that there is no guarantee of in-order,
 *    error-free delivery. 
 * 2. The maximum length of a packet is constrained, and as a result, the
 *    maximum length of a line in a configuration file is constrained. 
 *    Longer lines will lead to unpredictable results.
 * 3. Since this function is sending a line at a time, we can't update
 *    the control key through the configuration file (YUCK!!)
 *
 * Pearly: There are a few places where 'size_t' is cast to 'int' based
 * on the assumption that 'int' can hold the size of the involved
 * buffers without overflow.
 */
remotely configure an ntpd daemon using the
specified configuration file
SK: This function is a kludge at best and is full of bad design
bugs:
1. ntpq uses UDP, which means that there is no guarantee of in-order,
error-free delivery.
2. The maximum length of a packet is constrained, and as a result, the
maximum length of a line in a configuration file is constrained.
Longer lines will lead to unpredictable results.
3. Since this function is sending a line at a time, we can't update
the control key through the configuration file (YUCK!!)


remotely configure an ntpd daemon using the specified configuration file SK : This function is a kludge at best and is full of bad design bugs : 1 . ntpq uses UDP which means that there is no guarantee of in - order error - free delivery . 2 . The maximum length of a packet is constrained and as a result the maximum length of a line in a configuration file is constrained . Longer lines will lead to unpredictable results . 3 . Since this function is sending a line at a time we can ' t update the control key through the configuration file ( YUCK!! )
static void 
config_from_file (
	struct parse *pcmd,
	FILE *fp
	)
{
	u_short rstatus;
	size_t rsize;
	const char *rdata;
	char * cp;
	int res;
	FILE *config_fd;
	char config_cmd[MAXLINE];
	size_t config_len;
	int i;
	int retry_limit;
	if (debug > 2)
		fprintf(stderr,
			"In Config\n"
			"Keyword = %s\n"
			"Filename = %s\n", pcmd->keyword,
			pcmd->argval[0].string);
	config_fd = fopen(pcmd->argval[0].string, "r");
	if (NULL == config_fd) {
		printf("ERROR!! Couldn't open file: %s\n",
		       pcmd->argval[0].string);
		return;
	}
	printf("Sending configuration file, one line at a time.\n");
	i = 0;
	while (fgets(config_cmd, MAXLINE, config_fd) != NULL) {
		cp = strchr(config_cmd, '#');
		config_len = (NULL != cp)
		    ? (size_t)(cp - config_cmd)
		    : strlen(config_cmd);
		while (config_len != 0 &&
		       (u_char)config_cmd[config_len-1] <= ' ')
			--config_len;
		config_cmd[config_len] = '\0';
		++i;
		if (0 == config_len)
			continue;
		retry_limit = 2;
		do 
			res = doquery(CTL_OP_CONFIGURE, 0, 1,
				      config_len, config_cmd,
				      &rstatus, &rsize, &rdata);
		while (res != 0 && retry_limit--);
		if (res != 0) {
			printf("Line No: %d query failed: %.*s\n"
			       "Subsequent lines not sent.\n",
			       i, (int)config_len, config_cmd);
			fclose(config_fd);
			return;
		}
		while (rsize != 0 && (u_char)rdata[rsize - 1] <= ' ')
			--rsize;
		printf("Line No: %d %.*s: %.*s\n", i,
		       (int)rsize, rdata,
		       (int)config_len, config_cmd);
	}
	printf("Done sending file\n");
	fclose(config_fd);
}
static void config_from_file ( struct parse * pcmd , FILE * fp ) { u_short rstatus ; size_t rsize ; const char * rdata ; char * cp ; int res ; FILE * config_fd ; char config_cmd [ MAXLINE ] ; size_t config_len ; int i ; int retry_limit ; if ( debug > 2 ) fprintf ( stderr , " \n " " \n " " \n " , pcmd -> keyword , pcmd -> argval [ 0 ] . string ) ; config_fd = fopen ( pcmd -> argval [ 0 ] . string , " " ) ; if ( NULL == config_fd ) { printf ( " \n " , pcmd -> argval [ 0 ] . string ) ; return ; } printf ( " \n " ) ; i = 0 ; while ( fgets ( config_cmd , MAXLINE , config_fd ) != NULL ) { cp = strchr ( config_cmd , ' ' ) ; config_len = ( NULL != cp ) ? ( size_t ) ( cp - config_cmd ) : strlen ( config_cmd ) ; while ( config_len != 0 && ( u_char ) config_cmd [ config_len - 1 ] <= ' ' ) -- config_len ; config_cmd [ config_len ] = ' \0 ' ; ++ i ; if ( 0 == config_len ) continue ; retry_limit = 2 ; do res = doquery ( CTL_OP_CONFIGURE , 0 , 1 , config_len , config_cmd , & rstatus , & rsize , & rdata ) ; while ( res != 0 && retry_limit -- ) ; if ( res != 0 ) { printf ( " \n " " \n " , i , ( int ) config_len , config_cmd ) ; fclose ( config_fd ) ; return ; } while ( rsize != 0 && ( u_char ) rdata [ rsize - 1 ] <= ' ' ) -- rsize ; printf ( " \n " , i , ( int ) rsize , rdata , ( int ) config_len , config_cmd ) ; } printf ( " \n " ) ; fclose ( config_fd ) ; }
config_from_file - remotely configure an ntpd daemon using the
 specified configuration file
 SK: This function is a kludge at best and is full of bad design
 bugs:
 1. ntpq uses UDP, which means that there is no guarantee of in-order,
    error-free delivery.
config_from_file - remotely configure an ntpd daemon using the specified configuration file SK : This function is a kludge at best and is full of bad design bugs : 1 . ntpq uses UDP which means that there is no guarantee of in - order error - free delivery .
/* Eliminate comments first. */ /* [Bug 3015] make sure there's no trailing whitespace;
		 * the fix for [Bug 2853] on the server side forbids
		 * those. And don't transmit empty lines, as this would
		 * just be waste.
		 */ /* Right-strip the result code string, then output the
		 * last line executed, with result code. */