static void handle_execute_service(void)
{
    int i;
    int policy_pending_slot;
    pid_t pid;
    struct trigger_service_params untrusted_params, params;
    char remote_domain_id_str[10];

    if (libvchan_recv(vchan, &untrusted_params, sizeof(untrusted_params)) < 0)
        handle_vchan_error("recv params");

    /* sanitize start */
    ENSURE_NULL_TERMINATED(untrusted_params.service_name);
    ENSURE_NULL_TERMINATED(untrusted_params.target_domain);
    ENSURE_NULL_TERMINATED(untrusted_params.request_id.ident);
    sanitize_name(untrusted_params.service_name, "+");
    sanitize_name(untrusted_params.target_domain, "@:");
    sanitize_name(untrusted_params.request_id.ident, " ");
    params = untrusted_params;
    /* sanitize end */

    policy_pending_slot = find_policy_pending_slot();
    if (policy_pending_slot < 0) {
        fprintf(stderr, "Service request denied, too many pending requests\n");
        send_service_refused(vchan, &untrusted_params.request_id);
        return;
    }

    switch (pid=fork()) {
        case -1:
            perror("fork");
            exit(1);
        case 0:
            break;
        default:
            policy_pending[policy_pending_slot].pid = pid;
            policy_pending[policy_pending_slot].params = untrusted_params.request_id;
            return;
    }
    for (i = 3; i < MAX_FDS; i++)
        close(i);
    signal(SIGCHLD, SIG_DFL);
    signal(SIGPIPE, SIG_DFL);
    snprintf(remote_domain_id_str, sizeof(remote_domain_id_str), "%d",
            remote_domain_id);
    execl("/usr/bin/qrexec-policy", "qrexec-policy", "--",
            remote_domain_id_str, remote_domain_name, params.target_domain,
            params.service_name, params.request_id.ident, NULL);
    perror("execl");
    _exit(1);
}
Example #2
0
void handle_execute_predefined_command()
{
	int i;
	struct trigger_connect_params untrusted_params, params;

	check_children_count_and_wait_if_too_many();
	read_all_vchan_ext(&untrusted_params, sizeof(params));

	/* sanitize start */
	ENSURE_NULL_TERMINATED(untrusted_params.exec_index);
	ENSURE_NULL_TERMINATED(untrusted_params.target_vmname);
	ENSURE_NULL_TERMINATED(untrusted_params.process_fds.ident);
	sanitize_name(untrusted_params.exec_index);
	sanitize_name(untrusted_params.target_vmname);
	sanitize_name(untrusted_params.process_fds.ident);
	params = untrusted_params;
	/* sanitize end */

	switch (fork()) {
	case -1:
		perror("fork");
		exit(1);
	case 0:
		break;
	default:
		children_count++;
		return;
	}
	for (i = 3; i < MAX_FDS; i++)
		close(i);
	signal(SIGCHLD, SIG_DFL);
	signal(SIGPIPE, SIG_DFL);
	execl("/usr/lib/qubes/qrexec_policy", "qrexec_policy",
	      remote_domain_name, params.target_vmname,
	      params.exec_index, params.process_fds.ident, NULL);
	perror("execl");
	exit(1);
}
Example #3
0
std::string make_material_name(const MObject& src_node) {
  std::string candidate("Unknown");
  if (src_node.hasFn(MFn::kLambert)) {
    MFnLambertShader shader(src_node);
    candidate = shader.name().asChar();
  } else if (src_node.hasFn(MFn::kBlinn)) {
    MFnBlinnShader shader(src_node);
    candidate = shader.name().asChar();
  } else if (src_node.hasFn(MFn::kPhong)) {
    MFnPhongShader shader(src_node);
    candidate = shader.name().asChar();
  }
  return sanitize_name(candidate);
}
Example #4
0
/** sanitize

	Sanitizes a gridlabd model by clear names and position from object headers

    @returns 0 on success, -2 on error
 **/
