Exemple #1
0
static int pbap_setpath(struct obex_session *os, obex_object_t *obj,
		void *user_data)
{
	struct pbap_session *pbap = user_data;
	const char *name;
	uint8_t *nonhdr;
	char *fullname;
	int err;

	if (OBEX_ObjectGetNonHdrData(obj, &nonhdr) != 2) {
		error("Set path failed: flag and constants not found!");
		return -EBADMSG;
	}

	name = obex_get_name(os);

	DBG("name %s folder %s nonhdr 0x%x%x", name, pbap->folder,
							nonhdr[0], nonhdr[1]);

	fullname = phonebook_set_folder(pbap->folder, name, nonhdr[0], &err);
	if (err < 0)
		return err;

	g_free(pbap->folder);
	pbap->folder = fullname;

	/*
	 * FIXME: Define a criteria to mark the cache as invalid
	 */
	pbap->cache.valid = FALSE;
	pbap->cache.index = 0;
	cache_clear(&pbap->cache);

	return 0;
}
Exemple #2
0
void obexsrv_connect(obex_t *handle, obex_object_t *object)
{
	obex_headerdata_t 	hv;
	uint8_t			hi;
	int			hlen;
	uint8_t			*nonhdrdata;
	obex_target_t		target = {0, NULL};
	obexsrv_t		*srv = OBEX_GetUserData(handle);
	int			err;

	DBPRT("");

	if(OBEX_ObjectGetNonHdrData(object, &nonhdrdata) == 4) {
#ifdef CONFIG_AFFIX_DEBUG
		obex_connect_hdr_t	*hdr = (obex_connect_hdr_t*)nonhdrdata;
		DBPRT("Version: 0x%02x. Flags: 0x%02x  OBEX packet length:%d",
			hdr->version, hdr->flags, ntohs(hdr->mtu));
#endif
	} else {
		BTERROR("Invalid packet content.");
	}
	while(OBEX_ObjectGetNextHeader(handle, object, &hi, &hv, &hlen)) {
		switch (hi) {
			case OBEX_HDR_TARGET:
				target.data = (void*)hv.bs;
				target.len = hlen;
				if (hlen == 16)
					DBPRT("got TARGET. uuid_t: %08X-%04X-%04X-%04X-%08X%04X",
							*(uint32_t *)&target.data[0], *(uint16_t *)&target.data[4],
							*(uint16_t *)&target.data[6], *(uint16_t *)&target.data[8],
							*(uint32_t *)&target.data[10], *(uint16_t *)&target.data[14]);
				else
					DBPRT("got TARGET. unknown fmt");
				break;
			default:	
				DBPRT(" Skipped header %02x", hi);
				break;
		}
	}

	// call handler
	err = srv->connect(srv, &target);
	if (err < 0) {
		/* error */
		OBEX_ObjectSetRsp(object, OBEX_RSP_INTERNAL_SERVER_ERROR, OBEX_RSP_INTERNAL_SERVER_ERROR);
	} else {
		OBEX_ObjectSetRsp(object, OBEX_RSP_SUCCESS, OBEX_RSP_SUCCESS);
		if (target.data) {
			hv.bq4 = err;	/* set connection id */
			OBEX_ObjectAddHeader(handle, object, OBEX_HDR_CONNECTION, hv, 4, 0);
			hv.bs = target.data;
			OBEX_ObjectAddHeader(handle, object, OBEX_HDR_WHO, hv, target.len, 0);
		}
	}
}
Exemple #3
0
static void obex_connect_done(obex_t *handle,
					obex_object_t *object, int response)
{
	obex_context_t *context = OBEX_GetUserData(handle);
        obex_headerdata_t hd;
        uint32_t hl;
        uint8_t hi, *ptr;

	if (response != OBEX_RSP_SUCCESS)
		return;

	context->state = OBEX_CONNECTED;

	if (OBEX_ObjectGetNonHdrData(object, &ptr) == sizeof(obex_connect_hdr_t)) {
		obex_connect_hdr_t *chdr = (obex_connect_hdr_t *) ptr;
		uint16_t mtu = ntohs(chdr->mtu);
		int new_size;
		
		debug("Connect success. Version: 0x%02x. Flags: 0x%02x OBEX packet length: %d",
				chdr->version, chdr->flags, mtu);

		/* Leave space for headers */
		new_size = mtu - 200;
		if (new_size < context->tx_max) {
			debug("Resizing stream chunks to %d", new_size);
			context->tx_max = new_size;
		}
	}

	while (OBEX_ObjectGetNextHeader(handle, object, &hi, &hd, &hl)) {
		switch (hi) {
		case OBEX_HDR_CONNECTION:
			context->cid = hd.bq4;
			break;
		case OBEX_HDR_WHO:
			break;
		}
	}

	if (context->callback && context->callback->connect_cfm)
		context->callback->connect_cfm(handle, context->user_data);
}
Exemple #4
0
static int mas_setpath(struct obex_session *os, obex_object_t *obj,
		void *user_data)
{
	const char *name;
	uint8_t *nonhdr;
	struct mas_session *mas = user_data;

	if (OBEX_ObjectGetNonHdrData(obj, &nonhdr) != 2) {
		error("Set path failed: flag and constants not found!");
		return -EBADR;
	}

	name = obex_get_name(os);

	DBG("SETPATH: name %s nonhdr 0x%x%x", name, nonhdr[0], nonhdr[1]);

	if ((nonhdr[0] & 0x02) != 0x02) {
		DBG("Error: requested directory creation");
		return -EBADR;
	}

	return messages_set_folder(mas->backend_data, name, nonhdr[0] & 0x01);
}
Exemple #5
0
int ftp_setpath(struct obex_session *os, obex_object_t *obj, void *user_data)
{
	struct ftp_session *ftp = user_data;
	const char *root_folder, *name;
	uint8_t *nonhdr;
	char *fullname;
	struct stat dstat;
	gboolean root;
	int err;

	if (OBEX_ObjectGetNonHdrData(obj, &nonhdr) != 2) {
		error("Set path failed: flag and constants not found!");
		return -EBADMSG;
	}

	name = obex_get_name(os);
	root_folder = obex_get_root_folder(os);
	root = g_str_equal(root_folder, ftp->folder);

	DBG("%p name %s", ftp, name);

	/* Check flag "Backup" */
	if ((nonhdr[0] & 0x01) == 0x01) {
		DBG("Set to parent path");

		if (root)
			return -EPERM;

		fullname = g_path_get_dirname(ftp->folder);
		set_folder(ftp, fullname);
		g_free(fullname);

		DBG("Set to parent path: %s", ftp->folder);

		return 0;
	}

	if (!name) {
		DBG("Set path failed: name missing!");
		return -EINVAL;
	}

	if (strlen(name) == 0) {
		DBG("Set to root");
		set_folder(ftp, root_folder);
		return 0;
	}

	/* Check and set to name path */
	if (strstr(name, "/") || strcmp(name, "..") == 0) {
		error("Set path failed: name incorrect!");
		return -EPERM;
	}

	fullname = g_build_filename(ftp->folder, name, NULL);

	DBG("Fullname: %s", fullname);

	if (root && obex_get_symlinks(os))
		err = stat(fullname, &dstat);
	else
		err = lstat(fullname, &dstat);

	if (err < 0) {
		err = -errno;

		if (err == -ENOENT)
			goto not_found;

		DBG("%s: %s(%d)", root ? "stat" : "lstat",
				strerror(-err), -err);

		goto done;
	}

	if (S_ISDIR(dstat.st_mode) && (dstat.st_mode & S_IRUSR) &&
						(dstat.st_mode & S_IXUSR)) {
		set_folder(ftp, fullname);
		goto done;
	}

	err = -EPERM;
	goto done;

not_found:
	if (nonhdr[0] != 0) {
		err = -ENOENT;
		goto done;
	}

	if (mkdir(fullname, 0755) <  0) {
		err = -errno;
		DBG("mkdir: %s(%d)", strerror(-err), -err);
		goto done;
	}

	err = 0;
	set_folder(ftp, fullname);

done:
	g_free(fullname);
	return err;
}
Exemple #6
0
void obexsrv_setpath(obex_t *handle, obex_object_t *object)
{
	obex_headerdata_t	hv;
	uint8_t 		hi;
	int 			hlen;
	char			*name = NULL;
	int			con_id = -1;
	uint8_t			*nonhdrdata;
	int			flags;
	int			len;
	obexsrv_t		*srv = OBEX_GetUserData(handle);
	int			err;

	DBPRT("");
	
	if ((len = OBEX_ObjectGetNonHdrData(object, &nonhdrdata)) == 2) {
		obex_setpath_hdr_t	*hdr = (obex_setpath_hdr_t*)nonhdrdata;
		flags = hdr->flags;
		DBPRT("Flags= 0x%02x  Constants=%d", flags, hdr->constants);
		
	} else {
		BTERROR("Invalid packet content. len=%d", len);
		flags = 0;
	}

	while(OBEX_ObjectGetNextHeader(handle, object, &hi, &hv, &hlen)) {
		switch(hi) {
		case OBEX_HDR_NAME:
			if (hlen) {
				if ((name = malloc(hlen/2)))
					OBEX_UnicodeToChar(name, hv.bs, hlen);
			} else
				flags |= 0x80;	// name exits but zero -> set root
			DBPRT("Found name:  %s", name);
			break;

		case OBEX_HDR_CONNECTION:
			con_id = hv.bq4;
			DBPRT("Found connection id: %08x", con_id);
			break;
		
		default:
			DBPRT("Skipped header %02x", hi);
			break;
		}
	}
	// call handler
	err = srv->setpath(srv, name, flags);
	if (err)
		goto error;
		
	OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS);
	if (name)
		free(name);
	return;
