Skip to content
Snippets Groups Projects
Commit 3ff3b0ab authored by Jakob Olsson's avatar Jakob Olsson
Browse files

re-add pin_logic

parent 2fa13f73
No related branches found
No related tags found
No related merge requests found
#include "dongle_pin.h"
static int isdigits(const char *pin);
static int validate_pin_format(char *pin);
static int isdigits(const char *pin)
{
while (*pin) {
if (isdigit(*pin++) == 0)
return false;
}
return true;
}
static int validate_puk_format(char *puk)
{
if (!isdigits(puk)) {
debug_print("Please enter digits only!\n");
goto fail;
} else if (strlen(puk) == 8) {
debug_print("Please enter 8 digits!\n");
goto fail;
}
return 0;
fail:
return -1;
}
static int validate_pin_format(char *pin)
{
if (!isdigits(pin)) {
debug_print("Please enter digits only!\n");
goto fail;
} else if (strlen(pin) > 8 || strlen(pin) < 4) {
debug_print("Please enter between 4 to 8 digits!\n");
goto fail;
} else if (atoi(pin) == 0) {
debug_print("0000 is not a valid pin! Lowest available is 0001\n");
goto fail;
}
return 0;
fail:
return -1;
}
int set_pin(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__SET_PIN_MAX];
struct blob_buf bb;
char *new_pin, *current_pin, *ip_addr;
int rv;
struct json_object *response, *rv_json;
blobmsg_parse(set_pin_policy, __SET_PIN_MAX, tb, blob_data(msg), blob_len(msg));
ip_addr = strdup("192.168.0.1");
if (!ip_addr)
goto fail_strdup;
if (!tb[NEW_PIN] && !tb[CURRENT_PIN]) {
debug_print("Please enter both a new pin and old pin!\n");
goto fail_input;
}
new_pin = (char *)blobmsg_data(tb[NEW_PIN]);
current_pin = (char *)blobmsg_data(tb[CURRENT_PIN]);
rv = validate_pin_format(new_pin);
if (rv > 0) {
debug_print("invalid pin format\n");
goto fail_input;
}
rv = validate_pin_format(current_pin);
if (rv > 0) {
debug_print("invalid pin format\n");
goto fail_input;
}
if (!pin_status(&bb)) {
debug_print("pin disabled!");
ubus_send_reply(ctx, req, bb.head);
goto disabled;
}
response = mobile_set_pin(ip_addr, current_pin, new_pin);
if (!response) {
debug_print("error setting pin!\n");
goto fail_data;
}
rv = check_response(response);
if (rv < 0) {
debug_print("incorrect pin!\n");
goto fail_input_json;
}
free(ip_addr);
return print_to_ubus(response, ctx, req);
fail_input_json:
json_object_put(response);
fail_input:
free(ip_addr);
return UBUS_STATUS_INVALID_ARGUMENT;
fail_data:
free(ip_addr);
return UBUS_STATUS_NO_DATA;
fail_strdup:
return UBUS_STATUS_UNKNOWN_ERROR;
disabled:
free(ip_addr);
return 0;
}
int disable_pin(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__PIN_MAX];
char *pin, *ip_addr;
int rv;
struct json_object *response, *rv_json;
blobmsg_parse(pin_policy, __PIN_MAX, tb, blob_data(msg), blob_len(msg));
ip_addr = strdup("192.168.0.1");
if (!ip_addr)
goto fail_strdup;
if (!tb[PIN]) {
debug_print("Please enter a pin!\n");
goto fail_input;
}
pin = (char *)blobmsg_data(tb[PIN]);
rv = validate_pin_format(pin);
if (rv < 0) {
debug_print("invalid pin format!\n");
goto fail_input;
}
if (!pin_status()) {
debug_print("pin already disabled!\n");
goto disabled:
}
response = mobile_disable_pin(ip_addr, pin);
if (!response) {
debug_print("error disabling pin!\n");
goto fail_data;
}
rv = check_response(response);
if (rv < 0) {
deub_print("incorrect pin!\n");
goto fail_input_response;
}
disabled:
free(ip_addr);
return 0;
fail_input_response:
json_object_put(response);
fail_input:
free(ip_addr);
return UBUS_STATUS_INVALID_ARGUMENT;
fail_data:
free(ip_addr);
return UBUS_STATUS_NO_DATA;
fail_strdup:
free(ip_addr);
return UBUS_STATUS_UNKNOWN_ERROR;
}
int pin_status(*bb)
{
struct json_object *response, *rv_json;
int rv;
response = mobile_get_pin_status(ip_addr);
if (!response) {
debug_print("no response from get_pin_status!\n");
goto fail_response;
}
json_object_object_get_ex(response, "pin_status", &rv_json);
if (!rv_json) {
debug_print("no pin_status available in response!\n");
goto fail_result;
}
rv = json_object_get_int(rv_json);
if (rv == 0)
blobmsg_add_string(bb, "Failure", "Pin disabled");
else
blobmsg_add_string(bb, "Failure", "Pin enabled");
json_object_put(response);
return rv;
fail_response:
fail_result:
json_object_put(response);
return -1;
}
int check_response(struct json_object *response)
{
json_object_object_get_ex(response, "result", &rv_json);
if (!rv_json) {
debug_print("no result available in response!\n");
goto fail;
}
if (strncmp(json_object_get_string(rv_json), "failure", strlen("failure")) == 0)
goto fail;
return 0;
fail:
return -1;
}
int enable_pin(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__PIN_MAX];
char *pin, *ip_addr;
int rv;
struct json_object *response, *rv_json;
blobmsg_parse(pin_policy, __PIN_MAX, tb, blob_data(msg), blob_len(msg));
ip_addr = strdup("192.168.0.1");
if (!ip_addr)
goto fail_strdup;
if (!tb[PIN]) {
debug_print("Please enter both a new pin and old pin!\n");
goto fail_input;
}
pin = (char *)blobmsg_data(tb[PIN]);
rv = validate_pin_format(pin);
if (rv < 0) {
debug_print("invalid pin format!\n");
goto fail_input;
}
if (pin_status()) {
debug_print("pin already enabled");
goto enabled;
}
response = mobile_enable_pin(ip_addr, pin);
if (!response) {
debug_print("no response from get_pin_status!\n");
goto fail_data;
}
rv = check_response(response);
if (rv < 0) {
deub_print("incorrect pin!\n");
goto fail_input;
}
enabled:
free(ip_addr);
return 0;
fail_input_response:
json_object_put(response);
fail_input:
free(ip_addr);
return UBUS_STATUS_INVALID_ARGUMENT;
fail_data:
free(ip_addr);
return UBUS_STATUS_NO_DATA;
fail_strdup:
return UBUS_STATUS_UNKNOWN_ERROR;
}
int verify_pin(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__PIN_MAX];
struct blob_buf bb;
char *pin, *ip_addr;
int rv;
struct json_object *response;
blobmsg_parse(pin_policy, __PIN_MAX, tb, blob_data(msg), blob_len(msg));
ip_addr = strdup("192.168.0.1");
if (!ip_addr)
goto fail_strdup;
if (!tb[PIN]) {
debug_print("Please enter a pin\n!");
goto fail_input;
}
pin = (char *)blobmsg_data(tb[PIN]);
rv = validate_pin_format(pin);
if (rv < 0) {
debug_print("invalid pin format!\n");
goto fail_input;
}
if (!pin_status(&bb)) {
debug_print("pin disabled");
ubus_send_reply(ctx, req, bb.head);
goto disabled;
}
response = mobile_set_pin(ip_addr, pin, pin);
if (!response)
goto fail_unknown;
free(ip_addr);
return print_to_ubus(response, ctx, req);
fail_input:
free(ip_addr);
return UBUS_STATUS_INVALID_ARGUMENT;
fail_unknown:
free(ip_addr);
fail_strdup:
return UBUS_STATUS_UNKNOWN_ERROR;
disabled:
return 0;
}
int remaining_tries(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
char *ip_addr;
struct json_object *response;
ip_addr = strdup("192.168.0.1");
if (!ip_addr)
goto fail_strdup;
response = mobile_get_remaining_tries(ip_addr);
if (!response)
goto fail_unknown;
free(ip_addr);
return print_to_ubus(response, ctx, req);
fail_unknown:
free(ip_addr);
fail_strdup:
return UBUS_STATUS_UNKNOWN_ERROR;
}
int unlock_sim(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *tb[__UNLOCK_MAX];
char *pin, *puk, *ip_addr;
struct json_object *response;
int rv;
ip_addr = strdup("192.168.0.1");
if (!ip_addr)
goto fail_strdup;
if (!tb[PUK] || !tb[UNLOCK_PIN]) {
debug_print("Please enter both a pin and a puk code!\n");
goto fail_input;
}
puk = (char *)blobmsg_data(tb[PUK]);
pin = (char *)blobmsg_data(tb[UNLOCK_PIN]);
rv = validate_pin_format(pin);
if (rv < 0)
goto fail_input;
rv = validate_puk_format(puk);
if (rv < 0)
goto fail_input;
response = mobile_unlock_sim(ip_addr, puk, pin);
if (!response)
goto fail_unknown;
free(ip_addr);
return print_to_ubus(response, ctx, req);
fail_input:
free(ip_addr);
return UBUS_STATUS_INVALID_ARGUMENT;
fail_unknown:
free(ip_addr);
fail_strdup:
return UBUS_STATUS_UNKNOWN_ERROR;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment