summaryrefslogtreecommitdiff
path: root/nslcd/common.c
blob: da508f6674ff1971e7279c29716a2d56e67f4bd2 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
   common.c - common server code routines
   This file is part of the nss-pam-ldapd library.

   Copyright (C) 2006 West Consulting
   Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 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
*/

#include "config.h"

#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>
#include <limits.h>
#include <netdb.h>
#include <string.h>
#include <regex.h>
#include <stdlib.h>
#include <signal.h>

#include "nslcd.h"
#include "common.h"
#include "log.h"
#include "cfg.h"

/* simple wrapper around snptintf() to return non-zero in case
   of any failure (but always keep string 0-terminated) */
int mysnprintf(char *buffer, size_t buflen, const char *format, ...)
{
  int res;
  va_list ap;
  /* do snprintf */
  va_start(ap, format);
  res = vsnprintf(buffer, buflen, format, ap);
  va_end(ap);
  /* NULL-terminate the string just to be on the safe side */
  buffer[buflen - 1] = '\0';
  /* check if the string was completely written */
  return ((res < 0) || (((size_t)res) >= buflen));
}

/* get a name of a signal with a given signal number */
const char *signame(int signum)
{
  switch (signum)
  {
    case SIGHUP:  return "SIGHUP";  /* Hangup detected */
    case SIGINT:  return "SIGINT";  /* Interrupt from keyboard */
    case SIGQUIT: return "SIGQUIT"; /* Quit from keyboard */
    case SIGILL:  return "SIGILL";  /* Illegal Instruction */
    case SIGABRT: return "SIGABRT"; /* Abort signal from abort(3) */
    case SIGFPE:  return "SIGFPE";  /* Floating point exception */
    case SIGKILL: return "SIGKILL"; /* Kill signal */
    case SIGSEGV: return "SIGSEGV"; /* Invalid memory reference */
    case SIGPIPE: return "SIGPIPE"; /* Broken pipe */
    case SIGALRM: return "SIGALRM"; /* Timer signal from alarm(2) */
    case SIGTERM: return "SIGTERM"; /* Termination signal */
    case SIGUSR1: return "SIGUSR1"; /* User-defined signal 1 */
    case SIGUSR2: return "SIGUSR2"; /* User-defined signal 2 */
    case SIGCHLD: return "SIGCHLD"; /* Child stopped or terminated */
    case SIGCONT: return "SIGCONT"; /* Continue if stopped */
    case SIGSTOP: return "SIGSTOP"; /* Stop process */
    case SIGTSTP: return "SIGTSTP"; /* Stop typed at tty */
    case SIGTTIN: return "SIGTTIN"; /* tty input for background process */
    case SIGTTOU: return "SIGTTOU"; /* tty output for background process */
#ifdef SIGBUS
    case SIGBUS:  return "SIGBUS";  /* Bus error */
#endif
#ifdef SIGPOLL
    case SIGPOLL: return "SIGPOLL"; /* Pollable event */
#endif
#ifdef SIGPROF
    case SIGPROF: return "SIGPROF"; /* Profiling timer expired */
#endif
#ifdef SIGSYS
    case SIGSYS:  return "SIGSYS";  /* Bad argument to routine */
#endif
#ifdef SIGTRAP
    case SIGTRAP: return "SIGTRAP"; /* Trace/breakpoint trap */
#endif
#ifdef SIGURG
    case SIGURG:  return "SIGURG";  /* Urgent condition on socket */
#endif
#ifdef SIGVTALRM
    case SIGVTALRM: return "SIGVTALRM"; /* Virtual alarm clock */
#endif
#ifdef SIGXCPU
    case SIGXCPU: return "SIGXCPU"; /* CPU time limit exceeded */
#endif
#ifdef SIGXFSZ
    case SIGXFSZ: return "SIGXFSZ"; /* File size limit exceeded */
#endif
    default:      return "UNKNOWN";
  }
}

/* Checks if the specified name seems to be a valid user or group name. */
int isvalidname(const char *name)
{
  return regexec(&nslcd_cfg->validnames, name, 0, NULL, 0) == 0;
}

/* convert the provided string representation of a sid
   (e.g. S-1-5-21-1936905831-823966427-12391542-23578)
   to a format that can be used to search the objectSid property with */
char *sid2search(const char *sid)
{
  const char *tmpsid = sid;
  char *res, *tmp;
  int i = 0;
  long int l;
  /* check the beginning of the string */
  if (strncasecmp(sid, "S-", 2) != 0)
  {
    log_log(LOG_ERR, "error in SID %s", sid);
    exit(EXIT_FAILURE);
  }
  /* count the number of dashes in the sid */
  while (tmpsid != NULL)
  {
    i++;
    tmpsid = strchr(tmpsid + 1, '-');
  }
  i -= 2; /* number of security ids plus one because we add the uid later */
  /* allocate memory */
  res = malloc(3 + 3 + 6 * 3 + i * 4 * 3 + 1);
  if (res == NULL)
  {
    log_log(LOG_CRIT, "malloc() failed to allocate memory");
    exit(1);
  }
  /* build the first part */
  l = strtol(sid + 2, &tmp, 10);
  sprintf(res, "\\%02x\\%02x", (int)l & 0xff, (int)i);
  /* build authority part (we only handle 32 of the 48 bits) */
  l = strtol(tmp + 1, &tmp, 10);
  sprintf(res + strlen(res), "\\00\\00\\%02x\\%02x\\%02x\\%02x",
          (int)((l >> 24) & 0xff), (int)((l >> 16) & 0xff),
          (int)((l >> 8) & 0xff), (int)(l & 0xff));
  /* go over the rest of the bits */
  while (*tmp != '\0')
  {
    l = strtol(tmp + 1, &tmp, 10);
    sprintf(res + strlen(res), "\\%02x\\%02x\\%02x\\%02x",
            (int)(l & 0xff), (int)((l >> 8) & 0xff), (int)((l >> 16) & 0xff),
            (int)((l >> 24) & 0xff));
  }
  return res;
}

/* return the last security identifier of the binary sid */
long int binsid2id(const char *binsid)
{
  int i;
  /* find the position of the last security id */
  i = 2 + 6 + ((((int)binsid[1]) & 0xff) - 1) * 4;
  return (((long int)binsid[i]) & 0xff) |
         ((((long int)binsid[i + 1]) & 0xff) << 8) |
         ((((long int)binsid[i + 2]) & 0xff) << 16) |
         ((((long int)binsid[i + 3]) & 0xff) << 24);
}

#ifdef WANT_STRTOUI
/* provide a strtoui() implementation, similar to strtoul() but returning
   an range-checked unsigned int instead */
unsigned int strtoui(const char *nptr, char **endptr, int base)
{
  unsigned long val;
  val = strtoul(nptr, endptr, base);
  if (val > UINT_MAX)
  {
    errno = ERANGE;
    return UINT_MAX;
  }
  /* If errno was set by strtoul, we'll pass it back as-is */
  return (unsigned int)val;
}
#endif /* WANT_STRTOUI */