Blame view

libmu_auth/radius.c 12.3 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2 3
   Copyright (C) 2005, 2007, 2008, 2009, 2010 Free Software Foundation,
   Inc.
4 5 6 7

   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
8
   version 3 of the License, or (at your option) any later version.
9 10 11 12 13 14 15

   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
16 17
   Public License along with this library.  If not, see
   <http://www.gnu.org/licenses/>. */
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif

#include <mailutils/list.h>
#include <mailutils/iterator.h>
#include <mailutils/mailbox.h>
37
#include <mailutils/radius.h>
38
#include <mailutils/wordsplit.h>
39 40 41 42
#include <mailutils/mu_auth.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/nls.h>
43
#include <mailutils/io.h>
44
#include <mailutils/cctype.h>
45 46 47 48

#ifdef ENABLE_RADIUS

#include <radius/radius.h>
Sergey Poznyakoff authored
49
#include <radius/debug.h>
50

51 52
static int radius_auth_enabled;

Sergey Poznyakoff authored
53 54 55 56 57 58 59
static int MU_User_Name;
static int MU_UID;
static int MU_GID;
static int MU_GECOS;
static int MU_Dir;
static int MU_Shell;
static int MU_Mailbox;
60

61 62 63 64 65 66 67
static grad_avp_t *auth_request;
static grad_avp_t *getpwnam_request;
static grad_avp_t *getpwuid_request;


int
get_attribute (int *pattr, char *name)
68 69 70
{
  grad_dict_attr_t *attr = grad_attr_name_to_dict (name);
  if (!attr)
71
    {
72
      mu_error (_("RADIUS attribute %s not defined"), name);
73 74
      return 1;
    }
75
  *pattr = attr->value;
76
  return 0;
77 78 79 80
}

enum parse_state
  {
Sergey Poznyakoff authored
81 82
    state_lhs,
    state_op,
83 84 85 86 87
    state_rhs,
    state_delim
  };

int
88
parse_pairlist (grad_avp_t **plist, char *input)
89
{
90 91
  size_t i;
  struct mu_wordsplit ws;
92 93 94 95 96 97 98
  enum parse_state state;
  grad_locus_t loc;
  char *name;
  char *op; /* FIXME: It is actually ignored. Should it be? */

  if (!input)
    return 1;
Sergey Poznyakoff authored
99

100 101 102
  ws.ws_delim = ",";
  if (mu_wordsplit (input, &ws,
		    MU_WRDSF_DEFFLAGS|MU_WRDSF_DELIM|MU_WRDSF_RETURN_DELIMS))
103
    {
104 105
      mu_error (_("cannot parse input `%s': %s"), input,
		mu_wordsplit_strerror (&ws));
106 107
      return 1;
    }
108

109
  loc.file = "<configuration>"; /*FIXME*/
110
  loc.line = 0;
Sergey Poznyakoff authored
111

112
  for (i = 0, state = state_lhs; i < ws.ws_wordc; i++)
113 114
    {
      grad_avp_t *pair;
Sergey Poznyakoff authored
115

116 117 118
      switch (state)
	{
	case state_lhs:
119
	  name = ws.ws_wordv[i];
120 121
	  state = state_op;
	  break;
Sergey Poznyakoff authored
122

123
	case state_op:
124
	  op = ws.ws_wordv[i];
125 126
	  state = state_rhs;
	  break;
Sergey Poznyakoff authored
127

128 129
	case state_rhs:
	  loc.line = i; /* Just to keep track of error location */
130 131
	  pair = grad_create_pair (&loc, name, grad_operator_equal,
				   ws.ws_wordv[i]);
132
	  if (!pair)
133 134 135 136
	    {
	      mu_error (_("cannot create radius A/V pair `%s'"), name);
	      return 1;
	    }
137 138 139 140 141
	  grad_avl_merge (plist, &pair);
	  state = state_delim;
	  break;

	case state_delim:
142
	  if (strcmp (ws.ws_wordv[i], ","))
143
	    {
144
	      mu_error (_("expected `,' but found `%s'"), ws.ws_wordv[i]);
145 146
	      return 1;
	    }
147 148 149
	  state = state_lhs;
	}
    }
150 151
  mu_wordsplit_free (&ws);
  
152
  if (state != state_delim && state != state_delim)
153 154 155 156
    {
      mu_error (_("malformed radius A/V list"));
      return 1;
    }
Sergey Poznyakoff authored
157

158 159 160
  return 0;
}

