1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <pwd.h>
4 #include "readwrite.h"
5 #include "substdio.h"
6 #include "subfd.h"
7 #include "error.h"
8 #include "exit.h"
9 #include "byte.h"
10 #include "str.h"
11 #include "case.h"
12 #include "fmt.h"
13 #include "auto_usera.h"
14 #include "auto_break.h"
15 #include "qlx.h"
16 #include "stralloc.h"
17 
18 #define GETPW_USERLEN 32
19 
20 extern int getpw_mysql (char *username, int *UID, int *GID);
21 extern void disconnect_mysql();
22 extern int connect_mysql();
23 
24 char *local;
25 struct passwd *pw;
26 char *dash;
27 char *extension;
28 int connection;
29 
30 extern stralloc tmpname;
31 extern stralloc tmphome;
32 extern stralloc tmpshell;
33 
userext()34 int userext()
35 {
36   char username[GETPW_USERLEN];
37   struct stat st;
38   int num, uid, gid;
39 
40   extension = local + str_len(local);
41   for (;;) {
42     if (extension - local < sizeof(username))
43       if (!*extension || (*extension == *auto_break)) {
44 	byte_copy(username,extension - local,local);
45 	username[extension - local] = 0;
46 	case_lowers(username);
47 	errno = 0;
48 	if (errno == error_txtbsy) _exit(QLX_SYS);
49 	num = 0;
50 #ifdef SOLARIS_STUPIDITY
51   substdio_put(subfderr,"",1);
52 #endif
53 	if (connection) num = getpw_mysql(username, &uid, &gid);
54 	if (num == -1) _exit(QLX_SYS);
55 	else if (num) {
56 	  pw = (struct passwd *) malloc(sizeof(struct passwd));
57 	  pw->pw_name = tmpname.s;
58 	  pw->pw_uid = uid;
59 	  pw->pw_gid = gid;
60 	  pw->pw_dir = tmphome.s;
61 	  pw->pw_shell = tmpshell.s;
62 	}
63 	if (! pw) pw = getpwnam(username);
64 	if (pw)
65 	  if (pw->pw_uid)
66 	    if (stat(pw->pw_dir,&st) == 0) {
67 	      if (st.st_uid == pw->pw_uid) {
68 		dash = "";
69 		if (*extension) { ++extension; dash = auto_break; }
70 		return 1;
71 	      }
72 	    }
73 	    else
74 	      if (error_temp(errno)) _exit(QLX_NFS);
75       }
76     if (extension == local) {
77       /* we didn't find a system account */
78       /* but the database is down */
79       if (connection) return 0;
80       disconnect_mysql();
81       _exit(QLX_SYS);
82     }
83     --extension;
84   }
85 }
86 
87 char num[FMT_ULONG];
88 
main(argc,argv)89 int main(argc,argv)
90 int argc;
91 char **argv;
92 {
93   local = argv[1];
94   if (!local) _exit(100);
95 
96   connection = connect_mysql();
97 
98   if (!userext()) {
99     extension = local;
100     dash = auto_break;
101     pw = getpwnam(auto_usera);
102   }
103 
104   if (connection) disconnect_mysql();
105 
106   if (!pw) _exit(QLX_NOALIAS);
107 
108   substdio_puts(subfdoutsmall,pw->pw_name);
109   substdio_put(subfdoutsmall,"",1);
110   substdio_put(subfdoutsmall,num,fmt_ulong(num,(long) pw->pw_uid));
111   substdio_put(subfdoutsmall,"",1);
112   substdio_put(subfdoutsmall,num,fmt_ulong(num,(long) pw->pw_gid));
113   substdio_put(subfdoutsmall,"",1);
114   substdio_puts(subfdoutsmall,pw->pw_dir);
115   substdio_put(subfdoutsmall,"",1);
116   substdio_puts(subfdoutsmall,dash);
117   substdio_put(subfdoutsmall,"",1);
118   substdio_puts(subfdoutsmall,extension);
119   substdio_put(subfdoutsmall,"",1);
120   substdio_flush(subfdoutsmall);
121 
122   _exit(0);
123 }
124