Пример #1
0
static void knh_shell(CTX ctx)
{
	void *shell_status = NULL;
	BEGIN_LOCAL(ctx, lsfp, 2);
//	LOCAL_NEW(ctx, lsfp, 0, kInputStream *, bin, new_BytesInputStream(ctx, new_Bytes(ctx, "shell", K_PAGESIZE)));
	{
		CWB_t cwbbuf, *cwb = CWB_open(ctx, &cwbbuf);
		knh_showWelcome(ctx, cwb->w);
		knh_showSecurityAlert(ctx, cwb->w);
		shell_status = shell_init(ctx, CWB_totext(ctx, cwb), NULL);
		CWB_close(ctx, cwb);
	}
	while(1) {
		{
			CWB_t cwbbuf, *cwb = CWB_open(ctx, &cwbbuf);
			kstatus_t status = readstmt(ctx, cwb);
			if(status == K_BREAK) {
				CWB_close(ctx, cwb);
				break;
			}
			if(CWB_size(cwb) == 0) {
				CWB_close(ctx, cwb);
				continue;
			}
			status = shell_command(ctx, CWB_totext(ctx, cwb));
			if(status == K_BREAK) {
				CWB_close(ctx, cwb);
				break;
			}
			if(status == K_REDO) {
				CWB_close(ctx, cwb);
				continue;
			}
#ifdef K_USING_SUGAR
			kString *script = CWB_newString(ctx, cwb, 0);
			KNH_SETv(ctx, lsfp[0].o, script);
			knh_beval2(ctx, S_totext(script), 1);
#else
			kInputStream *bin = new_BytesInputStream(ctx, CWB_totext(ctx, cwb), CWB_size(cwb));
			KNH_SETv(ctx, lsfp[0].o, bin);
			knh_beval(ctx, bin, 1);
#endif
		}
		knh_OutputStream_flush(ctx, ctx->out);
		if(ctx->isEvaled == 1) {
			CWB_t cwbbuf, *cwb = CWB_open(ctx, &cwbbuf);
			knh_write_Object(ctx, cwb->w, ctx->evaled, FMT_dump);
			knh_showSecurityAlert(ctx, cwb->w);
			if(CWB_size(cwb) !=0) {
				shell_display(ctx, shell_status, CWB_totext(ctx, cwb));
			}
			CWB_close(ctx, cwb);
			WCTX(ctx)->isEvaled = 0;
		}
	}
	shell_cleanup(ctx, shell_status);
	END_LOCAL(ctx, lsfp);
}
Пример #2
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;
}