xref: /netbsd-src/external/bsd/openldap/dist/contrib/slapd-modules/nssov/rpc.c (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1 /* rpc.c - rpc lookup routines */
2 /* $OpenLDAP: pkg/ldap/contrib/slapd-modules/nssov/rpc.c,v 1.1.2.1 2008/07/08 18:53:57 quanah Exp $ */
3 /*
4  * Copyright 2008 by Howard Chu, Symas Corp.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /*
16  * This code references portions of the nss-ldapd package
17  * written by Arthur de Jong. The nss-ldapd code was forked
18  * from the nss-ldap library written by Luke Howard.
19  */
20 
21 #include "nssov.h"
22 
23 /* ( nisSchema.2.5 NAME 'oncRpc' SUP top STRUCTURAL
24  *	 DESC 'Abstraction of an Open Network Computing (ONC)
25  *				 [RFC1057] Remote Procedure Call (RPC) binding.
26  *				 This class maps an ONC RPC number to a name.
27  *				 The distinguished value of the cn attribute denotes
28  *				 the RPC service's canonical name'
29  *	 MUST ( cn $ oncRpcNumber )
30  *	 MAY description )
31  */
32 
33 /* the basic search filter for searches */
34 static struct berval rpc_filter = BER_BVC("(objectClass=oncRpc)");
35 
36 /* the attributes to request with searches */
37 static struct berval rpc_keys[] = {
38 	BER_BVC("cn"),
39 	BER_BVC("oncRpcNumber"),
40 	BER_BVNULL
41 };
42 
43 NSSOV_INIT(rpc)
44 
45 NSSOV_CBPRIV(rpc,
46 	char buf[256];
47 	struct berval name;
48 	struct berval numb;);
49 
50 /* write a single rpc entry to the stream */
51 static int write_rpc(nssov_rpc_cbp *cbp,Entry *entry)
52 {
53 	int32_t tmpint32,tmp2int32,tmp3int32;
54 	int i,numname,dupname,number;
55 	struct berval name,*names;
56 	Attribute *a;
57 	char *tmp;
58 
59 	/* get the most canonical name */
60 	nssov_find_rdnval( &entry->e_nname, cbp->mi->mi_attrs[0].an_desc, &name );
61 	/* get the other names for the rpc */
62 	a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[0].an_desc );
63 	if ( !a || !a->a_vals )
64 	{
65 		Debug(LDAP_DEBUG_ANY,"rpc entry %s does not contain %s value",
66 			entry->e_name.bv_val, cbp->mi->mi_attrs[0].an_desc->ad_cname.bv_val, 0 );
67 		return 0;
68 	}
69 	names = a->a_vals;
70 	numname = a->a_numvals;
71 	/* if the name is not yet found, get the first entry from names */
72 	if (BER_BVISNULL(&name)) {
73 		name=names[0];
74 		dupname = 0;
75 	} else {
76 		dupname = -1;
77 		for (i=0; i<numname; i++) {
78 			if ( ber_bvmatch(&name, &a->a_nvals[i])) {
79 				dupname = i;
80 				break;
81 			}
82 		}
83 	}
84 	/* get the rpc number */
85 	a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[1].an_desc );
86 	if ( !a || !a->a_vals )
87 	{
88 		Debug(LDAP_DEBUG_ANY,"rpc entry %s does not contain %s value",
89 			entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 );
90 		return 0;
91 	} else if ( a->a_numvals > 1 ) {
92 		Debug(LDAP_DEBUG_ANY,"rpc entry %s contains multiple %s values",
93 			entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 );
94 	}
95 	number=(int)strtol(a->a_vals[0].bv_val,&tmp,0);
96 	if (*tmp)
97 	{
98 		Debug(LDAP_DEBUG_ANY,"rpc entry %s contains non-numeric %s value",
99 			entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 );
100 		return 0;
101 	}
102 	/* write the entry */
103 	WRITE_INT32(cbp->fp,NSLCD_RESULT_SUCCESS);
104 	WRITE_BERVAL(cbp->fp,&name);
105 	if ( dupname >= 0 ) {
106 		WRITE_INT32(cbp->fp,numname-1);
107 	} else {
108 		WRITE_INT32(cbp->fp,numname);
109 	}
110 	for (i=0;i<numname;i++) {
111 		if (i == dupname) continue;
112 		WRITE_BERVAL(cbp->fp,&names[i]);
113 	}
114 	WRITE_INT32(cbp->fp,number);
115 	return 0;
116 }
117 
118 NSSOV_CB(rpc)
119 
120 NSSOV_HANDLE(
121 	rpc,byname,
122 	char fbuf[1024];
123     struct berval filter = {sizeof(fbuf)};
124     filter.bv_val = fbuf;
125     BER_BVZERO(&cbp.numb);
126     READ_STRING_BUF2(fp,cbp.buf,sizeof(cbp.buf));
127     cbp.name.bv_len = tmpint32;
128     cbp.name.bv_val = cbp.buf;,
129 	Debug(LDAP_DEBUG_TRACE,"nssov_rpc_byname(%s)",cbp.name.bv_val,0,0);,
130 	NSLCD_ACTION_RPC_BYNAME,
131 	nssov_filter_byname(cbp.mi,0,&cbp.name,&filter)
132 )
133 
134 NSSOV_HANDLE(
135 	rpc,bynumber,
136 	int number;
137 	char fbuf[1024];
138     struct berval filter = {sizeof(fbuf)};
139     filter.bv_val = fbuf;
140 	READ_INT32(fp,number);
141 	cbp.numb.bv_val = cbp.buf;
142 	cbp.numb.bv_len = snprintf(cbp.buf,sizeof(cbp.buf),"%d",number);
143 	BER_BVZERO(&cbp.name);,
144 	Debug(LDAP_DEBUG_TRACE,"nssov_rpc_bynumber(%d)",cbp.numb.bv_val,0,0);,
145 	NSLCD_ACTION_RPC_BYNUMBER,
146 	nssov_filter_byid(cbp.mi,1,&cbp.numb,&filter)
147 )
148 
149 NSSOV_HANDLE(
150 	rpc,all,
151 	struct berval filter;
152 	/* no parameters to read */,
153 	Debug(LDAP_DEBUG_TRACE,"nssov_rpc_all()",0,0,0);,
154 	NSLCD_ACTION_RPC_ALL,
155 	(filter=cbp.mi->mi_filter,0)
156 )
157