head 1.2; access; symbols; locks alexis:1.2; strict; comment @ * @; 1.2 date 97.01.31.14.43.34; author alexis; state Exp; branches; next 1.1; 1.1 date 97.01.31.10.22.48; author alexis; state Exp; branches; next ; desc @WORKING MILESTONE @ 1.2 log @MILEPOST - MAY NOT WORK @ text @#include /* for FILE* and NULL */ #include /* for strstr() */ #include "pppld_utils.h" /* for debug funcs */ extern char pppd_cmd[]; extern char chat_cmd[]; extern char announce_cmd[]; extern char port[]; extern char qualitycheck_remhost[]; extern char netmask[]; extern int speed; extern char chatfile[]; extern char login[]; extern char password[]; extern char telno[]; extern int idlecheck_timeout; extern int idlecheck_interval; extern int qualitycheck_interval; extern int qualitycheck_minquality; extern int qualitycheck_interval; static char *rcs_id = "$Id$"; int load_config( char *fname, int level) { FILE *fp; static FILE *chat_fp; char linebuf[128]; int lineno; char *cp; int rc; char *endcp, *valcp, *colcp, *cmdcp; debug(DBG_FUNCS, "load_config: sof"); if ((fp=fopen(fname, "r")) == NULL) { warning("can't open %s", fname); return(1); } if (level == 0) { *pppd_cmd = '\0'; *chat_cmd = '\0'; idlecheck_timeout = -1; qualitycheck_minquality = -1; qualitycheck_interval = -1; *announce_cmd = '\0'; *port = '\0'; *qualitycheck_remhost = '\0'; *netmask = '\0'; speed = -1; *login = '\0'; *password = '\0'; *telno = '\0'; sprintf(chatfile, "%s/%s.%d.chat", TMP_DIR, progname, getpid()); if ((chat_fp=fopen(chatfile, "w")) == NULL) { perror("fopen"); fclose(fp); return(8); } } for (lineno=1; fgets(linebuf, sizeof(linebuf), fp) != NULL; lineno++) { /* find the proper beginning of the line */ for (cmdcp=linebuf; *cmdcp && (*cmdcp=='\t' || *cmdcp==' '); cmdcp++) ; if (!*cmdcp) { continue; } else if (*cmdcp == '\n') { continue; } else if (*cmdcp == '#') { continue; } /* terminate the first word */ for (endcp=cmdcp; *endcp && *endcp!='\n' && *endcp!=':' && *endcp!=' ' && *endcp!='\t'; endcp++) ; if (*endcp == '\n' || !*endcp) { warning("no colon in line %d of file %s", lineno, fname); fclose(fp); if (level == 0) { fclose(chat_fp); unlink(chatfile); } return(2); } else if (endcp == cmdcp) { warning("no command in line %d of file %s", lineno, fname); fclose(fp); if (level == 0) { fclose(chat_fp); unlink(chatfile); } return(3); } /* but locate the colon first, 'cos terminating the first word may overwrite the colon and then we've lost it! */ for (colcp=endcp; *colcp && *colcp !=':'; colcp++) ; if (!*colcp) { warning("no colon in line %d of file %s", lineno, fname); fclose(fp); if (level == 0) { fclose(chat_fp); unlink(chatfile); } return(4); } /* now go back and terminate the word */ *endcp = '\0'; /* find the first letter of the value */ for (valcp=colcp+1; *valcp && (*valcp=='\n' || *valcp=='\t' || *valcp==' '); valcp++) ; if (!*valcp) { warning("command without value at line %d of file %s", lineno, fname); fclose(fp); if (level == 0) { fclose(chat_fp); unlink(chatfile); } return(5); } /* finally remove any trailing whitespace */ for (endcp=valcp+strlen(valcp)-1; endcp>valcp && (*endcp==' ' || *endcp=='\t' || *endcp=='\n'); endcp--) { ; } *(endcp+1) = '\0'; if (strcmp(cmdcp, "include") == 0) { if ((rc=load_config(valcp, level+1)) != 0) { fclose(fp); if (level == 0) { fclose(chat_fp); unlink(chatfile); } return(rc); } } else if (strcmp(cmdcp, "pppd") == 0) { strcpy(pppd_cmd, valcp); } else if (strcmp(cmdcp, "chat") == 0) { strcpy(chat_cmd, valcp); } else if (strcmp(cmdcp, "idle") == 0) { idlecheck_timeout = atoi(valcp); } else if (strcmp(cmdcp, "quality") == 0) { qualitycheck_minquality = atoi(valcp); } else if (strcmp(cmdcp, "announce") == 0) { strcpy(announce_cmd, valcp); } else if (strcmp(cmdcp, "login") == 0) { strcpy(login, valcp); } else if (strcmp(cmdcp, "password") == 0) { strcpy(password, valcp); } else if (strcmp(cmdcp, "telno") == 0) { strcpy(telno, valcp); } else if (strcmp(cmdcp, "qualitycheck_remhost") == 0) { strcpy(qualitycheck_remhost, valcp); } else if (strcmp(cmdcp, "port") == 0) { strcpy(port, valcp); } else if (strcmp(cmdcp, "netmask") == 0) { strcpy(netmask, valcp); } else if (strcmp(cmdcp, "speed") == 0) { speed = atoi(valcp); } else if (strcmp(cmdcp, "chatline") == 0) { if ((cp=strstr(valcp, "%T")) != NULL && !*telno) { warning("chatline using %T with no telephone number defined yet"); fclose(fp); if (level == 0) { fclose(chat_fp); unlink(chatfile); } return(9); } else if (cp != NULL) { *(cp+1) = 's'; fprintf(chat_fp, valcp, telno); } else if ((cp=strstr(valcp, "%I")) != NULL && idlecheck_timeout == -1) { warning("chatline using %I with no idle timeout defined yet"); fclose(fp); if (level == 0) { fclose(chat_fp); unlink(chatfile); } return(9); } else if (cp != NULL) { *(cp+1) = 'd'; fprintf(chat_fp, valcp, idlecheck_timeout); } else if ((cp=strstr(valcp, "%L")) != NULL && !*login) { warning("chatline using %L with no login defined yet"); fclose(fp); if (level == 0) { fclose(chat_fp); unlink(chatfile); } return(9); } else if (cp != NULL) { *(cp+1) = 's'; fprintf(chat_fp, valcp, login); } else if ((cp=strstr(valcp, "%P")) != NULL && !*password) { warning("chatline using %P with no password defined yet"); fclose(fp); if (level == 0) { fclose(chat_fp); unlink(chatfile); } return(9); } else if (cp != NULL) { *(cp+1) = 's'; fprintf(chat_fp, valcp, password); } else { fprintf(chat_fp, valcp); } fprintf(chat_fp, "\n"); } else { warning("invalid command '%s' at line %d of file %s", cmdcp, lineno, fname); fclose(fp); if (level == 0) { fclose(chat_fp); unlink(chatfile); } return(6); } } fclose(fp); if (level == 0) { fclose(chat_fp); if (!*pppd_cmd) { warning("pppd not defined in config files"); unlink(chatfile); return(7); } else if (!*chat_cmd) { warning("chat not defined in config files"); unlink(chatfile); return(7); } else if (idlecheck_timeout == -1) { warning("idle not defined in config files"); unlink(chatfile); return(7); } else if (qualitycheck_minquality == -1) { warning("quality not defined in config files"); unlink(chatfile); return(7); } else if (!*announce_cmd) { warning("announce command not defined in config files"); unlink(chatfile); return(7); } else if (!*netmask) { warning("netmask not defined in config files"); unlink(chatfile); return(7); } else if (!*qualitycheck_remhost) { warning("qualitycheck_remhost not defined in config files"); unlink(chatfile); return(7); } else if (!*port) { warning("port not defined in config files"); unlink(chatfile); return(7); } else if (!*telno) { warning("telno not defined in config files"); unlink(chatfile); return(7); } else if (!*password) { warning("password not defined in config files"); unlink(chatfile); return(7); } else if (!*login) { warning("login not defined in config files"); unlink(chatfile); return(7); } else if (speed == -1) { warning("speed not defined in config files"); unlink(chatfile); return(7); } if (qualitycheck_interval == -1) { info("qinterval not defined in config files, calculating"); qualitycheck_interval = 2 * idlecheck_timeout; } else if (qualitycheck_interval < 2*idlecheck_timeout && qualitycheck_minquality) { /* idle checks must fit entirely inside the quality check interval since the quality check alters the usage count */ warning("qinterval should be at least twice idle timeout!"); /* don't adjust it though */ } /* check the level every quarter of that interval - so a max idle of 1.25 * 300 - 1/inf */ idlecheck_interval = idlecheck_timeout / 4; } return(0); } @ 1.1 log @Initial revision @ text @d22 2 d36 2 a67 1 /* debug(DBG_FILES, "finding beginning of command"); */ a70 1 debug(DBG_FILES, "unterminated empty line"); a72 1 /* debug(DBG_FILES, "empty line"); */ a74 1 /* debug(DBG_FILES, "comment"); */ a78 1 /* debug(DBG_FILES, "terminating command"); */ a100 1 /* debug(DBG_FILES, "locating colon and terminating command"); */ a115 1 /* debug(DBG_FILES, "locating beginning of value"); */ a128 1 /* debug(DBG_FILES, "terminating value"); */ a129 1 /* debug(DBG_FILES, "examinging [%c]", *endcp ? *endcp : '0'); */ a133 1 debug(DBG_FILES, "cmd=[%s], value=[%s]", cmdcp, valcp); a135 1 debug(DBG_FILES, "subincluding %s", valcp); a145 1 debug(DBG_FILES, "reading pppd"); a148 1 debug(DBG_FILES, "reading chat"); a151 1 debug(DBG_FILES, "reading idle timeout"); a154 1 debug(DBG_FILES, "reading minimum quality"); a157 1 debug(DBG_FILES, "reading announce"); a160 1 debug(DBG_FILES, "reading login"); a163 1 debug(DBG_FILES, "reading password"); a166 1 debug(DBG_FILES, "reading telno"); a169 1 debug(DBG_FILES, "reading qualitycheck_remhost"); a172 1 debug(DBG_FILES, "reading port"); a175 1 debug(DBG_FILES, "reading netmask"); a178 1 debug(DBG_FILES, "reading speed"); a181 1 debug(DBG_FILES, "reading a chatline"); a190 1 debug(DBG_FILES, "chatline using %T and telephone number defined"); a201 1 debug(DBG_FILES, "chatline using %I and idle timeout defined"); a212 1 debug(DBG_FILES, "chatline using %%L and login defined"); a223 1 debug(DBG_FILES, "chatline using %P and password defined"); a226 1 debug(DBG_FILES, "chatline not using %P, %I, %%L or %T"); a295 1 debug(DBG_FILES, "all required words present"); @