Ejemplo n.º 1
0
// -1 Failed to parse
// -2 Buffer too small
static int
pack_shadow_struct(json_t *root, struct spwd *result, char *buffer, size_t buflen)
{

    char *next_buf = buffer;
    size_t bufleft = buflen;

    if (!json_is_object(root)) return -1;

    json_t *j_sp_namp = json_object_get(root, "sp_namp");
    json_t *j_sp_pwdp = json_object_get(root, "sp_pwdp");
    json_t *j_sp_lstchg = json_object_get(root, "sp_lstchg");
    json_t *j_sp_min = json_object_get(root, "sp_min");
    json_t *j_sp_max = json_object_get(root, "sp_max");
    json_t *j_sp_warn = json_object_get(root, "sp_warn");
    json_t *j_sp_inact = json_object_get(root, "sp_inact");
    json_t *j_sp_expire = json_object_get(root, "sp_expire");
    json_t *j_sp_flag = json_object_get(root, "sp_flag");

    if (!json_is_string(j_sp_namp)) return -1;
    if (!json_is_string(j_sp_pwdp)) return -1;
    if (!json_is_integer(j_sp_lstchg)) return -1;
    if (!json_is_integer(j_sp_min)) return -1;
    if (!json_is_integer(j_sp_max)) return -1;
    if (!json_is_integer(j_sp_warn)) return -1;
    if (!json_is_integer(j_sp_inact) && !json_is_null(j_sp_inact)) return -1;
    if (!json_is_integer(j_sp_expire) && !json_is_null(j_sp_expire)) return -1;
    if (!json_is_integer(j_sp_flag) && !json_is_null(j_sp_flag)) return -1;

    memset(buffer, '\0', buflen);

    if (bufleft <= j_strlen(j_sp_namp)) return -2;
    result->sp_namp = strncpy(next_buf, json_string_value(j_sp_namp), bufleft);
    next_buf += strlen(result->sp_namp) + 1;
    bufleft  -= strlen(result->sp_namp) + 1;

    if (bufleft <= j_strlen(j_sp_pwdp)) return -2;
    result->sp_pwdp = strncpy(next_buf, json_string_value(j_sp_pwdp), bufleft);
    next_buf += strlen(result->sp_pwdp) + 1;
    bufleft  -= strlen(result->sp_pwdp) + 1;

    // Yay, ints are so easy!
    result->sp_lstchg = json_integer_value(j_sp_lstchg);
    result->sp_min = json_integer_value(j_sp_min);
    result->sp_max = json_integer_value(j_sp_max);
    result->sp_warn = json_integer_value(j_sp_warn);

    if (!json_is_null(j_sp_inact)) result->sp_inact = json_integer_value(j_sp_inact);
    else result->sp_inact = -1;

    if (!json_is_null(j_sp_expire)) result->sp_expire = json_integer_value(j_sp_expire);
    else result->sp_expire = -1;

    if (!json_is_null(j_sp_flag)) result->sp_flag = json_integer_value(j_sp_flag);
    else result->sp_flag = ~0ul;

    return 0;
}
Ejemplo n.º 2
0
/*
 *  js_session_get -- find the session for a resource
 *
 *  Given a user and a resource, find the corresponding session
 *  if the user is logged in. Otherwise return NULL.
 *
 *  parameters
 *	user -- the user's udata record
 *	res -- the resource to search for
 *
 *  returns
 *	a pointer to the session if the user is logged in
 *	NULL if the user isn't logged in on this resource
 */
session js_session_get(udata user, char *res)
{
	session cur;		/* session pointer */

	/* screen out illeagal calls */
	if (user == NULL || res == NULL)
		return NULL;

	/* find the session and return it */
	SEM_LOCK(user->sem);
	for (cur = user->sessions; cur != NULL; cur = cur->next)
		if ((j_strcmp(res, cur->res) == 0) && (!cur->exit_flag)) {
			SEM_UNLOCK(user->sem);
			return cur;
		}

	/* find any matching resource that is a subset and return it */
	for (cur = user->sessions; cur != NULL; cur = cur->next)
		if ((j_strncmp(res, cur->res, j_strlen(cur->res)) == 0)
		    && (!cur->exit_flag)) {
			SEM_UNLOCK(user->sem);
			return cur;
		}

	SEM_UNLOCK(user->sem);

	/* if we got this far, there is no session */
	return NULL;

}
Ejemplo n.º 3
0
Archivo: jid.c Proyecto: named-data/muc
/**
 * resourceprep the resource identifier in a JID and check if it is valid
 *
 * @param jid data structure holding the JID
 * @return 0 if JID is valid, non zero otherwise
 */
