static Averages *GetCurrentAverages(char *timekey) { CF_DB *dbp; static Averages entry; if (!OpenDB(&dbp, dbid_observations)) { return NULL; } memset(&entry, 0, sizeof(entry)); AGE++; WAGE = AGE / SECONDS_PER_WEEK * CF_MEASURE_INTERVAL; if (ReadDB(dbp, timekey, &entry, sizeof(Averages))) { int i; for (i = 0; i < CF_OBSERVABLES; i++) { CfDebug("Previous values (%lf,..) for time index %s\n\n", entry.Q[i].expect, timekey); } } else { CfDebug("No previous value for time index %s\n", timekey); } CloseDB(dbp); return &entry; }
Bundle *AppendBundle(Bundle **start, char *name, char *type, Rlist *args) { Bundle *bp, *lp; CfDebug("Appending new bundle %s %s (", type, name); if (DEBUG) { ShowRlist(stdout, args); } CfDebug(")\n"); CheckBundle(name, type); bp = xcalloc(1, sizeof(Bundle)); if (*start == NULL) { *start = bp; } else { for (lp = *start; lp->next != NULL; lp = lp->next) { } lp->next = bp; } bp->name = xstrdup(name); bp->type = xstrdup(type); bp->args = args; return bp; }
long TimeAbs2Int(char *s) { time_t cftime; int i; char mon[4], h[3], m[3]; long month = 0, day = 0, hour = 0, min = 0, year = 0; static long days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (s == NULL) { return CF_NOINT; } year = Str2Int(VYEAR); if (year % 4 == 0) /* leap years */ { days[1] = 29; } if (strstr(s, ":")) /* Hr:Min */ { sscanf(s, "%2[^:]:%2[^:]:", h, m); month = Month2Int(VMONTH); day = Str2Int(VDAY); hour = Str2Int(h); min = Str2Int(m); } else /* date Month */ { sscanf(s, "%3[a-zA-Z] %ld", mon, &day); month = Month2Int(mon); if (Month2Int(VMONTH) < month) { /* Wrapped around */ year--; } } CfDebug("(%s)\n%ld=%s,%ld=%s,%ld,%ld,%ld\n", s, year, VYEAR, month, VMONTH, day, hour, min); cftime = 0; cftime += min * 60; cftime += hour * 3600; cftime += (day - 1) * 24 * 3600; cftime += 24 * 3600 * ((year - 1970) / 4); /* Leap years */ for (i = 0; i < month - 1; i++) { cftime += days[i] * 24 * 3600; } cftime += (year - 1970) * 365 * 24 * 3600; CfDebug("Time %s CORRESPONDS %s\n", s, cf_ctime(&cftime)); return (long) cftime; }
int IsIPV6Address(char *name) { char *sp; int count, max = 0; CfDebug("IsIPV6Address(%s)\n", name); if (name == NULL) { return false; } count = 0; for (sp = name; *sp != '\0'; sp++) { if (isalnum((int) *sp)) { count++; } else if ((*sp != ':') && (*sp != '.')) { return false; } if (*sp == 'r') { return false; } if (count > max) { max = count; } else { count = 0; } } if (max <= 2) { CfDebug("Looks more like a MAC address"); return false; } if (strstr(name, ":") == NULL) { return false; } if (strcasestr(name, "scope")) { return false; } return true; }
void RvalDestroy(Rval rval) { Rlist *clist, *next = NULL; CfDebug("DeleteRvalItem(%c)", rval.type); if (DEBUG) { RvalShow(stdout, rval); } CfDebug("\n"); if (rval.item == NULL) { CfDebug("DeleteRval NULL\n"); return; } switch (rval.type) { case RVAL_TYPE_SCALAR: ThreadLock(cft_lock); free((char *) rval.item); ThreadUnlock(cft_lock); break; case RVAL_TYPE_LIST: /* rval is now a list whose first item is clist->item */ for (clist = (Rlist *) rval.item; clist != NULL; clist = next) { next = clist->next; if (clist->item) { RvalDestroy((Rval) {clist->item, clist->type}); } free(clist); } break; case RVAL_TYPE_FNCALL: FnCallDestroy((FnCall *) rval.item); break; default: CfDebug("Nothing to do\n"); return; } }
int ArchiveToRepository(const char *file, Attributes attr, Promise *pp, const ReportContext *report_context) /* Returns true if the file was backup up and false if not */ { char destination[CF_BUFSIZE]; struct stat sb, dsb; if (!GetRepositoryPath(file, attr, destination)) { return false; } if (attr.copy.backup == cfa_nobackup) { return true; } if (IsItemIn(VREPOSLIST, file)) { CfOut(OUTPUT_LEVEL_INFORM, "", "The file %s has already been moved to the repository once. Multiple update will cause loss of backup.", file); return true; } ThreadLock(cft_getaddr); PrependItemList(&VREPOSLIST, file); ThreadUnlock(cft_getaddr); CfDebug("Repository(%s)\n", file); JoinPath(destination, CanonifyName(file)); if (!MakeParentDirectory(destination, attr.move_obstructions, report_context)) { } if (cfstat(file, &sb) == -1) { CfDebug("File %s promised to archive to the repository but it disappeared!\n", file); return true; } cfstat(destination, &dsb); CheckForFileHoles(&sb, pp); if (pp && CopyRegularFileDisk(file, destination, pp->makeholes)) { CfOut(OUTPUT_LEVEL_INFORM, "", "Moved %s to repository location %s\n", file, destination); return true; } else { CfOut(OUTPUT_LEVEL_INFORM, "", "Failed to move %s to repository location %s\n", file, destination); return false; } }
static void CheckParseReal(char *lval, char *s, const char *range) { Item *split; double max = (double) CF_LOWINIT, min = (double) CF_HIGHINIT, val; int n; char output[CF_BUFSIZE]; CfDebug("\nCheckParseReal(%s => %s/%s)\n", lval, s, range); if (s == NULL) { return; } if (strcmp(s, "inf") == 0) { ReportError("keyword \"inf\" has an integer value, cannot be used as real"); return; } if (IsCf3VarString(s)) { CfDebug("Validation: Unable to verify syntax of real %s due to variable expansion at this stage\n", s); return; } /* Numeric types are registered by range separated by comma str "min,max" */ split = SplitString(range, ','); if ((n = ListLen(split)) != 2) { FatalError("INTERN:format specifier for real rvalues is not ok for lval %s - %d items", lval, n); } sscanf(split->name, "%lf", &min); sscanf(split->next->name, "%lf", &max); DeleteItemList(split); if (min == CF_HIGHINIT || max == CF_LOWINIT) { FatalError("INTERN:could not parse format specifier for int rvalues for lval %s", lval); } val = Str2Double(s); if (val > max || val < min) { snprintf(output, CF_BUFSIZE, "Real item on rhs of lval \'%s\' give as {%s => %.3lf} is out of bounds (should be in [%s])", lval, s, val, range); ReportError(output); } CfDebug("CheckParseReal - syntax verified\n\n"); }
static int CheckParseString(char *lval, char *s, const char *range) { char output[CF_BUFSIZE]; CfDebug("\nCheckParseString(%s => %s/%s)\n", lval, s, range); if (s == NULL) { return true; } if (strlen(range) == 0) { return true; } if (IsNakedVar(s, '@') || IsNakedVar(s, '$')) { CfDebug("Validation: Unable to verify variable expansion of %s at this stage\n", s); return false; } /* Deal with complex strings as special cases */ if (strcmp(lval, "mode") == 0 || strcmp(lval, "search_mode") == 0) { mode_t plus, minus; if (!ParseModeString(s, &plus, &minus)) { snprintf(output, CF_BUFSIZE, "Error parsing Unix permission string %s)", s); ReportError(output); return false; } } if (FullTextMatch(range, s)) { return true; } if (IsCf3VarString(s)) { CfDebug("Validation: Unable to verify syntax of %s due to variable expansion at this stage\n", s); } else { snprintf(output, CF_BUFSIZE, "Scalar item in %s => { %s } in rvalue is out of bounds (value should match pattern %s)", lval, s, range); ReportError(output); return false; } return true; }
static SyntaxTypeMatch CheckParseInt(const char *lval, const char *s, const char *range) { Item *split; int n; long max = CF_LOWINIT, min = CF_HIGHINIT, val; /* Numeric types are registered by range separated by comma str "min,max" */ CfDebug("\nCheckParseInt(%s => %s/%s)\n", lval, s, range); split = SplitString(range, ','); if ((n = ListLen(split)) != 2) { ProgrammingError("INTERN: format specifier for int rvalues is not ok for lval %s - got %d items", lval, n); } sscanf(split->name, "%ld", &min); if (strcmp(split->next->name, "inf") == 0) { max = CF_INFINITY; } else { sscanf(split->next->name, "%ld", &max); } DeleteItemList(split); if (min == CF_HIGHINIT || max == CF_LOWINIT) { ProgrammingError("INTERN: could not parse format specifier for int rvalues for lval %s", lval); } if (IsCf3VarString(s)) { return SYNTAX_TYPE_MATCH_ERROR_UNEXPANDED; } val = IntFromString(s); if (val == CF_NOINT) { return SYNTAX_TYPE_MATCH_ERROR_INT_PARSE; } if (val > max || val < min) { return SYNTAX_TYPE_MATCH_ERROR_INT_OUT_OF_RANGE; } CfDebug("CheckParseInt - syntax verified\n\n"); return SYNTAX_TYPE_MATCH_OK; }
void ArgTemplate(FnCall *fp, const FnCallArg *argtemplate, Rlist *realargs) { int argnum, i; Rlist *rp = fp->args; char id[CF_BUFSIZE], output[CF_BUFSIZE]; const FnCallType *fn = FnCallTypeGet(fp->name); snprintf(id, CF_MAXVARSIZE, "built-in FnCall %s-arg", fp->name); for (argnum = 0; rp != NULL && argtemplate[argnum].pattern != NULL; argnum++) { if (rp->type != RVAL_TYPE_FNCALL) { /* Nested functions will not match to lval so don't bother checking */ SyntaxTypeMatch err = CheckConstraintTypeMatch(id, (Rval) {rp->item, rp->type}, argtemplate[argnum].dtype, argtemplate[argnum].pattern, 1); if (err != SYNTAX_TYPE_MATCH_OK && err != SYNTAX_TYPE_MATCH_ERROR_UNEXPANDED) { FatalError("in %s: %s", id, SyntaxTypeMatchToString(err)); } } rp = rp->next; } if (argnum != RlistLen(realargs) && !fn->varargs) { snprintf(output, CF_BUFSIZE, "Argument template mismatch handling function %s(", fp->name); RlistShow(stderr, realargs); fprintf(stderr, ")\n"); for (i = 0, rp = realargs; i < argnum; i++) { printf(" arg[%d] range %s\t", i, argtemplate[i].pattern); if (rp != NULL) { RvalShow(stdout, (Rval) {rp->item, rp->type}); rp = rp->next; } else { printf(" ? "); } printf("\n"); } FatalError("Bad arguments"); } for (rp = realargs; rp != NULL; rp = rp->next) { CfDebug("finalarg: %s\n", (char *) rp->item); } CfDebug("End ArgTemplate\n"); }
static int SelectOwnerMatch(char *path, struct stat *lstatptr, Rlist *crit) { AlphaList leafattrib; Rlist *rp; char ownerName[CF_BUFSIZE]; int gotOwner; InitAlphaList(&leafattrib); #ifndef MINGW // no uids on Windows char buffer[CF_SMALLBUF]; sprintf(buffer, "%jd", (uintmax_t) lstatptr->st_uid); PrependAlphaList(&leafattrib, buffer); #endif /* MINGW */ gotOwner = GetOwnerName(path, lstatptr, ownerName, sizeof(ownerName)); if (gotOwner) { PrependAlphaList(&leafattrib, ownerName); } else { PrependAlphaList(&leafattrib, "none"); } for (rp = crit; rp != NULL; rp = rp->next) { if (EvalFileResult((char *) rp->item, &leafattrib)) { CfDebug(" - ? Select owner match\n"); DeleteAlphaList(&leafattrib); return true; } if (gotOwner && FullTextMatch((char *) rp->item, ownerName)) { CfDebug(" - ? Select owner match\n"); DeleteAlphaList(&leafattrib); return true; } #ifndef MINGW if (FullTextMatch((char *) rp->item, buffer)) { CfDebug(" - ? Select owner match\n"); DeleteAlphaList(&leafattrib); return true; } #endif /* NOT MINGW */ } DeleteAlphaList(&leafattrib); return false; }
static int SelectOwnerMatch(char *path, struct stat *lstatptr, Rlist *crit) { Rlist *rp; char ownerName[CF_BUFSIZE]; int gotOwner; StringSet *leafattrib = StringSetNew(); #ifndef __MINGW32__ // no uids on Windows char buffer[CF_SMALLBUF]; snprintf(buffer, CF_SMALLBUF, "%jd", (uintmax_t) lstatptr->st_uid); StringSetAdd(leafattrib, xstrdup(buffer)); #endif /* __MINGW32__ */ gotOwner = GetOwnerName(path, lstatptr, ownerName, sizeof(ownerName)); if (gotOwner) { StringSetAdd(leafattrib, xstrdup(ownerName)); } else { StringSetAdd(leafattrib, xstrdup("none")); } for (rp = crit; rp != NULL; rp = rp->next) { if (EvalFileResult((char *) rp->item, leafattrib)) { CfDebug(" - ? Select owner match\n"); StringSetDestroy(leafattrib); return true; } if (gotOwner && (FullTextMatch((char *) rp->item, ownerName))) { CfDebug(" - ? Select owner match\n"); StringSetDestroy(leafattrib); return true; } #ifndef __MINGW32__ if (FullTextMatch((char *) rp->item, buffer)) { CfDebug(" - ? Select owner match\n"); StringSetDestroy(leafattrib); return true; } #endif /* !__MINGW32__ */ } StringSetDestroy(leafattrib); return false; }
static void NotePerformance(char *eventname, time_t t, double value) { CF_DB *dbp; Event e, newe; double lastseen; int lsea = SECONDS_PER_WEEK; time_t now = time(NULL); CfDebug("PerformanceEvent(%s,%.1f s)\n", eventname, value); if (!OpenDB(&dbp, dbid_performance)) { return; } if (ReadDB(dbp, eventname, &e, sizeof(e))) { lastseen = now - e.t; newe.t = t; newe.Q = QAverage(e.Q, value, 0.3); /* Have to kickstart variance computation, assume 1% to start */ if (newe.Q.var <= 0.0009) { newe.Q.var = newe.Q.expect / 100.0; } } else { lastseen = 0.0; newe.t = t; newe.Q.q = value; newe.Q.dq = 0; newe.Q.expect = value; newe.Q.var = 0.001; } if (lastseen > (double) lsea) { CfDebug("Performance record %s expired\n", eventname); DeleteDB(dbp, eventname); } else { CfOut(cf_verbose, "", "Performance(%s): time=%.4lf secs, av=%.4lf +/- %.4lf\n", eventname, value, newe.Q.expect, sqrt(newe.Q.var)); WriteDB(dbp, eventname, &newe, sizeof(newe)); } CloseDB(dbp); }
void ArgTemplate(FnCall *fp, const FnCallArg *argtemplate, Rlist *realargs) { int argnum, i; Rlist *rp = fp->args; char id[CF_BUFSIZE], output[CF_BUFSIZE]; const FnCallType *fn = FindFunction(fp->name); snprintf(id, CF_MAXVARSIZE, "built-in FnCall %s-arg", fp->name); for (argnum = 0; rp != NULL && argtemplate[argnum].pattern != NULL; argnum++) { if (rp->type != CF_FNCALL) { /* Nested functions will not match to lval so don't bother checking */ CheckConstraintTypeMatch(id, (Rval) {rp->item, rp->type}, argtemplate[argnum].dtype, argtemplate[argnum].pattern, 1); } rp = rp->next; } if (argnum != RlistLen(realargs) && !fn->varargs) { snprintf(output, CF_BUFSIZE, "Argument template mismatch handling function %s(", fp->name); ReportError(output); ShowRlist(stderr, realargs); fprintf(stderr, ")\n"); for (i = 0, rp = realargs; i < argnum; i++) { printf(" arg[%d] range %s\t", i, argtemplate[i].pattern); if (rp != NULL) { ShowRval(stdout, (Rval) {rp->item, rp->type}); rp = rp->next; } else { printf(" ? "); } printf("\n"); } FatalError("Bad arguments"); } for (rp = realargs; rp != NULL; rp = rp->next) { CfDebug("finalarg: %s\n", (char *) rp->item); } CfDebug("End ArgTemplate\n"); }
void GetProcessColumnNames(char *proc, char **names, int *start, int *end) { char *sp, title[16]; int col, offset = 0; for (col = 0; col < CF_PROCCOLS; col++) { start[col] = end[col] = -1; names[col] = NULL; } col = 0; for (sp = proc; *sp != '\0'; sp++) { offset = sp - proc; if (isspace((int) *sp)) { if (start[col] != -1) { CfDebug("End of %s is %d\n", title, offset - 1); end[col++] = offset - 1; if (col > CF_PROCCOLS - 1) { CfOut(cf_error, "", "Column overflow in process table"); break; } } continue; } else if (start[col] == -1) { start[col] = offset; sscanf(sp, "%15s", title); CfDebug("Start of %s is %d\n", title, offset); names[col] = xstrdup(title); CfDebug("Col[%d]=%s\n", col, names[col]); } } if (end[col] == -1) { CfDebug("End of %s is %d\n", title, offset); end[col] = offset; } }
static int SelectGroupMatch(struct stat *lstatptr, Rlist *crit) { AlphaList leafattrib; char buffer[CF_SMALLBUF]; struct group *gr; Rlist *rp; InitAlphaList(&leafattrib); sprintf(buffer, "%jd", (uintmax_t) lstatptr->st_gid); PrependAlphaList(&leafattrib, buffer); if ((gr = getgrgid(lstatptr->st_gid)) != NULL) { PrependAlphaList(&leafattrib, gr->gr_name); } else { PrependAlphaList(&leafattrib, "none"); } for (rp = crit; rp != NULL; rp = rp->next) { if (EvalFileResult((char *) rp->item, &leafattrib)) { CfDebug(" - ? Select group match\n"); DeleteAlphaList(&leafattrib); return true; } if (gr && FullTextMatch((char *) rp->item, gr->gr_name)) { CfDebug(" - ? Select owner match\n"); DeleteAlphaList(&leafattrib); return true; } if (FullTextMatch((char *) rp->item, buffer)) { CfDebug(" - ? Select owner match\n"); DeleteAlphaList(&leafattrib); return true; } } DeleteAlphaList(&leafattrib); return false; }
static int IsForeignFileSystem(struct stat *childstat, char *dir) /* Is FS NFS mounted ? */ { struct stat parentstat; char vbuff[CF_BUFSIZE]; strncpy(vbuff, dir, CF_BUFSIZE - 1); if (vbuff[strlen(vbuff) - 1] == FILE_SEPARATOR) { strcat(vbuff, ".."); } else { strcat(vbuff, FILE_SEPARATOR_STR); strcat(vbuff, ".."); } if (cfstat(vbuff, &parentstat) == -1) { CfOut(OUTPUT_LEVEL_VERBOSE, "stat", " !! Unable to stat %s", vbuff); return (false); } if (childstat->st_dev != parentstat.st_dev) { Rlist *rp; Mount *entry; CfDebug("[%s is on a different file system, not descending]\n", dir); for (rp = MOUNTEDFSLIST; rp != NULL; rp = rp->next) { entry = (Mount *) rp->item; if (!strcmp(entry->mounton, dir)) { if ((entry->options) && (strstr(entry->options, "nfs"))) { return (true); } } } } CfDebug("NotMountedFileSystem\n"); return (false); }
static SyntaxTypeMatch CheckParseOpts(const char *lval, const char *s, const char *range) { Item *split; /* List/menu types are separated by comma str "a,b,c,..." */ CfDebug("\nCheckParseOpts(%s => %s/%s)\n", lval, s, range); if (IsNakedVar(s, '@') || IsNakedVar(s, '$')) { return SYNTAX_TYPE_MATCH_ERROR_UNEXPANDED; } split = SplitString(range, ','); if (!IsItemIn(split, s)) { DeleteItemList(split); return SYNTAX_TYPE_MATCH_ERROR_OPTS_OUT_OF_RANGE; } DeleteItemList(split); return SYNTAX_TYPE_MATCH_OK; }
Item *SplitStringAsItemList(const char *string, char sep) /* Splits a string containing a separator like : into a linked list of separate items, */ { Item *liststart = NULL; char format[9]; char node[CF_MAXVARSIZE]; CfDebug("SplitStringAsItemList(%s,%c)\n", string, sep); sprintf(format, "%%255[^%c]", sep); /* set format string to search */ for (const char *sp = string; *sp != '\0'; sp++) { memset(node, 0, CF_MAXVARSIZE); sscanf(sp, format, node); if (strlen(node) == 0) { continue; } sp += strlen(node) - 1; AppendItem(&liststart, node, NULL); if (*sp == '\0') { break; } } return liststart; }
bool IsDefinedClass(const EvalContext *ctx, const char *context, const char *ns) { ParseResult res; if (!context) { return true; } res = ParseExpression(context, 0, strlen(context)); if (!res.result) { char *errexpr = HighlightExpressionError(context, res.position); CfOut(OUTPUT_LEVEL_ERROR, "", "Unable to parse class expression: %s", errexpr); free(errexpr); return false; } else { ExpressionValue r = EvalExpression(ctx, res.result, &EvalTokenAsClass, &EvalVarRef, (void *)ns); FreeExpression(res.result); CfDebug("Evaluate(%s) -> %d\n", context, r); /* r is EvalResult which could be ERROR */ return r == true; } }
int VarClassExcluded(EvalContext *ctx, Promise *pp, char **classes) { Constraint *cp = PromiseGetConstraint(ctx, pp, "ifvarclass"); if (cp == NULL) { return false; } *classes = (char *) ConstraintGetRvalValue(ctx, "ifvarclass", pp, RVAL_TYPE_SCALAR); if (*classes == NULL) { return true; } if (strchr(*classes, '$') || strchr(*classes, '@')) { CfDebug("Class expression did not evaluate"); return true; } if (*classes && IsDefinedClass(ctx, *classes, PromiseGetNamespace(pp))) { return false; } else { return true; } }
int IsIPV4Address(char *name) { char *sp; int count = 0; CfDebug("IsIPV4Address(%s)\n", name); if (name == NULL) { return false; } for (sp = name; *sp != '\0'; sp++) { if ((!isdigit((int) *sp)) && (*sp != '.')) { return false; } if (*sp == '.') { count++; } } if (count != 3) { return false; } return true; }
int SendTransaction(int sd, char *buffer, int len, char status) { char work[CF_BUFSIZE]; int wlen; memset(work, 0, sizeof(work)); if (len == 0) { wlen = strlen(buffer); } else { wlen = len; } if (wlen > CF_BUFSIZE - CF_INBAND_OFFSET) { CfOut(cf_error, "", "SendTransaction: wlen (%d) > %d - %d", wlen, CF_BUFSIZE, CF_INBAND_OFFSET); FatalError("SendTransaction software failure"); } snprintf(work, CF_INBAND_OFFSET, "%c %d", status, wlen); memcpy(work + CF_INBAND_OFFSET, buffer, wlen); CfDebug("Transaction Send[%s][Packed text]\n", work); if (SendSocketStream(sd, work, wlen + CF_INBAND_OFFSET, 0) == -1) { return -1; } return 0; }
void HashString(const char *buffer, int len, unsigned char digest[EVP_MAX_MD_SIZE + 1], enum cfhashes type) { EVP_MD_CTX context; const EVP_MD *md = NULL; int md_len; CfDebug("HashString(%c)\n", type); switch (type) { case cf_crypt: CfOut(OUTPUT_LEVEL_ERROR, "", "The crypt support is not presently implemented, please use another algorithm instead"); memset(digest, 0, EVP_MAX_MD_SIZE + 1); break; default: md = EVP_get_digestbyname(FileHashName(type)); if (md == NULL) { CfOut(OUTPUT_LEVEL_INFORM, "", " !! Digest type %s not supported by OpenSSL library", CF_DIGEST_TYPES[type][0]); } EVP_DigestInit(&context, md); EVP_DigestUpdate(&context, (unsigned char *) buffer, (size_t) len); EVP_DigestFinal(&context, digest, &md_len); break; } }
void HashFile(char *filename, unsigned char digest[EVP_MAX_MD_SIZE + 1], enum cfhashes type) { FILE *file; EVP_MD_CTX context; int len, md_len; unsigned char buffer[1024]; const EVP_MD *md = NULL; CfDebug("HashFile(%d,%s)\n", type, filename); if ((file = fopen(filename, "rb")) == NULL) { CfOut(OUTPUT_LEVEL_INFORM, "fopen", "%s can't be opened\n", filename); } else { md = EVP_get_digestbyname(FileHashName(type)); EVP_DigestInit(&context, md); while ((len = fread(buffer, 1, 1024, file))) { EVP_DigestUpdate(&context, buffer, len); } EVP_DigestFinal(&context, digest, &md_len); /* Digest length stored in md_len */ fclose(file); } }
int CheckParseClass(char *lval, char *s, const char *range) { char output[CF_BUFSIZE]; if (s == NULL) { return false; } CfDebug("\nCheckParseClass(%s => %s/%s)\n", lval, s, range); if (strlen(range) == 0) { return true; } if (FullTextMatch(range, s)) { return true; } snprintf(output, CF_BUFSIZE, "Class item on rhs of lval \'%s\' given as { %s } is out of bounds (should match %s)", lval, s, range); ReportError(output); return false; }
Rlist *RlistFromSplitString(const char *string, char sep) /* Splits a string containing a separator like "," into a linked list of separate items, supports escaping separators, e.g. \, */ { if (string == NULL) { return NULL; } Rlist *liststart = NULL; char node[CF_MAXVARSIZE]; int maxlen = strlen(string); CfDebug("SplitStringAsRList(%s)\n", string); for (const char *sp = string; *sp != '\0'; sp++) { if (*sp == '\0' || sp > string + maxlen) { break; } memset(node, 0, CF_MAXVARSIZE); sp += SubStrnCopyChr(node, sp, CF_MAXVARSIZE, sep); RlistAppendScalar(&liststart, node); } return liststart; }
static void KeepPromises(Policy *policy, const ReportContext *report_context) { Constraint *cp; Rval retval; for (cp = ControlBodyConstraints(policy, cf_monitor); cp != NULL; cp = cp->next) { if (IsExcluded(cp->classes, NULL)) { continue; } if (GetVariable("control_monitor", cp->lval, &retval) == cf_notype) { CfOut(cf_error, "", "Unknown lval %s in monitor control body", cp->lval); continue; } if (strcmp(cp->lval, CFM_CONTROLBODY[cfm_histograms].lval) == 0) { /* Keep accepting this option for backward compatibility. */ } if (strcmp(cp->lval, CFM_CONTROLBODY[cfm_tcpdump].lval) == 0) { MonNetworkSnifferEnable(GetBoolean(retval.item)); } if (strcmp(cp->lval, CFM_CONTROLBODY[cfm_forgetrate].lval) == 0) { sscanf(retval.item, "%lf", &FORGETRATE); CfDebug("forget rate = %f\n", FORGETRATE); } } }
void NewScalar(const char *scope, const char *lval, const char *rval, enum cfdatatype dt) { Rval rvald; Scope *ptr; CfDebug("NewScalar(%s,%s,%s)\n", scope, lval, rval); ptr = GetScope(scope); if (ptr == NULL) { CfOut(cf_error, "", "!! Attempt to add variable \"%s\" to non-existant scope \"%s\" - ignored", lval, scope); return; } // Newscalar allocates memory through NewAssoc if (GetVariable(scope, lval, &rvald) != cf_notype) { DeleteScalar(scope, lval); } /* * We know AddVariableHash does not change passed Rval structure or its * contents, but we have no easy way to express it in C type system, hence cast. */ AddVariableHash(scope, lval, (Rval) {(char *) rval, CF_SCALAR}, dt, NULL, 0); }
static int Dialogue(int sd, char *s) { int sent; char ch, f = '\0'; int charpos, rfclinetype = ' '; if ((s != NULL) && (*s != '\0')) { sent = send(sd, s, strlen(s), 0); CfDebug("SENT(%d)->%s", sent, s); } else { CfDebug("Nothing to send .. waiting for opening\n"); } charpos = 0; while (recv(sd, &ch, 1, 0)) { charpos++; if (f == '\0') { f = ch; } if (charpos == 4) /* Multiline RFC in form 222-Message with hyphen at pos 4 */ { rfclinetype = ch; } CfDebug("%c", ch); if ((ch == '\n') || (ch == '\0')) { charpos = 0; if (rfclinetype == ' ') { break; } } } return ((f == '2') || (f == '3')); /* return code 200 or 300 from smtp */ }