error:
	OBEX_ObjectSetRsp(object, OBEX_RSP_NOT_FOUND, OBEX_RSP_NOT_FOUND);
	if (name)
		free(name);
	return;
}
Exemple #7
0
/*
    Returns new reference that must be decref'd.
*/
static PyObject*
obexserver_notifynewrequest(OBEXServer *self, obex_object_t *obj, int obex_cmd, int *respcode)
{
    PyObject *resp;
    PyObject *respheaders;
    PyObject *tmpfileobj;

    PyObject *reqheaders;
    int nonhdrdata_len;
    PyObject *nonhdrdata_obj;
    uint8_t *nonhdrdata;

    DEBUG("%s() cmd=%d\n", __func__, obex_cmd);

    if (self->notifiednewrequest) {
        DEBUG("\tAlready called cb_newrequest");
        return NULL;
    }

    if (self->cb_newrequest == NULL) {  /* shouldn't happen */
        obexserver_errorstr(self, PyExc_IOError, "cb_newrequest is NULL");
        return NULL;
    }

    reqheaders = lightblueobex_readheaders(self->obex, obj);
    if (reqheaders == NULL) {
        obexserver_errorstr(self, PyExc_IOError,
                            "error reading request headers");
        return NULL;
    }

    nonhdrdata_len = OBEX_ObjectGetNonHdrData(obj, &nonhdrdata);
    if (nonhdrdata_len < 0) {
        obexserver_errorstr(self, PyExc_IOError,
                            "error reading non-header data");
        return NULL;
    }

    nonhdrdata_obj = PyBuffer_FromMemory(nonhdrdata,
                                         (Py_ssize_t)nonhdrdata_len);
    if (nonhdrdata_obj == NULL) {
        obexserver_errorstr(self, PyExc_IOError,
                            "error reading non-header buffer");
        return NULL;
    }

    resp = PyObject_CallFunction(self->cb_newrequest, "iOOO",
                                 obex_cmd, reqheaders, nonhdrdata_obj,
                                 (self->hasbodydata ? Py_True : Py_False));
    Py_DECREF(nonhdrdata_obj);
    self->notifiednewrequest = 1;

    if (resp == NULL) {
        DEBUG("\terror calling cb_newrequest\n");
        obexserver_errorfetch(self);
        return NULL;
    }

    if ( !PyTuple_Check(resp) || PyTuple_Size(resp) < 3 ||
            !PyInt_Check(PyTuple_GetItem(resp, 0)) ||
            !PyDict_Check(PyTuple_GetItem(resp, 1)) ) {
        obexserver_errorstr(self, PyExc_TypeError,
                            "callback must return (int, dict, fileobj | None) tuple");
        return NULL;
    }

    tmpfileobj = PyTuple_GetItem(resp, 2);

    if (obex_cmd == OBEX_CMD_PUT && self->hasbodydata &&
            !PyObject_HasAttrString(tmpfileobj, "write")) {
        obexserver_errorstr(self, PyExc_ValueError,
                            "specified file object does not have 'write' method for Put request");
        return NULL;
    }

    if (obex_cmd == OBEX_CMD_GET &&
            !PyObject_HasAttrString(tmpfileobj, "read")) {
        obexserver_errorstr(self, PyExc_ValueError,
                            "specified file object does not have 'read' method for Get request");
        return NULL;
    }

    *respcode = PyInt_AsLong(PyTuple_GetItem(resp, 0));
    if (PyErr_Occurred()) {
        PyErr_Clear();
        obexserver_errorstr(self, PyExc_IOError,
                            "error reading returned response code");
        return NULL;
    }

    Py_XDECREF(self->fileobj);
    Py_INCREF(tmpfileobj);
    self->fileobj = tmpfileobj;

    respheaders = PyTuple_GetItem(resp, 1);
    Py_INCREF(respheaders);
    return respheaders;
}
Exemple #8
0
static void set_server_path(obex_t *handle, obex_object_t *object)
{
	char *name = NULL;
	char fullname[WORK_PATH_MAX];

	// "Backup Level" and "Don't Create" flag in first byte
	uint8_t setpath_nohdr_dummy = 0;
	uint8_t *setpath_nohdr_data;
	obex_headerdata_t hv;
	uint8_t hi;
	uint32_t hlen;

	OBEX_ObjectGetNonHdrData(object, &setpath_nohdr_data);
	if (NULL == setpath_nohdr_data) {
		setpath_nohdr_data = &setpath_nohdr_dummy;
		printf("nohdr data not found\n");
	}
	printf("nohdr data: %x\n", *setpath_nohdr_data);

	while(OBEX_ObjectGetNextHeader(handle, object, &hi, &hv, &hlen))	{
		switch(hi)	{
		case OBEX_HDR_NAME:
			printf("%s() Found name\n", __FUNCTION__);
			if (0 < hlen)
			{
				if( (name = malloc(hlen / 2)))	{
					OBEX_UnicodeToChar((uint8_t*)name, hv.bs, hlen);
					printf("name:%s\n", name);
				}
			}
			else
			{
				if (verbose) printf("set path to root\n");
				chdir(init_work_path);
			}
			break;
			
		default:
			printf("%s() Skipped header %02x\n", __FUNCTION__, hi);
		}
	}	
	
	if (name)
	{
		if (strstr(name, "/../"))
		{
			OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_FORBIDDEN);
		} else {
		strcpy(fullname, CUR_DIR);
		strncat(fullname, name, sizeof(fullname)-1);
		if ((*setpath_nohdr_data & 2) == 0) {
			if (verbose) printf("mkdir %s\n", name);
			if (mkdir(fullname, 0755) < 0) {
				perror("requested mkdir failed");
			}
		}
		if (verbose) printf("Set path to %s\n",fullname);
		if (chdir(fullname) < 0)
		{
			perror("requested chdir failed\n");
			OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_FORBIDDEN);
		}
		}
		free(name);
		name = NULL;
	}
}