summaryrefslogtreecommitdiff
path: root/pam_tacplus.c
blob: 2f131e3df054aaeb4af90d39b6fd5d9c03d79aa4 (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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
/* pam_tacplus.c - PAM interface for TACACS+ protocol.
 *
 * Copyright (C) 2010, Pawel Krawczyk <pawel.krawczyk@hush.com> and
 * Jeroen Nijhof <jeroen@jeroennijhof.nl>
 *
 * Copyright 2015, 2016, 2017, 2018 Cumulus Networks, Inc.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program - see the file COPYING.
 *
 * See `CHANGES' file for revision history.
 */

#include "pam_tacplus.h"
#include "support.h"

#include <stdlib.h>     /* malloc */
#include <stdio.h>
#include <syslog.h>
#include <netdb.h>      /* gethostbyname */
#include <sys/socket.h> /* in_addr */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdarg.h>     /* va_ */
#include <signal.h>
#include <string.h>     /* strdup */
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <strings.h>

#ifdef HAVE_CONFIG_H
  #include "config.h"
#endif

#if defined(HAVE_OPENSSL_RAND_H) && defined(HAVE_LIBCRYPTO)
# include <openssl/rand.h>
#else
# include "libtac/lib/magic.h"
#endif

/* address of server discovered by pam_sm_authenticate */
tacplus_server_t active_server;

extern char *__vrfname;

/* privilege level, used for mapping from tacacs userid to local
 * tacacs{0...15} user
 */
static unsigned priv_level;

/* accounting task identifier */
static short unsigned int task_id = 0;


/* Helper functions */
int _pam_send_account(pam_handle_t *pamh, int tac_fd, int type,
    const char *user, char *tty, char *r_addr, char *cmd) {
    char buf[64];
    struct tac_attrib *attr = NULL;
    int retval = -1;
    struct areply re;

    re.msg = NULL;
    snprintf(buf, sizeof buf, "%lu", (unsigned long)time(NULL));

    if (type == TAC_PLUS_ACCT_FLAG_START) {
        tac_add_attrib(&attr, "start_time", buf);
    } else if (type == TAC_PLUS_ACCT_FLAG_STOP) {
        tac_add_attrib(&attr, "stop_time", buf);
    }
    snprintf(buf, sizeof buf, "%hu", task_id);
    tac_add_attrib(&attr, "task_id", buf);
    tac_add_attrib(&attr, "service", tac_service);
    if(tac_protocol[0] != '\0')
      tac_add_attrib(&attr, "protocol", tac_protocol);
    if (cmd != NULL) {
        tac_add_attrib(&attr, "cmd", cmd);
    }

    retval = tac_acct_send(tac_fd, type, user, tty, r_addr, attr);

    /* attribute is no longer needed */
    tac_free_attrib(&attr);

    if(retval < 0) {
        pam_syslog(pamh, LOG_WARNING, "%s: send %s accounting failed"
            " (task %hu)", __func__, tac_acct_flag2str(type), task_id);
    }
    else if( tac_acct_read(tac_fd, &re) != TAC_PLUS_ACCT_STATUS_SUCCESS ) {
        pam_syslog(pamh, LOG_WARNING, "%s: accounting %s failed (task %hu)",
            __func__, tac_acct_flag2str(type), task_id);
        retval = -1;
    }
    else
        retval = 0;

    if(re.msg != NULL)
        free(re.msg);

    return retval;
}


/*
 * If all servers have been unresponsive, clear that state, so we try
 * them all.  It might have been transient.
 */
static void tac_chk_anyresp(void)
{
    int i, anyok=0;

    for(i = 0; i < tac_srv_no; i++) {
        if (!tac_srv[i].not_resp)
            anyok++;
    }
    if (!anyok) {
        for(i = 0; i < tac_srv_no; i++)
            tac_srv[i].not_resp = 0;
    }
}


/*
 * Send an accounting record to the TACACS+ server.
 * We send the start/stop accounting records even if the user is not known
 * to the TACACS+ server.   This seems non-intuitive, but it's the way
 * this code is written to work.
 */
int _pam_account(pam_handle_t *pamh, int argc, const char **argv,
    int type, char *cmd) {

    int retval;
    int ctrl;
    char *user = NULL;
    char *tty = NULL;
    char *r_addr = NULL;
    char *typemsg;
    int status = PAM_SESSION_ERR;
    int srv_i, tac_fd;

    typemsg = tac_acct_flag2str(type);
    ctrl = _pam_parse (pamh, argc, argv);

    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: [%s] called (pam_tacplus"
            " v%u.%u.%u)", __func__, typemsg, PAM_TAC_VMAJ, PAM_TAC_VMIN,
            PAM_TAC_VPAT);

    _pam_get_user(pamh, &user);
    if (user == NULL)
        return PAM_USER_UNKNOWN;

    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: username [%s] obtained", __func__,
            user);

    if (!task_id)
#if defined(HAVE_OPENSSL_RAND_H) && defined(HAVE_LIBCRYPTO)
        RAND_pseudo_bytes((unsigned char *) &task_id, sizeof(task_id));
#else
        task_id = (short unsigned int) tac_magic();
#endif

    _pam_get_terminal(pamh, &tty);
    if(!strncmp(tty, "/dev/", 5))
        tty += 5;
    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: tty [%s] obtained", __func__, tty);

    _pam_get_rhost(pamh, &r_addr);
    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: rhost [%s] obtained", __func__,
            r_addr);

    /* checks for specific data required by TACACS+, which should
       be supplied in command line  */
    if(tac_protocol[0] == '\0') {
        pam_syslog(pamh, LOG_ERR, "ACC: TACACS+ protocol type not configured");
        return PAM_AUTHINFO_UNAVAIL;
    }

    /* when this module is called from within pppd or other
       application dealing with serial lines, it is likely
       that we will get hit with signal caused by modem hangup;
       this is important only for STOP packets, it's relatively
       rare that modem hangs up on accounting start */
    if(type == TAC_PLUS_ACCT_FLAG_STOP) {
        signal(SIGALRM, SIG_IGN);
        signal(SIGCHLD, SIG_IGN);
        signal(SIGHUP, SIG_IGN);
    }

    /*
     * If PAM_SESSION_ERR is used, then the pam config can't
     * ignore server failures, so use PAM_AUTHINFO_UNAVAIL.
     *
     * We have to make a new connection each time, because libtac is single
     * threaded (doesn't support multiple connects at the same time due to
     * use of globals)), and doesn't have support for persistent connections.
     * That's fixable, but not worth the effort at this point.
     *
     * TODO: this should be converted to use do_tac_connect eventually.
     */
    status = PAM_AUTHINFO_UNAVAIL;
    tac_chk_anyresp();
    for(srv_i = 0; srv_i < tac_srv_no; srv_i++) {
        if (tac_srv[srv_i].not_resp)
            continue; /*  don't retry if previously not responding */
        tac_fd = tac_connect_single(tac_srv[srv_i].addr, tac_srv[srv_i].key,
                                    tac_src_addr_info, __vrfname);
        if (tac_fd < 0) {
            pam_syslog(pamh, LOG_WARNING, "%s: error sending %s (fd)", __func__,
                typemsg);
            tac_srv[srv_i].not_resp = 1;
            continue;
        }
        if (ctrl & PAM_TAC_DEBUG)
            pam_syslog(pamh, LOG_DEBUG, "%s: connected with fd=%d"
                " to srv[%d] %s type=%s", __func__, tac_fd, srv_i,
                tac_srv[srv_i].addr ?  tac_ntop(tac_srv[srv_i].addr->ai_addr)
                  : "not set", typemsg);

        retval = _pam_send_account(pamh, tac_fd, type, user, tty, r_addr, cmd);
        close(tac_fd);
        if (retval < 0) {
            pam_syslog(pamh, LOG_WARNING, "%s: error sending %s (acct)",
                __func__, typemsg);
        } else {
            status = PAM_SUCCESS;
            if (ctrl & PAM_TAC_DEBUG)
                pam_syslog(pamh, LOG_DEBUG, "%s: [%s] for [%s] sent",
                    __func__, typemsg, user);
        }

        if ((status == PAM_SUCCESS) && !(ctrl & PAM_TAC_ACCT)) {
            /* do not send acct start/stop packets to _all_ servers */
            break;
        }
    }

    if (type == TAC_PLUS_ACCT_FLAG_STOP) {
        signal(SIGALRM, SIG_DFL);
        signal(SIGCHLD, SIG_DFL);
        signal(SIGHUP, SIG_DFL);
    }
    return status;
}


/*
 * Talk to the server for authentication
 */
static int tac_auth_converse(int ctrl, int fd, int *sptr,
    char *pass, pam_handle_t * pamh) {
    int msg;
    int ret = 1;
    struct areply re = { .attr = NULL, .msg = NULL, .status = 0, .flags = 0 };
    struct pam_message conv_msg = { .msg_style = 0, .msg = NULL };
    struct pam_response *resp = NULL;

    msg = tac_authen_read(fd, &re);

    if (NULL != re.msg) {
        conv_msg.msg = re.msg;
    }

    /* talk the protocol */
    switch (msg) {
        case TAC_PLUS_AUTHEN_STATUS_PASS:
            /* success */
            if (ctrl & PAM_TAC_DEBUG)
                pam_syslog(pamh, LOG_DEBUG, "tacacs status:"
                    " TAC_PLUS_AUTHEN_STATUS_PASS");
            if (NULL != conv_msg.msg) {
                int retval = -1;

                conv_msg.msg_style = PAM_TEXT_INFO;
                retval = converse(pamh, 1, &conv_msg, &resp);
                if (PAM_SUCCESS == retval) {
                    if (PAM_TAC_DEBUG == (ctrl & PAM_TAC_DEBUG))
                        pam_syslog(pamh, LOG_DEBUG, "send msg=\"%s\"",
                            conv_msg.msg);
                }
                else {
                    pam_syslog(pamh, LOG_WARNING, "%s: error sending"
                        " msg=\"%s\", retval=%d", __func__, conv_msg.msg,
                        retval);
                }
            }
            *sptr = PAM_SUCCESS;
            ret = 0;
            break;

        case TAC_PLUS_AUTHEN_STATUS_FAIL:
            /*
             * We've already validated that the user is known, so
             * this will be a password mismatch, or user not permitted
             * on this host, or at this time of day, etc.
             */
            if (ctrl & PAM_TAC_DEBUG)
                pam_syslog(pamh, LOG_DEBUG, "tacacs status:"
                    " TAC_PLUS_AUTHEN_STATUS_FAIL");
            if (NULL != conv_msg.msg) {
                int retval = -1;

                conv_msg.msg_style = PAM_ERROR_MSG;
                retval = converse(pamh, 1, &conv_msg, &resp);
                if (PAM_SUCCESS == retval) {
                    if (PAM_TAC_DEBUG == (ctrl & PAM_TAC_DEBUG))
                        pam_syslog(pamh, LOG_DEBUG, "send msg=\"%s\"",
                            conv_msg.msg);
                }
                else {
                    pam_syslog(pamh, LOG_WARNING, "%s: error sending msg="
                        "\"%s\", retval=%d", __func__, conv_msg.msg, retval);
                }
            }

            *sptr = PAM_AUTH_ERR;
            ret = 0;
            pam_syslog(pamh, LOG_NOTICE, "auth failed %d", msg);
            break;

        case TAC_PLUS_AUTHEN_STATUS_GETDATA:
            /* not implemented */
            if (PAM_TAC_DEBUG == (ctrl & PAM_TAC_DEBUG))
                pam_syslog(pamh, LOG_DEBUG, "tacacs status:"
                    " TAC_PLUS_AUTHEN_STATUS_GETDATA");

            if (NULL != conv_msg.msg) {
                int retval = -1;
                int echo_off = (0x1 == (re.flags & 0x1));

                conv_msg.msg_style = echo_off ? PAM_PROMPT_ECHO_OFF :
                    PAM_PROMPT_ECHO_ON;
                retval = converse(pamh, 1, &conv_msg, &resp);
                if (PAM_SUCCESS == retval) {
                    if (PAM_TAC_DEBUG == (ctrl & PAM_TAC_DEBUG))
                        pam_syslog(pamh, LOG_DEBUG, "sent msg=\"%s\", resp="
                            "\"%s\"", conv_msg.msg, resp->resp);

                    if (PAM_TAC_DEBUG == (ctrl & PAM_TAC_DEBUG))
                        pam_syslog(pamh, LOG_DEBUG, "%s: calling"
                            " tac_cont_send", __func__);

                    if (0 > tac_cont_send_seq(fd, resp->resp, re.seq_no + 1)) {
                        pam_syslog(pamh, LOG_ERR, "error sending continue req"
                            " to TACACS+ server");
                        *sptr = PAM_AUTHINFO_UNAVAIL;
                    }
                }
                else {
                    pam_syslog(pamh, LOG_WARNING, "%s: error sending msg="
                        "\"%s\", retval=%d (%s)", __func__, conv_msg.msg,
                        retval, pam_strerror(pamh, retval));
                    *sptr = PAM_AUTHINFO_UNAVAIL;
                }
            }
            else {
                pam_syslog(pamh, LOG_ERR, "GETDATA response with no message,"
                    " returning PAM_AUTHINFO_UNAVAIL");

                *sptr = PAM_AUTHINFO_UNAVAIL;
            }

            ret = 0;
            break;

        case TAC_PLUS_AUTHEN_STATUS_GETUSER:
            /* not implemented */
            if (ctrl & PAM_TAC_DEBUG)
                pam_syslog(pamh, LOG_DEBUG, "tacacs status:"
                    " TAC_PLUS_AUTHEN_STATUS_GETUSER");

            ret = 0;
            break;

        case TAC_PLUS_AUTHEN_STATUS_GETPASS:
            if (ctrl & PAM_TAC_DEBUG)
                pam_syslog(pamh, LOG_DEBUG, "tacacs status:"
                    " TAC_PLUS_AUTHEN_STATUS_GETPASS");

            if (ctrl & PAM_TAC_DEBUG)
                pam_syslog(pamh, LOG_DEBUG, "%s: tac_cont_send called",
                    __func__);

            if (tac_cont_send(fd, pass) < 0) {
                pam_syslog(pamh, LOG_ERR, "error sending continue req to"
                    " TACACS+ server");
                ret = 0;
                break;
            }
            /* continue the while loop; go read tac response */
            break;

        case TAC_PLUS_AUTHEN_STATUS_RESTART:
            /* try it again */
            if (ctrl & PAM_TAC_DEBUG)
                pam_syslog(pamh, LOG_DEBUG, "tacacs status:"
                    " TAC_PLUS_AUTHEN_STATUS_RESTART (not impl)");

            /*
             * not implemented
             * WdJ: I *think* you can just do tac_authen_send(user, pass) again
             *      but I'm not sure
             */
            ret = 0;
            break;

        case TAC_PLUS_AUTHEN_STATUS_ERROR:
            /* server has problems */
            if (ctrl & PAM_TAC_DEBUG)
                pam_syslog(pamh, LOG_DEBUG, "tacacs status:"
                    " TAC_PLUS_AUTHEN_STATUS_ERROR");

            ret = 0;
            break;

        case TAC_PLUS_AUTHEN_STATUS_FOLLOW:
            /* server tells to try a different server address */
            /* not implemented */
            if (ctrl & PAM_TAC_DEBUG)
                pam_syslog(pamh, LOG_DEBUG, "tacacs status:"
                    " TAC_PLUS_AUTHEN_STATUS_FOLLOW");

            ret = 0;
            break;

        default:
            if (msg < 0) {
                /* connection error */
                ret = 0;
                if (ctrl & PAM_TAC_DEBUG)
                    pam_syslog(pamh, LOG_DEBUG, "error communicating with"
                        " tacacs server");
                break;
            }

            /* unknown response code */
            ret = 0;
            if (ctrl & PAM_TAC_DEBUG)
                pam_syslog(pamh, LOG_DEBUG, "tacacs status: unknown response"
                    " 0x%02x", msg);
    }

    if (NULL != resp) {
       free(resp->resp);
       free(resp);
    }

    if (NULL != re.msg);
        free(re.msg);

    return ret;
}

/*
 * Only acct and auth now; should handle all the cases here
 * Talk to the tacacs server for each type of transaction conversation
 */
static void talk_tac_server(int ctrl, int fd, char *user, char *pass,
                            char *tty, char *r_addr, struct tac_attrib **attr,
                            int *sptr, struct areply *reply,
                            pam_handle_t * pamh) {
    if (!pass) { /*  accounting */
        int retval;
        struct areply arep;
        if (attr)
            retval = tac_author_send(fd, user, tty, r_addr, *attr);
        if(!attr || retval < 0) {
            pam_syslog(pamh, LOG_ERR, "error getting authorization");
            *sptr =  PAM_AUTHINFO_UNAVAIL;
            return;
        }

        if (ctrl & PAM_TAC_DEBUG)
            pam_syslog(pamh, LOG_DEBUG, "%s: sent authorization request for"
                " [%s]", __func__, user);

        arep.msg = NULL;
        tac_author_read(fd, &arep);
        if (reply)
            *reply = arep;

        if(arep.status != AUTHOR_STATUS_PASS_ADD &&
            arep.status != AUTHOR_STATUS_PASS_REPL) {
            /*
             * if !pass, we are validating that the user is known, and
             * these status values usually means the server does not know
             * about the user so set USER_UNKNOWN, so we go on to the next
             * pam module.  This is consistent with libnss_tacplus.
             *
             * If pass, then this will usually be authorization denied due
             * to one of the permission checks.
             */
            if (!pass) {
                *sptr = PAM_USER_UNKNOWN;
                /*  do any logging at caller, since this is debug */
            }
            else {
                *sptr = PAM_PERM_DENIED;
                pam_syslog(pamh, LOG_WARNING, "TACACS+ authorization failed"
                           " for [%s] (tacacs status=%d)", user, arep.status);
            }
            if(arep.msg != NULL && !reply)
                free (arep.msg); /*  if reply is set, caller will free */
            return;
        }
        else  {
            *sptr = PAM_SUCCESS;
            if(arep.msg && !reply)
                free (arep.msg); /* caller will free if reply is set */
        }
    }
    else { /* authentication */
        if (tac_authen_send(fd, user, pass, tty, r_addr, TAC_PLUS_AUTHEN_LOGIN)
            < 0) {
            pam_syslog(pamh, LOG_ERR, "error sending auth req to TACACS+"
                " server");
        }
        else {
            while ( tac_auth_converse(ctrl, fd, sptr, pass, pamh))
                    ;
        }
    }
}


/*
 * find a responding tacacs server, and converse with it.
 * See comments at do_tac_connect() below
 */
static void find_tac_server(int ctrl, int *tacfd, char *user, char *pass,
                           char *tty, char *r_addr, struct tac_attrib **attr,
                           int *sptr, struct areply *reply,
                           pam_handle_t * pamh) {
    int fd = -1, srv_i;

    tac_chk_anyresp();
    for (srv_i = 0; srv_i < tac_srv_no; srv_i++) {
        fd = -1;
        if (ctrl & PAM_TAC_DEBUG)
            pam_syslog(pamh, LOG_DEBUG, "%s: trying srv[%d] %s", __func__,
                srv_i, tac_srv[srv_i].addr ?
                tac_ntop(tac_srv[srv_i].addr->ai_addr) : "not set");

        /*
         * Try using our most recent server, if we had one.  This works for all
         * but accounting, where we should start from beginning of list.
         */
        if (active_server.addr) {
                fd = tac_connect_single(active_server.addr, active_server.key,
                                        tac_src_addr_info, __vrfname);
                if (fd < 0)
                    active_server.addr = NULL; /*  start from beginning */
        }
        if (fd < 0) {
            if (tac_srv[srv_i].not_resp)
                continue;
            /* no active_server, or it failed, and curr not failed */
            fd = tac_connect_single(tac_srv[srv_i].addr, tac_srv[srv_i].key,
                                    tac_src_addr_info, __vrfname);
        }
        if (fd < 0) {
            pam_syslog(pamh, LOG_ERR, "connection to srv[%d] %s failed: %m",
                srv_i, tac_srv[srv_i].addr ?
                tac_ntop(tac_srv[srv_i].addr->ai_addr) : "not set");
            active_server.addr = NULL;
            continue;
        }

        talk_tac_server(ctrl, fd, user, pass, tty, r_addr, attr, sptr,
            reply, pamh);

        if (*sptr == PAM_SUCCESS || *sptr == PAM_AUTH_ERR ||
            *sptr == PAM_USER_UNKNOWN) {
            if (ctrl & PAM_TAC_DEBUG)
                pam_syslog(pamh, LOG_DEBUG, "%s: srv[%d] %s, pam_status=%d",
                    __func__, srv_i, tac_ntop(tac_srv[srv_i].addr->ai_addr),
                    *sptr);
            if (*sptr == PAM_SUCCESS) {
                if (active_server.addr == NULL) {
                    active_server.addr = tac_srv[srv_i].addr;
                    active_server.key = tac_srv[srv_i].key;
                }
                break;
            }
            /*  else try other servers, if any. On errs, won't need fd */
        }
        if (active_server.addr) {
            /*
             * We connected, but got a failure, don't re-use, start
             * over on next pass or connection attempt
             */
            active_server.addr = NULL;
        }
        close(fd);
        fd = -1;
    }
    *tacfd = fd;
}

