MCPcopy Create free account
hub / github.com/F-Stack/f-stack / ACLLoadFromFile

Function ACLLoadFromFile

app/redis-6.2.6/src/acl.c:1502–1649  ·  view source on GitHub ↗

This function loads the ACL from the specified filename: every line * is validated and should be either empty or in the format used to specify * users in the redis.conf configuration or in the ACL file, that is: * * user ... rules ... * * Note that this function considers comments starting with '#' as errors * because the ACL file is meant to be rewritten, and comments would be

Source from the content-addressed store, hash-verified

1500 * NULL is returned. Otherwise an SDS string describing in a single line
1501 * a description of all the issues found is returned. */
1502sds ACLLoadFromFile(const char *filename) {
1503 FILE *fp;
1504 char buf[1024];
1505
1506 /* Open the ACL file. */
1507 if ((fp = fopen(filename,"r")) == NULL) {
1508 sds errors = sdscatprintf(sdsempty(),
1509 "Error loading ACLs, opening file '%s': %s",
1510 filename, strerror(errno));
1511 return errors;
1512 }
1513
1514 /* Load the whole file as a single string in memory. */
1515 sds acls = sdsempty();
1516 while(fgets(buf,sizeof(buf),fp) != NULL)
1517 acls = sdscat(acls,buf);
1518 fclose(fp);
1519
1520 /* Split the file into lines and attempt to load each line. */
1521 int totlines;
1522 sds *lines, errors = sdsempty();
1523 lines = sdssplitlen(acls,strlen(acls),"\n",1,&totlines);
1524 sdsfree(acls);
1525
1526 /* We need a fake user to validate the rules before making changes
1527 * to the real user mentioned in the ACL line. */
1528 user *fakeuser = ACLCreateUnlinkedUser();
1529
1530 /* We do all the loading in a fresh instance of the Users radix tree,
1531 * so if there are errors loading the ACL file we can rollback to the
1532 * old version. */
1533 rax *old_users = Users;
1534 user *old_default_user = DefaultUser;
1535 Users = raxNew();
1536 ACLInitDefaultUser();
1537
1538 /* Load each line of the file. */
1539 for (int i = 0; i < totlines; i++) {
1540 sds *argv;
1541 int argc;
1542 int linenum = i+1;
1543
1544 lines[i] = sdstrim(lines[i]," \t\r\n");
1545
1546 /* Skip blank lines */
1547 if (lines[i][0] == '\0') continue;
1548
1549 /* Split into arguments */
1550 argv = sdssplitlen(lines[i],sdslen(lines[i])," ",1,&argc);
1551 if (argv == NULL) {
1552 errors = sdscatprintf(errors,
1553 "%s:%d: unbalanced quotes in acl line. ",
1554 server.acl_filename, linenum);
1555 continue;
1556 }
1557
1558 /* Skip this line if the resulting command vector is empty. */
1559 if (argc == 0) {

Callers 2

ACLLoadUsersAtStartupFunction · 0.85
aclCommandFunction · 0.85

Calls 15

sdscatprintfFunction · 0.85
sdsemptyFunction · 0.85
sdscatFunction · 0.85
sdssplitlenFunction · 0.85
sdsfreeFunction · 0.85
ACLCreateUnlinkedUserFunction · 0.85
raxNewFunction · 0.85
ACLInitDefaultUserFunction · 0.85
sdstrimFunction · 0.85
sdslenFunction · 0.85
sdsfreesplitresFunction · 0.85
strcmpFunction · 0.85

Tested by

no test coverage detected