]> git.sesse.net Git - nbtscanner/blob - mysql_interface.c
Import nbtscanner 0.1.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 <stdlib.h>
27 #include <mysql.h>
28
29 #include "mysql_interface.h"
30 #include "configfile.h"
31
32 MYSQL *mysql = NULL;
33
34 /* veeeery detailed error reporting here ;-) */
35 void init_mysql(char *host, char *username, char *password)
36 {
37         if (mysql != NULL) return;      /* hack */
38
39         mysql = mysql_init(NULL);
40         if (mysql == NULL) {
41                 if (verbosity >= 1) {
42                         fprintf(stderr, "mysql_init() failed\n");
43                 }
44                 exit(1);
45         }
46
47         if (mysql_real_connect(mysql, mysql_host, username, password, "nbtscanner",
48                 0, NULL, 0) == 0) {
49                 if (verbosity >= 1) {
50                         fprintf(stderr, "mysql_real_connect() failed\n");
51                 }
52                 exit(1);
53         }
54 }
55
56 void add_record_mysql(char *ip, char *hostname, char *fileservername, char *groupname)
57 {
58         char query[256];
59         snprintf(query, 256,
60                 "REPLACE INTO nbtscanner.nbtscanner SET ip='%s', hostname='%s', fileservername='%s', groupname='%s';",
61                 ip, hostname, fileservername, groupname);
62
63         if (mysql_query(mysql, query) != 0) {
64                 if (verbosity >= 1) {
65                         fprintf(stderr, "mysql_query(`%s') failed\n", query);
66                 }
67                 exit(1);
68         }
69 }
70
71 void delete_record_mysql(char *ip)
72 {
73         char query[256];
74         snprintf(query, 256,
75                 "DELETE FROM nbtscanner.nbtscanner WHERE ip='%s';",
76                 ip);
77
78         if (mysql_query(mysql, query) != 0) {
79                 if (verbosity >= 1) {
80                         fprintf(stderr, "mysql_query(`%s') failed\n", query);
81                 }
82                 exit(1);
83         }
84 }
85
86 void print_all_records_mysql()
87 {
88         MYSQL_RES *result;      
89         unsigned long *lengths;
90         unsigned int num_fields;
91         int i;
92
93         if (mysql_query(mysql, "SELECT ip,fileservername,groupname,(fileservername <> '-unknown-nbtscanner-') FROM nbtscanner.nbtscanner;") != 0) {
94                 if (verbosity >= 1) {
95                         fprintf(stderr, "mysql_query('SELECT ip'...) failed\n");
96                 }
97                 exit(1);
98         }
99
100         result = mysql_store_result(mysql);
101         if (result == NULL) {
102                 if (verbosity >= 1) {
103                         fprintf(stderr, "mysql_store_result() failed\n");
104                 }
105                 exit(1);
106         }
107
108         num_fields = mysql_num_fields(result);
109         while (1) {
110                 MYSQL_ROW row = mysql_fetch_row(result);
111                 if (row == NULL) return;
112
113                 lengths = mysql_fetch_lengths(result);
114
115                 for (i = 0; i < num_fields; i++) {
116                         int j;
117
118                         /* strip away CR/LFs */
119                         for (j = 0; j < lengths[i]; j++) {
120                                 if (row[i][j] == 0 || row[i][j] == 10 || row[i][j] == 13) {
121                                         row[i][j] = '?';
122                                 }
123                         }
124                         printf("%.*s", (int) lengths[i], row[i] ? row[i] : "NULL");
125                         if (i != num_fields - 1) putchar(0);
126                 }
127                 printf("\r\n");
128         }
129 }
130
131 void finish_mysql()
132 {
133         /* don't care if this fails */
134         mysql_query(mysql, "OPTIMIZE TABLE nbtscanner.nbtscanner;");
135
136         mysql_close(mysql);
137 }
138