OSDN Git Service

Fixed CLang warning messages and other
authorwatanaby <watanaby@users.osdn.jp>
Sun, 21 Jun 2015 08:24:38 +0000 (17:24 +0900)
committerwatanaby <watanaby@users.osdn.jp>
Sun, 21 Jun 2015 08:24:38 +0000 (17:24 +0900)
19 files changed:
conf/opengatemmng.conf.sample
conf/warningmail.sample.en [new file with mode: 0644]
doc/Changes.html
doc/install.txt
ezxml/Makefile
ezxml/ezxml.c
mdsrc/Makefile
mdsrc/error.c
mdsrc/macdbcache.c
mngsrc/Makefile
mngsrc/auth-ftps.c
mngsrc/auth-pop3s.c
mngsrc/error.c
mngsrc/util.c
phpsrc/sendreportmail.php
phpsrc/showlog.php
phpsrc/showtable.php
phpsrc/updatemactable.php
phpsrc/updatevendortable.php

index da0f5fb..e8dc7fd 100644 (file)
@@ -3,7 +3,7 @@
 
        <!-- #########################################################
             ## Opengate gateway server hostname(FQDN or IP address)## -->
-       <OpengateServerName>opengate.og.saga-u.ac.jp</OpengateServerName>
+       <OpengateServerName>opengate.example.com</OpengateServerName>
 
        <!-- Debug dump level -->
        <!-- Set 0 to write only open/close and error messages to syslog -->
        <HtmlLangs>ja en</HtmlLangs>
 
        <!-- Path to Apache Contents -->
-       <DocumentRoot>/usr/local/www/apache22/data</DocumentRoot>
+       <DocumentRoot>/usr/local/www/apache24/data</DocumentRoot>
        <CgiDir>/cgi-bin</CgiDir>
        <OpengateDir>/opengate</OpengateDir>
 
diff --git a/conf/warningmail.sample.en b/conf/warningmail.sample.en
new file mode 100644 (file)
index 0000000..dda582c
--- /dev/null
@@ -0,0 +1,12 @@
+From opengate@cc.saga-u.ac.jp
+Mime-Version: 1.0
+Content-Type: text/plain; charset=ISO-2022-JP
+Content-Transfer-Encoding: 7bit
+Subject: Mac Address Registration
+To: %%MAILADDRESS%%
+
+
+Your device [%%DEVICENAME%%] will reach the limit date at [%%LIMITDATE%%].
+Please update the registration in the following page.
+
+http://www.cc.saga-u.ac.jp/opengate/mup.php
index d0c3b0f..030a8ff 100644 (file)
@@ -251,7 +251,10 @@ OpengateM History</H3>
        </DT><DD>
        Modified description of update and registration pages (opengatemup/mown/mreg).
        </DD>
+       <DT>Ver.0.9.6 at 2015.6.10
+       </DT><DD>
+       Fixed CLang warning messages, Modified PHP scripts (opengatem-all).
+       </DD>
 </DL>
-
 </BODY>
 </HTML>
index 81a7dbf..6d07690 100644 (file)
@@ -1,2 +1,3 @@
-Refer http://www.cc.saga-u.ac.jp/opengate/opengatem/index-e.html
+Refer http://opengatem.osdn.jp/index.html.en
+
 
index c8a99fc..d6bdab2 100644 (file)
@@ -21,7 +21,7 @@
 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-CC = gcc
+#CC = gcc
 AR = ar
 RM = rm -f
 CFLAGS = -Wall -O2
@@ -59,4 +59,4 @@ ezxml.o: ezxml.h ezxml.c
        $(CC) $(CFLAGS) -c -o $@ $<
 
 clean:
-       $(RM) $(OBJS) $(LIB) $(TEST) *~
\ No newline at end of file
+       $(RM) $(OBJS) $(LIB) $(TEST) *~
index 82b11fb..73fedae 100644 (file)
@@ -361,7 +361,7 @@ short ezxml_internal_dtd(ezxml_root_t root, char *s, size_t len)
             else *s = '\0'; // null terminate tag name
             for (i = 0; root->attr[i] && strcmp(n, root->attr[i][0]); i++);
 
