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

refactor dongle_pin to separate logic

parent b94990d9
Branches
No related tags found
No related merge requests found
...@@ -3,36 +3,6 @@ ...@@ -3,36 +3,6 @@
static int isdigits(const char *pin); static int isdigits(const char *pin);
static int validate_pin_format(char *pin); static int validate_pin_format(char *pin);
enum {
NEW_PIN,
CURRENT_PIN,
__SET_PIN_MAX
};
enum {
PIN,
__PIN_MAX
};
enum {
UNLOCK_PIN,
PUK,
__UNLOCK_MAX
};
const struct blobmsg_policy set_pin_policy[__SET_PIN_MAX] = {
[NEW_PIN] = {.name = "new_pin", .type = BLOBMSG_TYPE_STRING},
[CURRENT_PIN] = {.name = "current_pin", .type = BLOBMSG_TYPE_STRING},
};
const struct blobmsg_policy pin_policy[__PIN_MAX] = {
[PIN] = {.name = "pin", .type = BLOBMSG_TYPE_STRING},
};
const struct blobmsg_policy unlock_policy[__UNLOCK_MAX] = {
[UNLOCK_PIN] = {.name = "pin", .type = BLOBMSG_TYPE_STRING},
[PUK] = {.name = "puk", .type = BLOBMSG_TYPE_STRING},
};
static int isdigits(const char *pin) static int isdigits(const char *pin)
{ {
...@@ -81,6 +51,7 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -81,6 +51,7 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj,
struct blob_attr *msg) struct blob_attr *msg)
{ {
struct blob_attr *tb[__SET_PIN_MAX]; struct blob_attr *tb[__SET_PIN_MAX];
struct blob_buf bb;
char *new_pin, *current_pin, *ip_addr; char *new_pin, *current_pin, *ip_addr;
int rv; int rv;
struct json_object *response, *rv_json; struct json_object *response, *rv_json;
...@@ -110,70 +81,39 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -110,70 +81,39 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj,
goto fail_input; goto fail_input;
} }
response = mobile_get_pin_status(ip_addr); if (!pin_status(&bb)) {
if (!response) { debug_print("pin disabled!");
debug_print("error getting pin_status\n"); ubus_send_reply(ctx, req, bb.head);
goto fail_data; goto disabled;
} }
json_object_object_get_ex(response, "pin_status", &rv_json);
if (!rv_json) {
debug_print("no pin_status available\n");
goto fail_result;
}
/* Enable PIN if it is disabled */
if (!json_object_get_int(rv_json)) {
json_object_put(response);
response = mobile_enable_pin(ip_addr, current_pin);
if (!response) {
debug_print("error enabling pin!\n");
goto fail_unknown;
}
json_object_object_get_ex(response, "result", &rv_json);
if (!rv_json) {
debug_print("error getting result!\n");
goto fail_result;
}
if (strncmp(json_object_get_string(rv_json), "failure", strlen("failure")) == 0) {
debug_print("Incorrect pin!\n");
goto incorrect_pin;
}
}
json_object_put(response);
response = mobile_set_pin(ip_addr, current_pin, new_pin); response = mobile_set_pin(ip_addr, current_pin, new_pin);
if (!response) { if (!response) {
debug_print("error setting pin!\n"); debug_print("error setting pin!\n");
goto fail_data; goto fail_data;
} }
json_object_object_get_ex(response, "result", &rv_json); rv = check_response(response);
if (!rv_json) { if (rv < 0) {
debug_print("no result available from %s!", __func__); debug_print("incorrect pin!\n");
goto fail_result; goto fail_input_json;
} }
incorrect_pin:
free(ip_addr); free(ip_addr);
return print_to_ubus(response, ctx, req); return print_to_ubus(response, ctx, req);
fail_input_json:
json_object_put(response);
fail_input: fail_input:
free(ip_addr); free(ip_addr);
return UBUS_STATUS_INVALID_ARGUMENT; return UBUS_STATUS_INVALID_ARGUMENT;
fail_data: fail_data:
free(ip_addr); free(ip_addr);
return UBUS_STATUS_NO_DATA; return UBUS_STATUS_NO_DATA;
fail_result:
free(ip_addr);
json_object_put(response);
return UBUS_STATUS_UNKNOWN_ERROR;
fail_unknown:
free(ip_addr);
fail_strdup: fail_strdup:
return UBUS_STATUS_UNKNOWN_ERROR; return UBUS_STATUS_UNKNOWN_ERROR;
disabled:
free(ip_addr);
return 0;
} }
int disable_pin(struct ubus_context *ctx, struct ubus_object *obj, int disable_pin(struct ubus_context *ctx, struct ubus_object *obj,
...@@ -202,10 +142,48 @@ int disable_pin(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -202,10 +142,48 @@ int disable_pin(struct ubus_context *ctx, struct ubus_object *obj,
goto fail_input; 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); response = mobile_get_pin_status(ip_addr);
if (!response) { if (!response) {
debug_print("no response from get_pin_status!\n"); debug_print("no response from get_pin_status!\n");
goto fail_data; goto fail_response;
} }
json_object_object_get_ex(response, "pin_status", &rv_json); json_object_object_get_ex(response, "pin_status", &rv_json);
...@@ -214,41 +192,34 @@ int disable_pin(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -214,41 +192,34 @@ int disable_pin(struct ubus_context *ctx, struct ubus_object *obj,
goto fail_result; goto fail_result;
} }
if (!json_object_get_int(rv_json)) { rv = json_object_get_int(rv_json);
debug_print("already disabled!\n"); if (rv == 0)
goto success; //kind of... blobmsg_add_string(bb, "Failure", "Pin disabled");
} else
blobmsg_add_string(bb, "Failure", "Pin enabled");
json_object_put(response); json_object_put(response);
response = mobile_disable_pin(ip_addr, pin); return rv;
if (!response) { fail_response:
debug_print("error disabling pin!\n"); fail_result:
goto fail_data; json_object_put(response);
} return -1;
}
int check_response(struct json_object *response)
{
json_object_object_get_ex(response, "result", &rv_json); json_object_object_get_ex(response, "result", &rv_json);
if (!rv_json) { if (!rv_json) {
debug_print("no result available in response!\n"); debug_print("no result available in response!\n");
goto fail_result; goto fail;
} }
if (strncmp(json_object_get_string(rv_json), "failure", strlen("failure")) == 0) if (strncmp(json_object_get_string(rv_json), "failure", strlen("failure")) == 0)
debug_print("Incorrect pin!\n"); goto fail;
success: return 0;
free(ip_addr); fail:
return print_to_ubus(response, ctx, req); return -1;
fail_input:
free(ip_addr);
return UBUS_STATUS_INVALID_ARGUMENT;
fail_data:
free(ip_addr);
return UBUS_STATUS_NO_DATA;
fail_result:
free(ip_addr);
json_object_put(response);
fail_strdup:
return UBUS_STATUS_UNKNOWN_ERROR;
} }
int enable_pin(struct ubus_context *ctx, struct ubus_object *obj, int enable_pin(struct ubus_context *ctx, struct ubus_object *obj,
...@@ -278,50 +249,34 @@ int enable_pin(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -278,50 +249,34 @@ int enable_pin(struct ubus_context *ctx, struct ubus_object *obj,
goto fail_input; goto fail_input;
} }
response = mobile_get_pin_status(ip_addr); if (pin_status()) {
if (!response) { debug_print("pin already enabled");
debug_print("no response from get_pin_status!\n"); goto enabled;
goto fail_data;
}
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;
}
if (!json_object_get_int(rv_json)) {
debug_print("already enabled!\n");
goto success;
} }
json_object_put(response);
response = mobile_enable_pin(ip_addr, pin); response = mobile_enable_pin(ip_addr, pin);
if (!response) { if (!response) {
debug_print("no response from get_pin_status!\n"); debug_print("no response from get_pin_status!\n");
goto fail_data; goto fail_data;
} }
json_object_object_get_ex(response, "result", &rv_json); rv = check_response(response);
if (!rv_json) { if (rv < 0) {
debug_print("no result available in response!\n"); deub_print("incorrect pin!\n");
goto fail_result; goto fail_input;
} }
if (strncmp(json_object_get_string(rv_json), "failure", strlen("failure")) == 0) enabled:
debug_print("Incorrect pin!\n");
success:
free(ip_addr); free(ip_addr);
return print_to_ubus(response, ctx, req); return 0;
fail_input_response:
json_object_put(response);
fail_input: fail_input:
free(ip_addr); free(ip_addr);
return UBUS_STATUS_INVALID_ARGUMENT; return UBUS_STATUS_INVALID_ARGUMENT;
fail_data: fail_data:
free(ip_addr); free(ip_addr);
return UBUS_STATUS_NO_DATA; return UBUS_STATUS_NO_DATA;
fail_result:
free(ip_addr);
json_object_put(response);
fail_strdup: fail_strdup:
return UBUS_STATUS_UNKNOWN_ERROR; return UBUS_STATUS_UNKNOWN_ERROR;
} }
...@@ -331,6 +286,7 @@ int verify_pin(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -331,6 +286,7 @@ int verify_pin(struct ubus_context *ctx, struct ubus_object *obj,
struct blob_attr *msg) struct blob_attr *msg)
{ {
struct blob_attr *tb[__PIN_MAX]; struct blob_attr *tb[__PIN_MAX];
struct blob_buf bb;
char *pin, *ip_addr; char *pin, *ip_addr;
int rv; int rv;
struct json_object *response; struct json_object *response;
...@@ -353,6 +309,12 @@ int verify_pin(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -353,6 +309,12 @@ int verify_pin(struct ubus_context *ctx, struct ubus_object *obj,
goto fail_input; 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); response = mobile_set_pin(ip_addr, pin, pin);
if (!response) if (!response)
goto fail_unknown; goto fail_unknown;
...@@ -366,6 +328,8 @@ fail_unknown: ...@@ -366,6 +328,8 @@ fail_unknown:
free(ip_addr); free(ip_addr);
fail_strdup: fail_strdup:
return UBUS_STATUS_UNKNOWN_ERROR; return UBUS_STATUS_UNKNOWN_ERROR;
disabled:
return 0;
} }
int remaining_tries(struct ubus_context *ctx, struct ubus_object *obj, int remaining_tries(struct ubus_context *ctx, struct ubus_object *obj,
...@@ -397,20 +361,13 @@ int unlock_sim(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -397,20 +361,13 @@ int unlock_sim(struct ubus_context *ctx, struct ubus_object *obj,
{ {
struct blob_attr *tb[__UNLOCK_MAX]; struct blob_attr *tb[__UNLOCK_MAX];
char *pin, *puk, *ip_addr; char *pin, *puk, *ip_addr;
struct json_object *response;//, *rv_json; struct json_object *response;
int rv; int rv;
ip_addr = strdup("192.168.0.1"); ip_addr = strdup("192.168.0.1");
if (!ip_addr) if (!ip_addr)
goto fail_strdup; goto fail_strdup;
/*response = mobile_get_remaining_tries(ip_addr);
if (!response)
goto fail_unknown;
*/
/*json_object_object_get_ex(response, "pinnumber", &rv_json);
if (json_object_get_int(rv_json) > 0)
goto fail_unknown;
*/
if (!tb[PUK] || !tb[UNLOCK_PIN]) { if (!tb[PUK] || !tb[UNLOCK_PIN]) {
debug_print("Please enter both a pin and a puk code!\n"); debug_print("Please enter both a pin and a puk code!\n");
goto fail_input; goto fail_input;
...@@ -440,35 +397,3 @@ fail_unknown: ...@@ -440,35 +397,3 @@ fail_unknown:
fail_strdup: fail_strdup:
return UBUS_STATUS_UNKNOWN_ERROR; return UBUS_STATUS_UNKNOWN_ERROR;
} }
struct ubus_method pin_object_methods[] = {
UBUS_METHOD("set_pin", set_pin, set_pin_policy),
UBUS_METHOD("disable_pin", disable_pin, pin_policy),
UBUS_METHOD("enable_pin", enable_pin, pin_policy),
UBUS_METHOD("verify_pin", verify_pin, pin_policy),
UBUS_METHOD_NOARG("remaining_tries", remaining_tries),
UBUS_METHOD("unlock_sim", unlock_sim, unlock_policy),
};
struct ubus_object_type pin_object_type = UBUS_OBJECT_TYPE("dongle", pin_object_methods);
struct ubus_object pin_object = {
.name = "dongle.pin",
.type = &pin_object_type,
.methods = pin_object_methods,
.n_methods = ARRAY_SIZE(pin_object_methods),
};
int expose_pin_object(struct ubus_context *ctx)
{
int rv;
rv = ubus_add_object(ctx, &pin_object);
if (rv) {
debug_print("failed to add dongle.pin to ubus!\n");
return -1;
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment