]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_guest.c
- merge branch-netatalk-afp-3x-dev, HEAD was tagged before
[netatalk.git] / etc / uams / uams_guest.c
1 /*
2  * $Id: uams_guest.c,v 1.13 2005-04-28 20:49:50 bfernhomberg 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 #include <atalk/util.h>
37
38 #ifndef MIN
39 #define MIN(a,b) ((a) < (b) ? (a) : (b))
40 #endif /* MIN */
41
42 extern void append(void *, const char *, int);
43
44 /* login and login_ext are almost the same */
45 static int noauth_login(void *obj, struct passwd **uam_pwd,
46                         char *ibuf _U_, int ibuflen _U_, 
47                         char *rbuf _U_, int *rbuflen)
48 {
49     struct passwd *pwent;
50     char *guest, *username;
51
52     *rbuflen = 0;
53     LOG(log_info, logtype_uams, "login noauth" );
54
55     if (uam_afpserver_option(obj, UAM_OPTION_GUEST, (void *) &guest,
56                              NULL) < 0)
57       return AFPERR_MISC;
58
59     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, 
60                              (void *) &username, NULL) < 0)
61       return AFPERR_MISC;
62
63     strcpy(username, guest);
64     if ((pwent = getpwnam(guest)) == NULL) {
65         LOG(log_error, logtype_uams, "noauth_login: getpwnam( %s ): %s",
66                 guest, strerror(errno) );
67         return( AFPERR_BADUAM );
68     }
69
70 #ifdef AFS
71     if ( setpag() < 0 ) {
72         LOG(log_error, logtype_uams, "noauth_login: setpag: %s", strerror(errno) );
73         return( AFPERR_BADUAM );
74     }
75 #endif /* AFS */
76
77     *uam_pwd = pwent;
78     return( AFP_OK );
79 }
80
81 static int noauth_login_ext(void *obj, char *uname _U_, struct passwd **uam_pwd,
82                      char *ibuf, int ibuflen,
83                      char *rbuf, int *rbuflen)
84 {
85         return ( noauth_login (obj, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
86 }
87
88
89 /* Printer NoAuthUAM Login */
90 int noauth_printer(start, stop, username, out)
91         char    *start, *stop, *username;
92         struct papfile  *out;
93 {
94     char        *data, *p, *q;
95     static const char *loginok = "0\r";
96
97     data = (char *)malloc(stop - start + 1);
98     if (!data) {
99         LOG(log_info, logtype_uams,"Bad Login NoAuthUAM: malloc");
100         return(-1);
101     }
102
103     strlcpy(data, start, stop - start + 1);
104
105     /* We are looking for the following format in data:
106      * (username)
107      *
108      * Hopefully username doesn't contain a ")"
109      */
110
111     if ((p = strchr(data, '(' )) == NULL) {
112         LOG(log_info, logtype_uams,"Bad Login NoAuthUAM: username not found in string");
113         free(data);
114         return(-1);
115     }
116     p++;
117     if ((q = strchr(p, ')' )) == NULL) {
118         LOG(log_info, logtype_uams,"Bad Login NoAuthUAM: username not found in string");
119         free(data);
120         return(-1);
121     }
122     memcpy(username, p,  MIN( UAM_USERNAMELEN, q - p ));
123
124     /* Done copying username, clean up */
125     free(data);
126
127     if (getpwnam(username) == NULL) {
128         LOG(log_info, logtype_uams, "Bad Login NoAuthUAM: %s: %s",
129                username, strerror(errno) );
130         return(-1);
131     }
132
133     /* Login successful */
134     append(out, loginok, strlen(loginok));
135     LOG(log_info, logtype_uams, "Login NoAuthUAM: %s", username);
136     return(0);
137 }
138
139
140 static int uam_setup(const char *path)
141 {
142   if (uam_register(UAM_SERVER_LOGIN_EXT, path, "No User Authent",
143                    noauth_login, NULL, NULL, noauth_login_ext) < 0)
144         return -1;
145
146   if (uam_register(UAM_SERVER_PRINTAUTH, path, "NoAuthUAM",
147                 noauth_printer) < 0)
148         return -1;
149
150   return 0;
151 }
152
153 static void uam_cleanup()
154 {
155   uam_unregister(UAM_SERVER_LOGIN, "No User Authent");
156   uam_unregister(UAM_SERVER_PRINTAUTH, "NoAuthUAM");
157 }
158
159 UAM_MODULE_EXPORT struct uam_export uams_guest = {
160   UAM_MODULE_SERVER,
161   UAM_MODULE_VERSION,
162   uam_setup, uam_cleanup
163 };
164 #endif