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