databases handling tools chmod, chgrp, chown, ls

This commit is contained in:
2019-01-21 09:13:03 -05:00
parent 2fb12410ea
commit ea59731e1f
6 changed files with 244 additions and 25 deletions
+63
View File
@@ -0,0 +1,63 @@
/////
//
// File : absec_chown.c
// Author : Jean-Luc Cyr
// Date : 2018-10
//
// Description: tool to change ownership from mysql database
//
#include <stdio.h>
#include <string.h>
#include <mysql.h>
////////////////////////////////////////////////////////////////
// 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 <uid> <url>\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;
}
}
+70
View File
@@ -0,0 +1,70 @@
/////
//
// File : absec_ls.c
// Author : Jean-Luc Cyr
// Date : 2018-10
//
// Description: tool to query permissions from mysql database
//
#include <stdio.h>
#include <string.h>
#include <mysql.h>
////////////////////////////////////////////////////////////////
// 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 <perms> <url>\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;
}
}
+63
View File
@@ -0,0 +1,63 @@
/////
//
// File : absec_chown.c
// Author : Jean-Luc Cyr
// Date : 2018-10
//
// Description: tool to change ownership from mysql database
//
#include <stdio.h>
#include <string.h>
#include <mysql.h>
////////////////////////////////////////////////////////////////
// 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 <uid> <url>\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;
}
}
+10 -20
View File
@@ -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<br/>", mysql_error(conn));
return(0);
}
ap_rprintf(r, "Aucune donnee\n<br/>");
}*/
/* 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("%");
}
}
+33
View File
@@ -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"
+5 -5
View File
@@ -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<br/>\r\n", r->method);
ap_rprintf(r, "handler first : Before Method: %s<br/>\r\n", r->method);
// Is this module really called?
/*if (strcmp(r->handler, "absec")) {
ap_rprintf(r, "DECLINED<br/>\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<br/>\r\n", r->method);
//ap_rprintf(r, "Before Method: %s<br/>\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<br/>\r\n", r->method);
//ap_rprintf(r, "Before Method: %s<br/>\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<br/>\r\n");
//ap_rprintf(r, "Fichier propriétaire<br/>\r\n");
ap_log_rerror("mod_absec.c", 282, 1, APLOG_ERR, APR_SUCCESS, r, "authz_check_absec : Fichier propriétaire<br/>\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<br/>\r\n");
//ap_rprintf(r, "Fichier groupe<br/>\r\n");
ap_log_rerror("mod_absec.c", 282, 1, APLOG_ERR, APR_SUCCESS, r, "authz_check_absec : Fichier groupe<br/>\r\n");
return AUTHZ_GRANTED;
} else {