File: getobjectbyX.c

package info (click to toggle)
dnprogs 2.52
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,644 kB
  • ctags: 4,021
  • sloc: ansic: 26,737; cpp: 10,666; makefile: 832; sh: 537; awk: 13
file content (308 lines) | stat: -rw-r--r-- 6,740 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//getobjectbyX.c:

/*
    copyright 2008 Philipp 'ph3-der-loewe' Schafft <lion@lion.leolix.org>

    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
    version 3.

    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.
*/

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <netdb.h>

#include <netdnet/dn.h>
#include <netdnet/dnetdb.h>

#define DNETD_FILE SYSCONF_PREFIX "/etc/dnetd.conf"

static char * _dnet_objhinum_string   = NULL;
static int    _dnet_objhinum_handling = DNOBJHINUM_ERROR;

struct {
 int    num;
 char * name;
} _dnet_objdb[] = {
 { 17, "FAL"   },
 { 18, "HLD"   },
 { 19, "NML"   },
 { 19, "NICE"  }, // alias!
 { 23, "DTERM" },
 { 23, "REMACP"}, // alias! (nml.c)
 { 25, "MIRROR"},
 { 26, "EVR"   },
 { 27, "MAIL11"},
 { 27, "MAIL"  }, // alias!
 { 29, "PHONE" },
 { 42, "CTERM" },
 { 51, "VPM"   },
 { 63, "DTR"   },
 { -1, NULL}      // END OF LIST
};

int dnet_setobjhinum_handling (int handling, int min) {
 if ( handling == DNOBJHINUM_RESET ) {
  _dnet_objhinum_string = NULL;
  dnet_checkobjectnumber(256);
  return 0;
 }

 if ( min ) {
  dnet_checkobjectnumber(256);
  if ( _dnet_objhinum_handling > handling )
   return 1;
 }

 _dnet_objhinum_handling = handling;

 return 0;
}

int dnet_checkobjectnumber (int num) {

 if ( _dnet_objhinum_string == NULL ) {
  if ( (_dnet_objhinum_string = getenv(DNOBJ_HINUM_ENV)) == NULL )
   _dnet_objhinum_string = DNOBJ_HINUM_DEF;

  if ( !*_dnet_objhinum_string )
   _dnet_objhinum_string = DNOBJ_HINUM_DEF;

  if ( !strcasecmp(_dnet_objhinum_string, "error") ) {
   dnet_setobjhinum_handling(DNOBJHINUM_ERROR, 0); // error case
  } else if ( !strcasecmp(_dnet_objhinum_string, "zero") ) {
   dnet_setobjhinum_handling(DNOBJHINUM_ZERO, 0); // return as object number 0
  } else if ( !strcasecmp(_dnet_objhinum_string, "return") ) {
   dnet_setobjhinum_handling(DNOBJHINUM_RETURN, 0); // return as object number unchanged
  } else if ( !strcasecmp(_dnet_objhinum_string, "alwayszero") ) {
   dnet_setobjhinum_handling(DNOBJHINUM_ALWAYSZERO, 0); // specal case to prevent app from using numbered objects
  }
 }

 if ( _dnet_objhinum_handling == DNOBJHINUM_ALWAYSZERO && num != -1 )
  return 0;

 if ( num < 256 )
  return num;

 switch (_dnet_objhinum_handling) {
  case DNOBJHINUM_ERROR:
   errno = EINVAL;
   return -1;
  case DNOBJHINUM_ZERO:
   return 0;
  case DNOBJHINUM_RETURN:
   return num;
  default:
   errno = ENOSYS;
   return -1;
 }
}

int getobjectbyname_nis(char * name) {
 char           * cur, *next;
 char             proto[16];
 struct servent * se;
 static char    * search_order = NULL;

 if ( search_order == NULL ) {
  if ( (search_order = getenv(DNOBJ_SEARCH_ENV)) == NULL )
   search_order = DNOBJ_SEARCH_DEF;
  if ( !*search_order )
   search_order = DNOBJ_SEARCH_DEF;
 }

 cur = search_order;

 while (cur && *cur) {
  if ( (next = strstr(cur, " ")) == NULL ) {
   strncpy(proto, cur, 16);
  } else {
   strncpy(proto, cur, next-cur);
   proto[next-cur] = 0;
   next++;
  }
  cur = next;

  if ( (se = getservbyname(name, proto)) != NULL ) {
   return ntohs(se->s_port);
  }
 }

 errno = ENOENT;
 return -1;
}

