From ea59731e1f24ee67857b6580db6d8e1e13aa34aa Mon Sep 17 00:00:00 2001 From: Jean-Luc Cyr Date: Mon, 21 Jan 2019 09:13:03 -0500 Subject: [PATCH] databases handling tools chmod, chgrp, chown, ls --- absec_chgrp.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ absec_chmod.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ absec_chown.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ absec_ls.c | 30 +++++++++---------------- compile.sh | 33 ++++++++++++++++++++++++++++ mod_absec.c | 10 ++++----- 6 files changed, 244 insertions(+), 25 deletions(-) create mode 100644 absec_chgrp.c create mode 100644 absec_chmod.c create mode 100644 absec_chown.c diff --git a/absec_chgrp.c b/absec_chgrp.c new file mode 100644 index 0000000..b52790c --- /dev/null +++ b/absec_chgrp.c @@ -0,0 +1,63 @@ +///// +// +// File : absec_chown.c +// Author : Jean-Luc Cyr +// Date : 2018-10 +// +// Description: tool to change ownership from mysql database +// + +#include +#include +#include + +//////////////////////////////////////////////////////////////// +// mysql call +int mysql_update(char *url, int gid) +{ + MYSQL *conn; + MYSQL_RES *res; + MYSQL_ROW row; + + char *server = "localhost"; + char *user = "jlcyr"; + char *password = "password"; /* set me first */ + char *database = "absec"; + + conn = mysql_init(NULL); + + /* Connect to database */ + if (!mysql_real_connect(conn, server, + user, password, database, 0, NULL, 0)) { + printf("%s\n", mysql_error(conn)); + return(0); + } + + char query[256]; + sprintf(query, "update urls set gid=%d where url='%s'", gid, url); // where url='%s'", r->uri); + //printf("update urls set gid=%d where url='%s'\r\n", gid, url); // where url='%s'", r->uri); + /* send SQL query */ + //if (mysql_query(conn, "show tables")) { + if (mysql_query(conn, query)) { + printf("%s\n", mysql_error(conn)); + return(-1); + } + + mysql_close(conn); + return(0); +} + +void main(int argc, char** argv) +{ + int perms = 0; + switch (argc) { + default: + printf("Usage: abchgrp \r\n\r\n"); + exit(1); + break; + case 3: + printf("Updating group ownership on %s to %d\r\n", argv[2], atoi(argv[1])); + mysql_update(argv[2], atoi(argv[1])); + break; + } +} \ No newline at end of file diff --git a/absec_chmod.c b/absec_chmod.c new file mode 100644 index 0000000..cfedde3 --- /dev/null +++ b/absec_chmod.c @@ -0,0 +1,70 @@ +///// +// +// File : absec_ls.c +// Author : Jean-Luc Cyr +// Date : 2018-10 +// +// Description: tool to query permissions from mysql database +// + +#include +#include +#include + +//////////////////////////////////////////////////////////////// +// define PAM callback function +int mysql_update(char *url, int uid, int gid, int perms) +{ + MYSQL *conn; + MYSQL_RES *res; + MYSQL_ROW row; + + char *server = "localhost"; + char *user = "jlcyr"; + char *password = "password"; /* set me first */ + char *database = "absec"; + + conn = mysql_init(NULL); + + /* Connect to database */ + if (!mysql_real_connect(conn, server, + user, password, database, 0, NULL, 0)) { + printf("%s\n", mysql_error(conn)); + return(0); + } + + char query[256]; + sprintf(query, "update urls set uid=%d, gid=%d, perms=%d where url='%s'", uid, gid, perms, url); // where url='%s'", r->uri); + //printf("update urls set uid=%d, gid=%d, perms=%d where url='%s'\r\n", uid, gid, perms, url); // where url='%s'", r->uri); + /* send SQL query */ + //if (mysql_query(conn, "show tables")) { + if (mysql_query(conn, query)) { + printf("%s\n", mysql_error(conn)); + return(-1); + } + + /* close connection */ + mysql_free_result(res); + mysql_close(conn); + return(0); +} + +void main(int argc, char** argv) +{ + int perms = 0; + switch (argc) { + default: + printf("Usage: abchmod \r\n\r\n"); + exit(1); + break; + case 2: + printf("Updating perms on %s\r\n", argv[0]); + mysql_update(argv[1], 1000, 100, 501); + break; + case 3: + perms = strtol(argv[1], NULL, 8); + printf("Updating perms on %s to %o (%d)\r\n", argv[2], perms, perms); + mysql_update(argv[2], 1000, 100, perms); + break; + } +} \ No newline at end of file diff --git a/absec_chown.c b/absec_chown.c new file mode 100644 index 0000000..1207b80 --- /dev/null +++ b/absec_chown.c @@ -0,0 +1,63 @@ +///// +// +// File : absec_chown.c +// Author : Jean-Luc Cyr +// Date : 2018-10 +// +// Description: tool to change ownership from mysql database +// + +#include +#include +#include + +//////////////////////////////////////////////////////////////// +// mysql call +int mysql_update(char *url, int uid) +{ + MYSQL *conn; + MYSQL_RES *res; + MYSQL_ROW row; + + char *server = "localhost"; + char *user = "jlcyr"; + char *password = "password"; /* set me first */ + char *database = "absec"; + + conn = mysql_init(NULL); + + /* Connect to database */ + if (!mysql_real_connect(conn, server, + user, password, database, 0, NULL, 0)) { + printf("%s\n", mysql_error(conn)); + return(0); + } + + char query[256]; + sprintf(query, "update urls set uid=%d where url='%s'", uid, url); // where url='%s'", r->uri); + //printf("update urls set uid=%d where url='%s'\r\n", uid, url); // where url='%s'", r->uri); + /* send SQL query */ + //if (mysql_query(conn, "show tables")) { + if (mysql_query(conn, query)) { + printf("%s\n", mysql_error(conn)); + return(-1); + } + + mysql_close(conn); + return(0); +} + +void main(int argc, char** argv) +{ + int perms = 0; + switch (argc) { + default: + printf("Usage: abchown \r\n\r\n"); + exit(1); + break; + case 3: + printf("Updating ownership on %s to %d\r\n", argv[2], atoi(argv[1])); + mysql_update(argv[2], atoi(argv[1])); + break; + } +} \ No newline at end of file diff --git a/absec_ls.c b/absec_ls.c index 95cfcbd..32347f6 100644 --- a/absec_ls.c +++ b/absec_ls.c @@ -36,7 +36,7 @@ char* perms_to_string(int perms) //////////////////////////////////////////////////////////////// // define PAM callback function -int mysql_lookup()//request_rec *r, struct stat *fperm) +int mysql_lookup(char* pattern)//request_rec *r, struct stat *fperm) { MYSQL *conn; MYSQL_RES *res; @@ -57,37 +57,23 @@ int mysql_lookup()//request_rec *r, struct stat *fperm) } char query[256]; - sprintf(query, "select * from urls"); // where url='%s'", r->uri); + sprintf(query, "select * from urls where url like '%s'", pattern); // where url='%s'", r->uri); /* send SQL query */ //if (mysql_query(conn, "show tables")) { if (mysql_query(conn, query)) { - printf("%s\n", mysql_error(conn)); + printf("%s\r\n", mysql_error(conn)); return(-1); } res = mysql_use_result(conn); /* output table name */ - printf("ABSEC permissions\n"); + printf("ABSEC permissions\r\n"); int cnt = 0; while ((row = mysql_fetch_row(res)) != NULL) { - printf("%s\t%d\t%d\t%s \n", perms_to_string(atoi(row[3])), atoi(row[1]), atoi(row[2]), row[0] ); - - /*fperm->st_uid = atoi(row[1]); - fperm->st_gid = atoi(row[2]); - fperm->st_mode = atoi(row[3]);*/ - cnt = cnt + 1; + printf("%s\t%d\t%d\t%s \r\n", perms_to_string(atoi(row[3])), atoi(row[1]), atoi(row[2]), row[0] ); } - /*if (cnt==0) { - sprintf(query, "insert into urls (url, uid, gid, perms) values ('%s', 0, 0, 0)", r->uri); - if (mysql_query(conn, query)) { - ap_rprintf(r, "%s\n
", mysql_error(conn)); - return(0); - } - ap_rprintf(r, "Aucune donnee\n
"); - }*/ - /* close connection */ mysql_free_result(res); mysql_close(conn); @@ -96,5 +82,9 @@ int mysql_lookup()//request_rec *r, struct stat *fperm) void main(int argc, char** argv) { - mysql_lookup(); + if (argc>1) { + mysql_lookup(argv[1]); + } else { + mysql_lookup("%"); + } } \ No newline at end of file diff --git a/compile.sh b/compile.sh index a15200e..f81073e 100755 --- a/compile.sh +++ b/compile.sh @@ -58,6 +58,39 @@ echo "- absec_ls" gcc absec_ls.c `mysql_config --cflags --libs` -o abls echo "Running test" ./abls +./abls / + +echo "================" +echo "Compiling absec tools" +echo "- absec_chmod" +gcc absec_chmod.c `mysql_config --cflags --libs` -o abchmod +echo "Running test" +./abchmod 0 / +./abls / +./abchmod 666 / +./abls / + +echo "================" +echo "Compiling absec tools" +echo "- absec_chown" +gcc absec_chown.c `mysql_config --cflags --libs` -o abchown +echo "Running test" +./abchown 100 / +./abls / +./abchown 200 / +./abls / + +echo "================" +echo "Compiling absec tools" +echo "- absec_chgrp" +gcc absec_chgrp.c `mysql_config --cflags --libs` -o abchgrp +echo "Running test" +./abchgrp 100 / +./abls / +./abchgrp 200 / +./abls / + +exit echo "================" echo "Compiling apache absec module" diff --git a/mod_absec.c b/mod_absec.c index 70da0d6..b68523a 100755 --- a/mod_absec.c +++ b/mod_absec.c @@ -104,7 +104,7 @@ static int absec_handler_last(request_rec *r) /* Main routine - called before request processing */ static int absec_handler_first(request_rec *r) { - ap_rprintf(r, "Before Method: %s
\r\n", r->method); + ap_rprintf(r, "handler first : Before Method: %s
\r\n", r->method); // Is this module really called? /*if (strcmp(r->handler, "absec")) { ap_rprintf(r, "DECLINED
\r\n"); @@ -114,7 +114,7 @@ static int absec_handler_first(request_rec *r) //////// /* http method validate the perm asked (r/w vs get/post,put) */ - ap_rprintf(r, "Before Method: %s
\r\n", r->method); + //ap_rprintf(r, "Before Method: %s
\r\n", r->method); int permmask = 0; if (strcmp(r->method,"GET")==0) permmask=0444; // r @@ -312,7 +312,7 @@ static authz_status authz_check_absec(request_rec *r, const char *require_args, //////// /* http method validate the perm asked (r/w vs get/post,put) */ - ap_rprintf(r, "Before Method: %s
\r\n", r->method); + //ap_rprintf(r, "Before Method: %s
\r\n", r->method); int permmask = 0; if (strcmp(r->method,"GET")==0) permmask=0444; // r @@ -361,7 +361,7 @@ static authz_status authz_check_absec(request_rec *r, const char *require_args, // If file is user readable and user match return content if ((fperm.st_uid==pw->pw_uid) && (fperm.st_mode & 0700 & permmask)) { - ap_rprintf(r, "Fichier propriétaire
\r\n"); + //ap_rprintf(r, "Fichier propriétaire
\r\n"); ap_log_rerror("mod_absec.c", 282, 1, APLOG_ERR, APR_SUCCESS, r, "authz_check_absec : Fichier propriétaire
\r\n"); return AUTHZ_GRANTED; } else { @@ -370,7 +370,7 @@ static authz_status authz_check_absec(request_rec *r, const char *require_args, // If file is group readable and primary group match return content if ((fperm.st_gid==pw->pw_gid) && (fperm.st_mode & 0070 & permmask)) { - ap_rprintf(r, "Fichier groupe
\r\n"); + //ap_rprintf(r, "Fichier groupe
\r\n"); ap_log_rerror("mod_absec.c", 282, 1, APLOG_ERR, APR_SUCCESS, r, "authz_check_absec : Fichier groupe
\r\n"); return AUTHZ_GRANTED; } else {