v1.03: tiny performance improvement, lots of commenting
[libnss-afs.git] / nss_afs_test.c
1 #include <nss.h>
2 #include <sys/socket.h>
3 #include <netinet/in.h>
4 #include <sys/types.h>
5 #include <stdio.h>
6 #include <sys/time.h>
7 #include <sys/select.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <stdlib.h>
11
12 int main(int argc,char **argv,char **envp) {
13         int buflen=1000;
14         char buffer[buflen];
15         char *buf;
16         char *arg;
17         int uid;
18         int res;
19         
20         buf=buffer;
21         if ( argc != 2 ) {
22                 printf("Usage: %s [name or id]\n\nAsks (a hopefully running) ptdbnssd for an AFSID or a PT-username, returns both.\n",argv[0]);
23                 exit(1);
24         }
25         arg=argv[1];
26         if ( ( arg[0] > '0' ) && ( arg[0] < '9' ) ) {
27                 // NAME-Lookup
28                 uid=atoi(arg);
29                 res=ptsid2name(uid,&buf,&buflen);
30                 if ( res == NSS_STATUS_SUCCESS ) {
31                         printf("uid=%i name=%s\n",uid,buffer);
32                         //printf("homedir_method=%i, shell_method=%i\n",conf.homedirs_method,conf.shells_method);
33                         return 0;
34                 } else {
35                         if ( res == NSS_STATUS_NOTFOUND ) {
36                                 printf("W: AFSID %i not found.\n",uid);
37                                 return 1;
38                         } else {
39                                 if ( res == NSS_STATUS_UNAVAIL ) {
40                                         printf("E: Error talking to ptdbnssd.\n");
41                                         return 2;
42                                 }
43                         }
44                 }
45         } else {
46                 buf=buffer;
47                 res=ptsname2id(arg,&uid);
48                 if ( res == NSS_STATUS_SUCCESS ) {
49                         printf("uid=%i name=%s\n",uid,arg);
50                         //printf("homedir_method=%i, shell_method=%i\n",conf.homedirs_method,conf.shells_method);
51                         return 0;
52                 } else {
53                         if ( res == NSS_STATUS_NOTFOUND ) {
54                                 printf("W: PT-user '%s' not found.\n",buf);
55                                 return 1;
56                         } else {
57                                 if ( res == NSS_STATUS_UNAVAIL ) {
58                                         printf("E: Error talking to ptdbnssd.\n");
59                                         return 2;
60                                 }
61                         }
62                 }
63         }
64 }