summaryrefslogtreecommitdiff
path: root/nslcd/common.h
blob: 5658fa7c0c11e1ea32bfa7787bb6e09bdcf15fd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
   common.h - common server code routines
   This file is part of the nss-pam-ldapd library.

   Copyright (C) 2006 West Consulting
   Copyright (C) 2006-2014 Arthur de Jong

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301 USA
*/

#ifndef NSLCD__COMMON_H
#define NSLCD__COMMON_H 1

#include <errno.h>
#include <limits.h>
#include <string.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif /* HAVE_STDINT_H */
#include <sys/types.h>

#include "nslcd.h"
#include "common/nslcd-prot.h"
#include "common/tio.h"
#include "compat/attrs.h"
#include "cfg.h"
#include "hackers_watch.h"

#define NSS_MODULE_SONAME           NSS_LDAP_SONAME
#define NSS_MODULE_NAME             "ldap"
#define NSS_MODULE_ID_VERSION       "_nss_ldap_version"
#define NSS_MODULE_ID_ENABLELOOKUPS "_nss_ldap_enablelookups"

/* macros for basic read and write operations, the following
   ERROR_OUT* marcos define the action taken on errors
   the stream is not closed because the caller closes the
   stream */

#define ERROR_OUT_WRITEERROR(fp)                                            \
  do {                                                                      \
    if (errno == EPIPE)                                                     \
      log_log(LOG_DEBUG, "error writing to client: %s", strerror(errno));   \
    else                                                                    \
      log_log(LOG_WARNING, "error writing to client: %s", strerror(errno)); \
    return -1;                                                              \
  } while(0)

#define ERROR_OUT_READERROR(fp)                                             \
  do {                                                                      \
    log_log(LOG_WARNING, "error reading from client: %s", strerror(errno)); \
    return -1;                                                              \
  } while(0)

#define ERROR_OUT_BUFERROR(fp)                                              \
  do {                                                                      \
    log_log(LOG_ERR, "client supplied argument %d bytes too large",         \
            tmpint32);                                                      \
    return -1;                                                              \
  } while(0)

/* a simple wrapper around snprintf,
   returns 0 if ok, -1 on error */
int mysnprintf(char *buffer, size_t buflen, const char *format, ...)
  LIKE_PRINTF(3, 4);

/* get a name of a signal with a given signal number */
const char *signame(int signum);

/* checks to see if the specified string is a valid user or group name */
MUST_USE int isvalidname(const char *name);

/* check whether the nsswitch file should be reloaded */
void nsswitch_check_reload(void);

/* check whether the nsswitch.conf file has NSLCD as a naming source for db */
int nsswitch_shadow_uses_nslcd(void);

/* start a child process that holds onto the original privileges with the
   purpose of running external cache invalidation commands */
int invalidator_start(void);

/* signal invalidator to invalidate the selected external cache */
void invalidator_do(enum nss_map_selector map);

/* common buffer lengths */
#define BUFLEN_NAME         256  /* user, group names and such */
#define BUFLEN_PASSWORD     128  /* passwords */
#define BUFLEN_MESSAGE     1024  /* message strings */

/* these are the different functions that handle the database
   specific actions, see nslcd.h for the action descriptions */
#include "dispatch.h"

/* macros for generating service handling code */
#define NSLCD_HANDLE(db, fn, fndecls, fnread, fncheck, tentry, fnsearch, fnwrite, fnclean) \
  int nslcd_##db##_##fn(TFILE *fp, struct session *session)                 \
  NSLCD_HANDLE_BODY(db, fn, fndecls, fnread, fncheck, tentry, fnsearch, fnwrite, fnclean)
#define NSLCD_HANDLE_UID(db, fn, fndecls, fnread, fncheck, tentry, fnsearch, fnwrite, fnclean) \
  int nslcd_##db##_##fn(TFILE *fp, struct session *session, uid_t calleruid) \
  NSLCD_HANDLE_BODY(db, fn, fndecls, fnread, fncheck, tentry, fnsearch, fnwrite, fnclean)
#define NSLCD_HANDLE_BODY(db, fn, fndecls, fnread, fncheck, tentry, fnsearch, fnwrite, fnclean) \
  {                                                                         \
    /* define common variables */                                           \
    tentry *entry = NULL;                                                   \
    int rc = 1;                                                             \
    fndecls                                                                 \
    tentry *search(int *rcp) { fnsearch }                                   \
    int write(TFILE *fp, tentry *entry) { fnwrite }                         \
    void clean() { fnclean }                                                \
    /* read request parameters */                                           \
    fnread                                                                  \
    /* validate request parameters */                                       \
    fncheck                                                                 \
    /* write the response header */                                         \
    WRITE_INT32(fp, NSLCD_VERSION);                                         \
    WRITE_INT32(fp, NSLCD_ACTION_##db##_##fn);                              \
    /* go over results */                                                   \
    while ((entry = search(&rc)) != NULL)                                   \
    {                                                                       \
      if ( write(fp, entry) ) {                                             \
        clean();                                                            \
        return -1;                                                          \
      }                                                                     \
    }                                                                       \
    /* write the final result code */                                       \
    if (rc == 0)                                                            \
    {                                                                       \
      WRITE_INT32(fp, NSLCD_RESULT_END);                                    \
    }                                                                       \
    clean();                                                                \
    return 0;                                                               \
  }

/* macro to compare strings which uses the ignorecase config option to
   determine whether or not to do a case-sensitive match */
#define STR_CMP(str1, str2)                                                 \
  (nslcd_cfg->ignorecase == 1 ?                                             \
    strcasecmp(str1, str2) : strcmp(str1, str2))

#endif /* not NSLCD__COMMON_H */