File: passwd.c

package info (click to toggle)
userinfo 1.5-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 280 kB
  • ctags: 149
  • sloc: sh: 2,177; ansic: 1,636; makefile: 55
file content (149 lines) | stat: -rw-r--r-- 3,134 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
/*
    Copyright (C) 2001-2002  bjk <bjk@arbornet.org>

    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; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <errno.h>
#include "common.h"

static void gecosinfo(INFO *info)
{
    int i = 0;
    char *tmp, *buf;

    tmp = info->passwd->pw_gecos;

    info->gecos->name = NONE;
    info->gecos->office = NONE;
    info->gecos->officephone = NONE;
    info->gecos->homephone = NONE;

    while ((buf = strtok(tmp, ",")) != NULL) {
	switch (i) {
	    case 0:
		info->gecos->name = buf;
		break;
	    case 1:
		info->gecos->office = buf;
		break;
	    case 2:
		info->gecos->officephone = buf;
		break;
	    case 3:
		info->gecos->homephone = buf;
		break;
	    default:
		break;
	}

	tmp = NULL;
	i++;
    }

    return;
}

static void getgroups(INFO *info) {
    struct group *mygroup;
    GROUP *node;

    if ((info->group = (GROUP *) malloc(sizeof(GROUP))) == NULL) {
	perror("malloc");
	exit(errno);
    }

    node = info->group;

    if ((mygroup = getgrgid(info->passwd->pw_gid)) == NULL) {
	node->id = info->passwd->pw_gid;
	strncpy(node->name, UNKNOWN, sizeof(node->name));
	node->next = NULL;
	return;
    }

    node->id = mygroup->gr_gid;
    strncpy(node->name, mygroup->gr_name, sizeof(node->name));
    
    if ((node->next = (GROUP *) malloc(sizeof(GROUP))) == NULL) {
	perror("malloc");
	exit(errno);
    }

    node = node->next;
    
#ifdef HAVE_SETGROUPENT
    if (!setgroupent(1))
	fprintf(stderr, "could not keep group file open (will be slower)\n");
#else
    setgrent();
#endif

    group_info = 1;

    while ((mygroup = getgrent()) != NULL) {
	char *tmp;

	while (*mygroup->gr_mem) {
	    if (strncmp(*mygroup->gr_mem, info->passwd->pw_name,
			strlen(info->passwd->pw_name)) == 0) {
		
    		node->id = mygroup->gr_gid;
		strncpy(node->name, mygroup->gr_name, sizeof(node->name));
		
    		if ((node->next = (GROUP *) malloc(sizeof(GROUP))) == NULL) {
		    perror("malloc");
		    exit(errno);
		}

    		node = node->next;
	    }

	    tmp = *mygroup->gr_mem++;
	}
    }

    node->next = NULL;
    return;
}

void passwdinfo(INFO *info)
{
    int i;

    for (i = 0; i < ARRAYCNT(optspec); i++) {
	if (optspec[i] == 0)
	    break;

	switch (optspec[i]) {
	    case 'i':
		gecosinfo(info);
		break;
	    case 'g':
		getgroups(info);
		break;
	    default:
		break;
	}
    }

    return;
}