static int _jid_safe_resource(jid id) {
    int result=0;

    /* it is valid to have no resource identifier in the JID */
    if (id->resource == NULL)
	return 0;

    /* resource prep the resource identifier */
    result = _jid_cached_stringprep(id->resource, strlen(id->resource)+1, _jid_prep_cache_resource);
    if (result == STRINGPREP_TOO_SMALL_BUFFER) {
	/* resourceprep wants to expand the string, e.g. conversion from &szlig; to ss */
	size_t biggerbuffersize = 1024;
	char *biggerbuffer = pmalloc(id->p, biggerbuffersize);
	if (biggerbuffer == NULL)
	    return 1;
	strcpy(biggerbuffer, id->resource);
	result = _jid_cached_stringprep(id->resource, strlen(id->resource)+1, _jid_prep_cache_resource);
	id->resource = biggerbuffer;
    }
    if (result != STRINGPREP_OK)
	return 1;

    /* the resourcepreped resource must not be longer than 1023 bytes */
    if (j_strlen(id->resource) > 1023)
	return 1;

    /* check if resource was zeroed by stringprep */
    if (*id->resource == '\0')
	id->resource = NULL;

    /* if nothing failed, the resource is valid */
    return 0;

}
Ejemplo n.º 4
0
Archivo: jid.c Proyecto: named-data/muc
/**
 * nameprep the domain identifier in a JID and check if it is valid
 *
 * @param jid data structure holding the JID
 * @return 0 if JID is valid, non zero otherwise
 */
static int _jid_safe_domain(jid id) {
    int result=0;

    /* there must be a domain identifier */
    if (id->server == NULL || *id->server == '\0')
	return 1;

    /* nameprep the domain identifier */
    result = _jid_cached_stringprep(id->server, strlen(id->server)+1, _jid_prep_cache_domain);
    if (result == STRINGPREP_TOO_SMALL_BUFFER) {
	/* nameprep wants to expand the string, e.g. conversion from &szlig; to ss */
	size_t biggerbuffersize = 1024;
	char *biggerbuffer = pmalloc(id->p, biggerbuffersize);
	if (biggerbuffer == NULL)
	    return 1;
	strcpy(biggerbuffer, id->server);
	result = _jid_cached_stringprep(biggerbuffer, biggerbuffersize, _jid_prep_cache_domain);
	id->server = biggerbuffer;
    }
    if (result != STRINGPREP_OK)
	return 1;

    /* the namepreped domain must not be longer than 1023 bytes */
    if (j_strlen(id->server) > 1023)
	return 1;

    /* if nothing failed, the domain is valid */
    return 0;
}
Ejemplo n.º 5
0
Archivo: jid.c Proyecto: named-data/muc
/**
 * check if the resource identifier in a JID is valid
 *
 * @param jid data structure holding the JID
 * @return 0 if resource is valid, non zero otherwise
 */
static int _jid_safe_resource(jid id) {
    /* resources may not be longer than 1023 bytes */
    if (j_strlen(id->resource) > 1023)
	return 1;

    /* otherwise it's okay as far as we can tell without LIBIDN */
    return 0;
}
Ejemplo n.º 6
0
/* returns a valid nick from the list, x is the first <nick>foo</nick> xmlnode, checks siblings */
char *con_room_nick(cnr room, cnu user, xmlnode x)
{
  char *nick = NULL;
  xmlnode cur;
  int count = 1;

  if(room == NULL || user == NULL) 
  {
    log_warn(NAME, "[%s] Aborting - NULL attribute found", FZONE);
    return NULL;
  }

  log_debug(NAME, "[%s] looking for valid nick in room %s from starter %s", FZONE, jid_full(room->id), xmlnode2str(x));

  /* make-up-a-nick phase */
  if(x == NULL)
  {
    nick = pmalloco(user->p, j_strlen(user->realid->user) + 10);
    log_debug(NAME, "[%s] Malloc: Nick = %d", FZONE, j_strlen(user->realid->user) + 10);

    sprintf(nick, "%s", user->realid->user);
    while(g_hash_table_lookup(room->local, nick) != NULL)
      sprintf(nick, "%s%d", user->realid->user,count++);
    return nick;
  }

  /* scan the list */
  for(cur = x; cur != NULL; cur = xmlnode_get_nextsibling(cur))
  {
    if(j_strcmp(xmlnode_get_name(cur),"nick") == 0 && (nick = xmlnode_get_data(cur)) != NULL)
      if(g_hash_table_lookup(room->local, nick) == NULL)
        break;
  }

  if(is_registered(room->master, jid_full(jid_user(user->realid)), nick) == -1)
    nick = NULL;

  return nick;
}
Ejemplo n.º 7
0
Archivo: jid.c Proyecto: named-data/muc
/**
 * check if the node identifier in a JID is valid
 *
 * @param jid data structure holding the JID
 * @return 0 if node is valid, non zero otherwise
 */
