xref: /netbsd-src/external/bsd/openldap/dist/contrib/slapd-modules/nssov/protocol.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /* protocol.c - network protocol lookup routines */
2 /* $OpenLDAP: pkg/ldap/contrib/slapd-modules/nssov/protocol.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.4 NAME 'ipProtocol' SUP top STRUCTURAL
24  *   DESC 'Abstraction of an IP protocol. Maps a protocol number
25  *         to one or more names. The distinguished value of the cn
26  *         attribute denotes the protocol's canonical name'
27  *   MUST ( cn $ ipProtocolNumber )
28  *    MAY description )
29  */
30 
31 /* the basic search filter for searches */
32 static struct berval protocol_filter = BER_BVC("(objectClass=ipProtocol)");
33 
34 /* the attributes used in searches */
35 static struct berval protocol_keys[] = {
36 	BER_BVC("cn"),
37 	BER_BVC("ipProtocolNumber"),
38 	BER_BVNULL
39 };
40 
41 NSSOV_INIT(protocol)
42 
43 NSSOV_CBPRIV(protocol,
44 	char buf[256];
45 	struct berval name;
46 	struct berval numb;);
47 
48 static int write_protocol(nssov_protocol_cbp *cbp,Entry *entry)
49 {
50 	int32_t tmpint32,tmp2int32,tmp3int32;
51 	int i,numname,dupname,proto;
52 	struct berval name,*names;
53 	Attribute *a;
54 	char *tmp;
55 
56 	/* get the most canonical name */
57 	nssov_find_rdnval( &entry->e_nname, cbp->mi->mi_attrs[0].an_desc, &name );
58 	/* get the other names for the protocol */
59 	a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[0].an_desc );
60 	if ( !a || !a->a_vals )
61 	{
62 		Debug(LDAP_DEBUG_ANY,"protocol entry %s does not contain %s value",
63 			entry->e_name.bv_val, cbp->mi->mi_attrs[0].an_desc->ad_cname.bv_val, 0 );
64 		return 0;
65 	}
66 	names = a->a_vals;
67 	numname = a->a_numvals;
68 	/* if the name is not yet found, get the first entry from names */
69 	if (BER_BVISNULL(&name)) {
70 		name=names[0];
71 		dupname = 0;
72 	} else {
73 		dupname = -1;
74 		for (i=0; i<numname; i++) {
75 			if ( ber_bvmatch(&name, &a->a_nvals[i])) {
76 				dupname = i;
77 				break;
78 			}
79 		}
80 	}
81 	/* get the protocol number */
82 	a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[1].an_desc );
83 	if ( !a || !a->a_vals )
84 	{
85 		Debug(LDAP_DEBUG_ANY,"protocol entry %s does not contain %s value",
86 			entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 );
87 		return 0;
88 	} else if ( a->a_numvals > 1 ) {
89 		Debug(LDAP_DEBUG_ANY,"protocol entry %s contains multiple %s values",
90 			entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 );
91 	}
92 	proto=(int)strtol(a->a_vals[0].bv_val,&tmp,0);
93 	if (*tmp)
94 	{
95 		Debug(LDAP_DEBUG_ANY,"protocol entry %s contains non-numeric %s value",
96 			entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 );
97 		return 0;
98 	}
99 	/* write the entry */
100 	WRITE_INT32(cbp->fp,NSLCD_RESULT_SUCCESS);
101 	WRITE_BERVAL(cbp->fp,&name);
102 	if ( dupname >= 0 ) {
103 		WRITE_INT32(cbp->fp,numname-1);
104 	} else {
105 		WRITE_INT32(cbp->fp,numname);
106 	}
107 	for (i=0;i<numname;i++) {
108 		if (i == dupname) continue;
109 		WRITE_BERVAL(cbp->fp,&names[i]);
110 	}
111 	WRITE_INT32(cbp->fp,proto);
112 	return 0;
113 }
114 
115 NSSOV_CB(protocol)
116 
117 NSSOV_HANDLE(
118 	protocol,byname,
119 	char fbuf[1024];
120 	struct berval filter = {sizeof(fbuf)};
121 	filter.bv_val = fbuf;
122 	BER_BVZERO(&cbp.numb);
123 	READ_STRING_BUF2(fp,cbp.buf,sizeof(cbp.buf));
124 	cbp.name.bv_len = tmpint32;
125 	cbp.name.bv_val = cbp.buf;,
126 	Debug(LDAP_DEBUG_TRACE,"nssov_protocol_byname(%s)",cbp.name.bv_val,0,0);,
127 	NSLCD_ACTION_PROTOCOL_BYNAME,
128 	nssov_filter_byname(cbp.mi,0,&cbp.name,&filter)
129 )
130 
131 NSSOV_HANDLE(
132 	protocol,bynumber,
133 	int protocol;
134 	char fbuf[1024];
135 	struct berval filter = {sizeof(fbuf)};
136 	filter.bv_val = fbuf;
137 	READ_INT32(fp,protocol);
138 	cbp.numb.bv_val = cbp.buf;
139 	cbp.numb.bv_len = snprintf(cbp.buf,sizeof(cbp.buf),"%d",protocol);
140 	BER_BVZERO(&cbp.name);,
141 	Debug(LDAP_DEBUG_TRACE,"nssov_protocol_bynumber(%s)",cbp.numb.bv_val,0,0);,
142 	NSLCD_ACTION_PROTOCOL_BYNUMBER,
143 	nssov_filter_byid(cbp.mi,1,&cbp.numb,&filter)
144 )
145 
146 NSSOV_HANDLE(
147 	protocol,all,
148 	struct berval filter;
149 	/* no parameters to read */,
150 	Debug(LDAP_DEBUG_TRACE,"nssov_protocol_all()",0,0,0);,
151 	NSLCD_ACTION_PROTOCOL_ALL,
152 	(filter=cbp.mi->mi_filter,0)
153 )
154