char * getobjectbynumber_nis(int num) {
 char           * cur, *next;
 char             proto[16];
 struct servent * se;
 static char    * search_order = NULL;

 if ( search_order == NULL ) {
  if ( (search_order = getenv(DNOBJ_SEARCH_ENV)) == NULL )
   search_order = DNOBJ_SEARCH_DEF;
  if ( !*search_order )
   search_order = DNOBJ_SEARCH_DEF;
 }

 cur = search_order;

 num = htons(num);

 while (cur && *cur) {
  if ( (next = strstr(cur, " ")) == NULL ) {
   strncpy(proto, cur, 16);
  } else {
   strncpy(proto, cur, next-cur);
   proto[next-cur] = 0;
   next++;
  }
  cur = next;

  if ( (se = getservbyport(num, proto)) != NULL ) {
   if ( strcmp(proto, se->s_proto) == 0 ) /* check if we got what we requested,
                                             may help on buggy libcs */
    return se->s_name;
  }
 }

 errno = ENOENT;
 return NULL;
}

int getobjectbyname_static(char * name) {
 int i;

 for (i = 0; _dnet_objdb[i].num != -1; i++) {
  if ( !strcasecmp(name, _dnet_objdb[i].name) )
   return _dnet_objdb[i].num;
 }

 errno = ENOENT;
 return -1;
}

char * getobjectbynumber_static(int num) {
 int i;

 for (i = 0; _dnet_objdb[i].num != -1; i++) {
  if ( _dnet_objdb[i].num == num )
   return _dnet_objdb[i].name;
 }

 errno = ENOENT;
 return NULL;
}

int getobjectbyname_dnetd(char * name) {
 FILE * dnd;
 int    found = -1;
 int    ret;
 int    curr;
 char   line[1024], cname[16], rest[1024];

 cname[15] = 0; // work around bugy *scanf()s

 if ( (dnd = fopen(DNETD_FILE, "r")) == NULL ) {
  return -1;
 }

 while (fgets(line, 1024, dnd) != NULL) {
  if ( sscanf(line, "%15s %i %1024s\n", cname, &curr, rest) == 3 ) {
   if ( *cname != '#' && strcasecmp(name, cname) == 0 ) {
    found = curr;
    break;
   }
  }
 }

 fclose(dnd);

 if ( found == -1 )
  errno = ENOENT;

 return found;
}

char * getobjectbynumber_dnetd(int num) {
 FILE * dnd;
 int    curr;
 static char   cname[16]; // this is not thread safe
 char   line[1024], rest[1024];

 cname[15] = 0; // work around bugy *scanf()s

 if ( (dnd = fopen(DNETD_FILE, "r")) == NULL ) {
  return NULL;
 }

 while (fgets(line, 1024, dnd) != NULL) {
  if ( sscanf(line, "%15s %i %1024s\n", cname, &curr, rest) == 3 ) {
   if ( *cname != '#' && num == curr ) {
    fclose(dnd);
    return cname;
   }
  }
 }

 fclose(dnd);

 errno = ENOENT;
 return NULL;
}

int getobjectbyname(char * name) {
 int num;
 int old_errno = errno;

 if ( (num = getobjectbyname_nis(name)) == -1 )
  if ( (num = getobjectbyname_dnetd(name)) == -1 )
   num = getobjectbyname_static(name);

 if ( num != -1 )
  errno = old_errno;

 return dnet_checkobjectnumber(num);
}

int getobjectbynumber(int number, char * name, size_t name_len) {
 int num;
 char * rname = NULL;
 int old_errno = errno;

 if ( (num = dnet_checkobjectnumber(number)) == -1 ) { // errno is set correctly after this call
  return -1;
 }

 if ( name_len < 2 ) {
  errno = EINVAL;
  return -1;
 }

 if ( (rname = getobjectbynumber_nis(number)) == NULL )
  if ( (rname = getobjectbynumber_dnetd(number)) == NULL )
   rname = getobjectbynumber_static(number);

 if ( rname == NULL ) {
  errno = ENOENT;
  return -1;
 }

 errno = old_errno;

 strncpy(name, rname, name_len-1);
 name[name_len-1] = 0;

 return num;
}

//ll