]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_guest.c
more AFP 3.x stuff
[netatalk.git] / etc / uams / uams_guest.c
1 /*
2  * $Id: uams_guest.c,v 1.11 2002-10-13 06:18:14 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 static int noauth_login(void *obj, struct passwd **uam_pwd,
38                         char *ibuf, int ibuflen, 
39                         char *rbuf, int *rbuflen)
40 {
41     struct passwd *pwent;
42     char *guest, *username;
43
44     *rbuflen = 0;
45     LOG(log_info, logtype_uams, "login noauth" );
46
47     if (uam_afpserver_option(obj, UAM_OPTION_GUEST, (void *) &guest,
48                              NULL) < 0)
49       return AFPERR_MISC;
50
51     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, 
52                              (void *) &username, NULL) < 0)
53       return AFPERR_MISC;
54
55     strcpy(username, guest);
56     if ((pwent = getpwnam(guest)) == NULL) {
57         LOG(log_error, logtype_uams, "noauth_login: getpwnam( %s ): %s",
58                 guest, strerror(errno) );
59         return( AFPERR_BADUAM );
60     }
61
62 #ifdef AFS
63     if ( setpag() < 0 ) {
64         LOG(log_error, logtype_uams, "noauth_login: setpag: %s", strerror(errno) );
65         return( AFPERR_BADUAM );
66     }
67 #endif /* AFS */
68
69     *uam_pwd = pwent;
70     return( AFP_OK );
71 }
72
73
74 /* Printer NoAuthUAM Login */
75 int noauth_printer(start, stop, username, out)
76         char    *start, *stop, *username;
77         struct papfile  *out;
78 {
79     char        *data, *p, *q;
80     static const char *loginok = "0\r";
81
82     data = (char *)malloc(stop - start + 1);
83     strncpy(data, start, stop - start + 1);
84
85     /* We are looking for the following format in data:
86      * (username)
87      *
88      * Hopefully username doesn't contain a ")"
89      */
90
91     if ((p = strchr(data, '(' )) == NULL) {
92         LOG(log_info, logtype_uams,"Bad Login NoAuthUAM: username not found in string");
93         free(data);
94         return(-1);
95     }
96     p++;
97     if ((q = strchr(data, ')' )) == NULL) {
98         LOG(log_info, logtype_uams,"Bad Login NoAuthUAM: username not found in string");
99         free(data);
100         return(-1);
101     }
102     strncpy(username, p, q - p);
103
104     /* Done copying username, clean up */
105     free(data);
106
107     if (getpwnam(username) == NULL) {
108         LOG(log_info, logtype_uams, "Bad Login NoAuthUAM: %s: %s",
109                username, strerror(errno) );
110         return(-1);
111     }
112
113     /* Login successful */
114     append(out, loginok, strlen(loginok));
115     LOG(log_info, logtype_uams, "Login NoAuthUAM: %s", username);
116     return(0);
117 }
118
119
120 static int uam_setup(const char *path)
121 {
122   if (uam_register(UAM_SERVER_LOGIN, path, "No User Authent",
123                noauth_login, NULL, NULL) < 0)
124         return -1;
125   if (uam_register(UAM_SERVER_PRINTAUTH, path, "NoAuthUAM",
126                 noauth_printer) < 0)
127         return -1;
128
129   return 0;
130 }
131
132 static void uam_cleanup()
133 {
134   uam_unregister(UAM_SERVER_LOGIN, "No User Authent");
135   uam_unregister(UAM_SERVER_PRINTAUTH, "NoAuthUAM");
136 }
137
138 UAM_MODULE_EXPORT struct uam_export uams_guest = {
139   UAM_MODULE_SERVER,
140   UAM_MODULE_VERSION,
141   uam_setup, uam_cleanup
142 };
143 #endif