1 /*
2     ettercap -- services list module
3 
4     Copyright (C) ALoR & NaGA
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 
20 
21 */
22 
23 #include <ec.h>
24 #include <ec_file.h>
25 #include <ec_proto.h>
26 
27 /* globals */
28 
29 static SLIST_HEAD(, entry) serv_head;
30 
31 struct entry {
32    u_int32 serv;
33    u_int16 proto;
34    char *name;
35    SLIST_ENTRY(entry) next;
36 };
37 
38 /* protos */
39 
40 static void discard_servdb(void);
41 int services_init(void);
42 char * service_search(u_int32 serv, u_int8 proto);
43 
44 /*****************************************/
45 
46 
discard_servdb(void)47 static void discard_servdb(void)
48 {
49    struct entry *l;
50 
51    while (SLIST_FIRST(&serv_head) != NULL) {
52       l = SLIST_FIRST(&serv_head);
53       SLIST_REMOVE_HEAD(&serv_head, next);
54       SAFE_FREE(l->name);
55       SAFE_FREE(l);
56    }
57 
58    DEBUG_MSG("ATEXIT: discard_servdb");
59 
60    return;
61 }
62 
63 
services_init(void)64 int services_init(void)
65 {
66    struct entry *s;
67    FILE *f;
68 
69    char line[128];
70    char name[32];
71    char type[8];
72    u_int serv;
73    u_int8 proto;
74    int i = 0;
75 
76    /* errors are handled by the function */
77    f = open_data("share", SERVICES_NAMES, FOPEN_READ_TEXT);
78    ON_ERROR(f, NULL, "Cannot open %s", SERVICES_NAMES);
79 
80    while (fgets(line, 80, f) != 0) {
81 
82       if (sscanf(line, "%31s%u/%7s", name, &serv, type) != 3)
83          continue;
84 
85       /* only tcp and udp services */
86       if (!strcmp(type, "tcp"))
87          proto = NL_TYPE_TCP;
88       else if (!strcmp(type, "udp"))
89          proto = NL_TYPE_UDP;
90       else
91          continue;
92 
93       /* skip comments */
94       if (strstr(name, "#"))
95          continue;
96 
97       SAFE_CALLOC(s, 1, sizeof(struct entry));
98 
99       s->name = strdup(name);
100       s->serv = htons(serv);
101       s->proto = proto;
102 
103       SLIST_INSERT_HEAD(&serv_head, s, next);
104 
105       i++;
106 
107    }
108 
109    DEBUG_MSG("serv_init -- %d services loaded", i);
110    USER_MSG("%4d known services\n", i);
111 
112    fclose(f);
113 
114    atexit(discard_servdb);
115 
116    return i;
117 }
118 
119 /*
120  * search for a service and
121  * return its name
122  */
123 
service_search(u_int32 serv,u_int8 proto)124 char * service_search(u_int32 serv, u_int8 proto)
125 {
126    struct entry *s;
127 
128    SLIST_FOREACH(s, &serv_head, next) {
129       if (s->serv == serv && s->proto == proto)
130          return (s->name);
131    }
132 
133    /* empty name for unknown services */
134    return "";
135 }
136 
137 
138 /* EOF */
139 
140 // vim:ts=3:expandtab
141 
142