161 162 163 164 165 166 167 168
/* Assume radius support is needed if any of the above requests is
   defined. Actually, all of them should be, but it is the responsibility
   of init to check for consistency of the configuration */

#define NEED_RADIUS_P(cfg) \
  ((cfg) && \
   ((cfg)->auth_request || (cfg)->getpwnam_request || (cfg)->getpwuid_request))

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
static void
mu_grad_logger(int level,
	       const grad_request_t *req,
	       const grad_locus_t *loc,
	       const char *func_name, int en,
	       const char *fmt, va_list ap)
{
  static int mlevel[] = {
    MU_DIAG_EMERG,
    MU_DIAG_ALERT,
    MU_DIAG_CRIT,
    MU_DIAG_ERROR,
    MU_DIAG_WARNING,
    MU_DIAG_NOTICE,
    MU_DIAG_INFO,
    MU_DIAG_DEBUG
  };

  char *pfx = NULL;
  if (loc)
    {
      if (func_name)
191 192 193 194 195
	mu_asprintf (&pfx, "%s:%lu:%s: %s",
		     loc->file, (unsigned long) loc->line, func_name, fmt);
      else
	mu_asprintf (&pfx, "%s:%lu: %s",
		     loc->file, (unsigned long) loc->line, fmt);
196
    }
197
  mu_diag_voutput (mlevel[level & GRAD_LOG_PRIMASK], pfx ? pfx : fmt, ap);
198 199 200 201
  if (pfx)
    free(pfx);
}

202
int
203
mu_radius_module_init (enum mu_gocs_op op, void *data)
204
{
205 206
  struct mu_radius_module_data *cfg = data;

207 208
  if (op != mu_gocs_op_set)
    return 0;
209 210
  if (!NEED_RADIUS_P (cfg))
    return 0;
Sergey Poznyakoff authored
211

212
  grad_set_logger (mu_grad_logger);
213 214
  grad_config_dir = grad_estrdup (cfg->config_dir);

215 216
  grad_path_init ();
  srand (time (NULL) + getpid ());
Sergey Poznyakoff authored
217

218
  if (grad_dict_init ())
219
    {
220
      mu_error (_("cannot read radius dictionaries"));
221 222
      return 1;
    }
223 224

  /* Check whether mailutils attributes are defined */
225 226 227 228 229 230 231 232
  if (get_attribute (&MU_User_Name, "MU-User-Name")
      || get_attribute (&MU_UID, "MU-UID")
      || get_attribute (&MU_GID, "MU-GID")
      || get_attribute (&MU_GECOS, "MU-GECOS")
      || get_attribute (&MU_Dir, "MU-Dir")
      || get_attribute (&MU_Shell, "MU-Shell")
      || get_attribute (&MU_Mailbox, "MU-Mailbox"))
    return 1;
Sergey Poznyakoff authored
233

234
  /* Parse saved requests */
235 236 237 238
  if (parse_pairlist (&auth_request, cfg->auth_request)
      || parse_pairlist (&getpwnam_request, cfg->getpwnam_request)
      || parse_pairlist (&getpwuid_request, cfg->getpwuid_request))
    return 1;
239 240

  radius_auth_enabled = 1;
241
  return 0;
Sergey Poznyakoff authored
242
}
243

