File: NIS.xs

package info (click to toggle)
libnet-nis-perl 0.44-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 196 kB
  • sloc: perl: 232; makefile: 3; ansic: 1
file content (528 lines) | stat: -rw-r--r-- 10,579 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/* -*- c -*-
**
** Filename: NIS.xs - back end for the Net::NIS package
**
** $Id$
*/

#include <sys/types.h>		/* Needed on FreeBSD */
#include <rpc/rpc.h>
#include <rpcsvc/yp_prot.h>
#include <rpcsvc/ypclnt.h>

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "nis.h"

/*
** The *THX_ macros seem to be 5.6 and above.
**
** Nobody should be running 5.005 any more, but still, it's not my place
** to judge.  If someone wants to, let's try to let them.
*/
#ifndef	 pTHX_
# define pTHX_
#endif	/* pTHX */

/*
** FIXME: if we ever support new YPERR_xxx values, this must be updated!
*/
#define	YPERR_MAX	YPERR_BUSY


/* Solaris doesn't define YPERR_SUCCESS */
#ifndef	YPERR_SUCCESS
#define	YPERR_SUCCESS 0
#endif	/* YPERR_SUCCESS */



/*
** Slight interface change: NIS-a2 would always return ($status, $value).
** We use the GIMME_V macro to determine if our caller wants array or
** scalar.  If array, return the same as before.  If scalar, just return
** the desired value.  Error status will always be available through the
** magic "tied" variable.
*/
#define	YP_RETURN(val)							\
		do {							\
		    if (GIMME_V == G_ARRAY)				\
			XPUSHs(sv_2mortal(newSViv(yp_status)));		\
		    if (yp_status == YPERR_SUCCESS)			\
			XPUSHs(sv_2mortal(val));			\
		    else						\
			XPUSHs(&PL_sv_undef);				\
		} while (0)



struct callbackdata
{
  SV	*results;
  int	status;
};

/* ESM: What's this?  It doesn't seem to be used, nor part of the API?? */
static int
not_here(s)
char *s;
{
    croak("%s not implemented on this architecture", s);
    return -1;
}


/* Invoked by AUTOLOAD, returns int values of the YPERR_<xxx> constants */
static double
constant(name, arg)
char *name;
int arg;
{
    errno = 0;

    if (strEQ(name, "YPERR_ACCESS"))
#ifdef YPERR_ACCESS
	return YPERR_ACCESS;
#else
	goto not_there;
#endif
    if (strEQ(name, "YPERR_BADARGS"))
#ifdef YPERR_BADARGS
	return YPERR_BADARGS;
#else
	goto not_there;
#endif
    if (strEQ(name, "YPERR_BADDB"))
#ifdef YPERR_BADDB
	return YPERR_BADDB;
#else
	goto not_there;
#endif
    if (strEQ(name, "YPERR_BUSY"))
#ifdef YPERR_BUSY
	return YPERR_BUSY;
#else
	goto not_there;
#endif
    if (strEQ(name, "YPERR_DOMAIN"))
#ifdef YPERR_DOMAIN
	return YPERR_DOMAIN;
#else
	goto not_there;
#endif
    if (strEQ(name, "YPERR_KEY"))
#ifdef YPERR_KEY
	return YPERR_KEY;
#else
	goto not_there;
#endif
    if (strEQ(name, "YPERR_MAP"))
#ifdef YPERR_MAP
	return YPERR_MAP;
#else
	goto not_there;
#endif
    if (strEQ(name, "YPERR_NODOM"))
#ifdef YPERR_NODOM
	return YPERR_NODOM;
#else
	goto not_there;
#endif
    if (strEQ(name, "YPERR_NOMORE"))
#ifdef YPERR_NOMORE
	return YPERR_NOMORE;
#else
	goto not_there;
#endif
    if (strEQ(name, "YPERR_PMAP"))
#ifdef YPERR_PMAP
	return YPERR_PMAP;
#else
	goto not_there;
#endif
    if (strEQ(name, "YPERR_RESRC"))
#ifdef YPERR_RESRC
	return YPERR_RESRC;
#else
	goto not_there;
#endif
    if (strEQ(name, "YPERR_RPC"))
#ifdef YPERR_RPC
	return YPERR_RPC;
#else
	goto not_there;
#endif
    if (strEQ(name, "YPERR_SUCCESS"))
#ifdef YPERR_SUCCESS
	return YPERR_SUCCESS;
#else
	goto not_there;
#endif
    if (strEQ(name, "YPERR_VERS"))
#ifdef YPERR_VERS
	return YPERR_VERS;
#else
	goto not_there;
#endif
    if (strEQ(name, "YPERR_YPBIND"))
#ifdef YPERR_YPBIND
	return YPERR_YPBIND;
#else
	goto not_there;
#endif
    if (strEQ(name, "YPERR_YPERR"))
#ifdef YPERR_YPERR
	return YPERR_YPERR;
#else
	goto not_there;
#endif
    if (strEQ(name, "YPERR_YPSERV"))
#ifdef YPERR_YPSERV
	return YPERR_YPSERV;
#else
	goto not_there;
#endif

    errno = EINVAL;
    return 0;

not_there:
    errno = ENOENT;
    return 0;
}


