1 /*	$NetBSD: protocol.c,v 1.1.1.3 2010/12/12 15:19:10 adam Exp $	*/
2 
3 /* protocol.c - network protocol lookup routines */
4 /* OpenLDAP: pkg/ldap/contrib/slapd-modules/nssov/protocol.c,v 1.1.2.6 2010/04/15 21:32:57 quanah Exp */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 2008-2010 The OpenLDAP Foundation.
8  * Portions Copyright 2008 by Howard Chu, Symas Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted only as authorized by the OpenLDAP
13  * Public License.
14  *
15  * A copy of this license is available in the file LICENSE in the
16  * top-level directory of the distribution or, alternatively, at
17  * <http://www.OpenLDAP.org/license.html>.
18  */
19 /*
20 /* ACKNOWLEDGEMENTS:
21  * This code references portions of the nss-ldapd package
22  * written by Arthur de Jong. The nss-ldapd code was forked
23  * from the nss-ldap library written by Luke Howard.
24  */
25 
26 #include "nssov.h"
27 
28 /* ( nisSchema.2.4 NAME 'ipProtocol' SUP top STRUCTURAL
29  *   DESC 'Abstraction of an IP protocol. Maps a protocol number
30  *         to one or more names. The distinguished value of the cn
31  *         attribute denotes the protocol's canonical name'
32  *   MUST ( cn $ ipProtocolNumber )
33  *    MAY description )
34  */
35 
36 /* the basic search filter for searches */
37 static struct berval protocol_filter = BER_BVC("(objectClass=ipProtocol)");
38 
39 /* the attributes used in searches */
40 static struct berval protocol_keys[] = {
41 	BER_BVC("cn"),
42 	BER_BVC("ipProtocolNumber"),
43 	BER_BVNULL
44 };
45 
46 NSSOV_INIT(protocol)
47 
48 NSSOV_CBPRIV(protocol,
49 	char buf[256];
50 	struct berval name;
51 	struct berval numb;);
52 
53 static int write_protocol(nssov_protocol_cbp *cbp,Entry *entry)
54 {
55 	int32_t tmpint32,tmp2int32,tmp3int32;
56 	int i,numname,dupname,proto;
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 protocol */
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,"protocol 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, 0 );
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 protocol 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,"protocol 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, 0 );
92 		return 0;
93 	} else if ( a->a_numvals > 1 ) {
94 		Debug(LDAP_DEBUG_ANY,"protocol entry %s contains multiple %s values\n",
95 			entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 );
96 	}
97 	proto=(int)strtol(a->a_vals[0].bv_val,&tmp,0);
98 	if (*tmp)
99 	{
100 		Debug(LDAP_DEBUG_ANY,"protocol 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, 0 );
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,proto);
117 	return 0;
118 }
119 
120 NSSOV_CB(protocol)
121 
122 NSSOV_HANDLE(
123 	protocol,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_protocol_byname(%s)\n",cbp.name.bv_val,0,0);,
132 	NSLCD_ACTION_PROTOCOL_BYNAME,
133 	nssov_filter_byname(cbp.mi,0,&cbp.name,&filter)
134 )
135 
136 NSSOV_HANDLE(
137 	protocol,bynumber,
138 	int protocol;
139 	char fbuf[1024];
140 	struct berval filter = {sizeof(fbuf)};
141 	filter.bv_val = fbuf;
142 	READ_INT32(fp,protocol);
143 	cbp.numb.bv_val = cbp.buf;
144 	cbp.numb.bv_len = snprintf(cbp.buf,sizeof(cbp.buf),"%d",protocol);
145 	BER_BVZERO(&cbp.name);,
146 	Debug(LDAP_DEBUG_TRACE,"nssov_protocol_bynumber(%s)\n",cbp.numb.bv_val,0,0);,
147 	NSLCD_ACTION_PROTOCOL_BYNUMBER,
148 	nssov_filter_byid(cbp.mi,1,&cbp.numb,&filter)
149 )
150 
151 NSSOV_HANDLE(
152 	protocol,all,
153 	struct berval filter;
154 	/* no parameters to read */,
155 	Debug(LDAP_DEBUG_TRACE,"nssov_protocol_all()\n",0,0,0);,
156 	NSLCD_ACTION_PROTOCOL_ALL,
157 	(filter=cbp.mi->mi_filter,0)
158 )
159