File: getpwnam.c

package info (click to toggle)
cvm 0.97-0.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,036 kB
  • sloc: ansic: 4,065; sh: 2,758; makefile: 235; sql: 15
file content (81 lines) | stat: -rw-r--r-- 2,123 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
/* cvm/getpwnam.c - Handles getpwnam+getspnam+getuserpw combinations
 * Copyright (C) 2010  Bruce Guenter <bruce@untroubled.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 <ctype.h>
#include <errno.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>

#include <bglibs/sysdeps.h>

#include "module.h"

#ifdef HASSPNAM
#include <shadow.h>
static struct spwd* spw;
#endif

#ifdef HASUSERPW
#include <userpw.h>
static struct userpw* uwp;
#endif

static char* actbuf = 0;
static unsigned actlen = 0;

static const char* copyact(const char* account)
{
  unsigned len;
  char *ptr;
  if ((len = strlen(account)) > actlen) {
    if ((actbuf = realloc(actbuf, len+1)) == 0) return 0;
    actlen = len;
  }
  for (ptr = actbuf; *account != 0; ++ptr, ++account)
    *ptr = isupper(*account) ? tolower(*account) : *account;
  *ptr = 0;
  return actbuf;
}
    
int cvm_getpwnam(const char* account, struct passwd** pwp)
{
  struct passwd* pw;

  account = copyact(account);
  if ((pw = getpwnam(account)) == 0)
    return (errno == ETXTBSY) ? CVME_IO : CVME_PERMFAIL;

#ifdef HASUSERPW
  if ((upw = getuserpw(account)) == 0) {
    if (errno == ETXTBSY) return CVME_IO;
  }
  else if (upw->upw_passwd)
    pw->pw_passwd = upw->upw_passwd;
#endif

#ifdef HASSPNAM
  if ((spw = getspnam(account)) == 0) {
    if (errno == ETXTBSY) return CVME_IO;
  }
  else if (spw->sp_pwdp)
    pw->pw_passwd = spw->sp_pwdp;
#endif

  *pwp = pw;
  return 0;
}