2
common.h - common server code routines
3
This file is part of the nss-pam-ldapd library.
5
Copyright (C) 2006 West Consulting
6
Copyright (C) 2006, 2007, 2008, 2009 Arthur de Jong
8
This library is free software; you can redistribute it and/or
9
modify it under the terms of the GNU Lesser General Public
10
License as published by the Free Software Foundation; either
11
version 2.1 of the License, or (at your option) any later version.
13
This library is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
Lesser General Public License for more details.
18
You should have received a copy of the GNU Lesser General Public
19
License along with this library; if not, write to the Free Software
20
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24
#ifndef _SERVER_COMMON_H
25
#define _SERVER_COMMON_H 1
28
#include "common/nslcd-prot.h"
29
#include "common/tio.h"
30
#include "compat/attrs.h"
33
/* macros for basic read and write operations, the following
34
ERROR_OUT* marcos define the action taken on errors
35
the stream is not closed because the caller closes the
38
#define ERROR_OUT_WRITEERROR(fp) \
39
log_log(LOG_WARNING,"error writing to client"); \
42
#define ERROR_OUT_READERROR(fp) \
43
log_log(LOG_WARNING,"error reading from client"); \
46
#define ERROR_OUT_BUFERROR(fp) \
47
log_log(LOG_WARNING,"client supplied argument too large"); \
50
/* a simple wrapper around snprintf,
51
returns 0 if ok, -1 on error */
52
int mysnprintf(char *buffer,size_t buflen,const char *format, ...)
55
/* This tries to get the user password attribute from the entry.
56
It will try to return an encrypted password as it is used in /etc/passwd,
57
/etc/group or /etc/shadow depending upon what is in the directory.
58
This function will return NULL if no passwd is found and will return the
59
literal value in the directory if conversion is not possible. */
60
const char *get_userpassword(MYLDAP_ENTRY *entry,const char *attr);
62
/* write out an address, parsing the addr value */
63
int write_address(TFILE *fp,const char *addr);
65
/* a helper macro to write out addresses and bail out on errors */
66
#define WRITE_ADDRESS(fp,addr) \
67
if (write_address(fp,addr)) \
70
/* read an address from the stream */
71
int read_address(TFILE *fp,char *addr,int *addrlen,int *af);
73
/* helper macro to read an address from the stream */
74
#define READ_ADDRESS(fp,addr,len,af) \
75
len=(int)sizeof(addr); \
76
if (read_address(fp,addr,&(len),&(af))) \
79
/* checks to see if the specified string is a valid user or group name */
80
MUST_USE int isvalidname(const char *name);
82
/* Perform an LDAP lookup to translate the DN into a uid.
83
This function either returns NULL or a strdup()ed string. */
84
MUST_USE char *lookup_dn2uid(MYLDAP_SESSION *session,const char *dn,int *rcp);
86
/* transforms the DN info a uid doing an LDAP lookup if needed */
87
MUST_USE char *dn2uid(MYLDAP_SESSION *session,const char *dn,char *buf,size_t buflen);
89
/* use the user id to lookup an LDAP entry */
90
MYLDAP_ENTRY *uid2entry(MYLDAP_SESSION *session,const char *uid);
92
/* transforms the uid into a DN by doing an LDAP lookup */
93
MUST_USE char *uid2dn(MYLDAP_SESSION *session,const char *uid,char *buf,size_t buflen);
95
/* these are the functions for initialising the database specific
97
void alias_init(void);
98
void ether_init(void);
99
void group_init(void);
100
void host_init(void);
101
void netgroup_init(void);
102
void network_init(void);
103
void passwd_init(void);
104
void protocol_init(void);
106
void service_init(void);
107
void shadow_init(void);
109
/* these are the different functions that handle the database
110
specific actions, see nslcd.h for the action descriptions */
111
int nslcd_alias_byname(TFILE *fp,MYLDAP_SESSION *session);
112
int nslcd_alias_all(TFILE *fp,MYLDAP_SESSION *session);
113
int nslcd_ether_byname(TFILE *fp,MYLDAP_SESSION *session);
114
int nslcd_ether_byether(TFILE *fp,MYLDAP_SESSION *session);
115
int nslcd_ether_all(TFILE *fp,MYLDAP_SESSION *session);
116
int nslcd_group_byname(TFILE *fp,MYLDAP_SESSION *session);
117
int nslcd_group_bygid(TFILE *fp,MYLDAP_SESSION *session);
118
int nslcd_group_bymember(TFILE *fp,MYLDAP_SESSION *session);
119
int nslcd_group_all(TFILE *fp,MYLDAP_SESSION *session);
120
int nslcd_host_byname(TFILE *fp,MYLDAP_SESSION *session);
121
int nslcd_host_byaddr(TFILE *fp,MYLDAP_SESSION *session);
122
int nslcd_host_all(TFILE *fp,MYLDAP_SESSION *session);
123
int nslcd_netgroup_byname(TFILE *fp,MYLDAP_SESSION *session);
124
int nslcd_network_byname(TFILE *fp,MYLDAP_SESSION *session);
125
int nslcd_network_byaddr(TFILE *fp,MYLDAP_SESSION *session);
126
int nslcd_network_all(TFILE *fp,MYLDAP_SESSION *session);
127
int nslcd_passwd_byname(TFILE *fp,MYLDAP_SESSION *session,uid_t calleruid);
128
int nslcd_passwd_byuid(TFILE *fp,MYLDAP_SESSION *session,uid_t calleruid);
129
int nslcd_passwd_all(TFILE *fp,MYLDAP_SESSION *session,uid_t calleruid);
130
int nslcd_protocol_byname(TFILE *fp,MYLDAP_SESSION *session);
131
int nslcd_protocol_bynumber(TFILE *fp,MYLDAP_SESSION *session);
132
int nslcd_protocol_all(TFILE *fp,MYLDAP_SESSION *session);
133
int nslcd_rpc_byname(TFILE *fp,MYLDAP_SESSION *session);
134
int nslcd_rpc_bynumber(TFILE *fp,MYLDAP_SESSION *session);
135
int nslcd_rpc_all(TFILE *fp,MYLDAP_SESSION *session);
136
int nslcd_service_byname(TFILE *fp,MYLDAP_SESSION *session);
137
int nslcd_service_bynumber(TFILE *fp,MYLDAP_SESSION *session);
138
int nslcd_service_all(TFILE *fp,MYLDAP_SESSION *session);
139
int nslcd_shadow_byname(TFILE *fp,MYLDAP_SESSION *session);
140
int nslcd_shadow_all(TFILE *fp,MYLDAP_SESSION *session);
141
int nslcd_pam_authc(TFILE *fp,MYLDAP_SESSION *session);
142
int nslcd_pam_authz(TFILE *fp,MYLDAP_SESSION *session);
143
int nslcd_pam_sess_o(TFILE *fp,MYLDAP_SESSION *session);
144
int nslcd_pam_sess_c(TFILE *fp,MYLDAP_SESSION *session);
145
int nslcd_pam_pwmod(TFILE *fp,MYLDAP_SESSION *session);
147
/* macros for generating service handling code */
148
#define NSLCD_HANDLE(db,fn,readfn,logcall,action,mkfilter,writefn) \
149
NSLCD_HANDLE_PARAMS(db,fn,,readfn,logcall,action,mkfilter,writefn)
151
#define NSLCD_HANDLE_UID(db,fn,readfn,logcall,action,mkfilter,writefn) \
152
NSLCD_HANDLE_PARAMS(db,fn,COMMA uid_t calleruid,readfn,logcall,action,mkfilter,writefn)
153
#define NSLCD_HANDLE_PARAMS(db,fn,params,readfn,logcall,action,mkfilter,writefn) \
154
int nslcd_##db##_##fn(TFILE *fp,MYLDAP_SESSION *session params ) \
156
/* define common variables */ \
158
MYLDAP_SEARCH *search; \
159
MYLDAP_ENTRY *entry; \
162
/* read request parameters */ \
166
/* write the response header */ \
167
WRITE_INT32(fp,NSLCD_VERSION); \
168
WRITE_INT32(fp,action); \
169
/* prepare the search filter */ \
172
log_log(LOG_WARNING,"nslcd_" __STRING(db) "_" __STRING(fn) "(): filter buffer too small"); \
175
/* perform a search for each search base */ \
176
for (i=0; (base=db##_bases[i])!=NULL; i++) \
178
/* do the LDAP search */ \
179
if ((search=myldap_search(session,base,db##_scope,filter,db##_attrs))==NULL) \
181
/* go over results */ \
182
while ((entry=myldap_get_entry(search,&rc))!=NULL) \
188
/* write the final result code */ \
189
if (rc==LDAP_SUCCESS) \
191
WRITE_INT32(fp,NSLCD_RESULT_END); \
196
#endif /* not _SERVER_COMMON_H */