]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_guest.c
support for function 0x7a
[netatalk.git] / etc / uams / uams_guest.c
1 /*
2  * $Id: uams_guest.c,v 1.12 2003-03-12 15:07:03 didg Exp $
3  *
4  * (c) 2001 (see COPYING)
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif /* HAVE_CONFIG_H */
10
11 #ifndef ATACC
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <errno.h>
15
16 /* STDC check */
17 #if STDC_HEADERS
18 #include <string.h>
19 #else /* STDC_HEADERS */
20 #ifndef HAVE_STRCHR
21 #define strchr index
22 #define strrchr index
23 #endif /* HAVE_STRCHR */
24 char *strchr (), *strrchr ();
25 #ifndef HAVE_MEMCPY
26 #define memcpy(d,s,n) bcopy ((s), (d), (n))
27 #define memmove(d,s,n) bcopy ((s), (d), (n))
28 #endif /* ! HAVE_MEMCPY */
29 #endif /* STDC_HEADERS */
30
31 #include <pwd.h>
32 #include <atalk/logger.h>
33
34 #include <atalk/afp.h>
35 #include <atalk/uam.h>
36
37 /* login and login_ext are almost the same */
38 static int noauth_login(void *obj, struct passwd **uam_pwd,
39                         char *ibuf, int ibuflen, 
40                         char *rbuf, int *rbuflen)
41 {
42     struct passwd *pwent;
43     char *guest, *username;
44
45     *rbuflen = 0;
46     LOG(log_info, logtype_uams, "login noauth" );
47
48     if (uam_afpserver_option(obj, UAM_OPTION_GUEST, (void *) &guest,
49                              NULL) < 0)
50       return AFPERR_MISC;
51
52     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, 
53                              (void *) &username, NULL) < 0)
54       return AFPERR_MISC;
55
56     strcpy(username, guest);
57     if ((pwent = getpwnam(guest)) == NULL) {
58         LOG(log_error, logtype_uams, "noauth_login: getpwnam( %s ): %s",
59                 guest, strerror(errno) );
60         return( AFPERR_BADUAM );
61     }
62
63 #ifdef AFS
64     if ( setpag() < 0 ) {
65         LOG(log_error, logtype_uams, "noauth_login: setpag: %s", strerror(errno) );
66         return( AFPERR_BADUAM );
67     }
68 #endif /* AFS */
69
70     *uam_pwd = pwent;
71     return( AFP_OK );
72 }
73
74 static int noauth_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
75                      char *ibuf, int ibuflen,
76                      char *rbuf, int *rbuflen)
77 {
78         return ( noauth_login (obj, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
79 }
80
81
82 /* Printer NoAuthUAM Login */
83 int noauth_printer(start, stop, username, out)
84         char    *start, *stop, *username;
85         struct papfile  *out;
86 {
87     char        *data, *p, *q;
88     static const char *loginok = "0\r";
89
90     data = (char *)malloc(stop - start + 1);
91     strncpy(data, start, stop - start + 1);
92
93     /* We are looking for the following format in data:
94      * (username)
95      *
96      * Hopefully username doesn't contain a ")"
97      */
98
99     if ((p = strchr(data, '(' )) == NULL) {
100         LOG(log_info, logtype_uams,"Bad Login NoAuthUAM: username not found in string");
101         free(data);
102         return(-1);
103     }
104     p++;
105     if ((q = strchr(data, ')' )) == NULL) {
106         LOG(log_info, logtype_uams,"Bad Login NoAuthUAM: username not found in string");
107         free(data);
108         return(-1);
109     }
110     strncpy(username, p, q - p);
111
112     /* Done copying username, clean up */
113     free(data);
114
115     if (getpwnam(username) == NULL) {
116         LOG(log_info, logtype_uams, "Bad Login NoAuthUAM: %s: %s",
117                username, strerror(errno) );
118         return(-1);
119     }
120
121     /* Login successful */
122     append(out, loginok, strlen(loginok));
123     LOG(log_info, logtype_uams, "Login NoAuthUAM: %s", username);
124     return(0);
125 }
126
127
128 static int uam_setup(const char *path)
129 {
130   if (uam_register(UAM_SERVER_LOGIN_EXT, path, "No User Authent",
131                    noauth_login, NULL, NULL, noauth_login_ext) < 0)
132         return -1;
133
134   if (uam_register(UAM_SERVER_PRINTAUTH, path, "NoAuthUAM",
135                 noauth_printer) < 0)
136         return -1;
137
138   return 0;
139 }
140
141 static void uam_cleanup()
142 {
143   uam_unregister(UAM_SERVER_LOGIN, "No User Authent");
144   uam_unregister(UAM_SERVER_PRINTAUTH, "NoAuthUAM");
145 }
146
147 UAM_MODULE_EXPORT struct uam_export uams_guest = {
148   UAM_MODULE_SERVER,
149   UAM_MODULE_VERSION,
150   uam_setup, uam_cleanup
151 };
152 #endif