-            while (*(n = ++s + strspn(s, EZXML_WS)) && *n != '>') {
+            while (++s, *(n = s + strspn(s, EZXML_WS)) && *n != '>') {
                 if (*(s = n + strcspn(n, EZXML_WS))) *s = '\0'; // attr name
                 else { ezxml_err(root, t, "malformed <!ATTLIST"); break; }
 
index 1d20103..8e7e457 100644 (file)
@@ -17,7 +17,7 @@ LIBSQLITE = -lsqlite3 -lpthread
 ## This is memory leak check tool for debugging
 #LIBCCMALLOC = -lccmalloc -L/usr/local/lib
 
-CFLAGS= -g -O4 -Wall -I/usr/local/include ${CFLAGSMYSQL}
+CFLAGS= -g -O3 -Wall -I/usr/local/include ${CFLAGSMYSQL}
 
 LIBS = -lezxml -lpcap -L../ezxml -L/usr/local/lib  ${LIBSQLITE} ${LIBMYSQL} ${LIBCCMALLOC}
 
index 0589769..2683219 100644 (file)
@@ -133,7 +133,7 @@ err_doit(int errnoflag, int level, const char *fmt, va_list ap)
        strcat(buf, "\n");
 
        if (daemon_proc) {
-               syslog(level, buf);
+               syslog(level, "%s", buf);
        } else {
                fflush(stdout);         /* in case stdout and stderr are the same */
                fputs(buf, stderr);
index 74505d1..fafab99 100644 (file)
@@ -63,7 +63,7 @@ int addMacCacheItem(char* macAddress, char* userId, char* extraId, int found) {
 
   /** setup hash value **/
   /* hash value : string "<found>SPACE<unixtime>SPACE<userId>SPACE<extraId>" */
-  snprintf(hashValueStr,BUFFMAXLN,"%d %d %s %s",
+  snprintf(hashValueStr,BUFFMAXLN,"%d %ld %s %s",
           found,time(NULL),userId,extraId);
   hashVal.data = hashValueStr;
   hashVal.size = strlen(hashValueStr) + 1;    
@@ -107,7 +107,7 @@ int queryMacFromMacCache(char* macAddress, char* userId, char* extraId){
 
   /* get is successed */
   /* pick up the hash values */
-  ret=sscanf(hashVal.data,"%d %d %s %s",&found,&entryTime,userId,extraId);
+  ret=sscanf(hashVal.data,"%d %ld %s %s",&found,&entryTime,userId,extraId);
 
   /* found, entryTime should be obtained */
   if(ret<=1) return ERROR;
index a10a9dd..c1038aa 100644 (file)
@@ -2,7 +2,7 @@
 ##  some other parameters exist in opengatemmng.h  ########
 ##########################################################
 CONFIGPATH = /etc/opengate        ## directory of config file
-WWWTOP = /usr/local/www/apache22  ## apache directory top
+WWWTOP = /usr/local/www/apache24  ## apache directory top
 DOCDIR = /data                   ## apache data directory(related to wwwtop)
 CGIDIR = /cgi-bin                ## apache cgi directory(related to wwtop)
 OPENGATEDIR = /opengate                  ## opengate web directory(related to docdir)
@@ -29,7 +29,7 @@ LIBLDAP = -lldap -llber
 ## This is memory leak check tool for debugging
 #LIBCCMALLOC = -lccmalloc -L/usr/local/lib
 
-CFLAGS= -g -O4 -Wall -I/usr/local/include ${CFLAGSMYSQL}
+CFLAGS= -g -O3 -Wall -I/usr/local/include ${CFLAGSMYSQL}
 
 LIBS = -lezxml -lssl -lcrypto -lradius -lpam -L../ezxml -L/usr/local/lib ${LIBLDAP} ${LIBSQLITE} ${LIBMYSQL} ${LIBCCMALLOC}
 
@@ -155,6 +155,7 @@ install-conf:
 .endif
                cp  ../conf/opengatemmng.conf.sample ${CONFIGPATH}
                cp  ../conf/warningmail.sample ${CONFIGPATH}
+               cp  ../conf/warningmail.sample.en ${CONFIGPATH}
                #
                #-------------------------------------------------------#
                # COPY xx.conf.sample to xx and EDIT it.                #
index 5cdb286..3a27080 100644 (file)
@@ -43,7 +43,7 @@ int authFtpse(char *userid, char *passwd)
   char* port;      /* auth server port */
   SSL_CTX    *ctx;
   SSL        *ssl;
-  SSL_METHOD *meth;
+  const SSL_METHOD *meth;
 
   /* get auth server address */
   serverAddr=GetConfValue("AuthServer/Address");
@@ -192,7 +192,7 @@ int authFtpsi(char *userid, char *passwd)
   char* port;      /* auth server port */
   SSL_CTX    *ctx;
   SSL        *ssl;
-  SSL_METHOD *meth;
+  const SSL_METHOD *meth;
 
   /* get auth server address */
   serverAddr=GetConfValue("AuthServer/Address");
index 85367c1..2f97c4a 100644 (file)
@@ -42,7 +42,7 @@ int authPop3s(char *userid, char *passwd)
   int           authResult;
   SSL_CTX    *ctx;
   SSL        *ssl;
-  SSL_METHOD *meth;
+  const SSL_METHOD *meth;
 
   /* get auth server address */
   serverAddr=GetConfValue("AuthServer/Address");
index 80e7062..67bdc12 100644 (file)
@@ -117,7 +117,7 @@ err_doit(int errnoflag, int level, const char *fmt, va_list ap)
        strcat(buf, "\n");
 
        if (daemon_proc) {
-               syslog(level, buf);
+               syslog(level, "%s", buf);
        } else {
                fflush(stdout);         /* in case stdout and stderr are the same */
                fputs(buf, stderr);
index d2b83d0..df44462 100644 (file)
@@ -327,7 +327,7 @@ void createCookie(char *cookie)
   char str[BUFFMAXLN];
 
   /* make Http-cookie from pid&time */
-  snprintf(str, BUFFMAXLN, "%d%d", getpid(),time(NULL));
+  snprintf(str, BUFFMAXLN, "%d%ld", getpid(),time(NULL));
   md5hex(cookie, SIDMAXLN, str);
 }
 
index 75bb6c0..11a7e7a 100755 (executable)
@@ -29,18 +29,18 @@ if(!$link)return;
 
 // get mail address and others from mysql db
 if(!(list($device, $mailAddress)=getDataFromMysql($macAddress))){
-       mysql_close($link);
+       mysqli_close($link);
        return;
 }
 
 // if recent report exists, skip to send report
-if(skipReporting($macAddress, $gatewayName, $reportInterval)){
-       mysql_close($link);
+if(skipReporting($link, $macAddress, $gatewayName, $reportInterval)){
+       mysqli_close($link);
        return;
 }
 
 // close database
-mysql_close($link);
+mysqli_close($link);
 
 // send mail to the user
 sendMailToUser($mailSender, $mailAddress, $device, $gatewayName, 
@@ -82,19 +82,19 @@ prepare mysql connection
 function prepareMysql($mysqlServer, $mysqlUser, $mysqlPassword){
 
        // connect and access to MySql DB
-       $link = mysql_connect($mysqlServer, $mysqlUser, $mysqlPassword);
+       $link = mysqli_connect($mysqlServer, $mysqlUser, $mysqlPassword);
        if (!$link){
-               syslog(LOG_INFO, 'ERR: Cannot connect DB '.mysql_error());
+               syslog(LOG_INFO, 'ERR: Cannot connect DB '.mysqli_error());
                return FALSE;
        }
 
        // use opengatem database
-       $db_selected = mysql_select_db('opengatem', $link);
+       $db_selected = mysqli_select_db($link, 'opengatem');
        if (!$db_selected){
-               syslog(LOG_INFO, 'ERR: Cannot select DB '.mysql_error());
+               syslog(LOG_INFO, 'ERR: Cannot select DB '.mysqli_error());
                return FALSE;
        }
-       mysql_set_charset('utf8');
+       mysqli_set_charset($link, 'utf8');
        return $link;
 }
 
@@ -106,15 +106,15 @@ function getDataFromMysql($macAddress){
        $mailAddress="?";
 
        // query
-       $result = mysql_query('SELECT device, mailAddress FROM macaddrs 
+       $result = mysqli_query($link, 'SELECT device, mailAddress FROM macaddrs 
                WHERE macAddress="'.$macAddress.'" AND status!="D"');
        if (!$result){
-               syslog(LOG_INFO, 'ERR: Fail DB query '.mysql_error());
+               syslog(LOG_INFO, 'ERR: Fail DB query '.mysqli_error());
                return FALSE;
        }
 
        // get result
-       if($row = mysql_fetch_row($result)){
+       if($row = mysqli_fetch_row($result)){
                $device = $row[0];
                $mailAddress = $row[1];
        }else{
@@ -130,10 +130,10 @@ to avoid to send too many mails,
 skip if there are recent logs having same macaddress, and same gateway 
 PLEASE MODIFY to control the report frequency.
 ***/
-function skipReporting($macAddress, $gatewayName, $reportInterval){
+function skipReporting($link, $macAddress, $gatewayName, $reportInterval){
        
        // query
-       $result = mysql_query('SELECT count(*) FROM sessionmd '
+       $result = mysqli_query($link, 'SELECT count(*) FROM sessionmd '
                .'WHERE EXISTS (SELECT * FROM sessionmd '
                .'WHERE macAddress="'.$macAddress.'" '
                .'AND gatewayName LIKE "'.$gatewayName.'.%" '
@@ -143,12 +143,12 @@ function skipReporting($macAddress, $gatewayName, $reportInterval){
                );
 
        if (!$result){
-               syslog(LOG_INFO, 'ERR: Fail query '.mysql_error());
+               syslog(LOG_INFO, 'ERR: Fail query '.mysqli_error());
                return TRUE;
        }
 
        // get data
-       if($row = mysql_fetch_row($result)) $count = $row[0];
+       if($row = mysqli_fetch_row($result)) $count = $row[0];
        else    $count = 0;
 
        // if recent logs exist, skip is true
index 80e3123..85dda3e 100644 (file)
@@ -8,9 +8,6 @@
 
 <?php
 
-  // check basic auth 
-  if(getenv('REMOTE_USER')=='')  die('Cannot get auth');
-
   // set default values
   $userId=$device='%';
   $now=date('Y-m-d H:i:s');
   print("</table></p>");
 
   // connect and access to MySql db
-  $link = mysql_connect('localhost', 'root', '');
-  if (!$link) die('Cannot connet to DB'.mysql_error());
+  $link = mysqli_connect('localhost', 'root', '');
+  if (!$link) die('Cannot connet to DB'.mysqli_error());
 
-  $db_selected = mysql_select_db('opengatem', $link);
-  if (!$db_selected) die('Cannot select DB'.mysql_error());
+  $db_selected = mysqli_select_db($link, 'opengatem');
+  if (!$db_selected) die('Cannot select DB'.mysqli_error());
 
-  mysql_set_charset('utf8');
+  mysqli_set_charset($link, 'utf8');
 
-  $result = mysql_query('SELECT * FROM sessionview where userId like "'.$userId.'" and device like "'.$device.'" and "'.$fromTime.'"<openTime and openTime<"'.$toTime.'"');
+  $result = mysqli_query($link, 'SELECT * FROM sessionview where userId like "'.$userId.'" and device like "'.$device.'" and "'.$fromTime.'"<openTime and openTime<"'.$toTime.'"');
 
-  if (!$result) die('Fail query'.mysql_error());
+  if (!$result) die('Fail query'.mysqli_error());
 
   // show header line
   print("<table border=1>");
   print('<tr>');
   $count=0;
-  while ($field = mysql_fetch_field($result)) {
+  while ($field = mysqli_fetch_field($result)) {
     print('<td>'.$field->name.'</td>');
     $count++;
   }
   print('</tr>');
 
   // show rows
-  while ($row = mysql_fetch_row($result)) {
+  while ($row = mysqli_fetch_row($result)) {
     print('<tr>');
     for($i=0; $i<$count; $i++){
       print('<td>'.$row[$i].'</td>');
@@ -66,7 +63,7 @@
   }
 
   print("</table>");
-  $close_flag = mysql_close($link);
+  $close_flag = mysqli_close($link);
 
 ?>
 
index 263347f..6c86044 100644 (file)
 
 <?php
 
-  // check basic auth
-  if(getenv('REMOTE_USER')=='')  die('Cannot get auth');
-
   // no table is indicated 
   if(!isset($_GET['table']))return;
 
   // connect and access to MySql DB
-  $link = mysql_connect('localhost', 'root', '');
-  if (!$link) die('Cannot connect DB'.mysql_error());
+  $link = mysqli_connect('localhost', 'root', '');
+  if (!$link) die('Cannot connect DB'.mysqli_error());
 
-  $db_selected = mysql_select_db('opengatem', $link);
-  if (!$db_selected) die('Cannot select DB'.mysql_error());
+  $db_selected = mysqli_select_db($link, 'opengatem');
+  if (!$db_selected) die('Cannot select DB'.mysqli_error());
 
-  mysql_set_charset('utf8');
+  mysqli_set_charset($link, 'utf8');
 
-  $result = mysql_query('SELECT * FROM '.$_GET['table'].' limit 1000');
-  if (!$result) die('Fail query'.mysql_error());
+  $result = mysqli_query($link, 'SELECT * FROM '.$_GET['table'].' limit 1000');
+  if (!$result) die('Fail query'.mysqli_error());
 
   // print header
   print("<table border=1>");
   print('<tr>');
   $count=0;
-  while ($field = mysql_fetch_field($result)) {
+  while ($field = mysqli_fetch_field($result)) {
     print('<td>'.$field->name.'</td>');
     $count++;
   }
   print('</tr>');
 
   // print rows
-  while ($row = mysql_fetch_row($result)) {
+  while ($row = mysqli_fetch_row($result)) {
     print('<tr>');
     for($i=0; $i<$count; $i++){
       print('<td>'.$row[$i].'</td>');
@@ -52,7 +49,7 @@
     print('</tr>');
   }
   print("</table>");
-  $close_flag = mysql_close($link);
+  $close_flag = mysqli_close($link);
 
 ?>
 
index 7b67e28..e5d0e6b 100644 (file)
@@ -8,12 +8,9 @@
 
 <?php
 
-  // check basic auth
-  if(getenv('REMOTE_USER')=='')  die('Cannot get auth');
-
   // if userid is not set, request input and return
   if(!isset($_POST['userId'])){
-    print('<form method=POST action=updatemac.php>');
+    print('<form method=POST action=updatemactable.php>');
     print('UserId:<input type=text name=userId>');
     print('<input type=submit value="Send">');
     print('<input type=reset value="Reset">');
   $userId=$_POST['userId'];
   
   // setup MySql DB
-  $link = mysql_connect('localhost', 'root', '');
-  if (!$link) die('Cannot connect DB'.mysql_error());
+  $link = mysqli_connect('localhost', 'root', '');
+  if (!$link) die('Cannot connect DB'.mysqli_error());
 
-  $db_selected = mysql_select_db('opengatem', $link);
-  if (!$db_selected) die('cannot select DB'.mysql_error());
+  $db_selected = mysqli_select_db($link, 'opengatem');
+  if (!$db_selected) die('cannot select DB'.mysqli_error());
 
-  mysql_set_charset('utf8');
+  mysqli_set_charset($link, 'utf8');
 
   // if data is posted, update DB
   $i=0;
       // UPDATE macaddrs SET status="D",device="postdata",limitdate=now(),mailAddress="postdata"
       //       WHERE macAddress="postdata" and userId="postdata" and status!="D"
 
-      $result = mysql_query('UPDATE macaddrs SET status="'.$_POST['status'][$i]
+      $result = mysqli_query($link, 'UPDATE macaddrs SET status="'.$_POST['status'][$i]
               .'", device="'.$_POST['device'][$i].'", limitDate=now()'
               .', mailAddress="'.$_POST['mailAddress'][$i].'" WHERE macAddress="'
               .$_POST['macAddress'][$i].'" and userId="'.$userId.'" and status!="D"');
-      if (!$result) die('Fail update query'.mysql_error());
+      if (!$result) die('Fail update query'.mysqli_error());
 
     }else if($_POST['status'][$i]=='A'||$_POST['status'][$i]=='I'){
       // other cases
       // UPDATE macaddrs SET status="D",device="postdata",limitdate="postdata",mailAddress="postdata"
       //       WHERE macAddress="postdata" and userId="postdata" and status!="D"
 
-      $result = mysql_query('UPDATE macaddrs SET status="'.$_POST['status'][$i]
+      $result = mysqli_query($link, 'UPDATE macaddrs SET status="'.$_POST['status'][$i]
               .'", device="'.$_POST['device'][$i].'", limitDate="'.$_POST['limitDate'][$i]
               .'", mailAddress="'.$_POST['mailAddress'][$i].'" WHERE macAddress="'
               .$_POST['macAddress'][$i].'" and userId="'.$userId.'" and status!="D"');
-      if (!$result) die('Fail update query'.mysql_error());
+      if (!$result) die('Fail update query'.mysqli_error());
 
     }else{
       print("<font color=red>Illegal status value</font>");
@@ -64,9 +61,9 @@
   }
 
   // get data from DB to show on web
-  $result = mysql_query('SELECT macAddress,status,device,limitDate,mailAddress FROM macaddrs where userId="'
+  $result = mysqli_query($link, 'SELECT macAddress,status,device,limitDate,mailAddress FROM macaddrs where userId="'
             .$userId.'"');
-  if (!$result) die('Fail select query'.mysql_error());
+  if (!$result) die('Fail select query'.mysqli_error());
 
  // print html form
   print('<form method=POST action=updatemac.php>');
@@ -78,7 +75,7 @@
   print("<table border=1>");
   print('<tr>');
   $count=0;
-  while ($field = mysql_fetch_field($result)) {
+  while ($field = mysqli_fetch_field($result)) {
     print('<td>'.$field->name.'</td>');
     $fieldName[$count]=$field->name;
     $count++;
@@ -86,7 +83,7 @@
   print('</tr>');
 
   // print rows
-  while ($row = mysql_fetch_row($result)) {
+  while ($row = mysqli_fetch_row($result)) {
     print('<tr>');
     if($row[1]=='D'){  // deleted items
       for($i=0; $i<$count; $i++){
   }
   print('</table>');
   print('</form>');
-  $close_flag = mysql_close($link);
+  $close_flag = mysqli_close($link);
 
 ?>
 <p>status char: A=Actice, I=InActive, D=Deleted</p>
index 8cba8e9..5883fb7 100644 (file)
@@ -1,62 +1,60 @@
-<?php\r
-\r
-// Create or Update the vendor table in Management DB.\r
-// The table is used in opengatemchk\r
-//\r
-// At installation, run to create vendor table\r
-//\r
-// After installation, run periodically to update vendor table.\r
-\r
-\r
-//connect to Management DB (* set proper parameters *)\r
-$link = mysql_connect("localhost", "root", "");\r
-if (!$link) die('Cannot connect to DB.'.mysql_error());\r
-\r
-// select opengatem database\r
-$db_selected = mysql_select_db('opengatem', $link);\r
-if (!$db_selected) die('Cannot select DB.'.mysql_error());\r
-\r
-// create table if not exists\r
-$sql = "CREATE TABLE IF NOT EXISTS nicvendors(oui CHAR(8), org TINYTEXT)";\r
-mysql_query($sql);\r
-\r
-// connect to oui list in IEEE site\r
-$fp = fopen("http://standards.ieee.org/develop/regauth/oui/oui.txt", "r");\r
-if(!$fp) die('Cannot connect to IEEE site. Check network connection');\r
-\r
-// scan the download list\r
-while (!feof($fp)) {\r
-\r
-  // get one line\r
-  $line = fgets($fp);\r
-\r
-  // columns 0-7 may be oui \r
-  $oui = substr($line, 0, 8);\r
-\r
-  //if $oui is not "**-**-**", skip the line\r
-  if(preg_match('/^[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}$/',$oui)!=1)continue;\r
-\r
-  // reformat $oui from "01-56-AB" to "01:56:ab"\r
-  $oui = str_replace("-",":",$oui);\r
-  $oui = strtolower($oui);\r
-\r
-  // column 16- should be vendor name (trim CR code)\r
-  $org = substr($line, 16);\r
-\r
-  // remove CR code and quotation code\r
-  $org = str_replace(array("\r\n","\n","\r"), "", $org);\r
-  $org = str_replace("\"", "", $org);\r
-  $org = str_replace("\t", "", $org);\r
-\r
-  // insert to db\r
-  $sql = sprintf(\r
-    'REPLACE INTO nicvendors (oui,org) VALUES( "%s", "%s")',\r
-    $oui, $org);\r
-  mysql_query($sql);\r
-\r
-  // log to console\r
-  print $oui." ".$org."<br>\n";\r
-}\r
-fclose($fp);\r
-?>\r
-\r
+<?php
+
+// Create or Update the vendor table in Management DB.
+// The table is used in opengatemchk
+//
+// At installation, run to create vendor table
+//
+// After installation, run periodically to update vendor table.
+
+
+//connect to Management DB (* set proper parameters *)
+$link = mysqli_connect("localhost", "root", "");
+if (!$link) die('Cannot connect to DB.'.mysqli_error());
+
+// select opengatem database
+$db_selected = mysqli_select_db($link, 'opengatem');
+if (!$db_selected) die('Cannot select DB.'.mysqli_error());
+
+// create table if not exists
+$sql = "CREATE TABLE IF NOT EXISTS nicvendors(oui CHAR(8), org TINYTEXT)";
+mysqli_query($link, $sql);
+
+// open oui list in IEEE site
+// $fp = fopen("http://standards.ieee.org/develop/regauth/oui/oui.txt", "r");
+$fp = fopen("./oui.txt", "r");
+if(!$fp) die('Cannot connect to IEEE data');
+
+// scan the download list
+while (!feof($fp)) {
+
+  // get one line
+  $line = fgets($fp);
+
+  // get oui(MAC-left-3bytes) and org(company name) from header line
+  // such as "AB-CD-EF   (hex)   Foo-bar company"
+  $pattern='/([0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2})\s*\(hex\)\s*(.*)$/';
+  if(preg_match($pattern, $line, $matches)!=1) continue;
+  $oui=$matches[1];
+  $org=$matches[2];
+
+  // replace AB-CD-EF to ab:cd:ef
+  $oui = str_replace("-",":",$oui);
+  $oui = strtolower($oui);
+
+  // remove quotation and tab code
+  $org = str_replace("\"", "", $org);
+  $org = str_replace("\t", "", $org);
+
+  // insert to db
+  $sql = sprintf(
+    'REPLACE INTO nicvendors (oui,org) VALUES( "%s", "%s")',
+    $oui, $org);
+  mysqli_query($link, $sql);
+
+  // log to console
+  print $oui." ".$org."<br>\n";
+}
+fclose($fp);
+?>
+