]> git.sesse.net Git - nbtscanner/blob - mysql_interface.c
Import nbtscanner 0.2.0.
[nbtscanner] / mysql_interface.c
1 /*
2  * nbtscanner -- a tool for scanning large networks for SMB servers.
3  *
4  * mysql_interface.c: Interface routines for the MySQL table. 
5  * Copyright (C) 2000 Steinar H. Gunderson
6  *
7  * Large amounts of code adapted from Samba (http://www.samba.org/)
8  * Copyright (C) Andrew Tridgell 1994-1998, and others.
9  * 
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <mysql.h>
29
30 #include "mysql_interface.h"
31 #include "configfile.h"
32
33 MYSQL *mysql = NULL;
34
35 /* veeeery detailed error reporting here ;-) */
36 void init_mysql(char *host, char *username, char *password)
37 {
38         if (mysql != NULL) return;      /* hack */
39
40         mysql = mysql_init(NULL);
41         if (mysql == NULL) {
42                 if (verbosity >= 1) {
43                         fprintf(stderr, "mysql_init() failed\n");
44                 }
45                 exit(1);
46         }
47
48         if (mysql_real_connect(mysql, mysql_host, username, password, "nbtscanner",
49                 0, NULL, 0) == 0) {
50                 if (verbosity >= 1) {
51                         fprintf(stderr, "mysql_real_connect() failed\n");
52                 }
53                 exit(1);
54         }
55 }
56
57 void add_record_mysql(char *ip, char *hostname, char *fileservername, char *groupname,
58                       char messenger)
59 {
60         char query[256];
61         char hname[256], fsname[256], gname[256];
62
63         mysql_real_escape_string(mysql, hname, hostname, strlen(hostname));
64         mysql_real_escape_string(mysql, fsname, fileservername, strlen(fileservername));
65         mysql_real_escape_string(mysql, gname, groupname, strlen(groupname));
66
67         snprintf(query, 256,
68                 "REPLACE INTO nbtscanner.nbtscanner SET ip='%s', hostname='%s', fileservername='%s', groupname='%s', active='y', lastactive=now(), messenger='%c';",
69                 ip, hname, fsname, gname, messenger);
70
71         if (mysql_query(mysql, query) != 0) {
72                 if (verbosity >= 1) {
73                         fprintf(stderr, "mysql_query(`%s') failed\n", query);
74                 }
75                 exit(1);
76         }
77 }
78
79 void delete_record_mysql(char *ip)
80 {
81         char query[256];
82         snprintf(query, 256,
83                 "UPDATE nbtscanner.nbtscanner SET ACTIVE='n' WHERE ip='%s';",
84                 ip);
85
86         if (mysql_query(mysql, query) != 0) {
87                 if (verbosity >= 1) {
88                         fprintf(stderr, "mysql_query(`%s') failed\n", query);
89                 }
90                 exit(1);
91         }
92 }
93
94 void print_all_records_mysql()
95 {
96         MYSQL_RES *result;      
97         unsigned long *lengths;
98         unsigned int num_fields;
99         int i;
100         char query[512];
101
102         snprintf(query, 512, "SELECT ip,(CASE WHEN fileservername='-unknown-nbtscanner-' THEN hostname ELSE fileservername END),groupname,(fileservername <> '-unknown-nbtscanner-') FROM nbtscanner.nbtscanner WHERE active='y' OR lastactive>DATE_SUB(NOW(), INTERVAL %d SECOND);",
103                 ghost_lifetime);
104
105         if (mysql_query(mysql, query) != 0) {
106                 if (verbosity >= 1) {
107                         fprintf(stderr, "mysql_query('SELECT ip'...) failed\n");
108                 }
109                 exit(1);
110         }
111
112         result = mysql_store_result(mysql);
113         if (result == NULL) {
114                 if (verbosity >= 1) {
115                         fprintf(stderr, "mysql_store_result() failed\n");
116                 }
117                 exit(1);
118         }
119
120         num_fields = mysql_num_fields(result);
121         while (1) {
122                 MYSQL_ROW row = mysql_fetch_row(result);
123                 if (row == NULL) return;
124
125                 lengths = mysql_fetch_lengths(result);
126
127                 for (i = 0; i < num_fields; i++) {
128                         int j;
129
130                         /* strip away CR/LFs */
131                         for (j = 0; j < lengths[i]; j++) {
132                                 if (row[i][j] == 0 || row[i][j] == 10 || row[i][j] == 13 || row[i][j] == ',') {
133                                         row[i][j] = '?';
134                                 }
135                         }
136                         printf("%.*s", (int) lengths[i], row[i] ? row[i] : "NULL");
137                         if (i != num_fields - 1) printf(",");
138                 }
139                 printf("\r\n");
140         }
141 }
142
143 void finish_mysql()
144 {
145         /* don't care if this fails */
146         mysql_query(mysql, "OPTIMIZE TABLE nbtscanner.nbtscanner;");
147
148         mysql_close(mysql);
149 }
150