File: main.cpp

package info (click to toggle)
ukui-control-center 3.22.1.29-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,420 kB
  • sloc: cpp: 77,963; xml: 2,408; sh: 30; makefile: 4
file content (164 lines) | stat: -rw-r--r-- 4,355 bytes parent folder | download
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  * Copyright (C) 2023, KylinSoft Co., Ltd.
  *
  * 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 3, 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; if not, see <http://www.gnu.org/licenses/>.
  *
 **/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>
#include <fcntl.h>
#include <security/pam_appl.h>
#include <sys/mman.h>
static int toParent = 0;
static int fromChild = 0;

typedef struct pam_message PAM_MESSAGE;
typedef struct pam_response PAM_RESPONSE;

static void
writeData(int fd, const void *buf, ssize_t count)
{
    if(write(fd, buf, count) != count)
    printf("write to parent failed:  %s\n",strerror(errno));
}

static void
writeString(int fd, const char *data)
{
    int length = data ? strlen(data) : -1;
    writeData(fd, &length, sizeof(length));
    if(data)
        writeData(fd, data, sizeof(char) * length);
}

static int
readData(int fd, void *buf, size_t count)
{
    ssize_t nRead = read(fd, buf, count);
    if(nRead < 0)
    printf("read data failed:  %s\n",strerror(errno));
    return nRead;
}

static char *
readString(int fd)
{
    int length;

    if(readData(fd, &length, sizeof(length)) <= 0)
        return NULL;
    if(length <= 0)
        return NULL;

    char *value = (char *)malloc(sizeof(char) * (length + 1));
    readData(fd, value, length);
    value[length] = '\0';

    return value;
}

static int
pam_conversation(int msgLength, const struct pam_message **msg,
                PAM_RESPONSE **resp, void */*appData*/)
{
    PAM_RESPONSE *response = (PAM_RESPONSE*)calloc(msgLength,sizeof(PAM_RESPONSE));

    int authComplete = 0;
    writeData(toParent, (const void*)&authComplete, sizeof(authComplete));
    writeData(toParent, (const void*)&msgLength, sizeof(msgLength));
    //发送pam消息
    for(int i = 0; i < msgLength; i++)
    {
        const struct pam_message *m = msg[i];
        writeData(toParent, (const void *)&m->msg_style, sizeof(m->msg_style));
        writeString(toParent, m->msg);
    }
    //读取响应
    for(int i = 0; i < msgLength; i++)
    {
        PAM_RESPONSE *r = &response[i];
        readData(fromChild, &r->resp_retcode, sizeof(r->resp_retcode));
        r->resp = readString(fromChild);
    }
    *resp = response;
    return PAM_SUCCESS;
}

static void
_authenticate(const char *userName)
{
//    printf("authenticate  %s\n",userName);

    pam_handle_t *pamh = NULL;
    char *newUser;
    int ret;
    int authRet;
    struct pam_conv conv;

    conv.conv = pam_conversation;
    conv.appdata_ptr = NULL;

    ret = pam_start("control-center", userName, &conv, &pamh);
    if(ret != PAM_SUCCESS)
    {
        printf("failed to start PAM:  = %s\n", pam_strerror(NULL, ret));
    }

    authRet = pam_authenticate(pamh, 0);

    ret = pam_get_item(pamh, PAM_USER, (const void **)&newUser);
    if(ret != PAM_SUCCESS)
    {
        pam_end(pamh, 0);
        printf("failed to get username\n");
    }
    free(newUser);
    newUser = NULL;
//    fprintf(stderr, "authentication result: %d\n", authRet);

    // 发送认证结果
    int authComplete = 1;
    writeData(toParent, (const void*)&authComplete, sizeof(authComplete));
    writeData(toParent, (const void *)&authRet, sizeof(authRet));

    /* ---认证完成\n*/
    _exit(EXIT_SUCCESS);
}

int main(int argc, char **argv)
{
    if (argc != 4)
    {
        return EXIT_FAILURE;
    }
    toParent = atoi (argv[1]);
    fromChild = atoi (argv[2]);
    if (toParent == 0 || fromChild == 0)
    {
        printf ("Invalid file descriptors %s %s\n", argv[2], argv[3]);
        return EXIT_FAILURE;
    }
    //mlockall (MCL_CURRENT | MCL_FUTURE);
    fcntl (toParent, F_SETFD, FD_CLOEXEC);
    fcntl (fromChild, F_SETFD, FD_CLOEXEC);


    _authenticate(argv[3]);

}