extern "C" int sanitize(int argc, char *argv[])
{
	OBJECT *obj;
	FILE *fp;
	double delta_latitude, delta_longitude;

	// lat/lon change
	if ( strcmp(global_sanitizeoffset,"")==0 )
	{
		delta_latitude = random_uniform(NULL,-5,+5);
		delta_longitude = random_uniform(NULL,-180,+180);
	}
	else if ( global_sanitizeoffset=="destroy" )
		delta_latitude = delta_longitude = QNAN;
	else if ( sscanf(global_sanitizeoffset.get_string(),"%lf%*[,/]%lf",&delta_latitude,&delta_longitude)!=2 )
	{
		output_error("sanitize_offset lat/lon '%s' is not valid", global_sanitizeoffset.get_string());
		return -2;
	}

	// sanitize object names
	for ( obj=object_get_first() ; obj!=NULL ; obj=object_get_next(obj) )
	{
		if ( obj->name!=NULL && (global_sanitizeoptions&SO_NAMES)==SO_NAMES )
			sanitize_name(obj);
		if ( isfinite(obj->latitude) && (global_sanitizeoptions&SO_GEOCOORDS)==SO_GEOCOORDS )
		{
			obj->latitude += delta_latitude;
			if ( obj->latitude<-90 ) obj->latitude = -90;
			if ( obj->latitude>+90 ) obj->latitude = +90;
		}
		if ( isfinite(obj->longitude) && (global_sanitizeoptions&SO_GEOCOORDS)==SO_GEOCOORDS )
			obj->longitude = fmod(obj->longitude+delta_longitude,360);
	}

	// dump object name index
	if ( strcmp(global_sanitizeindex,".xml")==0 )
	{
		strcpy(global_sanitizeindex,global_modelname);
		char *ext = strrchr(global_sanitizeindex,'.');
		if ( ext && strcmp(ext,".glm")==0 ) strcpy(ext,"-index.xml");
		else strcat(global_sanitizeindex,"-index.xml");
	}
	else if ( strcmp(global_sanitizeindex,".txt")==0 )
	{
		strcpy(global_sanitizeindex,global_modelname);
		char *ext = strrchr(global_sanitizeindex,'.');
		if ( ext && strcmp(ext,".glm")==0 ) strcpy(ext,"-index.txt");
		else strcat(global_sanitizeindex,"-index.txt");
	}
	else if ( global_sanitizeindex[0]=='.' )
	{
		output_error("sanitization index file spec '%s' is not recognized", global_sanitizeindex.get_string());
		return -2;
	}
	if ( strcmp(global_sanitizeindex,"")!=0 )
	{
		char *ext = strrchr(global_sanitizeindex,'.');
		bool use_xml = (ext && strcmp(ext,".xml")==0) ;
		fp = fopen(global_sanitizeindex,"w");
		if ( fp )
		{
			SAFENAME *item;
			if ( use_xml )
			{
				fprintf(fp,"<data>\n");
				fprintf(fp,"\t<modelname>%s</modelname>\n",global_modelname);
				fprintf(fp,"\t<geographic_offsets>\n");
				fprintf(fp,"\t\t<latitude>%.6f</latitude>\n",delta_latitude);
				fprintf(fp,"\t\t<longitude>%.6f</longitude>\n",delta_longitude);
				fprintf(fp,"\t</geographic_offsets>\n");
				fprintf(fp,"\t<safename_list>\n");
				for ( item=safename_list ; item!=NULL ; item=item->next )
					fprintf(fp,"\t\t<name>\n\t\t\t<safe>%s</safe>\n\t\t\t<unsafe>%s</unsafe>\n\t\t</name>\n", item->name, item->old);
				fprintf(fp,"\t</safename_list>\n");
				fprintf(fp,"</data>\n");
			}
			else
			{
				fprintf(fp,"modelname\t= %s\n", global_modelname);
				fprintf(fp,"\n[POSITIONS]\n");
				fprintf(fp,"latitude\t= %.6f\n",delta_latitude);
				fprintf(fp,"longitude\t= %.6f\n",delta_longitude);
				fprintf(fp,"\n[NAMES]\n");
				for ( item=safename_list ; item!=NULL ; item=item->next )
					fprintf(fp,"%s\t= %s\n", item->name, item->old);
			}
			fclose(fp);
		}
	}

	return 0;
}
Example #5
0
std::string
sanitize_name(std::string name, const std::vector<std::string>& brethren)
{
  sanitize_name(name);
  bool is_here = false;
  ossia::optional<int> name_instance;
  ossia::small_vector<int, 16> instance_num;
  instance_num.reserve(brethren.size());

  // First get the root name : the first part of the "a.b"
  std::string root_name = name;
  {
    auto pos = name.find_last_of('.');
    if (pos != std::string::npos)
    {
      try
      {
        name_instance
            = boost::lexical_cast<int>(name.substr(pos + 1)); // OPTIMIZEME
        root_name = name.substr(0, pos);
      }
      catch (...)
      {
      }
    }
  }

  const auto root_len = root_name.size();
  for (const std::string& n_name : brethren)
  {
    if (n_name == name)
    {
      is_here = true;
    }

    if (n_name.size() < (root_len + 1))
      continue;

    bool same_root = (n_name.compare(0, root_len, root_name) == 0);
    if (same_root && (n_name[root_len] == '.'))
    {
      // Instance
      try
      {
        int n = boost::lexical_cast<int>(
            n_name.substr(root_len + 1)); // OPTIMIZEME
        instance_num.push_back(n);
      }
      catch (...)
      {
        continue;
      }
    }
    // case where we have the "default" instance without .0
    else if (same_root && root_len == n_name.length())
    {
      instance_num.push_back(0);
    }
  }

  if (!is_here)
  {
    return name;
  }
  else
  {
    auto n = instance_num.size();
    if ((n == 0) || ((n == 1) && (instance_num[0] == 0)))
    {
      return root_name + ".1";
    }
    else
    {
      std::sort(instance_num.begin(), instance_num.end());
      return root_name + "."
             + boost::lexical_cast<std::string>(instance_num.back() + 1);
    }
  }
}
Example #6
0
void sanitize_name(
    std::string& name, const ossia::net::node_base::children_t& brethren)
{
  sanitize_name(name);
  bool is_here = false;
  ossia::optional<int> name_instance;
  ossia::small_vector<int, 16> instance_num;
  instance_num.reserve(brethren.size());

  // First get the root name : the first part of the "a.b"
  std::string root_name = name;
  {
    auto pos = name.find_last_of('.');
    if (pos != std::string::npos)
    {
      int n{};
      if(boost::conversion::detail::try_lexical_convert(name.substr(pos + 1), n))
      {
        // OPTIMIZEME
        name_instance = n;
        root_name = name.substr(0, pos);
      }
    }
  }

  const auto root_len = root_name.size();
  for (const auto& bro : brethren)
  {
    const auto& n_name = bro->get_name();
    if (n_name == name)
    {
      is_here = true;
    }

    if (n_name.size() < (root_len + 1))
      continue;

    bool same_root = (n_name.compare(0, root_len, root_name) == 0);
    if (same_root && (n_name[root_len] == '.'))
    {
      // Instance
      int n{};
      // OPTIMIZEME
      if(boost::conversion::detail::try_lexical_convert(n_name.substr(root_len + 1), n))
      {
        instance_num.push_back(n);
      }
    }
    // case where we have the "default" instance without .0
    else if (same_root && root_len == n_name.length())
    {
      instance_num.push_back(0);
    }
  }

  if (!is_here)
  {
    return;
  }
  else
  {
    auto n = instance_num.size();
    if ((n == 0) || ((n == 1) && (instance_num[0] == 0)))
    {
      name = root_name;
      name += ".1";
    }
    else
    {
      std::sort(instance_num.begin(), instance_num.end());
      name = root_name;
      name += '.';
      name += boost::lexical_cast<std::string>(instance_num.back() + 1);
    }
  }
}
Example #7
0
static void
mat_write_signal(const mdf_t *const mdf, 
                 const uint32_t can_channel,
                 const uint32_t number_of_records,
                 const uint16_t channel_type,
                 const char *const message_name,
                 const char *const signal_name,
                 const double *const timeValue,
                 const filter_t *const filter,
                 const void *const cbData)
{
  char *filter_signal_name_in;
  char *filter_message_name_in;
  char *filter_name_out;
  size_t dims[2];
  mdftomat_t *const mdftomat = (mdftomat_t *)cbData;

  dims[0] = number_of_records;
  dims[1] = 2;

  if(   ((can_channel != 0) && (channel_type == 0)) /* Vector CAN */
     || ((can_channel == 0) && (channel_type == 0)) /* DIM */ ) {
    if(mdf->verbose_level >= 2) {
       uint32_t ir;
       printf("ch=%lu n=%lu m=%s s=%s\n",
              (unsigned long)can_channel,
              (unsigned long)number_of_records,
              message_name,
              signal_name);
      if(mdf->verbose_level >= 3) {
       for(ir=0;ir<number_of_records;ir++) {
          printf("%g %g\n",timeValue[ir],timeValue[ir+number_of_records]);
       }
      }
    }
    /* sanitize variable name */
    filter_signal_name_in = sanitize_name(signal_name);
    filter_message_name_in = sanitize_name(message_name);
    filter_name_out = filter_apply(filter, can_channel,
                                   filter_message_name_in,
                                   filter_signal_name_in);
    if(mdf->verbose_level >= 2) {
      printf("    CNBLOCK can_ch=%lu\n"
             "            message      = %s\n"
             "            signal_name  = %s\n"
             "            filter_input = %s\n"
             "            filter_output= %s\n",
             (unsigned long)can_channel, filter_message_name_in,
             signal_name, filter_signal_name_in,
             (filter_name_out!=NULL)?filter_name_out:"<rejected by filter>" );
      if(filter_name_out != NULL) {
        printf("+ %d %s %s %s\n",
               can_channel,
               filter_message_name_in,
               filter_signal_name_in,
               filter_name_out );
      }
    }
    free(filter_signal_name_in);
    free(filter_message_name_in);

    if(filter_name_out != NULL) {
      matvar_t *matvar;
      int rv;

      /* write matlab variable */
      matvar = Mat_VarCreate(filter_name_out, MAT_C_DOUBLE, MAT_T_DOUBLE,
                             2, dims, (double *)timeValue, 0);
      rv = Mat_VarWrite(mdftomat->mat, matvar, mdftomat->compress);
      assert(rv == 0);
      Mat_VarFree(matvar);
      free(filter_name_out);
    }
  }
}
Example #8
0
ssize_t read_chos_file(char *user_conf_file, char *dir, char *osenv)
{
    int conf;
    int i;
    ssize_t count = 0;
    int ret;
    char userfile[MAX_LEN+1];

    /* Build the full path to the user configuration file. */
    ret = snprintf(userfile,MAX_LEN+1,"%.100s/%.20s",dir,user_conf_file);

    if (ret < 1) {
        syslog(LOG_ERR, "Could not construct path to user configuration file.\n");
        return -1;
    }
    else if (ret > MAX_LEN+1) {
        syslog(LOG_ERR, "Path to user configuration file is too long.\n");
        return -1;
    }

    /*
     * Open the user configuration file and read the contents into
     * osenv.  osenv must have at least MAXLINE+1 of space available.
     *
     * If no bytes or read or the configuration file cannot be opened,
     * write the default environment name instead.
     */
    conf=open(userfile,O_RDONLY);

    if (conf >= 0) {
        /* Read environment name from user configuration file. */
        count=read(conf,osenv,MAXLINE);

        if (count == 0) {
            /* EOF was received and no bytes were read.  Log a warning and
             * continue. */
            syslog(LOG_WARNING,
                   "Warning: Read 0 bytes from user configuration file (%s)\n",
                   userfile);
        }
        else if (count < 0) {
            /* read() returned an error.  Log a warning and confinue. */
            syslog(LOG_WARNING,
                   "Warning: failed to read() user configuration file (%s): %s\n",
                   userfile, strerror(errno));
            count = 0;
        }

        /* Explicitly set osenv[count] to the null byte.  On error or EOF,
         * this sets osenv[0] to '\0'.  On successful reads, it ensures
         * the string is null-terminated. */
        osenv[count] = '\0';

        if (close(conf) != 0) {
            syslog(LOG_WARNING,
                   "Could not close() user configuration file (%s): %s\n",
                   userfile, strerror(errno));
            return(-1);
        }
    }
    else if(errno != ENOENT) {
        /* The user configuration file could not be opened.  Log a warning
         * if open() failed for a reason other than ENOENT, and continue.
         */
        syslog(LOG_WARNING,
               "Warning: Could not open user configuration file (%s): %s\n",
               userfile, strerror(errno));
    }

    if (count == 0) {
        /* If no bytes were read, then set osenv to the default
         * environment name.
         */
        snprintf(osenv, MAXLINE+1, "%s", DEFAULT_ENV_NAME);
        count=strlen(osenv) + 1;
    }

    for (i = 0; i < count; i++) {
        if ((osenv[i]=='\n') || (osenv[i]=='\0') )
            break;
    }

    osenv[i]=0;

    sanitize_name(osenv,strlen(osenv));

    return count;
}
Example #9
0
int get_chos_info(int argc, const char **argv, int fd, char *user_conf_dir) {

    char osenv[MAXLINE+1];
    char *os;
    const char *env;
    FILE *f;
    pam_chos_config cfg;

    if(!(init_pam_chos_config(&cfg))) {
        syslog(LOG_ERR, "Failed to initialize configuration\n");
        return -1;
    }

    if(parse_pam_chos_args(&cfg, argc, argv) != 1) {
        syslog(LOG_ERR, "Failed to parse configuration\n");
        return -1;
    }

    if( (read_chos_file(cfg.user_conf_file, user_conf_dir, osenv)) < 0) {
        syslog(LOG_ERR, "Error while handling user configuration file\n");
        return -1;
    }

    /* If $CHOS is set, use its value as the OS name. */
    if ((env=getenv("CHOS"))) {
        if ( strlen(env) > MAXLINE ) {
            syslog(LOG_WARNING, "Warning: $CHOS value is too long, ignoring\n");
        }
        else {
            strncpy(osenv,env,MAXLINE+1);
            sanitize_name(osenv, strlen(osenv));
        }
    }


    /*
     * Attempt to retrieve the environment path from the environment
     * name.
     * */
    os=check_chos(osenv);


    if (os == NULL) {
        /* We were unable to match the environment name against a valid
         * environment path. */

        if ( (strcmp(osenv,DEFAULT_ENV_NAME)==0) ||
                (cfg.fail_to_default > 0) ) {
            /* If the environment name is "default", or fail_to_default==1,
             * then fail back to the default environment. */
            syslog(LOG_WARNING,
                   "Warning: requested os (%s) is not recognized; using default (fail_to_default=%d).\n",
                   osenv, cfg.fail_to_default);
            os=check_chos(DEFAULT_ENV_NAME);
        }

        else {
            /* Try the (likely invalid) environment name. */
            syslog(LOG_WARNING,
                   "Warning: requested os (%s) is not recognized.\n",osenv);
            os=osenv;
        }
    }


    /* Write the environment name and path to fd */
    f = fdopen(fd,"w");
    if(!f) {
        syslog(LOG_ERR, "fdopen() failed: %s\n",strerror(errno));
        return -1;
    }

    /* Write the os path and name to the file. */
    fputs(os,f);
    fputc('\n',f);
    fputs(osenv,f);
    fputc('\n',f);

    /* We are done writing to the pipe, so we can close it now. */
    if(fclose(f) != 0) {
        syslog(LOG_ERR, "fclose() failed: %s\n",strerror(errno));
        return -1;
    }

    return 1;
}