diff --git a/common.c b/common.c index 7e429dd09c3772cc4088be2a06ace5abc55e41a4..7284aa407dac279adc6596aab845b699081e9e8f 100644 --- a/common.c +++ b/common.c @@ -190,7 +190,7 @@ fail: return -1; } -int pin_status(struct blob_buf *bb) +int pin_status(struct blob_buf *bb, char *ip_addr) { struct json_object *response, *rv_json; int rv; @@ -223,6 +223,8 @@ fail_result: int check_response(struct json_object *response) { + struct json_object *rv_json; + json_object_object_get_ex(response, "result", &rv_json); if (!rv_json) { debug_print("no result available in response!\n"); diff --git a/common.h b/common.h index 56e4391c4ba2534530865232c1936a5c5a6736c0..6fb0dbe9c9ea77255078d7258a06451f6c13bba3 100644 --- a/common.h +++ b/common.h @@ -80,6 +80,6 @@ void xml_to_json_converter(struct write_result *result); int isdigits(const char *pin); int validate_puk_format(char *puk); int validate_pin_format(char *pin); -int pin_status(struct blob_buf *bb); +int pin_status(struct blob_buf *bb, char *ip_addr); int check_response(struct json_object *response); #endif diff --git a/dongle_infrastructure.c b/dongle_infrastructure.c index c715a77ca4c8b4886c357da1c969bc3c1aa933bb..e1fe8bd54802806a406b7f788f8ae26213defe07 100644 --- a/dongle_infrastructure.c +++ b/dongle_infrastructure.c @@ -55,7 +55,7 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj, struct blob_buf bb; char *new_pin, *current_pin, *ip_addr; int rv; - struct json_object *response, *rv_json; + struct json_object *response; blobmsg_parse(set_pin_policy, __SET_PIN_MAX, tb, blob_data(msg), blob_len(msg)); @@ -82,7 +82,7 @@ int set_pin(struct ubus_context *ctx, struct ubus_object *obj, goto fail_input; } - if (!pin_status(&bb)) { + if (!pin_status(&bb, ip_addr)) { debug_print("pin disabled!"); ubus_send_reply(ctx, req, bb.head); goto disabled; @@ -122,9 +122,10 @@ int disable_pin(struct ubus_context *ctx, struct ubus_object *obj, struct blob_attr *msg) { struct blob_attr *tb[__PIN_MAX]; + struct blob_buf bb; char *pin, *ip_addr; int rv; - struct json_object *response, *rv_json; + struct json_object *response; blobmsg_parse(pin_policy, __PIN_MAX, tb, blob_data(msg), blob_len(msg)); @@ -143,9 +144,9 @@ int disable_pin(struct ubus_context *ctx, struct ubus_object *obj, goto fail_input; } - if (!pin_status()) { + if (!pin_status(&bb, ip_addr)) { debug_print("pin already disabled!\n"); - goto disabled: + goto disabled; } response = mobile_disable_pin(ip_addr, pin); @@ -156,7 +157,7 @@ int disable_pin(struct ubus_context *ctx, struct ubus_object *obj, rv = check_response(response); if (rv < 0) { - deub_print("incorrect pin!\n"); + debug_print("incorrect pin!\n"); goto fail_input_response; } @@ -181,9 +182,10 @@ int enable_pin(struct ubus_context *ctx, struct ubus_object *obj, struct blob_attr *msg) { struct blob_attr *tb[__PIN_MAX]; + struct blob_buf bb; char *pin, *ip_addr; int rv; - struct json_object *response, *rv_json; + struct json_object *response; blobmsg_parse(pin_policy, __PIN_MAX, tb, blob_data(msg), blob_len(msg)); @@ -203,7 +205,7 @@ int enable_pin(struct ubus_context *ctx, struct ubus_object *obj, goto fail_input; } - if (pin_status()) { + if (pin_status(&bb, ip_addr)) { debug_print("pin already enabled"); goto enabled; } @@ -216,8 +218,8 @@ int enable_pin(struct ubus_context *ctx, struct ubus_object *obj, rv = check_response(response); if (rv < 0) { - deub_print("incorrect pin!\n"); - goto fail_input; + debug_print("incorrect pin!\n"); + goto fail_input_response; } enabled: @@ -263,7 +265,7 @@ int verify_pin(struct ubus_context *ctx, struct ubus_object *obj, goto fail_input; } - if (!pin_status(&bb)) { + if (!pin_status(&bb, ip_addr)) { debug_print("pin disabled"); ubus_send_reply(ctx, req, bb.head); goto disabled; diff --git a/dongle_pin.c b/dongle_pin.c index fbad0dad8d3255e2dc3a175e5717b0e1867a2fcc..557385818ab113d0f5d514aec024ecec01e1169b 100644 --- a/dongle_pin.c +++ b/dongle_pin.c @@ -1,399 +1,2 @@ #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; -}