aboutsummaryrefslogtreecommitdiffstats
path: root/USER/USER.xs
blob: c294d0687591740b6cc1108a09497fde247f7d4a (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
/* Copyright (C) 2003 MandrakeSoft SA  Daouda Lo (daouda at mandrakesoft dot com) 
 * This program is free software; you can redistribute it and/or
 * modify it under the same terms as Perl itself.
 */

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "ppport.h"

#include <grp.h>
#include <pwd.h>
#include <stdlib.h>
#include <unistd.h>
#include <glib.h>
#include <libuser/user.h>
#include <libuser/user_private.h>

typedef struct context USER__ADMIN;
typedef struct lu_ent USER__ENT;
typedef struct lu_error USER__ERR;

static SV ** convert_value_array_list(register SV **sp, GValueArray *array) {
        GValue *value;
        int i;
        long l;
        STRLEN len;
        const char *s;
        for (i = 0; (array != NULL) && (i < array->n_values); i++) {
                value = g_value_array_get_nth(array, i);
                /* If the item is a G_TYPE_LONG, add it as a double. */
             if (G_VALUE_HOLDS_LONG(value)) {
                        l = g_value_get_long(value);
                        XPUSHs(sv_2mortal(newSViv(l)));
		} else if (G_VALUE_HOLDS_STRING(value)) {
                        s = g_value_get_string(value);
			XPUSHs(sv_2mortal(newSVpv(s, len)));
		}
	}
	return sp;
}

char * 
get_name(USER__ENT *ent, const char *attribute) 
{
        GValueArray *values;
        GValue *value;
        char *name;
        values = lu_ent_get(ent, attribute);
        value = gvalue_array_get_nth(values, 0);
        strcpy(name, g_value_get_string(value));
        return name;
}

static int 
get_int(USER__ENT *ent, const char *attribute) 
{
        GValueArray *values;
        GValue *value;
        int i;
        values = lu_ent_get(ent, attribute);
        value = gvalue_array_get_nth(values, 0);
        i = g_value_get_long(value);
        return i ? i : -1;
}

MODULE = USER       PACKAGE = USER::ADMIN         PREFIX = Admin_

USER::ADMIN *
Admin_new(CLASS)
        char *CLASS
        CODE:
        USER__ERR *error = NULL;
        RETVAL = (USER__ADMIN *)lu_start(NULL, 0, NULL, NULL, lu_prompt_console_quiet, NULL, &error);
        if( RETVAL == NULL ){
                warn("unable to malloc USER__ADMIN");
                XSRETURN_UNDEF;
	}
        OUTPUT:
        RETVAL

void        
Admin_DESTROY(self)
        USER::ADMIN *self
        CODE:
        printf("freeing ADMIN");
        if (self) {
                lu_end(self);
        }; 

void
Admin_lookup_user_name(self, name)
        USER::ADMIN *self
        char *name
        PPCODE:
        USER__ENT *ent;
        USER__ERR *error = NULL;
        ent = lu_ent_new();
        if (lu_user_lookup_name(self, name, ent, &error)) {
                XPUSHs(sv_2mortal(newSViv(ent)));
        } else {
                lu_ent_free(ent);
        }

AV *
Admin_users_enumerate_full(self, pattern)
        USER::ADMIN *self
        char *pattern
        CODE:
        int c;
        USER__ERR *error = NULL;
        RETVAL = (AV*)sv_2mortal((SV*)newAV());
        GPtrArray *accounts;
        accounts = lu_users_enumerate_full(self, pattern, &error);
        for (c = 0; (accounts != NULL) && (c < accounts->len); c++) {
                if( av_store(RETVAL, c, newSViv(g_ptr_array_index(accounts, c))) == NULL ){
                        warn("XS_UsersEnumerateFull: failed to store elems");
                }
        }
        g_ptr_array_free(accounts, TRUE);
        OUTPUT:
        RETVAL

AV * 
Admin_groups_enumerate_full(self, pattern)
        USER::ADMIN *self
        char *pattern
        PREINIT:
        int c;
        USER__ERR *error = NULL;
        CODE:
        RETVAL = (AV*)sv_2mortal((SV*)newAV());
        GPtrArray *accounts;
        accounts = lu_groups_enumerate_full(self, pattern, &error);
        for (c = 0; (accounts != NULL) && (c < accounts->len); c++) {
                if( av_store( RETVAL, c, newSViv(g_ptr_array_index(accounts, c)) ) == NULL ){
                        warn("XS_UsersEnumerateFull: failed to store elems");
                }
        }
        g_ptr_array_free(accounts, TRUE);
        OUTPUT:
        RETVAL

AV *
Admin_get_user_shells(self)        
        USER::ADMIN *self
        CODE:
        int i = 0;
        const char *shell;
        RETVAL = (AV*)sv_2mortal((SV*)newAV());
        setusershell(); 
        while ((shell = getusershell()) != NULL) {
                av_store(RETVAL, i, newSVpv(shell, 0));
                i++;
        }
        endusershell();
        OUTPUT:
        RETVAL

MODULE = USER   PACKAGE = USER::ENT    PREFIX = Ent_

USER::ENT *
Ent_new (CLASS)
        char *CLASS
        CODE:
        RETVAL = (USER__ENT *)safemalloc( sizeof( USER__ENT ) );
        if( RETVAL == NULL ){
                warn("unable to malloc USER__ENT");
                XSRETURN_UNDEF;
	}
        RETVAL = lu_ent_new();
        OUTPUT:
        RETVAL

void
Ent_DESTROY(self)
        USER::ENT *self
        CODE:
        lu_ent_free(self);
        safefree(self);

HV * 
Ent_get_attributes(self)
	USER::ENT *self
        CODE:
        int i,j;
        RETVAL = (HV*)sv_2mortal((SV*)newHV());
        switch (self->type) {
                case lu_invalid:
                        break;
                case lu_user:
                        hv_store(RETVAL, "type", 4, newSVpv("user", 0), 0);
                        hv_store(RETVAL, "username", 8, newSVpv(get_name(self, LU_USERNAME), 0), 0);
                        hv_store(RETVAL, "uid", 3, newSViv(get_int(self, LU_UIDNUMBER)), 0);
                        hv_store(RETVAL, "gid", 3, newSViv(get_int(self, LU_GIDNUMBER)), 0);
                        hv_store(RETVAL, "gecos", 5, newSVpv(get_name(self, LU_GECOS), 0), 0);
                        hv_store(RETVAL, "home", 4, newSVpv(get_name(self, LU_HOMEDIRECTORY), 0), 0);
                        hv_store(RETVAL, "shell", 5, newSVpv(get_name(self, LU_LOGINSHELL), 0), 0);
                        hv_store(RETVAL, "shadowpass", 10, newSVpv(get_name(self, LU_SHADOWPASSWORD), 0), 0);
                        hv_store(RETVAL, "shadowlastchange", 16, newSViv(get_name(self, LU_SHADOWLASTCHANGE)), 0);
                        hv_store(RETVAL, "shadowmin", 9, newSViv(get_name(self, LU_SHADOWMIN)), 0);
                        hv_store(RETVAL, "shadowmax", 9, newSViv(get_name(self, LU_SHADOWMAX)), 0);
                        hv_store(RETVAL, "shadowwarning", 13, newSVpv(get_name(self, LU_SHADOWWARNING), 0), 0);
                        hv_store(RETVAL, "shadowinact", 11, newSViv(get_name(self, LU_SHADOWINACTIVE)), 0);
                        hv_store(RETVAL, "shadowexpire", 12, newSViv(get_name(self, LU_SHADOWINACTIVE)), 0);
                        hv_store(RETVAL, "shadowflag", 10, newSViv(get_name(self, LU_SHADOWINACTIVE)), 0);
                case lu_group:
                        hv_store(RETVAL, "type", 4, newSVpv("group", 0), 0);
                        hv_store(RETVAL, "groupname", 8, newSVpv(get_name(self, LU_GROUPNAME), 0), 0);
                        hv_store(RETVAL, "gid", 3, newSViv(get_int(self, LU_GIDNUMBER)), 0);
                        break;
                default:
                        break;
        }
        OUTPUT:
        RETVAL

MODULE = USER       PACKAGE = USER         PREFIX = User_
	
void
User_read_config_files()
        CODE:
        /*force read of /etc/sysconfig/userdrakefilter*/