summaryrefslogtreecommitdiffstats
path: root/lib/libutil/login_auth.c
blob: e54a5dc45c029d936dbfade6aaf8a777ba73b481 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*-
 * Copyright (c) 1996 by
 * Sean Eric Fagan <sef@kithrup.com>
 * David Nugent <davidn@blaze.net.au>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, is permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice immediately at the beginning of the file, without modification,
 *    this list of conditions, and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. This work was done expressly for inclusion into FreeBSD.  Other use
 *    is permitted provided this notation is included.
 * 4. Absolutely no warranty of function or purpose is made by the authors.
 * 5. Modifications may be freely made to this file providing the above
 *    conditions are met.
 *
 * Low-level routines relating to the user capabilities database
 *
 *	$FreeBSD$
 */

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <login_cap.h>
#include <stdarg.h>
#include <paths.h>
#include <sys/wait.h>

extern char *fgetline(FILE *, int*);

#ifdef RLIM_LONG
# define STRTOV strtol
#else
# define STRTOV strtoq
#endif

#define AUTHMAXLINES  1024
#define AUTHMAXARGS   16

struct auth_info {
  int reject;
  int auths;
  int env_count;
  char **env;
  int file_count;
  char **files;
};

static struct auth_info auth_info;

/*
 * free_auth_info()
 * Go through the auth_info structure, and free() anything of interest.
 * This includes the string arrays, and any individual element.
 * All part of being environmentally conscious ;).
 */

static void
free_auth_info(void)
{
  int i;
  char *ptr;

  auth_info.reject = 0;
  auth_info.auths = 0;
  if (auth_info.env) {
    for (i = 0; i < auth_info.env_count; i++) {
      if (auth_info.env[i])
	free(auth_info.env[i]);
    }
    free(auth_info.env);
    auth_info.env = NULL;
  }
  if (auth_info.files) {
    for (i = 0; i < auth_info.file_count; i++) {
      if (auth_info.files[i])
	free(auth_info.files[i]);
    }
    free(auth_info.files);
    auth_info.files = NULL;
  }
}


/*
 * collect_info()
 * Read from <fd>, a list of authorization commands.
 * These commands are:
 *	reject
 *	authorize [root|secure]
 *	setenv <name>[ <value>]
 *	remove <file>
 * A single reject means the entire thing is bad;
 * multiple authorize statements can be present (it would be
 * silly, but that's what the spec says).
 * The commands are collected, and are accted upon by:
 *	auth_scan()	-- check for authorization or rejection
 *	auth_rmfiles()	-- remove the specified files
 *	auth_env()	-- set the specified environment variables
 * We only get up to AUTHMAXLINES lines of input from the program.
 */
#define STRSIZEOF(x)  (sizeof(x)-1)
static void
collect_info(int fd)
{
  char *line;
  FILE *fp;
  char *ptr;
  int len;
  int line_count = 0;

  fp = fdopen(fd, "r");

  while ((line = fgetline(fp, &len)) != NULL) {
    if (++line_count > AUTHMAXLINES)
      break;
    if (strncasecmp(line, BI_REJECT, STRSIZEOF(BI_REJECT)) == 0) {
      auth_info.reject = 1;
    } else if (strncasecmp(line, BI_AUTH, STRSIZEOF(BI_AUTH)) == 0) {
      ptr = line + STRSIZEOF(BI_AUTH);
      ptr += strspn(ptr, " \t");
      if (!*ptr)
	auth_info.auths |= AUTH_OKAY;
      else if (strncasecmp(ptr, BI_ROOTOKAY, STRSIZEOF(BI_ROOTOKAY)) == 0)
	auth_info.auths |= AUTH_ROOTOKAY;
      else if (strncasecmp(ptr, BI_SECURE, STRSIZEOF(BI_SECURE)) == 0)
	auth_info.auths |= AUTH_SECURE;
    } else if (strncasecmp(line, BI_SETENV, STRSIZEOF(BI_SETENV)) == 0) {
      ptr = line + STRSIZEOF(BI_SETENV);
      ptr += strspn(ptr, " \t");
      if (*ptr) {
	char **tmp = realloc(auth_info.env, sizeof(char*) * (auth_info.env_count + 1));
	if (tmp != NULL) {
	  auth_info.env = tmp;
	  if ((auth_info.env[auth_info.env_count] = strdup(ptr)) != NULL)
	    auth_info.env_count++;
	}
      }
    } else if (strncasecmp(line, BI_REMOVE, STRSIZEOF(BI_REMOVE)) == 0) {
      ptr = line + STRSIZEOF(BI_REMOVE);
      ptr += strspn(ptr, " \t");
      if (*ptr) {
	char **tmp = realloc(auth_info.files, sizeof(char*) * (auth_info.file_count + 1));
	if (tmp != NULL) {
	  auth_info.files = tmp;
	  if ((auth_info.files[auth_info.file_count] = strdup(ptr)) != NULL)
	    auth_info.file_count++;
	}
      }
    }
  }
  fclose(fp);
}

      
/*
 * authenticate()
 * Starts an auth_script() for the given <user>, with a class <class>,
 * style <style>, and service <service>.  <style> is necessary,
 * as are <user> and <class>, but <service> is optional -- it defaults
 * to "login".
 * Since auth_script() expects an execl'able program name, authenticate()
 * also concatenates <style> to _PATH_AUTHPROG.
 * Lastly, calls auth_scan(AUTH_NONE) to see if there are any "reject" statements,
 * or lack of "auth" statements.
 * Returns -1 on error, 0 on rejection, and >0 on success.
 * (See AUTH_* for the return values.)
 *
 */
