Skip to content

Commit

Permalink
Added optional PIN code to CLI 'grant'
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Oct 13, 2023
1 parent aa492c7 commit bc9f2a2
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 181 deletions.
32 changes: 5 additions & 27 deletions pico/controller/common/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ void query(txrx, void *);

void on_card_command(char *cmd, handler fn, txrx, void *);

void grant(uint32_t, uint32_t, txrx, void *);
void revoke(uint32_t, uint32_t, txrx, void *);
void list_acl(txrx, void *);
void read_acl(txrx, void *);
Expand Down Expand Up @@ -82,15 +81,15 @@ void execw(char *cmd, txrx f, void *context) {
} else if (strncasecmp(cmd, "format", 6) == 0) {
format(f, context);
} else if (strncasecmp(cmd, "list acl", 8) == 0) {
cli_list_acl(f, context);
cli_acl_list(f, context);
} else if (strncasecmp(cmd, "clear acl", 9) == 0) {
cli_clear_acl(f, context);
cli_acl_clear(f, context);
} else if (strncasecmp(cmd, "read acl", 8) == 0) {
read_acl(f, context);
} else if (strncasecmp(cmd, "write acl", 9) == 0) {
cli_write_acl(f, context);
cli_acl_write(f, context);
} else if (strncasecmp(cmd, "grant ", 6) == 0) {
on_card_command(&cmd[6], grant, f, context);
cli_acl_grant(&cmd[6], f, context);
} else if (strncasecmp(cmd, "revoke ", 7) == 0) {
on_card_command(&cmd[7], revoke, f, context);
} else if (strncasecmp(cmd, "passcodes", 9) == 0) {
Expand Down Expand Up @@ -139,27 +138,6 @@ void query(txrx f, void *context) {
f(context, s);
}

/* Adds a card number to the ACL.
*
*/
void grant(uint32_t facility_code, uint32_t card, txrx f, void *context) {
char s[64];
char c[16];

snprintf(c, sizeof(c), "%u%05u", facility_code, card);

if (acl_grant(facility_code, card)) {
snprintf(s, sizeof(s), "CARD %-8s %s", c, "GRANTED");
} else {
snprintf(s, sizeof(s), "CARD %-8s %s", c, "ERROR");
}

f(context, s);
logd_log(s);

cli_write_acl(f, context);
}

/* Removes a card number from the ACL.
*
*/
Expand All @@ -177,7 +155,7 @@ void revoke(uint32_t facility_code, uint32_t card, txrx f, void *context) {

f(context, s);
logd_log(s);
cli_write_acl(f, context);
cli_acl_write(f, context);
}

/* Loads an ACL from the SD card
Expand Down
19 changes: 0 additions & 19 deletions pico/controller/pico/base/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,6 @@ int main() {
free(b);
}

// if ((v & MSG) == MSG_CARD) {
// on_card_read(v & 0x0fffffff);
//
// if (last_card.ok && mode == CONTROLLER) {
// if (acl_allowed(last_card.facility_code, last_card.card_number)) {
// last_card.granted = GRANTED;
// led_blink(1);
// door_unlock(5000);
// } else {
// last_card.granted = DENIED;
// led_blink(3);
// }
// }
//
// char s[64];
// cardf(&last_card, s, sizeof(s), false);
// logd_log(s);
// }

if ((v & MSG) == MSG_LED) {
led_event(v & 0x0fffffff);
}
Expand Down
19 changes: 0 additions & 19 deletions pico/controller/pico/usb/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,6 @@ int main() {
free(b);
}

// if ((v & MSG) == MSG_CARD) {
// on_card_read(v & 0x0fffffff);
//
// if (last_card.ok && mode == CONTROLLER) {
// if (acl_allowed(last_card.facility_code, last_card.card_number)) {
// last_card.granted = GRANTED;
// led_blink(1);
// door_unlock(5000);
// } else {
// last_card.granted = DENIED;
// led_blink(3);
// }
// }
//
// char s[64];
// cardf(&last_card, s, sizeof(s), false);
// logd_log(s);
// }

if ((v & MSG) == MSG_LED) {
led_event(v & 0x0fffffff);
}
Expand Down
19 changes: 0 additions & 19 deletions pico/controller/picow/base/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,6 @@ int main() {
free(b);
}

// if ((v & MSG) == MSG_CARD) {
// on_card_read(v & 0x0fffffff);
//
// if (last_card.ok && mode == CONTROLLER) {
// if (acl_allowed(last_card.facility_code, last_card.card_number)) {
// last_card.granted = GRANTED;
// led_blink(1);
// door_unlock(5000);
// } else {
// last_card.granted = DENIED;
// led_blink(3);
// }
// }
//
// char s[64];
// cardf(&last_card, s, sizeof(s), false);
// logd_log(s);
// }

if ((v & MSG) == MSG_LED) {
led_event(v & 0x0fffffff);
}
Expand Down
19 changes: 0 additions & 19 deletions pico/controller/picow/wifi+usb/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,25 +118,6 @@ int main() {
free(b);
}

// if ((v & MSG) == MSG_CARD) {
// on_card_read(v & 0x0fffffff);
//
// if (last_card.ok && mode == CONTROLLER) {
// if (acl_allowed(last_card.facility_code, last_card.card_number)) {
// last_card.granted = GRANTED;
// led_blink(1);
// door_unlock(5000);
// } else {
// last_card.granted = DENIED;
// led_blink(3);
// }
// }
//
// char s[64];
// cardf(&last_card, s, sizeof(s), false);
// logd_log(s);
// }

if ((v & MSG) == MSG_LED) {
led_event(v & 0x0fffffff);
}
Expand Down
19 changes: 0 additions & 19 deletions pico/controller/picow/wifi/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,6 @@ int main() {
free(b);
}

// if ((v & MSG) == MSG_CARD) {
// on_card_read(v & 0x0fffffff);
//
// if (last_card.ok && mode == CONTROLLER) {
// if (acl_allowed(last_card.facility_code, last_card.card_number)) {
// last_card.granted = GRANTED;
// led_blink(1);
// door_unlock(5000);
// } else {
// last_card.granted = DENIED;
// led_blink(3);
// }
// }
//
// char s[64];
// cardf(&last_card, s, sizeof(s), false);
// logd_log(s);
// }

if ((v & MSG) == MSG_LED) {
led_event(v & 0x0fffffff);
}
Expand Down
2 changes: 1 addition & 1 deletion pico/core/include/acl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern int acl_save();

extern int acl_list(uint32_t *[]);
extern bool acl_clear();
extern bool acl_grant(uint32_t, uint32_t);
extern bool acl_grant(uint32_t, uint32_t, const char *);
extern bool acl_revoke(uint32_t, uint32_t);
extern bool acl_allowed(uint32_t, uint32_t);

Expand Down
8 changes: 4 additions & 4 deletions pico/core/include/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

typedef void (*txrx)(void *, const char *);

// CLI functions
void cli_reboot(txrx, void *);
void cli_set_time(char *, txrx, void *);
void cli_blink(txrx, void *);
void cli_unlock_door(txrx, void *);
void cli_on_door_open(txrx, void *);
void cli_on_door_close(txrx, void *);
void cli_list_acl(txrx, void *);
void cli_clear_acl(txrx, void *);
void cli_write_acl(txrx, void *);
void cli_acl_list(txrx, void *);
void cli_acl_clear(txrx, void *);
void cli_acl_grant(char *, txrx, void *);
void cli_acl_write(txrx, void *);
void cli_set_passcodes(char *, txrx, void *);

void cli_swipe(char *, txrx f, void *);
Expand Down
2 changes: 1 addition & 1 deletion pico/core/src/acl.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ bool acl_clear() {
/* Adds a card to the ACL.
*
*/
bool acl_grant(uint32_t facility_code, uint32_t card) {
bool acl_grant(uint32_t facility_code, uint32_t card, const char *PIN) {
uint32_t v = (facility_code * 100000) + (card % 100000);
datetime_t now;

Expand Down
70 changes: 66 additions & 4 deletions pico/core/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void cli_on_door_close(txrx f, void *context) {
/* Lists the ACL cards.
*
*/
void cli_list_acl(txrx f, void *context) {
void cli_acl_list(txrx f, void *context) {
uint32_t *cards;
int N = acl_list(&cards);

Expand All @@ -97,7 +97,7 @@ void cli_list_acl(txrx f, void *context) {
/* Removes all cards from the ACL.
*
*/
void cli_clear_acl(txrx f, void *context) {
void cli_acl_clear(txrx f, void *context) {
char s[64];

bool ok = acl_clear();
Expand All @@ -112,14 +112,14 @@ void cli_clear_acl(txrx f, void *context) {
logd_log(s);

if (ok) {
cli_write_acl(f, context);
cli_acl_write(f, context);
}
}

/* Writes the ACL to flash/SD card.
*
*/
void cli_write_acl(txrx f, void *context) {
void cli_acl_write(txrx f, void *context) {
char s[64];

if (!gpio_get(SD_DET)) {
Expand All @@ -138,6 +138,68 @@ void cli_write_acl(txrx f, void *context) {
f(context, s);
}

/* Grants card permissions.
* Extract the facility code and card number and (optional) PIN and updates
* the ACL.
*
*/
void cli_acl_grant(char *cmd, txrx f, void *context) {
// ... extract facility code, card and (optional) PIN code
uint32_t facility_code = FACILITY_CODE;
uint32_t card = 0;
char *PIN = "";

// ... facility code and card
char *p = strtok(cmd, " ");

if (p != NULL) {
int N = strlen(p);
int rc;

if (N < 5) {
if ((rc = sscanf(p, "%0u", &card)) < 1) {
return;
}
} else {
if ((rc = sscanf(&p[N - 5], "%05u", &card)) < 1) {
return;
}

if (N == 6 && ((rc = sscanf(p, "%01u", &facility_code)) < 1)) {
return;
} else if (N == 7 && ((rc = sscanf(p, "%02u", &facility_code)) < 1)) {
return;
} else if (N == 8 && ((rc = sscanf(p, "%03u", &facility_code)) < 1)) {
return;
} else if (N > 8) {
return;
}
}

// ... optional PIN code
if ((p = strtok(NULL, " ")) != NULL) {
PIN = p;
}

// ... grant access
char s[64];
bool ok = acl_grant(facility_code, card, PIN);

if (ok) {
snprintf(s, sizeof(s), "CARD %u%05u %s", facility_code, card, "GRANTED");
} else {
snprintf(s, sizeof(s), "CARD %u%05u %s", facility_code, card, "ERROR");
}

f(context, s);
logd_log(s);

if (ok) {
cli_acl_write(f, context);
}
}
}

/* Sets the override passcodes.
*
*/
Expand Down
Loading

0 comments on commit bc9f2a2

Please sign in to comment.