244
static char *
245 246
_expand_query (const char *query, const char *ustr, const char *passwd)
{
247 248 249 250 251 252 253 254 255 256 257 258 259 260
  struct mu_wordsplit ws;
  const char *env[2 * 2 + 1];
  char *ret;
  
  env[0] = "user";
  env[1] = (char*) ustr;
  env[2] = "passwd";
  env[3] = (char*) passwd;
  env[4] = NULL;

  ws.ws_env = env;
  if (mu_wordsplit (query, &ws,
		    MU_WRDSF_NOSPLIT | MU_WRDSF_NOCMD |
		    MU_WRDSF_ENV | MU_WRDSF_ENV_KV))
261
    {
262 263 264
      mu_error (_("cannot expand line `%s': %s"), query,
		mu_wordsplit_strerror (&ws));
      return NULL;
265
    }
266
  else if (ws.ws_wordc == 0)
267
    {
268 269 270
      mu_error (_("expanding %s yields empty string"), query);
      mu_wordsplit_free (&ws);
      return NULL;
271
    }
272 273 274 275
  
  ret = grad_emalloc (strlen (ws.ws_wordv[0]) + 1);
  strcpy (ret, ws.ws_wordv[0]);
  mu_wordsplit_free (&ws);
276
  return ret;
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
static grad_avp_t *
create_request (grad_avp_t *template, const char *ustr, const char *passwd)
{
  grad_avp_t *newp, *p;

  newp = grad_avl_dup (template);
  for (p = newp; p; p = p->next)
    {
      if (p->type == GRAD_TYPE_STRING)
	{
	  char *value = _expand_query (p->avp_strvalue, ustr, passwd);
	  grad_free (p->avp_strvalue);
	  p->avp_strvalue = value;
	  p->avp_strlength = strlen (value);
	}
    }
  return newp;
}



grad_request_t *
send_request (grad_avp_t *pairs, int code,
	      const char *user, const char *passwd)
{
  grad_avp_t *plist = create_request (pairs, user, passwd);
  if (plist)
    {
      grad_server_queue_t *queue = grad_client_create_queue (1, 0, 0);
      grad_request_t *reply = grad_client_send (queue,
						GRAD_PORT_AUTH, code,
						plist);
      grad_client_destroy_queue (queue);
      grad_avl_free (plist);
      return reply;
    }
  return NULL;
}

#define DEFAULT_HOME_PREFIX "/home/"
#define DEFAULT_SHELL "/dev/null"

int
decode_reply (grad_request_t *reply, const char *user_name, char *password,
	      struct mu_auth_data **return_data)
{
  grad_avp_t *p;
  int rc;
Sergey Poznyakoff authored
328

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
  uid_t uid = -1;
  gid_t gid = -1;
  char *gecos = "RADIUS User";
  char *dir = NULL;
  char *shell = NULL;
  char *mailbox = NULL;

  p = grad_avl_find (reply->avlist, MU_User_Name);
  if (p)
    user_name = p->avp_strvalue;

  p = grad_avl_find (reply->avlist, MU_UID);
  if (p)
    uid = p->avp_lvalue;
  else
    {
345
      mu_error (_("radius server did not return UID for `%s'"),  user_name);
346 347
      return -1;
    }
Sergey Poznyakoff authored
348

349 350 351 352 353
  p = grad_avl_find (reply->avlist, MU_GID);
  if (p)
    gid = p->avp_lvalue;
  else
    {
354
      mu_error (_("radius server did not return GID for `%s'"),  user_name);
355 356
      return -1;
    }
Sergey Poznyakoff authored
357

358 359 360
  p = grad_avl_find (reply->avlist, MU_GECOS);
  if (p)
    gecos = p->avp_strvalue;
Sergey Poznyakoff authored
361

362 363 364 365 366 367 368 369 370 371
  p = grad_avl_find (reply->avlist, MU_Dir);
  if (p)
    dir = strdup (p->avp_strvalue);
  else /* Try to provide a reasonable default */
    {
      dir = malloc (sizeof DEFAULT_HOME_PREFIX + strlen (user_name));
      if (!dir) /* FIXME: Error code */
	return 1;
      strcat (strcpy (dir, DEFAULT_HOME_PREFIX), user_name);
    }
Sergey Poznyakoff authored
372

373 374 375 376 377
  p = grad_avl_find (reply->avlist, MU_Shell);
  if (p)
    shell = p->avp_strvalue;
  else
    shell = DEFAULT_SHELL;
Sergey Poznyakoff authored
378

379 380 381 382 383 384 385 386 387
  p = grad_avl_find (reply->avlist, MU_Mailbox);
  if (p)
    mailbox = strdup (p->avp_strvalue);
  else
    {
      rc = mu_construct_user_mailbox_url (&mailbox, user_name);
      if (rc)
	return rc;
    }
Sergey Poznyakoff authored
388

389 390 391 392 393 394 395 396 397 398
  rc = mu_auth_data_alloc (return_data,
			   user_name,
			   password,
			   uid,
			   gid,
			   gecos,
			   dir,
			   shell,
			   mailbox,
			   1);
Sergey Poznyakoff authored
399

400 401 402 403 404 405
  free (dir);
  free (mailbox);
  return rc;
}

int
406
mu_radius_authenticate (struct mu_auth_data **return_data MU_ARG_UNUSED,
407
			const void *key,
408
			void *func_data MU_ARG_UNUSED, void *call_data)
409
{
410
  int rc;
411 412 413
  grad_request_t *reply;
  const struct mu_auth_data *auth_data = key;

414
  if (!radius_auth_enabled)
415
    return ENOSYS;
Sergey Poznyakoff authored
416

417 418
  if (!auth_request)
    {
419
      mu_error (_("radius request for auth is not specified"));
420
      return EINVAL;
421
    }
Sergey Poznyakoff authored
422

423 424
  reply = send_request (auth_request, RT_ACCESS_REQUEST,
			auth_data->name, (char*) call_data);
425 426 427 428 429 430 431 432 433 434 435 436 437
  if (!reply)
    return EAGAIN;

  switch (reply->code) {
  case RT_ACCESS_ACCEPT:
    rc = 0;
    break;

  case RT_ACCESS_CHALLENGE:
    /* Should return another code here? */
  default:
    rc = MU_ERR_AUTH_FAILURE;
  }
Sergey Poznyakoff authored
438

439
  grad_request_free (reply);
440

441 442 443 444 445 446 447 448
  return rc;
}

static int
mu_auth_radius_user_by_name (struct mu_auth_data **return_data,
			     const void *key,
			     void *unused_func_data, void *unused_call_data)
{
449
  int rc = MU_ERR_AUTH_FAILURE;
450
  grad_request_t *reply;
Sergey Poznyakoff authored
451

452
  if (!radius_auth_enabled)
453
    return ENOSYS;
454

455 456
  if (!getpwnam_request)
    {
457
      mu_error (_("radius request for getpwnam is not specified"));
458
      return MU_ERR_FAILURE;
459
    }
Sergey Poznyakoff authored
460

461 462
  reply = send_request (getpwnam_request, RT_ACCESS_REQUEST, key, NULL);
  if (!reply)
463 464 465 466
    {
      mu_error (_("radius server did not respond"));
      rc = EAGAIN;
    }
467
  else
468 469 470 471 472 473 474
    {
      if (reply->code != RT_ACCESS_ACCEPT)
	mu_error (_("%s: server returned %s"),
		  (char*) key,
		  grad_request_code_to_name (reply->code));
      else
	rc = decode_reply (reply, key, "x", return_data);
Sergey Poznyakoff authored
475

476 477
      grad_request_free (reply);
    }
478 479 480 481 482 483 484 485
  return rc;
}

static int
mu_auth_radius_user_by_uid (struct mu_auth_data **return_data,
			    const void *key,
			    void *func_data, void *call_data)
{
486
  int rc = MU_ERR_AUTH_FAILURE;
487 488
  grad_request_t *reply;
  char uidstr[64];
Sergey Poznyakoff authored
489

490
  if (!radius_auth_enabled)
491
    return ENOSYS;
492

493
  if (!key)
494
    return EINVAL;
495 496 497

  if (!getpwuid_request)
    {
498
      mu_error (_("radius request for getpwuid is not specified"));
499
      return MU_ERR_FAILURE;
500
    }
Sergey Poznyakoff authored
501

502 503
  snprintf (uidstr, sizeof (uidstr), "%u", *(uid_t*)key);
  reply = send_request (getpwuid_request, RT_ACCESS_REQUEST, uidstr, NULL);
504 505 506 507 508
  if (!reply)
    {
      mu_error (_("radius server did not respond"));
      rc = EAGAIN;
    }
509 510 511 512 513 514
  if (reply->code != RT_ACCESS_ACCEPT)
    {
      mu_error (_("uid %s: server returned %s"), uidstr,
		grad_request_code_to_name (reply->code));
    }
  else
515 516
    rc = decode_reply (reply, uidstr, "x", return_data);

517 518 519 520 521 522
  grad_request_free (reply);
  return rc;
}

#else
static int
523
mu_radius_authenticate (struct mu_auth_data **return_data MU_ARG_UNUSED,
524
			const void *key,
525
			void *func_data MU_ARG_UNUSED, void *call_data)
526
{
527
  return ENOSYS;
528 529 530
}

static int
531 532 533 534
mu_auth_radius_user_by_name (struct mu_auth_data **return_data MU_ARG_UNUSED,
			     const void *key MU_ARG_UNUSED,
			     void *func_data MU_ARG_UNUSED,
			     void *call_data MU_ARG_UNUSED)
535
{
536
  return ENOSYS;
537 538 539 540 541 542 543
}

static int
mu_auth_radius_user_by_uid (struct mu_auth_data **return_data,
			    const void *key,
			    void *func_data, void *call_data)
{
544
  return ENOSYS;
545 546 547 548 549 550
}
#endif

struct mu_auth_module mu_auth_radius_module = {
  "radius",
#ifdef ENABLE_RADIUS
551
  mu_radius_module_init,
552 553 554 555 556 557 558 559 560 561
#else
  NULL,
#endif
  mu_radius_authenticate,
  NULL,
  mu_auth_radius_user_by_name,
  NULL,
  mu_auth_radius_user_by_uid,
  NULL
};