Massively cleaned up #include's, so they're in a consistent
[freeradius.git] / src / modules / rlm_unix / rlm_unix.c
1 /*
2  * rlm_unix.c   authentication: Unix user authentication
3  *              accounting:     Functions to write radwtmp file.
4  *              Also contains handler for "Group".
5  *
6  * Version:     $Id$
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation; either version 2 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This program 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
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  *
22  * Copyright 2000,2006  The FreeRADIUS server project
23  * Copyright 2000  Jeff Carneal <jeff@apex.net>
24  * Copyright 2000  Alan Curry <pacman@world.std.com>
25  */
26
27 #include        <freeradius-devel/ident.h>
28 RCSID("$Id$")
29
30 #include        <freeradius-devel/radiusd.h>
31
32 #include        <grp.h>
33 #include        <pwd.h>
34 #include        <sys/stat.h>
35
36 #include "config.h"
37
38 #ifdef HAVE_SHADOW_H
39 #  include      <shadow.h>
40 #endif
41
42 #ifdef OSFC2
43 #  include      <sys/security.h>
44 #  include      <prot.h>
45 #endif
46
47 #ifdef OSFSIA
48 #  include      <sia.h>
49 #  include      <siad.h>
50 #endif
51
52 #include        <freeradius-devel/modules.h>
53 #include        <freeradius-devel/sysutmp.h>
54
55 static char trans[64] =
56    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
57 #define ENC(c) trans[c]
58
59 struct unix_instance {
60         const char *radwtmp;
61 };
62
63 static const CONF_PARSER module_config[] = {
64         { "radwtmp",  PW_TYPE_STRING_PTR,
65           offsetof(struct unix_instance,radwtmp), NULL,   "NULL" },
66
67         { NULL, -1, 0, NULL, NULL }             /* end the list */
68 };
69
70
71 /*
72  *      The Group = handler.
73  */
74 static int groupcmp(void *instance, UNUSED REQUEST *req, VALUE_PAIR *request,
75                     VALUE_PAIR *check, VALUE_PAIR *check_pairs,
76                     VALUE_PAIR **reply_pairs)
77 {
78         struct passwd   *pwd;
79         struct group    *grp;
80         char            **member;
81         char            *username;
82         int             retval;
83         VALUE_PAIR      *vp;
84
85         instance = instance;
86         check_pairs = check_pairs;
87         reply_pairs = reply_pairs;
88
89         /*
90          *      No user name, doesn't compare.
91          */
92         vp = pairfind(request, PW_STRIPPED_USER_NAME);
93         if (!vp) {
94                 vp = pairfind(request, PW_USER_NAME);
95                 if (!vp) {
96                         return -1;
97                 }
98         }
99         username = (char *)vp->vp_strvalue;
100
101         pwd = getpwnam(username);
102         if (pwd == NULL)
103                 return -1;
104
105         grp = getgrnam((char *)check->vp_strvalue);
106         if (grp == NULL)
107                 return -1;
108
109         retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
110         if (retval < 0) {
111                 for (member = grp->gr_mem; *member && retval; member++) {
112                         if (strcmp(*member, pwd->pw_name) == 0)
113                                 retval = 0;
114                 }
115         }
116         return retval;
117 }
118
119
120 /*
121  *      Detach.
122  */
123 static int unix_detach(void *instance)
124 {
125 #define inst ((struct unix_instance *)instance)
126
127         paircompare_unregister(PW_GROUP, groupcmp);
128 #ifdef PW_GROUP_NAME
129         paircompare_unregister(PW_GROUP_NAME, groupcmp);
130 #endif
131 #undef inst
132         free(instance);
133         return 0;
134 }
135
136 /*
137  *      Read the config
138  */
139 static int unix_instantiate(CONF_SECTION *conf, void **instance)
140 {
141         struct unix_instance *inst;
142
143         /*
144          *      Allocate room for the instance.
145          */
146         inst = *instance = rad_malloc(sizeof(*inst));
147         if (!inst) {
148                 return -1;
149         }
150         memset(inst, 0, sizeof(*inst));
151
152         /*
153          *      Parse the configuration, failing if we can't do so.
154          */
155         if (cf_section_parse(conf, inst, module_config) < 0) {
156                 unix_detach(inst);
157                 return -1;
158         }
159
160         /* FIXME - delay these until a group file has been read so we know
161          * groupcmp can actually do something */
162         paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp, NULL);
163 #ifdef PW_GROUP_NAME /* compat */
164         paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
165 #endif
166
167 #undef inst
168
169         return 0;
170 }
171
172
173 /*
174  *      Pull the users password from where-ever, and add it to
175  *      the given vp list.
176  */
177 static int unix_getpw(UNUSED void *instance, REQUEST *request,
178                       VALUE_PAIR **vp_list)
179 {
180         const char      *name;
181         const char      *encrypted_pass;
182 #ifdef HAVE_GETSPNAM
183         struct spwd     *spwd = NULL;
184 #endif
185 #ifdef OSFC2
186         struct pr_passwd *pr_pw;
187 #else
188         struct passwd   *pwd;
189 #endif
190 #ifdef HAVE_GETUSERSHELL
191         char            *shell;
192 #endif
193         VALUE_PAIR      *vp;
194
195         /*
196          *      We can only authenticate user requests which HAVE
197          *      a User-Name attribute.
198          */
199         if (!request->username) {
200                 return RLM_MODULE_NOOP;
201         }
202
203         name = (char *)request->username->vp_strvalue;
204         encrypted_pass = NULL;
205
206 #ifdef OSFC2
207         if ((pr_pw = getprpwnam(name)) == NULL)
208                 return RLM_MODULE_NOTFOUND;
209         encrypted_pass = pr_pw->ufld.fd_encrypt;
210
211         /*
212          *      Check if account is locked.
213          */
214         if (pr_pw->uflg.fg_lock!=1) {
215                 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
216                 return RLM_MODULE_USERLOCK;
217         }
218 #else /* OSFC2 */
219         if ((pwd = getpwnam(name)) == NULL) {
220                 return RLM_MODULE_NOTFOUND;
221         }
222         encrypted_pass = pwd->pw_passwd;
223 #endif /* OSFC2 */
224
225 #ifdef HAVE_GETSPNAM
226         /*
227          *      See if there is a shadow password.
228          *
229          *      Only query the _system_ shadow file if the encrypted
230          *      password from the passwd file is < 10 characters (i.e.
231          *      a valid password would never crypt() to it).  This will
232          *      prevents users from using NULL password fields as things
233          *      stand right now.
234          */
235         if ((encrypted_pass == NULL) || (strlen(encrypted_pass) < 10)) {
236                 if ((spwd = getspnam(name)) == NULL) {
237                         return RLM_MODULE_NOTFOUND;
238                 }
239                 encrypted_pass = spwd->sp_pwdp;
240         }
241 #endif  /* HAVE_GETSPNAM */
242
243 /*
244  *      These require 'pwd != NULL', which isn't true on OSFC2
245  */
246 #ifndef OSFC2
247 #ifdef DENY_SHELL
248         /*
249          *      Users with a particular shell are denied access
250          */
251         if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
252                 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
253                 return RLM_MODULE_REJECT;
254         }
255 #endif
256
257 #ifdef HAVE_GETUSERSHELL
258         /*
259          *      Check /etc/shells for a valid shell. If that file
260          *      contains /RADIUSD/ANY/SHELL then any shell will do.
261          */
262         while ((shell = getusershell()) != NULL) {
263                 if (strcmp(shell, pwd->pw_shell) == 0 ||
264                     strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
265                         break;
266                 }
267         }
268         endusershell();
269         if (shell == NULL) {
270                 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell [%s]",
271                        name, pwd->pw_shell);
272                 return RLM_MODULE_REJECT;
273         }
274 #endif
275 #endif /* OSFC2 */
276
277 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
278         /*
279          *      Check if password has expired.
280          */
281         if (spwd && spwd->sp_expire > 0 &&
282             (request->timestamp / 86400) > spwd->sp_expire) {
283                 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
284                 return RLM_MODULE_REJECT;
285         }
286 #endif
287
288 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
289         /*
290          *      Check if password has expired.
291          */
292         if ((pwd->pw_expire > 0) &&
293             (request->timestamp > pwd->pw_expire)) {
294                 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
295                 return RLM_MODULE_REJECT;
296         }
297 #endif
298
299         /*
300          *      We might have a passwordless account.
301          *
302          *      FIXME: Maybe add Auth-Type := Accept?
303          */
304         if (encrypted_pass[0] == 0)
305                 return RLM_MODULE_NOOP;
306
307         vp = pairmake("Crypt-Password", encrypted_pass, T_OP_SET);
308         if (!vp) return RLM_MODULE_FAIL;
309
310         pairmove(vp_list, &vp);
311         pairfree(&vp);          /* might not be NULL; */
312
313         return RLM_MODULE_UPDATED;
314 }
315
316
317 /*
318  *      Pull the users password from where-ever, and add it to
319  *      the given vp list.
320  */
321 static int unix_authorize(void *instance, REQUEST *request)
322 {
323         return unix_getpw(instance, request, &request->config_items);
324 }
325
326 /*
327  *      Pull the users password from where-ever, and add it to
328  *      the given vp list.
329  */
330 static int unix_authenticate(void *instance, REQUEST *request)
331 {
332 #ifdef OSFSIA
333         char            *info[2];
334         char            *progname = "radius";
335         SIAENTITY       *ent = NULL;
336
337         info[0] = progname;
338         info[1] = NULL;
339         if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
340             SIASUCCESS)
341                 return RLM_MODULE_NOTFOUND;
342         if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
343                 if (ret & SIASTOP)
344                         sia_ses_release (&ent);
345                 return RLM_MODULE_NOTFOUND;
346         }
347         if (sia_ses_estab (NULL, ent) != SIASUCCESS) {
348                 sia_ses_release (&ent);
349                 return RLM_MODULE_NOTFOUND;
350         }
351 #else  /* OSFSIA */
352         int rcode;
353         VALUE_PAIR *vp = NULL;
354
355         if (!request->password ||
356             (request->password->attribute != PW_USER_PASSWORD)) {
357                 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.");
358                 return RLM_MODULE_INVALID;
359         }
360
361         rcode = unix_getpw(instance, request, &vp);
362         if (rcode != RLM_MODULE_UPDATED) return rcode;
363
364         /*
365          *      0 means "ok"
366          */
367         if (lrad_crypt_check((char *) request->password->vp_strvalue,
368                              (char *) vp->vp_strvalue) != 0) {
369                 radlog(L_AUTH, "rlm_unix: [%s]: invalid password",
370                        request->username->vp_strvalue);
371                 return RLM_MODULE_REJECT;
372         }
373 #endif /* OSFFIA */
374
375         return RLM_MODULE_OK;
376 }
377
378
379 /*
380  *      UUencode 4 bits base64. We use this to turn a 4 byte field
381  *      (an IP address) into 6 bytes of ASCII. This is used for the
382  *      wtmp file if we didn't find a short name in the naslist file.
383  */
384 static char *uue(void *in)
385 {
386         int i;
387         static unsigned char res[7];
388         unsigned char *data = (unsigned char *)in;
389
390         res[0] = ENC( data[0] >> 2 );
391         res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
392         res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
393         res[3] = ENC( data[2] & 077 );
394
395         res[4] = ENC( data[3] >> 2 );
396         res[5] = ENC( (data[3] << 4) & 060 );
397         res[6] = 0;
398
399         for(i = 0; i < 6; i++) {
400                 if (res[i] == ' ') res[i] = '`';
401                 if (res[i] < 32 || res[i] > 127)
402                         printf("uue: protocol error ?!\n");
403         }
404         return (char *)res;
405 }
406
407
408 /*
409  *      Unix accounting - write a wtmp file.
410  */
411 static int unix_accounting(void *instance, REQUEST *request)
412 {
413         VALUE_PAIR      *vp;
414         FILE            *fp;
415         struct utmp     ut;
416         time_t          t;
417         char            buf[64];
418         const char      *s;
419         int             delay = 0;
420         int             status = -1;
421         int             nas_address = 0;
422         int             framed_address = 0;
423         int             protocol = -1;
424         int             nas_port = 0;
425         int             port_seen = 0;
426         int             nas_port_type = 0;
427         struct unix_instance *inst = (struct unix_instance *) instance;
428
429         /*
430          *      No radwtmp.  Don't do anything.
431          */
432         if (!inst->radwtmp) {
433                 DEBUG2("rlm_unix: No radwtmp file configured.  Ignoring accounting request.");
434                 return RLM_MODULE_NOOP;
435         }
436
437         if (request->packet->src_ipaddr.af != AF_INET) {
438                 DEBUG2("rlm_unix: IPv6 is not supported!");
439                 return RLM_MODULE_NOOP;
440         }
441
442         /*
443          *      Which type is this.
444          */
445         if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
446                 radlog(L_ERR, "rlm_unix: no Accounting-Status-Type attribute in request.");
447                 return RLM_MODULE_NOOP;
448         }
449         status = vp->lvalue;
450
451         /*
452          *      FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
453          */
454         if (status != PW_STATUS_START &&
455             status != PW_STATUS_STOP)
456                 return RLM_MODULE_NOOP;
457
458         /*
459          *      We're only interested in accounting messages
460          *      with a username in it.
461          */
462         if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
463                 return RLM_MODULE_NOOP;
464
465         t = request->timestamp;
466         memset(&ut, 0, sizeof(ut));
467
468         /*
469          *      First, find the interesting attributes.
470          */
471         for (vp = request->packet->vps; vp; vp = vp->next) {
472                 switch (vp->attribute) {
473                         case PW_USER_NAME:
474                                 if (vp->length >= sizeof(ut.ut_name)) {
475                                         memcpy(ut.ut_name, (char *)vp->vp_strvalue, sizeof(ut.ut_name));
476                                 } else {
477                                         strlcpy(ut.ut_name, (char *)vp->vp_strvalue, sizeof(ut.ut_name));
478                                 }
479                                 break;
480                         case PW_LOGIN_IP_HOST:
481                         case PW_FRAMED_IP_ADDRESS:
482                                 framed_address = vp->lvalue;
483                                 break;
484                         case PW_FRAMED_PROTOCOL:
485                                 protocol = vp->lvalue;
486                                 break;
487                         case PW_NAS_IP_ADDRESS:
488                                 nas_address = vp->lvalue;
489                                 break;
490                         case PW_NAS_PORT:
491                                 nas_port = vp->lvalue;
492                                 port_seen = 1;
493                                 break;
494                         case PW_ACCT_DELAY_TIME:
495                                 delay = vp->lvalue;
496                                 break;
497                         case PW_NAS_PORT_TYPE:
498                                 nas_port_type = vp->lvalue;
499                                 break;
500                 }
501         }
502
503         /*
504          *      We don't store !root sessions, or sessions
505          *      where we didn't see a NAS-Port attribute.
506          */
507         if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
508                 return RLM_MODULE_NOOP;
509
510         s = "";
511
512         /*
513          *      If we didn't find out the NAS address, use the
514          *      originator's IP address.
515          */
516         if (nas_address == 0) {
517                 RADCLIENT *cl;
518                 nas_address = request->packet->src_ipaddr.ipaddr.ip4addr.s_addr;
519                 
520                 if ((cl = client_find_old(&request->packet->src_ipaddr)) != NULL)
521                         s = cl->shortname;
522         }
523         if (!s || s[0] == 0) s = uue(&(nas_address));
524         
525 #ifdef __linux__
526         /*
527          *      Linux has a field for the client address.
528          */
529         ut.ut_addr = framed_address;
530 #endif
531         /*
532          *      We use the tty field to store the terminal servers' port
533          *      and address so that the tty field is unique.
534          */
535         sprintf(buf, "%03d:%s", nas_port, s);
536         strlcpy(ut.ut_line, buf, sizeof(ut.ut_line));
537
538         /*
539          *      We store the dynamic IP address in the hostname field.
540          */
541 #ifdef UT_HOSTSIZE
542         if (framed_address) {
543                 ip_ntoa(buf, framed_address);
544                 strlcpy(ut.ut_host, buf, sizeof(ut.ut_host));
545         }
546 #endif
547 #ifdef HAVE_UTMPX_H
548         ut.ut_xtime = t- delay;
549 #else
550         ut.ut_time = t - delay;
551 #endif
552 #ifdef USER_PROCESS
553         /*
554          *      And we can use the ID field to store
555          *      the protocol.
556          */
557         if (protocol == PW_PPP)
558                 strcpy(ut.ut_id, "P");
559         else if (protocol == PW_SLIP)
560                 strcpy(ut.ut_id, "S");
561         else
562                 strcpy(ut.ut_id, "T");
563         ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
564 #endif
565         if (status == PW_STATUS_STOP)
566                 ut.ut_name[0] = 0;
567
568         /*
569          *      Write a RADIUS wtmp log file.
570          *
571          *      Try to open the file if we can't, we don't write the
572          *      wtmp file. If we can try to write. If we fail,
573          *      return RLM_MODULE_FAIL ..
574          */
575         if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
576                 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
577                         fclose(fp);
578                         return RLM_MODULE_FAIL;
579                 }
580                 fclose(fp);
581         } else
582                 return RLM_MODULE_FAIL;
583
584         return RLM_MODULE_OK;
585 }
586
587 /* globally exported name */
588 module_t rlm_unix = {
589         RLM_MODULE_INIT,
590         "System",
591         RLM_TYPE_THREAD_UNSAFE,        /* type */
592         unix_instantiate,               /* instantiation */
593         unix_detach,                    /* detach */
594         {
595                 unix_authenticate,    /* authentication */
596                 unix_authorize,       /* authorization */
597                 NULL,                 /* preaccounting */
598                 unix_accounting,      /* accounting */
599                 NULL,                  /* checksimul */
600                 NULL,                   /* pre-proxy */
601                 NULL,                   /* post-proxy */
602                 NULL                    /* post-auth */
603         },
604 };