1 /* cvm/getpwnam.c - Handles getpwnam+getspnam+getuserpw combinations
2  * Copyright (C) 2010  Bruce Guenter <bruce@untroubled.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 #include <ctype.h>
19 #include <errno.h>
20 #include <pwd.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include <bglibs/sysdeps.h>
25 
26 #include "module.h"
27 
28 #ifdef HASSPNAM
29 #include <shadow.h>
30 static struct spwd* spw;
31 #endif
32 
33 #ifdef HASUSERPW
34 #include <userpw.h>
35 static struct userpw* uwp;
36 #endif
37 
38 static char* actbuf = 0;
39 static unsigned actlen = 0;
40 
copyact(const char * account)41 static const char* copyact(const char* account)
42 {
43   unsigned len;
44   char *ptr;
45   if ((len = strlen(account)) > actlen) {
46     if ((actbuf = realloc(actbuf, len+1)) == 0) return 0;
47     actlen = len;
48   }
49   for (ptr = actbuf; *account != 0; ++ptr, ++account)
50     *ptr = isupper(*account) ? tolower(*account) : *account;
51   *ptr = 0;
52   return actbuf;
53 }
54 
cvm_getpwnam(const char * account,struct passwd ** pwp)55 int cvm_getpwnam(const char* account, struct passwd** pwp)
56 {
57   struct passwd* pw;
58 
59   account = copyact(account);
60   if ((pw = getpwnam(account)) == 0)
61     return (errno == ETXTBSY) ? CVME_IO : CVME_PERMFAIL;
62 
63 #ifdef HASUSERPW
64   if ((upw = getuserpw(account)) == 0) {
65     if (errno == ETXTBSY) return CVME_IO;
66   }
67   else if (upw->upw_passwd)
68     pw->pw_passwd = upw->upw_passwd;
69 #endif
70 
71 #ifdef HASSPNAM
72   if ((spw = getspnam(account)) == 0) {
73     if (errno == ETXTBSY) return CVME_IO;
74   }
75   else if (spw->sp_pwdp)
76     pw->pw_passwd = spw->sp_pwdp;
77 #endif
78 
79   *pwp = pw;
80   return 0;
81 }
82