/*
 * We have to make a new connection each time, because libtac is single
 * threaded (doesn't support multiple connects at the same time due to
 * use of globals), and doesn't have support for persistent connections.
 * That's fixable, but not worth the effort at this point.
 *
 * Trying to make this common code is ugly, but worth it to simplify
 * maintenance and debugging.
 *
 * The problem is that the definition allows for multiple tacacs
 * servers to be consulted, but a lot of the code was written such
 * that once a server is found that responds, it keeps using it.
 * That means when we are finding a server we need to do the full sequence.
 * The related issue is that the lower level code can't communicate
 * with multiple servers at the same time, and can't keep a connection
 * open.
 *
 * TODO: Really should have a structure to pass user, pass, tty, and r_addr
 * around everywhere.
 */
static int do_tac_connect(int ctrl, char *user, char *pass,
                          char *tty, char *r_addr, struct tac_attrib **attr,
                          struct areply *reply, pam_handle_t * pamh) {
    int status = PAM_AUTHINFO_UNAVAIL, fd;

    if (active_server.addr == NULL) { /* find a server with the info we want */
        find_tac_server(ctrl, &fd, user, pass, tty, r_addr, attr, &status,
            reply, pamh);
    }
    else { /* connect to the already chosen server, so we get
            * consistent results.  */
        if (ctrl & PAM_TAC_DEBUG)
            pam_syslog(pamh, LOG_DEBUG, "%s: use previous server %s", __func__,
               tac_ntop(active_server.addr->ai_addr));

        fd = tac_connect_single(active_server.addr, active_server.key,
                                tac_src_addr_info, __vrfname);
        if (fd < 0)
            pam_syslog(pamh, LOG_ERR, "reconnect failed to %s: %m",
                       tac_ntop(active_server.addr->ai_addr));
        else
            talk_tac_server(ctrl, fd, user, pass, tty, r_addr, attr, &status,
                reply, pamh);
    }

    /*
     * this is debug because we can get called for any user for
     * commands like sudo, not just tacacs users, so it's not an
     * error to fail here.  The caller can handle the logging.
     */
    if ((ctrl & PAM_TAC_DEBUG) && status != PAM_SUCCESS &&
        status != PAM_USER_UNKNOWN)
        pam_syslog(pamh, LOG_DEBUG, "no more servers to connect");
    if (fd != -1)
        close(fd); /* acct caller doesn't need connection */
    return status;
}

