Beispiel #1
0
int
fixExecError(int action,
             u_char * var_val,
             u_char var_val_type,
             size_t var_val_len,
             u_char * statP, oid * name, size_t name_len)
{

    struct extensible *exten;
    long            tmp = 0;
    int             fd;
    static struct extensible ex;
    FILE           *file;

    if ((exten = get_exten_instance(extens, name[10]))) {
        if (var_val_type != ASN_INTEGER) {
            snmp_log(LOG_ERR, "Wrong type != int\n");
            return SNMP_ERR_WRONGTYPE;
        }
        tmp = *((long *) var_val);
        if ((tmp == 1) && (action == COMMIT) && (exten->fixcmd[0] != 0)) {
            sprintf(ex.command, exten->fixcmd);
            if ((fd = get_exec_output(&ex)) != -1) {
                file = fdopen(fd, "r");
                while (fgets(ex.output, sizeof(ex.output), file) != NULL);
                fclose(file);
                wait_on_exec(&ex);
            }
        }
        return SNMP_ERR_NOERROR;
    }
    return SNMP_ERR_WRONGTYPE;
}
Beispiel #2
0
int exec_command (struct extensible *ex)
{
#if defined (HAVE_EXECV) || defined (WIN32)
    int fd;

    FILE *file;

    if ((fd = get_exec_output (ex)) != -1)
    {
        file = fdopen (fd, "r");
        if (fgets (ex->output, sizeof (ex->output), file) == NULL)
        {
            ex->output[0] = 0;
        }
        fclose (file);
        wait_on_exec (ex);
    }
    else
#endif                            /* HAVE_EXECV */
    {
        ex->output[0] = 0;
        ex->result = 0;
    }
    return (ex->result);
}
Beispiel #3
0
int
sh_count_regexp_procs(char *procname, pcre *regexp)
{
    char            line[STRMAX], *cptr, *cp;
    int             ret = 0, fd;
    FILE           *file;
#ifndef NETSNMP_EXCACHETIME
#endif
    struct extensible ex;
    int             slow = strstr(PSCMD, "ax") != NULL;

    strcpy(ex.command, PSCMD);
    if ((fd = get_exec_output(&ex)) >= 0) {
        if ((file = fdopen(fd, "r")) == NULL) {
            setPerrorstatus("fdopen");
            close(fd);
            return (-1);
        }
        while (fgets(line, sizeof(line), file) != NULL) {
            if (slow) {
                cptr = find_field(line, 5);
                cp = strrchr(cptr, '/');
                if (cp)
                    cptr = cp + 1;
                else if (*cptr == '-')
                    cptr++;
                else if (*cptr == '[') {
                    cptr++;
                    cp = strchr(cptr, ']');
                    if (cp)
                        *cp = 0;
                }
                copy_nword(cptr, line, sizeof(line));
                cp = line + strlen(line) - 1;
                if (*cp == ':')
                    *cp = 0;
            } else {
                if ((cptr = find_field(line, NETSNMP_LASTFIELD)) == NULL)
                    continue;
                copy_nword(cptr, line, sizeof(line));
            }
            if (!strcmp(line, procname))
                ret++;
        }
        if (ftell(file) < 2) {
#ifdef USING_UCD_SNMP_ERRORMIB_MODULE
            seterrorstatus("process list unreasonable short (mem?)", 2);
#endif
            ret = -1;
        }
        fclose(file);
        wait_on_exec(&ex);
    } else {
        ret = -1;
    }
    return (ret);
}
Beispiel #4
0
void
swapmode(void)
{
    struct extensible ext;
    int             fd;
    FILE           *file;

    strcpy(ext.command, "/usr/sbin/swapinfo -k");

    if ((fd = get_exec_output(&ext)) != -1) {
        file = fdopen(fd, "r");

        while (fgets(ext.output, sizeof(ext.output), file) != NULL);

        fclose(file);
        wait_on_exec(&ext);

        sscanf(ext.output, "%*s%*d%qd%qd", &swapUsed, &swapFree);

        swapTotal = swapUsed + swapFree;
    }
}
Beispiel #5
0
int
fixExecError(int action,
	     u_char *var_val,
	     u_char var_val_type,
	     int var_val_len,
	     u_char *statP,
	     oid *name,
	     int name_len)
{
  
  struct extensible *exten;
  long tmp=0;
  int tmplen=1000, fd;
  static struct extensible ex;
  FILE *file;

  if ((exten = get_exten_instance(extens,name[10]))) {
    if (var_val_type != ASN_INTEGER) {
      printf("Wrong type != int\n");
      return SNMP_ERR_WRONGTYPE;
    }
    asn_parse_int(var_val,&tmplen,&var_val_type,&tmp,sizeof(int));
    if ((tmp == 1) && (action == COMMIT) && (exten->fixcmd[0] != 0)) {
      sprintf(ex.command, exten->fixcmd);
      if ((fd = get_exec_output(&ex))) {
        file = fdopen(fd,"r");
        while (fgets(ex.output,STRMAX,file) != NULL);
        fclose(file);
        close(fd);
        wait_on_exec(&ex);
      }
    }
    return SNMP_ERR_NOERROR;
  }
  return SNMP_ERR_WRONGTYPE;
}
Beispiel #6
0
u_char         *
var_extensible_pass(struct variable *vp,
                    oid * name,
                    size_t * length,
                    int exact,
                    size_t * var_len, WriteMethod ** write_method)
{
    oid             newname[MAX_OID_LEN];
    int             i, rtest, fd, newlen;
    static long     long_ret;
    static in_addr_t addr_ret;
    char            buf[SNMP_MAXBUF];
    static char     buf2[SNMP_MAXBUF];
    static oid      objid[MAX_OID_LEN];
    struct extensible *passthru;
    FILE           *file;

    long_ret = *length;
    for (i = 1; i <= numpassthrus; i++) {
        passthru = get_exten_instance(passthrus, i);
        rtest = snmp_oid_min_compare(name, *length,
                                     passthru->miboid, passthru->miblen);
        if ((exact && rtest == 0) || (!exact && rtest <= 0)) {
            /*
             * setup args
             */
            if (passthru->miblen >= *length || rtest < 0)
                sprint_mib_oid(buf, passthru->miboid, passthru->miblen);
            else
                sprint_mib_oid(buf, name, *length);
            if (exact)
                snprintf(passthru->command, sizeof(passthru->command),
                         "%s -g %s", passthru->name, buf);
            else
                snprintf(passthru->command, sizeof(passthru->command),
                         "%s -n %s", passthru->name, buf);
            passthru->command[ sizeof(passthru->command)-1 ] = 0;
            DEBUGMSGTL(("ucd-snmp/pass", "pass-running:  %s\n",
                        passthru->command));
            /*
             * valid call.  Exec and get output
             */
            if ((fd = get_exec_output(passthru)) != -1) {
                file = fdopen(fd, "r");
                if (fgets(buf, sizeof(buf), file) == NULL) {
                    fclose(file);
                    wait_on_exec(passthru);
                    if (exact) {
                        /*
                         * to enable creation
                         */
                        *write_method = setPass;
                        *var_len = 0;
                        return (NULL);
                    }
                    continue;
                }
                newlen = parse_miboid(buf, newname);

                /*
                 * its good, so copy onto name/length
                 */
                memcpy((char *) name, (char *) newname,
                       (int) newlen * sizeof(oid));
                *length = newlen;

                /*
                 * set up return pointer for setable stuff
                 */
                *write_method = setPass;

                if (newlen == 0 || fgets(buf, sizeof(buf), file) == NULL
                        || fgets(buf2, sizeof(buf2), file) == NULL) {
                    *var_len = 0;
                    fclose(file);
                    wait_on_exec(passthru);
                    return (NULL);
                }
                fclose(file);
                wait_on_exec(passthru);

                /*
                 * buf contains the return type, and buf2 contains the data
                 */
                if (!strncasecmp(buf, "string", 6)) {
                    buf2[strlen(buf2) - 1] = 0; /* zap the linefeed */
                    *var_len = strlen(buf2);
                    vp->type = ASN_OCTET_STR;
                    return ((unsigned char *) buf2);
                }
                else if (!strncasecmp(buf, "integer64", 9)) {
                    static struct counter64 c64;
                    uint64_t v64 = strtoull(buf2, NULL, 10);

                    void * _c64 = (void *)&c64;
                    if (sizeof(long) > 4) {    /* 64-bit machine */
                        c64.high = v64 >> 32;
                        c64.low = v64 & 0xffffffff;
                    }
                    else {    /* 32-bit machine */
                        *(uint64_t *)_c64 = v64;
                    }
                    *var_len = sizeof(c64);
                    vp->type = ASN_INTEGER64;
                    return ((unsigned char *) &c64);
                }
                else if (!strncasecmp(buf, "integer", 7)) {
Beispiel #7
0
unsigned char *var_extensible_relocatable(struct variable *vp,
					  oid *name,
					  int *length,
					  int exact,
					  int *var_len,
					  WriteMethod **write_method)
{

  int i, fd;
  FILE *file;
  struct extensible *exten = 0;
  static long long_ret;
  static char errmsg[STRMAX];
  struct variable myvp;
  oid tname[MAX_OID_LEN];

  memcpy(&myvp,vp,sizeof(struct variable));

  long_ret = *length;
  for(i=1; i<= numrelocs; i++) {
    exten = get_exten_instance(relocs,i);
    if (exten->miblen == vp->namelen-1){
      memcpy(myvp.name,exten->miboid,exten->miblen*sizeof(oid));
      myvp.namelen = exten->miblen;
      *length = vp->namelen;
      memcpy(tname,vp->name,vp->namelen*sizeof(oid));
      if (!header_simple_table(&myvp,tname,length,-1,var_len,write_method, -1))
        break;
      else
        exten = NULL;
    }
  }
  if (i > numrelocs || exten == NULL) {
    *length = long_ret;
    *var_len = 0;
    *write_method = NULL;
    return(NULL);
  }

  *length = long_ret;
  if (header_simple_table(vp,name,length,exact,var_len,write_method,
               ((vp->magic == ERRORMSG) ? MAXMSGLINES : 1)))
    return(NULL);
  
  switch (vp->magic) {
    case MIBINDEX:
      long_ret = name[*length-1];
      return((u_char *) (&long_ret));
    case ERRORNAME: /* name defined in config file */
      *var_len = strlen(exten->name);
      return((u_char *) (exten->name));
    case SHELLCOMMAND:
      *var_len = strlen(exten->command);
      return((u_char *) (exten->command));
    case ERRORFLAG:  /* return code from the process */
      if (exten->type == EXECPROC)
        exec_command(exten);
      else
        shell_command(exten);
      long_ret = exten->result;
      return((u_char *) (&long_ret));
    case ERRORMSG:   /* first line of text returned from the process */
      if (exten->type == EXECPROC) {
        if ((fd = get_exec_output(exten))){
          file = fdopen(fd,"r");
          for (i=0;i != name[*length-1];i++) {
            if (fgets(errmsg,sizeof(errmsg),file) == NULL) {
              *var_len = 0;
              fclose(file);
              close(fd);
              wait_on_exec(exten);
              return(NULL);
            }
          }
          fclose(file);
          close(fd);
          wait_on_exec(exten);
        } else
          errmsg[0] = 0;
      }
      else {
        if (*length > 1) {
          *var_len = 0;
          return(NULL);
        }
        shell_command(exten);
        strcpy(errmsg,exten->output);
      }
      *var_len = strlen(errmsg);
      return((u_char *) (errmsg));
    case ERRORFIX:
      *write_method = fixExecError;
      long_return = 0;
      return ((u_char *) &long_return);
  }
  return NULL;
}
Beispiel #8
0
u_char         *
var_extensible_pass(struct variable *vp,
                    oid * name,
                    size_t * length,
                    int exact,
                    size_t * var_len, WriteMethod ** write_method)
{
    oid             newname[MAX_OID_LEN];
    int             i, rtest, fd, newlen;
    char            buf[SNMP_MAXBUF];
    static char     buf2[SNMP_MAXBUF];
    struct extensible *passthru;
    FILE           *file;

    for (i = 1; i <= numpassthrus; i++) {
        passthru = get_exten_instance(passthrus, i);
        rtest = snmp_oidtree_compare(name, *length,
                                     passthru->miboid, passthru->miblen);
        if ((exact && rtest == 0) || (!exact && rtest <= 0)) {
            /*
             * setup args 
             */
            if (passthru->miblen >= *length || rtest < 0)
                sprint_mib_oid(buf, passthru->miboid, passthru->miblen);
            else
                sprint_mib_oid(buf, name, *length);
            if (exact)
                snprintf(passthru->command, sizeof(passthru->command),
                         "%s -g %s", passthru->name, buf);
            else
                snprintf(passthru->command, sizeof(passthru->command),
                         "%s -n %s", passthru->name, buf);
            passthru->command[ sizeof(passthru->command)-1 ] = 0;
            DEBUGMSGTL(("ucd-snmp/pass", "pass-running:  %s\n",
                        passthru->command));
            /*
             * valid call.  Exec and get output 
             */
            if ((fd = get_exec_output(passthru)) != -1) {
                file = fdopen(fd, "r");
                if (fgets(buf, sizeof(buf), file) == NULL) {
                    fclose(file);
                    wait_on_exec(passthru);
                    if (exact) {
                        /*
                         * to enable creation
                         */
                        *write_method = setPass;
                        *var_len = 0;
                        return (NULL);
                    }
                    continue;
                }
                newlen = parse_miboid(buf, newname);

                /*
                 * its good, so copy onto name/length 
                 */
                memcpy((char *) name, (char *) newname,
                       (int) newlen * sizeof(oid));
                *length = newlen;

                /*
                 * set up return pointer for setable stuff 
                 */
                *write_method = setPass;

                if (newlen == 0 || fgets(buf, sizeof(buf), file) == NULL
                    || fgets(buf2, sizeof(buf2), file) == NULL) {
                    *var_len = 0;
                    fclose(file);
                    wait_on_exec(passthru);
                    return (NULL);
                }
                fclose(file);
                wait_on_exec(passthru);

                return netsnmp_internal_pass_parse(buf, buf2, var_len, vp);
            }
            *var_len = 0;
            return (NULL);
        }
    }
    if (var_len)
        *var_len = 0;
    *write_method = NULL;
    return (NULL);
}