int
authenticate(const char * name, const char * class, const char * style, const char *service)
{
  int retval;

  if (style == NULL || *style == '\0')
    retval = -1;
  else {
    char buf[sizeof(_PATH_AUTHPROG) + 64];

    if (service == NULL || *service == '\0')
      service = LOGIN_DEFSERVICE;

    free_auth_info();

    if (snprintf(buf, sizeof buf, _PATH_AUTHPROG "%s", style) >= sizeof buf)
      retval = -1;
    else {
      retval = auth_script(buf, style, "-s", service, name, class, NULL);
      if (retval >= 0)
	retval = auth_scan(AUTH_NONE);
    }
  }
  return retval;
}


/*
 * auth_script()
 * Runs an authentication program with specified arguments.
 * It sets up file descriptor 3 for the program to write to;
 * it stashes the output somewhere.  The output of the program
 * consists of statements:
 *	reject
 *	authorize [root|secure]
 *	setenv <name> [<value>]
 *	remove <file>
 *
 * Terribly exciting, isn't it?  There is no limit specified in
 * BSDi's API for how much output can be present, but we should
 * keep it fairly small, I think.
 * No more than AUTHMAXLINES lines.
 */

int
auth_script(const char * path, ...)
{
  va_list ap;
  int pid, status;
  int argc = 0;
  int p[2];	/* pipes */
  char *argv[AUTHMAXARGS+1];

  va_start(ap, path);
  while (argc < AUTHMAXARGS && (argv[argc++] = va_arg(ap, char*)) != NULL)
    ;
  argv[argc] = NULL;
  va_end(ap);

  fflush(NULL);

  if (pipe(p) >= 0) {
    if ((pid = fork()) == -1) {
      close(p[0]);
      close(p[1]);
    } else if (pid == 0) {    /* Child */
      close(p[0]);
      dup2(p[1], 3);
      if (setenv("PATH", _PATH_DEFPATH, 1)==0 && setenv("SHELL", _PATH_BSHELL, 1)==0)
	execv(path, argv);
      _exit(1);
    } else {
      close(p[1]);
      collect_info(p[0]);
      if (waitpid(pid, &status, 0) != -1 && WIFEXITED(status) && !WEXITSTATUS(status))
	return 0;
    }
  }
  return -1;
}


/*
 * auth_env()
 * Processes the stored "setenv" lines from the stored authentication
 * output.
 */

int
auth_env(void)
{
  int i;

  for (i = 0; i < auth_info.env_count; i++) {
    char *nam = auth_info.env[i];
    char *ptr = nam + strcspn(nam, " \t=");
    if (*ptr) {
      *ptr++ = '\0';
      ptr += strspn(ptr, " \t");
    }
    setenv(nam, ptr, 1);
  }
}


/*
 * auth_scan()
 * Goes through the output of the auth_script/authenticate, and
 * checks for a failure or authentication.
 * <ok> is a default authentication value -- if there are no
 * rejection or authentication statements, then it is returned
 * unmodified.
 * AUTH_NONE is returned if there were any reject statements
 * from the authentication program (invoked by auth_script()), and
 * AUTH, AUTH_ROOTOKAY, and/or AUTH_SECURE are returned if the
 * appropriate directives were found.  Note that AUTH* are
 * *bitmasks*!
 */

int
auth_scan(int ok)
{
  if (auth_info.reject)
    return 0;
  return ok | auth_info.auths;
}


/*
 * auth_rmfiles()
 * Removes any files that the authentication program said needed to be
 * removed, said files having come from a previous execution of
 * auth_script().
 */

int
auth_rmfiles(void)
{
  int i = auth_info.file_count;
  while (i-- > 0) {
    unlink(auth_info.files[i]);
    free(auth_info.files[i]);
    auth_info.files[i] = NULL;
  }
}


/*
 * auth_checknologin()
 * Checks for the existance of a nologin file in the login_cap
 * capability <lc>.  If there isn't one specified, then it checks
 * to see if this class should just ignore nologin files.  Lastly,
 * it tries to print out the default nologin file, and, if such
 * exists, it exits.
 */

void
auth_checknologin(login_cap_t *lc)
{
  char *file;

  /* Do we ignore a nologin file? */
  if (login_getcapbool(lc, "ignorenologin", 0))
    return;

  /* Note that <file> will be "" if there is no nologin capability */
  if ((file = login_getcapstr(lc, "nologin", "", NULL)) == NULL)
    exit(1);

  /*
   * *file is true IFF there was a "nologin" capability
   * Note that auth_cat() returns 1 only if the specified
   * file exists, and is readable.  E.g., /.nologin exists.
   */
  if ((*file && auth_cat(file)) || auth_cat(_PATH_NOLOGIN))
    exit(1);
}


/*
 * auth_cat()
 * Checks for the readability of <file>; if it can be opened for
 * reading, it prints it out to stdout, and then exits.  Otherwise,
 * it returns 0 (meaning no nologin file).
 */
int
auth_cat(const char *file)
{
  int fd, count;
  char buf[BUFSIZ];

  if ((fd = open(file, O_RDONLY)) < 0)
    return 0;
  while ((count = read(fd, buf, sizeof(buf))) > 0)
    write(fileno(stdout), buf, count);
  close(fd);
  return 1;
}
OpenPOWER on IntegriCloud