/* Main PAM functions */

/* authenticates user on remote TACACS+ server
 * returns PAM_SUCCESS if the supplied username and password
 * pair is valid
 * First check to see if the user is known to the server(s),
 * so we don't go through the password phase for unknown
 * users, and can return PAM_USER_UNKNOWN to the process.
 */
PAM_EXTERN
int pam_sm_authenticate (pam_handle_t * pamh, int flags,
    int argc, const char **argv) {
    int ctrl, retval;
    char *user, *puser;
    char *pass;
    char *tty;
    char *r_addr;
    int status;
    struct tac_attrib **attr, *attr_s = NULL;

    priv_level = 0;
    user = pass = tty = r_addr = NULL;

    ctrl = _pam_parse(pamh, argc, argv);

    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: called (pam_tacplus v%u.%u.%u)",
            __func__, PAM_TAC_VMAJ, PAM_TAC_VMIN, PAM_TAC_VPAT);

    /* reset static state in case we are re-entered */
    _reset_saved_user(pamh, ctrl & PAM_TAC_DEBUG);

    /*
     * If a mapped user entry already exists, we are probably being
     * used for su or sudo, so we need to get the original user password,
     * rather than the mapped user.
     * Decided based on auid != uid and then do the lookup, similar to
     * find_pw_user() in nss_tacplusc
     */
    _pam_get_user(pamh, &puser);
    user = get_user_to_auth(puser);
    if (user == NULL)
        return PAM_USER_UNKNOWN;
    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: user [%s] obtained", __func__, user);

    _pam_get_terminal(pamh, &tty);
    if (!strncmp(tty, "/dev/", 5))
        tty += 5;
    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: tty [%s] obtained", __func__, tty);

    _pam_get_rhost(pamh, &r_addr);
    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: rhost [%s] obtained", __func__,
            r_addr);

    /*
     * Since these attributes are just for validating that the user is known to
     * at least one server, it doesn't really matter whether these are "correct"
     */
    attr = &attr_s;
    tac_add_attrib(attr, "service", tac_service?tac_service:"shell");
    tac_add_attrib(attr, "protocol", tac_protocol?tac_protocol:"ssh");
    tac_add_attrib(attr, "cmd", "");

    status = do_tac_connect(ctrl, user, NULL, tty, r_addr, &attr_s, NULL, pamh);
    tac_free_attrib(&attr_s);
    if (status != PAM_SUCCESS) {
        if (ctrl & PAM_TAC_DEBUG)
            pam_syslog(pamh, LOG_DEBUG, "TACACS+ user [%s] unknown,"
                       " (pam status=%d)", user, status);
        goto err;
    }

    retval = tacacs_get_password (pamh, flags, ctrl, &pass);
    if (retval != PAM_SUCCESS || pass == NULL || *pass == '\0') {
        pam_syslog(pamh, LOG_ERR, "unable to obtain password");
        status = PAM_CRED_INSUFFICIENT;
        goto err;
    }

    retval = pam_set_item (pamh, PAM_AUTHTOK, pass);
    if (retval != PAM_SUCCESS) {
        pam_syslog(pamh, LOG_ERR, "unable to set password");
        status = PAM_CRED_INSUFFICIENT;
        goto err;
    }

    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: password obtained", __func__);

    status = do_tac_connect(ctrl, user, pass, tty, r_addr, NULL, NULL, pamh);

    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: exit with pam status: %d", __func__,
            status);

err:
    if (user && user != puser)
        free(user); /* it was stdrup'ed */
    if (NULL != pass) {
        bzero(pass, strlen (pass));
        free(pass);
    }

    return status;
}    /* pam_sm_authenticate */


/* no-op function to satisfy PAM authentication module */
PAM_EXTERN
int pam_sm_setcred (pam_handle_t * pamh, int flags,
    int argc, const char **argv) {

    int ctrl = _pam_parse (pamh, argc, argv);

    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: called (pam_tacplus v%u.%u.%u)",
            __func__, PAM_TAC_VMAJ, PAM_TAC_VMIN, PAM_TAC_VPAT);

    return PAM_SUCCESS;
}    /* pam_sm_setcred */


/* authorizes user on remote TACACS+ server, i.e. checks
 * his permission to access requested service
 * returns PAM_SUCCESS if the service is allowed
 */