static int _jid_safe_node(jid id) {
    char *str;

    /* node identifiers may not be longer than 1023 bytes */
    if (j_strlen(id->user) > 1023)
	return 1;

    /* check for low and invalid ascii characters in the username */
    if(id->user != NULL)
        for(str = id->user; *str != '\0'; str++)
            if(*str <= 32 || *str == ':' || *str == '@' || *str == '<' || *str == '>' || *str == '\'' || *str == '"' || *str == '&' || *str == '/') return 1;

    /* otherwise it's okay as far as we can tell without LIBIDN */
    return 0;
}
Ejemplo n.º 8
0
static void execute_dpof_job(void)
{
    char *dpof_file = NULL;
    char *dpof_jobs = NULL;
    sicd_object_t *obj = NULL;
    jint_t i;
    dps_job_config_t jconfig;
    dps_job_info_t jinfo;
    char *filename = NULL;
    DECLARE_FNAME("execute_dpof_job");

    DBG_X(DSLAVE_DPS_API, ("%s: entered\n", fname));

    dpof_file = (char*)jmalloc(MAX_DPOF_FILE_SIZE, M_ZERO);
    dpof_jobs = (char*)jmalloc(MAX_DPOF_FILE_SIZE, M_ZERO);
    if (!dpof_file || !dpof_jobs)
    {
        DBG_E(DSLAVE_DPS_API, ("%s: failed to allocate job info\n", fname));
        goto Error;
    }

    /* Generate JOB sections */
    *dpof_jobs = '\0';
    for (i=0; sample_image[i].image; i++)
    {
        if (!sicd_store_find_object(app_ctx.store_id, sample_image[i].id))
        {
            DBG_E(DSLAVE_DPS_API, ("%s: could not find picture object "
                "(%X) in store\n", filename, sample_image[i].id));
            goto Error;
        }

        filename = j_strdup(sample_image[i].name);
        if (!filename)
        {
            DBG_E(DSLAVE_DPS_API, ("%s: error allocating memory (strdup)."
                "skipping %s\n", fname, sample_image[i].name));
            continue;
        }

        to_upper(filename);
        
        /* TODO: In a real system, the file name given here should be
         * the image's path relative to the DPOF file */
        j_snprintf(dpof_jobs, MAX_DPOF_FILE_SIZE, DPOF_JOB_TEMPLATE,
            dpof_jobs, i+1, TEST_NUMBER_OF_COPIES, DCF_PREFIX,
            filename);

        jfree(filename);
    }

    dpof_jobs[MAX_DPOF_FILE_SIZE-1] = '\0';

    /* Generate header */
    j_snprintf(dpof_file, MAX_DPOF_FILE_SIZE, DPOF_HDR_TEMPLATE, dpof_jobs);
    dpof_file[MAX_DPOF_FILE_SIZE-1] = '\0';

    DBG_X(DSLAVE_DPS_API, ("%s: DPOF file: \n%s\n\n", fname, dpof_file));

    /*
     *   DPOF file generation ends here. Proper API sample code
     *   for DPOF printing begins here.
     */
    
    /* Add DPOF virtual file to store */
    obj = jmalloc(sizeof(sicd_object_t), M_ZERO);
    if (!obj)
    {
        DBG_E(DSLAVE_DPS_API, ("%s: failed to allocate sicd_object_t "
            "for sample image %u\n", fname, i+1));
        goto Error;
    }

    app_ctx.dpof_file_id = obj->id = sicd_get_new_obj_handle();
    obj->info.storage_id = app_ctx.store_id;
    obj->info.format = PTP_OBJ_FORMAT_DPOF;
    obj->info.compressed_size = j_strlen(dpof_file)+1;
    obj->info.filename = ptp_string_atow("/MISC/AUTPRINT.MRK");
    obj->data = (juint8_t*)dpof_file;

    if (sicd_store_add_object(app_ctx.store_id, obj))
    {
        DBG_E(DSLAVE_DPS_API, ("generate_dpof: failed on "
            "sicd_store_add_object (%u)\n"));
        goto Error;
    }

    /* Create JobInfo for DPOF file */
    j_memset(&jconfig, 0, sizeof(jconfig));
    jconfig.file_type = DPS_FILE_TYPE_DPOF;

    j_memset(&jinfo, 0, sizeof(jinfo));
    jinfo.file_id = obj->id;
    j_strlcpy(jinfo.file_name, "/MISC/AUTPRINT.MRK", DPS_FILENAME_SIZE + 1);

    /* If we are testing DPOF restart, add relevant parameters */
    if (TEST_DPOF_RESTART && app_ctx.can_restart)
    {
        j_memcpy(&jinfo.dpof_restart, &app_ctx.dpof_restart_info,
            sizeof(dpof_params_t));
    }

    /* Send Job */
    dps_start_job(app_ctx.dpsh, &jconfig, &jinfo, 1);
    goto Exit;

Error:
    if (obj)
        sicd_free_object(NULL, obj);
    else if (dpof_file)
        jfree(dpof_file);
Exit:
    if (dpof_jobs)
        jfree(dpof_jobs);
}