/*****************************************************************************\
**                     Whee!  Magic yp_status variable!                      *|
\*****************************************************************************/
static int yp_status;

/*
** When perl scripts read its value, e.g.:
**
**     tie %map, 'Net::NIS', $mapname
**       or die "Could not tie $mapname: $yp_status\n"
**
*/
static int yp_status_get(pTHX_ SV *sv, MAGIC *m)
{
    /*
    ** First set the numeric value (double -- int doesn't do the right thing),
    ** then set the string value.  The SvNOK_on() is copied unaltered from
    ** perl-5.6.1/mg.c, including the wonderful comment.  It makes "sv"
    ** work both as a string and as a number.
    **
    ** Note that if yp_status is 0 we use an emtpy string, so script can do:
    **
    **        $yp_status_varname and die "blah blah: $yp_..."
    **
    ** ...without worrying about "0" status being translated as "Success"
    */
    sv_setnv (sv, yp_status);
    sv_setpv (sv, yp_status == YPERR_SUCCESS ? "" : yperr_string (yp_status));

    SvNOK_on(sv);   /* what a wonderful hack! */

    return 1;
}

/*
** This seems useful only for testing: $yperr = 0 (or something else)
*/
static int yp_status_set(pTHX_ SV *sv, MAGIC *m)
{
    int new_val = SvIV(sv);

    /* Only allow setting this var to one of our supported YPERR_xxx vals */
    if (new_val < YPERR_SUCCESS || YPERR_MAX < new_val)
	croak("Value out of range");

    yp_status = new_val;

    return 1;
}

MGVTBL yp_status_accessors = {
    yp_status_get, yp_status_set,
};


/*****************************************************************************\
|*                     Callback used by yp_all()                             *|
\*****************************************************************************/

int
ypallcallback(instatus, inkey, inkeylen, inval, invallen, indata)
int     instatus;
char    *inkey;
int     inkeylen;
char    *inval;
int     invallen;
char    *indata;
{
    if (instatus == YP_TRUE)
    {
	/* Ugh.  Sometimes we get back keys (or values) with a trailing NUL. */
	if (0 < inkeylen && inkey[inkeylen - 1] == '\0')
	    --inkeylen;
	if (0 < invallen && inval[invallen - 1] == '\0')
	    --invallen;

	/* Don't allow a zero-length key -- but zero-length value ("") is OK */
	if (0 < inkeylen) {
	    hv_store((HV*)SvRV(((struct callbackdata *)indata)->results),
		             inkey, inkeylen,
		     newSVpv(inval, invallen), (U32)0);
	}
    }

    ((struct callbackdata *)indata)->status = ypprot_err(instatus);

    if (instatus < 0) return instatus;
    return 0;
}


MODULE = Net::NIS	PACKAGE = Net::NIS

 #
 # YPERR_xxx constants, enumerated above
 #
double
constant(name,arg)
	char *		name
	int		arg

int
yp_bind(domain)
  char *	domain

void
yp_unbind(domain)
  char *	domain

void
yp_get_default_domain()
  PPCODE:
  {
    char	*domain;

    yp_status = yp_get_default_domain(&domain);

    YP_RETURN(newSVpv(domain, strlen(domain)));
  }

 #
 # Looks up a key in a given map (this implements the FETCH part)
 #
void
yp_match(domain, map, key)
  char *	domain
  char *	map
  SV *		key
  PPCODE:
  {
    char	*inkey;
    STRLEN	inkeylen;
    char	*outval;
    int		outvallen = 0;

    if (SvPOK(key))
    {
	inkey = SvPV(key, inkeylen);

	yp_status = yp_match(domain, map, inkey, (int)inkeylen,
			     &outval, &outvallen);
	/* Sigh.  Sometimes we have to include the trailing NUL */
	if (yp_status == YPERR_KEY)
	    yp_status = yp_match(domain, map, inkey, (int)inkeylen 1,
				 &outval, &outvallen);

	/* Like above, sometimes we get an extra trailing NUL char */
	if (yp_status == YPERR_SUCCESS)
	    if (0 < outvallen && outval[outvallen-1] == '\0')
		--outvallen;

    }
    else
    {
	yp_status = YPERR_BADARGS;
    }

    YP_RETURN(newSVpv(outval, outvallen));
  }

