/* ############################################################################### # HCCLogIP - A custom API module for CPanel # Version 0.2 beta - 23/SEP/2003 # (c) 2003 Juan R. Pozo # http://html.conclase.net/cp/scripts/ # mailto:jrpozo@conclase.net # Mailing-list: http://www.conclase.net/mailman/listinfo/cpanel_conclase.net ############################################################################### # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation; either version 2 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ############################################################################### # For INSTALLATION and DOCUMENTATION please see: # http://html.conclase.net/cp/scripts/HCCLogIP.txt (plain text), or # http://html.conclase.net/cp/scripts/HCCLogIP.pdf (Acrobat) ############################################################################### # License: http://html.conclase.net/cp/scripts/license.html ############################################################################### # Please consider making a donation today. Visit my amazon.com wishlist at: # http://html.conclase.net/link/wishlist # Thank you :) ############################################################################### */ #include #include #include #include #include #include #include unsigned int doquery(MYSQL *m, const char *fmt, ...); int main (int argc, char **argv) { MYSQL mysql; /* Check that we are inside cpanel */ if (!getenv("SERVER_PORT") || !getenv("REMOTE_USER")) { /* Error 1: Not invoked from CPanel (no SERVER_PORT or REMOTE_USER) */ exit(1); } if (strcmp(getenv("SERVER_PORT"), "2082") && strcmp(getenv("SERVER_PORT"), "2083")) { /* Error 2: Not invoked from CPanel (port is not 2082 or 2083) */ exit(2); } /* Check that REMOTE_USER corresponds with caller's uid */ if (strcmp(getpwuid(getuid())->pw_name, getenv("REMOTE_USER"))) { /* Error 3: Not invoked from CPanel (REMOTE_USER is not caller's uid) */ exit(3); } /* Check that we have no arguments */ if (argc != 1) { /* Error 4: Wrong number of arguments */ exit(4); } /* Open database with root permissions */ mysql_init(&mysql); mysql_options(&mysql, MYSQL_READ_DEFAULT_FILE, "/root/.my.cnf"); if (!mysql_real_connect(&mysql, "localhost", NULL, NULL, "HCCLogIP", 3306, NULL, 0)) { /* Error 5: Failed to connect to database */ exit(5); } /* Log IP */ if (!doquery(&mysql, "INSERT INTO HCCLogIP (username, ip) VALUES ('%s', '%s')", getenv("REMOTE_USER"), getenv("REMOTE_ADDR"))) { /* Error 6: Failed to execute query */ exit(6); } mysql_close(&mysql); exit(0); } unsigned int doquery(MYSQL *mysql, const char *fmt, ...) { int i, phs, err; va_list argp; size_t len = 0; char *query = NULL; /* Count number of %s placeholders */ for (i = 0, phs = 0; fmt[i] && fmt[i+1]; i++) { if (fmt[i] == '%' && fmt[i+1] == 's') { phs++; i++; } } /* Compute total length */ len = strlen(fmt) - phs * 2; va_start(argp, fmt); for (i = 0; i < phs; i++) { len += strlen(va_arg(argp, char *)); } va_end(argp); /* Allocate memory and let sprintf do the subsitutions */ query = (char *)calloc(len + 1, sizeof(char)); va_start(argp, fmt); vsprintf(query, fmt, argp); va_end(argp); /* Query database, free buffer and return 1 in case of error */ err = mysql_query(mysql, query); free(query); return (err == 0) ? 1 : 0; }