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