1/* protocol.c - network protocol lookup routines */
2/* $OpenLDAP$ */
3/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2008-2011 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/*
18/* ACKNOWLEDGEMENTS:
19 * This code references portions of the nss-ldapd package
20 * written by Arthur de Jong. The nss-ldapd code was forked
21 * from the nss-ldap library written by Luke Howard.
22 */
23
24#include "nssov.h"
25
26/* ( nisSchema.2.4 NAME 'ipProtocol' SUP top STRUCTURAL
27 *   DESC 'Abstraction of an IP protocol. Maps a protocol number
28 *         to one or more names. The distinguished value of the cn
29 *         attribute denotes the protocol's canonical name'
30 *   MUST ( cn $ ipProtocolNumber )
31 *    MAY description )
32 */
33
34/* the basic search filter for searches */
35static struct berval protocol_filter = BER_BVC("(objectClass=ipProtocol)");
36
37/* the attributes used in searches */
38static struct berval protocol_keys[] = {
39	BER_BVC("cn"),
40	BER_BVC("ipProtocolNumber"),
41	BER_BVNULL
42};
43
44NSSOV_INIT(protocol)
45
46NSSOV_CBPRIV(protocol,
47	char buf[256];
48	struct berval name;
49	struct berval numb;);
50
51static int write_protocol(nssov_protocol_cbp *cbp,Entry *entry)
52{
53	int32_t tmpint32,tmp2int32,tmp3int32;
54	int i,numname,dupname,proto;
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 protocol */
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,"protocol entry %s does not contain %s value\n",
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 ( bvmatch(&name, &a->a_nvals[i])) {
79				dupname = i;
80				break;
81			}
82		}
83	}
84	/* get the protocol 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,"protocol entry %s does not contain %s value\n",
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,"protocol entry %s contains multiple %s values\n",
93			entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 );
94	}
95	proto=(int)strtol(a->a_vals[0].bv_val,&tmp,0);
96	if (*tmp)
97	{
98		Debug(LDAP_DEBUG_ANY,"protocol entry %s contains non-numeric %s value\n",
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_BEGIN);
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,proto);
115	return 0;
116}
117
118NSSOV_CB(protocol)
119
120NSSOV_HANDLE(
121	protocol,byname,
122	char fbuf[1024];
123	struct berval filter = {sizeof(fbuf)};
124	filter.bv_val = fbuf;
125	BER_BVZERO(&cbp.numb);
126	READ_STRING(fp,cbp.buf);
127	cbp.name.bv_len = tmpint32;
128	cbp.name.bv_val = cbp.buf;,
129	Debug(LDAP_DEBUG_TRACE,"nssov_protocol_byname(%s)\n",cbp.name.bv_val,0,0);,
130	NSLCD_ACTION_PROTOCOL_BYNAME,
131	nssov_filter_byname(cbp.mi,0,&cbp.name,&filter)
132)
133
134NSSOV_HANDLE(
135	protocol,bynumber,
136	int protocol;
137	char fbuf[1024];
138	struct berval filter = {sizeof(fbuf)};
139	filter.bv_val = fbuf;
140	READ_INT32(fp,protocol);
141	cbp.numb.bv_val = cbp.buf;
142	cbp.numb.bv_len = snprintf(cbp.buf,sizeof(cbp.buf),"%d",protocol);
143	BER_BVZERO(&cbp.name);,
144	Debug(LDAP_DEBUG_TRACE,"nssov_protocol_bynumber(%s)\n",cbp.numb.bv_val,0,0);,
145	NSLCD_ACTION_PROTOCOL_BYNUMBER,
146	nssov_filter_byid(cbp.mi,1,&cbp.numb,&filter)
147)
148
149NSSOV_HANDLE(
150	protocol,all,
151	struct berval filter;
152	/* no parameters to read */,
153	Debug(LDAP_DEBUG_TRACE,"nssov_protocol_all()\n",0,0,0);,
154	NSLCD_ACTION_PROTOCOL_ALL,
155	(filter=cbp.mi->mi_filter,0)
156)
157