void
yp_first(domain, map)
  char *	domain
  char *	map
  PPCODE:
  {
    char	*outkey;
    int		outkeylen;
    char	*outval;
    int		outvallen;

    yp_status = yp_first(domain, map, &outkey, &outkeylen,
			              &outval, &outvallen);
    XPUSHs(sv_2mortal(newSViv(yp_status)));
    if (yp_status == 0)
    {
	XPUSHs(sv_2mortal(newSVpv(outkey, outkeylen)));
	XPUSHs(sv_2mortal(newSVpv(outval, outvallen)));
    }
  }

void
yp_next(domain, map, key)
  char *	domain
  char *	map
  SV *		key
  PPCODE:
  {
    char	*inkey;
    STRLEN	inkeylen;
    char	*outkey;
    int 	outkeylen;
    char	*outval;
    int		outvallen;

    if (SvPOK(key))
    {
	inkey = SvPV(key, inkeylen);

	yp_status = yp_next(domain, map, inkey, (int)inkeylen,
			    &outkey, &outkeylen, &outval, &outvallen);
	XPUSHs(sv_2mortal(newSViv(yp_status)));
	if (yp_status == 0)
	{
	    XPUSHs(sv_2mortal(newSVpv(outkey, outkeylen)));
	    XPUSHs(sv_2mortal(newSVpv(outval, outvallen)));
	}
    }
    else
    {
	XPUSHs(sv_2mortal(newSViv(YPERR_BADARGS)));
    }
  }

void
yp_all(domain, map)
  char *	domain
  char *	map
  PPCODE:
  {
    struct ypall_callback	callback;
    struct callbackdata		data;

    data.results = newRV((SV *) newHV());
    data.status = 0;
    callback.foreach = ypallcallback;
    callback.data = (char *)&data;
    yp_status = yp_all(domain, map, &callback);

    if (yp_status == YPERR_SUCCESS) {
	# Linux & Solaris see NOMORE, FreeBSD gets a plain 0
	if (data.status == YPERR_NOMORE ||
	    data.status == YPERR_SUCCESS) {
	    yp_status = YPERR_SUCCESS;
	}
	else {			/* Unexpected value in callback status block */
	    yp_status = data.status;
	}
    }

    YP_RETURN((SV*)data.results);
  }

void
yp_order(domain, map)
  char *	domain
  char *	map
  PPCODE:
  {
    unsigned int	order;

    yp_status = yp_order(domain, map, &order);
    YP_RETURN(newSViv(order));
  }

void
yp_master(domain, map)
  char *	domain
  char *	map
  PPCODE:
  {
    char	*name;

    yp_status = yp_master(domain, map, &name);
    YP_RETURN(newSVpv(name, strlen(name)));
  }

char *
yperr_string(code)
  int		code

int
ypprot_err(code)
  int		code

 #
 # Tie the magic yp_status variable.  This should be called from our .pm
 #
void
_yp_tie_status(sv)
	SV*	sv
    PREINIT:
	MAGIC *m;
    CODE:
	sv_magic(sv, NULL, '~', "Net::NIS::yp_status_variable",
		         strlen("Net::NIS::yp_status_variable"));
	m = mg_find(sv, '~');
	m->mg_virtual = &yp_status_accessors;
	SvMAGICAL_on(sv);

#ifdef	__linux

  #
  # Returns an array of all the YP map names
  #
void
yp_maplist(domain)
  char *	domain
  PREINIT:
	int		  ret;
	struct ypmaplist *ypmap = NULL;
	AV		 *retval;
  PPCODE:
  {
    ret = yp_maplist( domain, &ypmap );

    if (ret == YPERR_SUCCESS) {
      struct ypmaplist *y, *old;

      for (y=ypmap; y;) {
	// FIXME: check that y->map is not NULL?
	XPUSHs(newSVpv(y->ypml_name,strlen(y->ypml_name)));
	old = y;
	y   = y->ypml_next;
	free(old);
      }
    }
  }


#else	/* Linux */

void
yp_maplist(domain)
    char *	domain
  PPCODE:
  {
    warn("Net::NIS::yp_maplist() -- not implemented on this OS");
    XSRETURN_EMPTY;
  }

#endif