Skip to content

Commit

Permalink
clear-acl command
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Oct 3, 2023
1 parent d71447f commit 374b2f5
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 86 deletions.
9 changes: 4 additions & 5 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
- [x] controller: execute
- [x] unlock on valid passcode
- [x] master passcode
- [ ] set passcodes
- [x] set passcodes
- [x] include passcodes in CRC
- (?) clear ACL
- [ ] Weird card after ACL revoking a card in the middle of a list
- [x] clear ACL
- [ ] Weird card after ACL revoking a card in the middle of a list
- (?) list-acl issue probably
```
> ACL 10058399
> ACL 2147483647
Expand All @@ -23,8 +24,6 @@
- [ ] card + PIN writer
- [ ] card + PIN read



- [ ] PicoW+TCP/IP
- [ ] Figure out SD card detect interrupt conflict
- (?) Use GPIO poll
Expand Down
58 changes: 9 additions & 49 deletions pico/controller/common/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ 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 *);
void write_acl(txrx, void *);

void mount(txrx, void *);
void unmount(txrx, void *);
Expand Down Expand Up @@ -83,17 +82,19 @@ 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) {
list_acl(f, context);
cli_list_acl(f, context);
} else if (strncasecmp(cmd, "clear acl", 9) == 0) {
cli_clear_acl(f, context);
} else if (strncasecmp(cmd, "read acl", 8) == 0) {
read_acl(f, context);
} else if (strncasecmp(cmd, "write acl", 9) == 0) {
write_acl(f, context);
cli_write_acl(f, context);
} else if (strncasecmp(cmd, "grant ", 6) == 0) {
on_card_command(&cmd[6], grant, f, context);
} else if (strncasecmp(cmd, "revoke ", 7) == 0) {
on_card_command(&cmd[7], revoke, f, context);
} else if (strncasecmp(cmd, "passcodes", 9) == 0) {
set_passcodes(&cmd[9], f, context);
cli_set_passcodes(&cmd[9], f, context);
} else if (strncasecmp(cmd, "debug", 5) == 0) {
debug(f, context);
} else {
Expand Down Expand Up @@ -149,7 +150,7 @@ void grant(uint32_t facility_code, uint32_t card, txrx f, void *context) {
f(context, s);
logd_log(s);

write_acl(f, context);
cli_write_acl(f, context);
}

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

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

/* Lists the ACL cards.
*
*/
void list_acl(txrx f, void *context) {
uint32_t *cards;
int N = acl_list(&cards);

if (N == 0) {
f(context, "ACL NO CARDS");
logd_log("ACL NO CARDS");
} else {
for (int i = 0; i < N; i++) {
char s[32];
snprintf(s, sizeof(s), "ACL %u", cards[i]);
f(context, s);
}
}

free(cards);
cli_write_acl(f, context);
}

/* Loads an ACL from the SD card
Expand All @@ -215,38 +195,18 @@ void read_acl(txrx f, void *context) {
f(context, s);
}

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

if (!gpio_get(SD_DET)) {
f(context, "DISK NO SDCARD");
logd_log("DISK NO SDCARD");
}

int rc = acl_save();

if (rc < 0) {
snprintf(s, sizeof(s), "DISK ACL WRITE ERROR (%d)", rc);
} else {
snprintf(s, sizeof(s), "DISK ACL WRITE OK (%d)", rc);
}

f(context, s);
}

/* Displays a list of the supported commands.
*
*/
void help(txrx f, void *context) {
f(context, "-----");
f(context, "Commands:");
f(context, "TIME YYYY-MM-DD HH:mm:ss Set date/time (YYYY-MM-DD HH:mm:ss)");
f(context, "");
f(context, "GRANT nnnnnn Grant card access rights");
f(context, "REVOKE nnnnnn Revoke card access rights");
f(context, "LIST ACL List cards in ACL");
f(context, "CLEAR ACL Revoke all cards in ACL");
f(context, "READ ACL Read ACL from SD card");
f(context, "WRITE ACL Write ACL to SD card");
f(context, "PASSCODES Sets the override passcodes");
Expand Down
1 change: 1 addition & 0 deletions pico/core/include/acl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern int acl_load();
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_revoke(uint32_t, uint32_t);
extern bool acl_allowed(uint32_t, uint32_t);
Expand Down
5 changes: 4 additions & 1 deletion pico/core/include/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ 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_list_acl(txrx, void *);
void cli_clear_acl(txrx, void *);
void cli_write_acl(txrx, void *);
void cli_set_passcodes(char *, txrx, void *);
void keypad(char *, txrx, void *);
void set_passcodes(char *, txrx, void *);
11 changes: 11 additions & 0 deletions pico/core/src/acl.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ int acl_list(uint32_t *list[]) {
return count;
}

/* Removes all cards from the ACL.
*
*/
bool acl_clear() {
for (int i = 0; i < ACL_SIZE; i++) {
ACL[i].card_number = 0xffffffff;
}

return true;
}

/* Adds a card to the ACL.
*
*/
Expand Down
67 changes: 66 additions & 1 deletion pico/core/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,75 @@ void cli_unlock_door(txrx f, void *context) {
}
}

/* Lists the ACL cards.
*
*/
void cli_list_acl(txrx f, void *context) {
uint32_t *cards;
int N = acl_list(&cards);

if (N == 0) {
f(context, "ACL NO CARDS");
logd_log("ACL NO CARDS");
} else {
for (int i = 0; i < N; i++) {
char s[32];
snprintf(s, sizeof(s), "ACL %u", cards[i]);
f(context, s);
}
}

free(cards);
}

/* Removes all cards from the ACL.
*
*/
void cli_clear_acl(txrx f, void *context) {
char s[64];

bool ok = acl_clear();

if (ok) {
snprintf(s, sizeof(s), "ACL CLEARED");
} else {
snprintf(s, sizeof(s), "ACL CLEAR ERROR");
}

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

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

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

if (!gpio_get(SD_DET)) {
f(context, "DISK NO SDCARD");
logd_log("DISK NO SDCARD");
}

int rc = acl_save();

if (rc < 0) {
snprintf(s, sizeof(s), "DISK ACL WRITE ERROR (%d)", rc);
} else {
snprintf(s, sizeof(s), "DISK ACL WRITE OK (%d)", rc);
}

f(context, s);
}

/* Sets the override passcodes.
*
*/
void set_passcodes(char *cmd, txrx f, void *context) {
void cli_set_passcodes(char *cmd, txrx f, void *context) {
uint32_t passcodes[] = {0, 0, 0, 0};
int ix = 0;
char *token = strtok(cmd, " ,");
Expand Down
2 changes: 1 addition & 1 deletion pico/core/src/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ datetime_t bin2date(uint32_t yyyymmdd) {

// Ref. https://lxp32.github.io/docs/a-simple-example-crc32-calculation
uint32_t crc32(const char *s, size_t N) {
uint32_t crc = 0xFFFFFFFF;
uint32_t crc = 0xffffffff;

for (size_t i = 0; i < N; i++) {
char ch = s[i];
Expand Down
38 changes: 9 additions & 29 deletions pico/reference/common/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ 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 *);
void write_acl(txrx, void *);

void mount(txrx, void *);
void unmount(txrx, void *);
Expand Down Expand Up @@ -88,16 +87,18 @@ void execw(char *cmd, txrx f, void *context) {
on_press_button(f, context);
} else if (strncasecmp(cmd, "release", 7) == 0) {
on_release_button(f, context);
} else if (strncasecmp(cmd, "list acl", 8) == 0) {
list_acl(f, context);
} else if (strncasecmp(cmd, "clear acl", 9) == 0) {
cli_clear_acl(f, context);
} else if (strncasecmp(cmd, "grant ", 6) == 0) {
on_card_command(&cmd[6], grant, f, context);
} else if (strncasecmp(cmd, "revoke ", 7) == 0) {
on_card_command(&cmd[7], revoke, f, context);
} else if (strncasecmp(cmd, "read acl", 8) == 0) {
read_acl(f, context);
} else if (strncasecmp(cmd, "write acl", 9) == 0) {
write_acl(f, context);
} else if (strncasecmp(cmd, "list acl", 8) == 0) {
list_acl(f, context);
cli_write_acl(f, context);
} else if (strncasecmp(cmd, "mount", 5) == 0) {
mount(f, context);
} else if (strncasecmp(cmd, "unmount", 7) == 0) {
Expand Down Expand Up @@ -156,7 +157,7 @@ void grant(uint32_t facility_code, uint32_t card, txrx f, void *context) {
f(context, s);
logd_log(s);

write_acl(f, context);
cli_write_acl(f, context);
}

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

write_acl(f, context);
cli_write_acl(f, context);
}

/* Lists the ACL cards.
Expand Down Expand Up @@ -292,28 +293,6 @@ void read_acl(txrx f, void *context) {
f(context, s);
}

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

if (!gpio_get(SD_DET)) {
f(context, "DISK NO SDCARD");
logd_log("DISK NO SDCARD");
}

int rc = acl_save();

if (rc < 0) {
snprintf(s, sizeof(s), "DISK ACL WRITE ERROR (%d)", rc);
} else {
snprintf(s, sizeof(s), "DISK ACL WRITE OK (%d)", rc);
}

f(context, s);
}

/* Displays a list of the supported commands.
*
*/
Expand All @@ -331,9 +310,10 @@ void help(txrx f, void *context) {
f(context, "");
f(context, "GRANT nnnnnn Grant card access rights");
f(context, "REVOKE nnnnnn Revoke card access rights");
f(context, "LIST ACL Lists the cards in the ACL");
f(context, "CLEAR ACL Revoke all cards in ACL");
f(context, "READ ACL Read ACL from SD card");
f(context, "WRITE ACL Write ACL to SD card");
f(context, "LIST ACL Lists the cards in the ACL");
f(context, "QUERY Display last card read/write");
f(context, "");
f(context, "MOUNT Mount SD card");
Expand Down

0 comments on commit 374b2f5

Please sign in to comment.