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
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
 
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
#include "module.h" 
#include "config.h" 
 
#if defined(HAVE_RPCSVC_YPCLNT_H) && defined(HAVE_RPCSVC_YP_PROT_H) 
#ifdef HAVE_RPC_TYPES_H 
#include <rpc/types.h> 
#endif /* HAVE_RPC_TYPES_H */ 
#ifdef HAVE_RPC_RPC_H 
#include <rpc/rpc.h> 
#endif /* HAVE_RPC_RPC_H */ 
#ifdef HAVE_RPC_CLNT_H 
#include <rpc/clnt.h> 
#endif /* HAVE_RPC_CLNT_H */ 
#include <rpcsvc/yp_prot.h> 
#include <rpcsvc/ypclnt.h> 
 
#include "pike_error.h" 
#include "pike_macros.h" 
#include "constants.h" 
#include "interpret.h" 
#include "module_support.h" 
 
 
#define sp Pike_sp 
 
#ifdef HAVE_YPERR_STRING 
#ifdef YPERR_STRING_PROTOTYPE_MISSING 
char *yperr_string(int incode); 
#endif /* YPERR_STRING_PROTOTYPE_MISSING */ 
 
#define YPERROR(e) do{ if(err) Pike_error("%s\n", yperr_string(e)); }while(0) 
#else /* !HAVE_YPERR_STRING */ 
#define YPERROR(e) do{ if(e) Pike_error("YP error %d.\n", (e)); }while(0) 
#endif /* HAVE_YPERR_STRING */ 
 
struct my_yp_domain 
{ 
  char *domain; 
  int last_size; /* Optimize some allocations */ 
}; 
 
#define this ((struct my_yp_domain *)Pike_fp->current_storage) 
 
/*! @module Yp 
 *! 
 *! This module is an interface to the Yellow Pages functions. Yp is also 
 *! known as NIS (Network Information System) and is most commonly used to 
 *! distribute passwords and similar information within a network. 
 */ 
 
/*! @decl string default_domain() 
 *! 
 *! Returns the default yp-domain. 
 */ 
static void f_default_domain(INT32 args) 
{ 
  int err; 
  char *ret; 
 
  err = yp_get_default_domain(&ret); 
 
  YPERROR( err ); 
 
  pop_n_elems( args ); 
  push_text( ret ); 
} 
 
/*! @class Domain 
 */ 
 
/*! @decl string server(string map) 
 *! 
 *! Returns the hostname of the server serving the map @[map]. @[map] 
 *! is the YP-map to search in. This must be the full map name. 
 *! eg @tt{passwd.byname@} instead of just @tt{passwd@}. 
 */ 
static void f_server(INT32 args) 
{ 
  int err; 
  char *ret, *map; 
 
  get_all_args(NULL, args, "%s", &map); 
  err = yp_master(this->domain, map, &ret); 
 
  YPERROR( err ); 
 
  pop_n_elems( args ); 
  push_text( ret ); 
} 
 
/*! @decl void create(string|void domain) 
 *! @decl void bind(string domain) 
 *! 
 *! If @[domain] is not specified , the default domain will be used. 
 *! (As returned by @[Yp.default_domain()]). 
 *! 
 *! If there is no YP server available for the domain, this 
 *! function call will block until there is one. If no server appears 
 *! in about ten minutes or so, an error will be returned. This timeout 
 *! is not configurable. 
 *! 
 *! @seealso 
 *! @[Yp.default_domain()] 
 */ 
static void f_create(INT32 args) 
{ 
  int err; 
  if(!args) 
  { 
    f_default_domain(0); 
    args = 1; 
  } 
  check_all_args(NULL, args, BIT_STRING,0); 
 
  if(this->domain) 
  { 
    yp_unbind( this->domain ); 
    free(this->domain); 
  } 
  this->domain = strdup( sp[-args].u.string->str ); 
  err = yp_bind( this->domain ); 
 
  YPERROR( err ); 
} 
 
/*! @decl mapping(string:string) all(string map) 
 *! 
 *! Returns the whole map as a mapping. 
 *! 
 *! @[map] is the YP-map to search in. This must be the full map name, 
 *! you have to use @tt{passwd.byname@} instead of just @tt{passwd@}. 
 */ 
static void f_all(INT32 args) 
{ 
  int err, num=0; 
  char *retval, *retkey; 
  int retlen, retkeylen; 
  char *map; 
  struct mapping *res_map; 
  check_all_args(NULL, args, BIT_STRING, 0); 
 
  map = sp[-1].u.string->str; 
  res_map = allocate_mapping( (this->last_size?this->last_size+2:40) ); 
 
  if(!(err = yp_first(this->domain, map, &retkey,&retkeylen, &retval,&retlen))) 
    do { 
      push_string(make_shared_binary_string(retkey, retkeylen)); 
      push_string(make_shared_binary_string(retval, retlen)); 
      mapping_insert( res_map, sp-2, sp-1 ); 
      pop_stack(); pop_stack(); 
 
      err = yp_next(this->domain, map, retkey, retkeylen, 
                    &retkey, &retkeylen, &retval, &retlen); 
      num++; 
    } while(!err); 
 
  if(err != YPERR_NOMORE) 
  { 
    free_mapping( res_map ); 
    YPERROR( err ); 
  } 
 
  this->last_size = num; 
  pop_n_elems(args); 
  push_mapping( res_map ); 
} 
 
/*! @decl void map(string map, function(string, string:void) fun) 
 *! 
 *! For each entry in @[map], call the function specified by @[fun]. 
 *! 
 *! @[fun] will get two arguments, the first being the key, and the 
 *! second the value. 
 *! 
 *! @[map] is the YP-map to search in. This must be the full map name. 
 *! eg @tt{passwd.byname@} instead of just @tt{passwd@}. 
 */ 
static void f_map(INT32 args) 
{ 
  int err; 
  char *retval, *retkey; 
  int retlen, retkeylen; 
  char *map; 
 
  struct svalue *f = &sp[-1]; 
 
  check_all_args(NULL, args, BIT_STRING, BIT_FUNCTION|BIT_ARRAY, 0 ); 
 
  map = sp[-2].u.string->str; 
 
  if(!(err = yp_first(this->domain,map, &retkey,&retkeylen, &retval, &retlen))) 
    do { 
      push_string(make_shared_binary_string(retkey, retkeylen)); 
      push_string(make_shared_binary_string(retval, retlen)); 
      apply_svalue( f, 2 ); 
 
      err = yp_next(this->domain, map, retkey, retkeylen, 
                    &retkey, &retkeylen, &retval, &retlen); 
    } while(!err); 
 
  if(err != YPERR_NOMORE) 
    YPERROR( err ); 
} 
 
/*! @decl int order(string map) 
 *! 
 *! Returns the 'order' number for the map @[map]. 
 *! 
 *! This is usually the number of seconds since Jan 1 1970 (see @[time()]). 
 *! When the map is changed, this number will change as well. 
 *! 
 *! @[map] is the YP-map to search in. This must be the full map name. 
 *! eg @tt{passwd.byname@} instead of just @tt{passwd@}. 
 */ 
static void f_order(INT32 args) 
{ 
  int err; 
  YP_ORDER_TYPE ret; 
 
  check_all_args(NULL, args, BIT_STRING, 0); 
 
  err = yp_order( this->domain, sp[-args].u.string->str, &ret); 
 
  YPERROR( err ); 
 
  pop_n_elems( args ); 
  push_int( (INT32) ret ); 
} 
 
/*! @decl string match(string map, string key) 
 *! 
 *! Search for the key @[key] in the Yp-map @[map]. 
 *! 
 *! @returns 
 *! If there is no @[key] in the map, 0 (zero) will be returned, 
 *! otherwise the string matching the key will be returned. 
 *! 
 *! @note 
 *! @[key] must match exactly, no pattern matching of any kind is done. 
 */ 
static void f_match(INT32 args) 
{ 
  int err; 
  char *retval; 
  int retlen; 
 
  check_all_args(NULL, args, BIT_STRING, BIT_STRING, 0); 
 
  err = yp_match( this->domain, sp[-args].u.string->str, 
                  sp[-args+1].u.string->str, sp[-args+1].u.string->len, 
                  &retval, &retlen ); 
 
  if(err == YPERR_KEY) 
  { 
    pop_n_elems( args ); 
    push_undefined(); 
    return; 
  } 
 
  YPERROR( err ); 
 
  pop_n_elems( args ); 
  push_string(make_shared_binary_string( retval, retlen )); 
} 
 
static void init_yp_struct( struct object *UNUSED(o) ) 
{ 
  this->domain = 0; 
  this->last_size = 0; 
} 
 
static void exit_yp_struct( struct object *UNUSED(o) ) 
{ 
  if(this->domain) 
  { 
    yp_unbind( this->domain ); 
    free(this->domain); 
  } 
} 
 
/*! @endclass 
 */ 
 
/*! @endmodule 
 */ 
 
/******************** PUBLIC FUNCTIONS BELOW THIS LINE */ 
 
 
PIKE_MODULE_INIT 
{ 
  /* function(void:string) */ 
  ADD_FUNCTION("default_domain", f_default_domain,tFunc(tVoid,tStr), 
           OPT_EXTERNAL_DEPEND); 
 
  start_new_program(); 
 
  ADD_STORAGE(struct my_yp_domain); 
 
  set_init_callback( init_yp_struct ); 
  set_exit_callback( exit_yp_struct ); 
 
  /* function(string|void:void) */ 
  ADD_FUNCTION("create", f_create,tFunc(tOr(tStr,tVoid),tVoid), 0); 
  /* function(string:void) */ 
  ADD_FUNCTION("bind", f_create,tFunc(tStr,tVoid), 0); 
 
  /* function(string,string:string) */ 
  ADD_FUNCTION("match", f_match,tFunc(tStr tStr,tStr), 0); 
  /* function(string:string) */ 
  ADD_FUNCTION("server", f_server,tFunc(tStr,tStr), 0); 
  /* function(string:mapping(string:string)) */ 
  ADD_FUNCTION("all", f_all,tFunc(tStr,tMap(tStr,tStr)), 0); 
  /* function(string,function|array(function):void) */ 
  ADD_FUNCTION("map", f_map,tFunc(tStr tOr(tFunction,tArr(tFunction)),tVoid), 0); 
  /* function(string:int) */ 
  ADD_FUNCTION("order", f_order,tFunc(tStr,tInt), 0); 
 
  end_class("Domain", 0); 
} 
 
PIKE_MODULE_EXIT 
{ 
 
} 
#else 
 
PIKE_MODULE_INIT {} 
PIKE_MODULE_EXIT {} 
 
#endif