PAM_EXTERN
int pam_sm_acct_mgmt (pam_handle_t * pamh, int flags,
    int argc, const char **argv) {

    int ctrl, status=PAM_AUTH_ERR;
    char *user;
    char *tty;
    char *r_addr;
    struct areply arep;
    struct tac_attrib *attr_s = NULL, *attr;

    user = tty = r_addr = NULL;
    memset(&arep, 0, sizeof(arep));

    /* this also obtains service name for authorization
       this should be normally performed by pam_get_item(PAM_SERVICE)
       but since PAM service names are incompatible TACACS+
       we have to pass it via command line argument until a better
       solution is found ;) */
    ctrl = _pam_parse (pamh, argc, argv);

    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: called (pam_tacplus v%u.%u.%u)",
            __func__, PAM_TAC_VMAJ, PAM_TAC_VMIN, PAM_TAC_VPAT);

    _pam_get_user(pamh, &user);
    if (user == NULL)
        return PAM_USER_UNKNOWN;


    _pam_get_terminal(pamh, &tty);
    if(!strncmp(tty, "/dev/", 5))
        tty += 5;

    _pam_get_rhost(pamh, &r_addr);

    /* checks for specific data required by TACACS+, which should
       be supplied in pam module command line  */
    if(!*tac_service) {
        pam_syslog(pamh, LOG_ERR, "TACACS+ service type not configured");
        return PAM_AUTHINFO_UNAVAIL;
    }
    tac_add_attrib(&attr_s, "service", tac_service);

    if(tac_protocol != NULL && tac_protocol[0] != '\0')
          tac_add_attrib(&attr_s, "protocol", tac_protocol);
    else
          pam_syslog(pamh, LOG_ERR, "TACACS+ protocol type not configured"
              " (IGNORED)");

    tac_add_attrib(&attr_s, "cmd", "");

    memset(&arep, 0, sizeof arep);

    /*
     * Check if user is authorized, independently of authentication.
     * Authentication may have happened via ssh public key, rather than
     * via TACACS+.  PAM should not normally get to this entry point if
     * user is not yet authenticated.
     * We only write the mapping entry (if needed) when authorization
     * is succesful.
     * attr is not used here, but having a non-NULL value is how
     * talk_tac_server() distinguishes that it is an acct call, vs auth
     * TODO: use a different mechanism
    */
    status = do_tac_connect(ctrl, user, NULL, tty, r_addr, &attr_s,
        &arep, pamh);
    tac_free_attrib(&attr_s);
    if(active_server.addr == NULL) {
        /* we need to return PAM_AUTHINFO_UNAVAIL here, rather than
         * PAM_AUTH_ERR, or we can't use "ignore" or auth_err=bad in the
         * pam configuration
         */
        status = PAM_AUTHINFO_UNAVAIL;
        goto cleanup;
    }

    if(status) {
        if (ctrl & PAM_TAC_DEBUG)
            pam_syslog(pamh, LOG_NOTICE, "No TACACS mapping for %s after auth"
                " failure", user);
        goto cleanup;
    }

    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: user [%s] successfully authorized",
            __func__, user);

    attr = arep.attr;
    while (attr != NULL)  {
        const int new_len = attr->attr_len+1; /* 1 longer for terminating 0 */
        char attribute[new_len];
        char value[new_len];
        char attrenv[new_len];
        char tmpstr[new_len];
        char *sep;

        snprintf(tmpstr, sizeof tmpstr, "%*s", attr->attr_len,attr->attr);
        sep = index(tmpstr, '=');
        if(sep == NULL)
            sep = index(tmpstr, '*');
        if(sep != NULL) {
            *sep = '\0';
            snprintf(attribute, sizeof attribute, "%s", tmpstr);
            snprintf(value, sizeof value, "%s", ++sep);

            size_t i;
            for (i = 0; attribute[i] != '\0'; i++) {
                attribute[i] = toupper(attribute[i]);
                if (attribute[i] == '-')
                    attribute[i] = '_';
            }

            if (ctrl & PAM_TAC_DEBUG)
                pam_syslog(pamh, LOG_DEBUG, "%s: returned attribute `%s(%s)'"
                    " from server", __func__, attribute, value);

            if(strncmp(attribute, "PRIV", 4) == 0) {
                char *ok;

                priv_level = (unsigned)strtoul(value, &ok, 0);
                /* if this fails, we leave priv_level at 0, which is
                 * least privileged, so that's OK, but at least report it
                 */
                if (ok == value)
                    pam_syslog(pamh, LOG_WARNING,
                        "%s: non-numeric privilege for %s, got (%s)",
                        __func__, attribute, value);
            }

            /*
             * make returned attributes available for other PAM modules via PAM
             * environment. Since separator can be = or *, ensure it's = for
             * the env.
             */
            snprintf(attrenv, sizeof attrenv, "%s=%s", attribute, value);
            if (pam_putenv(pamh, attrenv) != PAM_SUCCESS)
                pam_syslog(pamh, LOG_WARNING, "%s: unable to set PAM"
                    " environment (%s)", __func__, attribute);

        } else {
            pam_syslog(pamh, LOG_WARNING, "%s: invalid attribute `%s',"
                " no separator", __func__, attr->attr);
        }
        attr = attr->next;
    }

    update_mapped(pamh, user, priv_level, r_addr);


cleanup:
    /* free returned attributes */
    if(arep.attr != NULL)
        tac_free_attrib(&arep.attr);

    if(arep.msg != NULL)
        free (arep.msg);

    return status;
}    /* pam_sm_acct_mgmt */

/*
 * accounting packets may be directed to any TACACS+ server,
 * independent from those used for authentication and authorization;
 * they may be also directed to all specified servers
 */

static short unsigned int session_taskid;

/*
 * send START accounting request to the remote TACACS+ server
 * returns PAM error only if the request was refused or there
 * were problems connection to the server
 * sets sess_taskid so it can be used in close_session, so that
 * accounting start and stop records have the same task_id, as
 * the specification requires.
 */
PAM_EXTERN
int pam_sm_open_session (pam_handle_t * pamh, int flags,
    int argc, const char **argv) {

    if (!task_id)
#if defined(HAVE_OPENSSL_RAND_H) && defined(HAVE_LIBCRYPTO)
        RAND_pseudo_bytes((unsigned char *) &task_id, sizeof(task_id));
#else
        task_id=(short int) tac_magic();
#endif
    session_taskid = task_id;
    return _pam_account(pamh, argc, argv, TAC_PLUS_ACCT_FLAG_START, NULL);
}    /* pam_sm_open_session */

/* sends STOP accounting request to the remote TACACS+ server
 * returns PAM error only if the request was refused or there
 * were problems connection to the server
 */
PAM_EXTERN
int pam_sm_close_session (pam_handle_t * pamh, int flags,
    int argc, const char **argv) {
    int rc;
    char *user;

    _pam_get_user(pamh, &user);

    task_id = session_taskid; /* task_id must match start */
    rc = _pam_account(pamh, argc, argv, TAC_PLUS_ACCT_FLAG_STOP, NULL);
    __update_loguid(user); /* now dead, cleanup mapping */
    return rc;
}    /* pam_sm_close_session */


#ifdef PAM_SM_PASSWORD
/* Tested for servers that require password change during challenge/response */
PAM_EXTERN
int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
    int argc, const char **argv) {

    int ctrl;
    char *user;
    char *pass;
    char *tty;
    char *r_addr;
    const void *pam_pass = NULL;
    int status;

    user = pass = tty = r_addr = NULL;

    ctrl = _pam_parse(pamh, argc, argv);

    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: called (pam_tacplus v%u.%u.%u)"
            " (flags=%d, argc=%d)", __func__, PAM_TAC_VMAJ, PAM_TAC_VMIN,
            PAM_TAC_VPAT, flags, argc);

    if (   (pam_get_item(pamh, PAM_OLDAUTHTOK, &pam_pass) == PAM_SUCCESS)
        && (pam_pass != NULL) ) {
         if ((pass = strdup(pam_pass)) == NULL)
              return PAM_BUF_ERR;
    } else {
        pass = strdup("");
    }

    _pam_get_user(pamh, &user);
    if (user == NULL) {
        if(pass) {
                free(pass);
        }
        return PAM_USER_UNKNOWN;
    }

    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: user [%s] obtained", __func__, user);

    _pam_get_terminal(pamh, &tty);
    if (tty && !strncmp(tty, "/dev/", 5))
        tty += 5;
    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: tty [%s] obtained", __func__,
            tty?tty:"UNKNOWN");

    _pam_get_rhost(pamh, &r_addr);
    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: rhost [%s] obtained", __func__,
            r_addr?r_addr:"UNKNOWN");

    if (PAM_SILENT != (flags & PAM_SILENT))
        status = do_tac_connect(ctrl, user, pass, tty, r_addr, NULL, NULL,
                                pamh);
    else
        status = PAM_AUTHTOK_ERR;

    if (status != PAM_SUCCESS && status != PAM_AUTHTOK_ERR)
        pam_syslog(pamh, LOG_ERR, "no more servers to connect");

    if (ctrl & PAM_TAC_DEBUG)
        pam_syslog(pamh, LOG_DEBUG, "%s: exit with pam status: %d", __func__,
            status);

    if (NULL != pass) {
        bzero(pass, strlen(pass));
        free(pass);
        pass = NULL;
    }

    return status;

}    /* pam_sm_chauthtok */
#endif


#ifdef PAM_STATIC
struct pam_module _pam_tacplus_modstruct {
    "pam_tacplus",
    pam_sm_authenticate,
    pam_sm_setcred,
    pam_sm_acct_mgmt,
    pam_sm_open_session,
    pam_sm_close_session,
#ifdef PAM_SM_PASSWORD
    pam_sm_chauthtok
#else
    NULL
#endif
};
#endif